Documentation ¶
Overview ¶
Package hood provides a database agnostic, transactional ORM for the sql package
Index ¶
- Constants
- func NewValidationError(id int, text string) error
- func RegisterDialect(name string, dialect Dialect)
- type Dialect
- type Field
- type Hood
- func (hood *Hood) Begin() *Hood
- func (hood *Hood) Commit() error
- func (hood *Hood) CreateTable(table interface{}) error
- func (hood *Hood) Delete(f interface{}) (Id, error)
- func (hood *Hood) DeleteAll(f interface{}) ([]Id, error)
- func (hood *Hood) DropTable(table interface{}) error
- func (hood *Hood) Exec(query string, args ...interface{}) (sql.Result, error)
- func (hood *Hood) Find(out interface{}) error
- func (hood *Hood) GroupBy(key string) *Hood
- func (hood *Hood) Having(condition string, args ...interface{}) *Hood
- func (hood *Hood) Join(op, table, condition string) *Hood
- func (hood *Hood) Limit(limit int) *Hood
- func (hood *Hood) Offset(offset int) *Hood
- func (hood *Hood) OrderBy(key string) *Hood
- func (hood *Hood) QueryRow(query string, args ...interface{}) *sql.Row
- func (hood *Hood) Reset()
- func (hood *Hood) Rollback() error
- func (hood *Hood) Save(f interface{}) (Id, error)
- func (hood *Hood) SaveAll(f interface{}) ([]Id, error)
- func (hood *Hood) Select(selector string, table interface{}) *Hood
- func (hood *Hood) Validate(f interface{}) error
- func (hood *Hood) Where(query string, args ...interface{}) *Hood
- type Id
- type Model
- type Postgres
- func (d *Postgres) Insert(hood *Hood, model *Model, query string, args ...interface{}) (Id, error, bool)
- func (d *Postgres) KeywordAutoIncrement() string
- func (d *Postgres) KeywordDefault(s string) string
- func (d *Postgres) KeywordNotNull() string
- func (d *Postgres) KeywordPrimaryKey() string
- func (d *Postgres) Marker(pos int) string
- func (d *Postgres) SqlType(f interface{}, size int) string
- func (d *Postgres) ValueToField(value reflect.Value, field reflect.Value) error
- type Sqlite3
- func (d *Sqlite3) Insert(hood *Hood, model *Model, query string, args ...interface{}) (Id, error, bool)
- func (d *Sqlite3) KeywordAutoIncrement() string
- func (d *Sqlite3) KeywordDefault(s string) string
- func (d *Sqlite3) KeywordNotNull() string
- func (d *Sqlite3) KeywordPrimaryKey() string
- func (d *Sqlite3) Marker(pos int) string
- func (d *Sqlite3) SqlType(f interface{}, size int) string
- func (d *Sqlite3) ValueToField(value reflect.Value, field reflect.Value) error
- type ValidationError
- type VarChar
Constants ¶
const ( ValidationErrorValueNotSet = (1<<16 + iota) ValidationErrorValueTooSmall ValidationErrorValueTooBig ValidationErrorValueTooShort ValidationErrorValueTooLong )
Variables ¶
This section is empty.
Functions ¶
func NewValidationError ¶
NewValidationError returns a new validation error with the specified id and text. The id's purpose is to distinguish different validation error types. Built-in validation error ids start at 65536, so you should keep your custom ids under that value.
func RegisterDialect ¶
RegisterDialect registers a new dialect using the specified name and dialect.
Types ¶
type Dialect ¶
type Dialect interface { // Marker returns the dialect specific markers for prepared statements, // for instance $1, $2, ... and so on. The position starts at 0. Marker(pos int) string // SqlType returns the SQL type for the provided interface type. The size // parameter delcares the data size for the column (e.g. for VARCHARs). SqlType(f interface{}, size int) string // ValueToField converts from an SQL Value to the coresponding interface Value. // It is the opposite of SqlType, in a sense. // For example: time.Time objects needs to be marshalled back and forth // as Strings for databases that don't have a native "time" type. ValueToField(value reflect.Value, field reflect.Value) error // Insert takes the generated query and modifies it. E.g. Postgres does not // return the inserted IDs after executing INSERT, unless a RETURNING // keyword is appended. If a dialect needs a custom INSERT, it should return // implemented == true. Insert(hood *Hood, model *Model, query string, args ...interface{}) (id Id, err error, implemented bool) // KeywordNotNull returns the dialect specific keyword for 'NOT NULL'. KeywordNotNull() string // KeywordDefault returns the dialect specific keyword for 'DEFAULT'. KeywordDefault(s string) string // KeywordPrimaryKey returns the dialect specific keyword for 'PRIMARY KEY'. KeywordPrimaryKey() string // KeywordAutoIncrement returns the dialect specific keyword for 'AUTO_INCREMENT'. KeywordAutoIncrement() string }
type Field ¶
type Field struct { Name string // Column name Value interface{} // Value SqlTags map[string]string // The sql struct tags for this field ValidateTags map[string]string // The validate struct tags for this field }
Field represents a schema field.
func (*Field) Int ¶
Int returns the field int value and a bool flag indication if the conversion was successful
func (*Field) PrimaryKey ¶
PrimaryKey tests if the field is declared using the sql tag "pk" or is of type Id
func (*Field) String ¶
String returns the field string value and a bool flag indicating if the conversion was successful
type Hood ¶
Hood is an ORM handle.
func Open ¶
Open opens a new database connection using the specified driver and data source name. It matches the sql.Open method signature.
func (*Hood) Begin ¶
Begin starts a new transaction and returns a copy of Hood. You have to call subsequent methods on the newly returned object.
func (*Hood) CreateTable ¶
CreateTable creates a new table based on the provided schema.
func (*Hood) Find ¶
Find performs a find using the previously specified query. If no explicit SELECT clause was specified earlier, the select is inferred from the passed interface type.
func (*Hood) Join ¶
Join performs a JOIN on tables, for example
Join("INNER JOIN", "users", "orders.user_id == users.id")
func (*Hood) QueryRow ¶
QueryRow executes a query that is expected to return at most one row. QueryRow always return a non-nil value. Errors are deferred until Row's Scan method is called.
func (*Hood) Select ¶
Select adds a SELECT clause to the query with the specified columsn and table. The table can either be a string or it's name can be inferred from the passed interface{} type.
type Postgres ¶
type Postgres struct{}
func (*Postgres) KeywordAutoIncrement ¶
func (*Postgres) KeywordDefault ¶
func (*Postgres) KeywordNotNull ¶
func (*Postgres) KeywordPrimaryKey ¶
type Sqlite3 ¶
type Sqlite3 struct{}
func (*Sqlite3) KeywordAutoIncrement ¶
func (*Sqlite3) KeywordDefault ¶
func (*Sqlite3) KeywordNotNull ¶
func (*Sqlite3) KeywordPrimaryKey ¶
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
Validation error type
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Id ¶
func (e *ValidationError) Id() int