Documentation ¶
Overview ¶
Package hood provides a database agnostic, transactional ORM for the sql package
Index ¶
- Constants
- func NewValidationError(id int, field string) error
- func RegisterDialect(name string, dialect Dialect)
- type Config
- type Created
- type Dialect
- type Environment
- type Hood
- func (hood *Hood) AddColumns(table, columns interface{}) error
- func (hood *Hood) And(a Path, op string, b interface{}) *Hood
- func (hood *Hood) Asc() *Hood
- func (hood *Hood) Begin() *Hood
- func (hood *Hood) ChangeColumns(table, column interface{}) error
- func (hood *Hood) Commit() error
- func (hood *Hood) Copy() *Hood
- func (hood *Hood) CreateIndex(table interface{}, name string, unique bool, columns ...string) error
- func (hood *Hood) CreateTable(table interface{}) error
- func (hood *Hood) CreateTableIfNotExists(table interface{}) error
- func (hood *Hood) Delete(f interface{}) (Id, error)
- func (hood *Hood) DeleteAll(f interface{}) ([]Id, error)
- func (hood *Hood) DeleteFrom(table interface{}) error
- func (hood *Hood) Desc() *Hood
- func (hood *Hood) DropIndex(table interface{}, name string) error
- func (hood *Hood) DropTable(table interface{}) error
- func (hood *Hood) DropTableIfExists(table interface{}) error
- func (hood *Hood) Exec(query string, args ...interface{}) (sql.Result, error)
- func (hood *Hood) Find(out interface{}) error
- func (hood *Hood) FindOne(out interface{}) error
- func (hood *Hood) FindOneSql(out interface{}, query string, args ...interface{}) error
- func (hood *Hood) FindSql(out interface{}, query string, args ...interface{}) error
- func (hood *Hood) GoSchema() string
- func (hood *Hood) GroupBy(path Path) *Hood
- func (hood *Hood) Having(condition string, args ...interface{}) *Hood
- func (hood *Hood) IsTransaction() bool
- func (hood *Hood) Join(op Join, table interface{}, a Path, b Path) *Hood
- func (hood *Hood) Limit(limit int) *Hood
- func (hood *Hood) Offset(offset int) *Hood
- func (hood *Hood) Or(a Path, op string, b interface{}) *Hood
- func (hood *Hood) OrderBy(path Path) *Hood
- func (hood *Hood) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (hood *Hood) QueryRow(query string, args ...interface{}) *sql.Row
- func (hood *Hood) RemoveColumns(table, columns interface{}) error
- func (hood *Hood) RenameColumn(table interface{}, from, to string) error
- func (hood *Hood) RenameTable(from, to interface{}) error
- 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(table interface{}, paths ...Path) *Hood
- func (hood *Hood) Validate(f interface{}) error
- func (hood *Hood) Where(a Path, op string, b interface{}) *Hood
- type Id
- type Index
- type Indexed
- type Indexes
- type Join
- type Model
- type ModelField
- func (field *ModelField) Default() string
- func (field *ModelField) GoDeclaration() string
- func (field *ModelField) Int() (int64, bool)
- func (field *ModelField) NotNull() bool
- func (field *ModelField) PrimaryKey() bool
- func (field *ModelField) Size() int
- func (field *ModelField) String() (string, bool)
- func (field *ModelField) Validate() error
- func (field *ModelField) Zero() bool
- type Path
- type Schema
- type Updated
- type ValidationError
Constants ¶
const ( ValidationErrorValueNotSet = (1<<16 + iota) ValidationErrorValueTooSmall ValidationErrorValueTooBig ValidationErrorValueTooShort ValidationErrorValueTooLong ValidationErrorValueNotMatch )
const ( InnerJoin = Join(iota) LeftJoin RightJoin FullJoin )
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 { // NextMarker returns the dialect specific marker for a prepared statement, // for instance $1, $2, ... and increments the position by one. // The position starts at 0. NextMarker(pos *int) string // Quote will quote identifiers in a SQL statement. Quote(s string) 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 // If database do not support boolean type this can be used to parse int // value to boolean value. ParseBool(value reflect.Value) bool // SetModelValue sets a model field from a db value. // // For example: time.Time objects needs to be marshalled back and forth // as Strings for databases that don't have a native "time" type. SetModelValue(value reflect.Value, field reflect.Value) error // ConvertHoodType converts special types such as Created or Updated to // values the driver can understand. ConvertHoodType(f interface{}) interface{} // QuerySql returns the resulting query sql and attributes. QuerySql(hood *Hood) (sql string, args []interface{}) // Insert inserts the values in model and returns the inserted rows Id. Insert(hood *Hood, model *Model) (Id, error) // InsertSql returns the sql for inserting the passed model. InsertSql(model *Model) (sql string, args []interface{}) // Update updates the values in the specified model and returns the // updated rows Id. Update(hood *Hood, model *Model) (Id, error) // UpdateSql returns the sql for updating the specified model. UpdateSql(model *Model) (string, []interface{}) // Delete drops the row matching the primary key of model and returns the affected Id. Delete(hood *Hood, model *Model) (Id, error) // DeleteSql returns the sql for deleting the row matching model's primary key. DeleteSql(model *Model) (string, []interface{}) // DeleteFrom deletes the matching rows in the specified table DeleteFrom(hood *Hood, table string) error // DeleteFromSql returns the sql for DeleteFrom DeleteFromSql(hood *Hood, table string) (string, []interface{}) // CreateTable creates the table specified in model. CreateTable(hood *Hood, model *Model) error // CreateTableIfNotExists creates the table specified in model if it does not exist. CreateTableIfNotExists(hood *Hood, model *Model) error // CreateTableSql returns the sql for creating a table. CreateTableSql(model *Model, ifNotExists bool) string // DropTable drops the specified table. DropTable(hood *Hood, table string) error // DropTableIfExists drops the table if it exists. DropTableIfExists(hood *Hood, table string) error // DropTableSql returns the sql for dropping the specified table. DropTableSql(table string, ifExists bool) string // RenameTable renames the specified table. RenameTable(hood *Hood, from, to string) error // RenameTableSql returns the sql for renaming the specified table. RenameTableSql(from, to string) string // AddColumn adds the columns to the corresponding table. AddColumn(hood *Hood, table, column string, typ interface{}, size int) error // AddColumnSql returns the sql for adding the specified column in table. AddColumnSql(table, column string, typ interface{}, size int) string // RenameColumn renames a table column in the specified table. RenameColumn(hood *Hood, table, from, to string) error // RenameColumnSql returns the sql for renaming the specified column in table. RenameColumnSql(table, from, to string) string // ChangeColumn changes the data type of the specified column. ChangeColumn(hood *Hood, table, column string, typ interface{}, size int) error // ChangeColumnSql returns the sql for changing the column data type. ChangeColumnSql(table, column string, typ interface{}, size int) string // DropColumn removes the specified column. DropColumn(hood *Hood, table, column string) error // DropColumnSql returns the sql for removing the column. DropColumnSql(table, column string) string // CreateIndex creates an index on the specified column. CreateIndex(hood *Hood, name, table string, unique bool, columns ...string) error // CreateIndexSql returns the sql for creating an index on the specified column. CreateIndexSql(name, table string, unique bool, columns ...string) string // DropIndex drops the index. DropIndex(hood *Hood, name string) error // DropIndexSql returns the sql for dropping the index. DropIndexSql(name string) string // 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 }
func NewPostgres ¶
func NewPostgres() Dialect
func NewSqlite3 ¶
func NewSqlite3() Dialect
type Environment ¶
Environment represents a configuration map for each environment specified in the config.json file
type Hood ¶
Hood is an ORM handle.
func Load ¶
Load opens a new database from a config.json file with the specified environment, or development if none is specified.
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) AddColumns ¶
AddColumns adds the columns in the specified schema to the table.
func (*Hood) And ¶
Where adds a AND clause to the WHERE query. You can concatenate using the And and Or methods.
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) ChangeColumns ¶
ChangeColumn changes the data type of the specified column.
func (*Hood) Commit ¶
Commit commits a started transaction and will report the first error that occurred inside the transaction.
func (*Hood) CreateIndex ¶
CreateIndex creates the specified index on table.
func (*Hood) CreateTable ¶
CreateTable creates a new table based on the provided schema.
func (*Hood) CreateTableIfNotExists ¶
CreateTableIfNotExists creates a new table based on the provided schema if it does not exist yet.
func (*Hood) DeleteFrom ¶
DeleteFrom deletes the rows matched by the previous Where clause. table can either be a table struct or a string.
Example:
hd.Where("amount", "=", 0).DeleteFrom("stock")
func (*Hood) DropTableIfExists ¶
DropTableIfExists drops the table matching the provided table name if it exists.
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) FindOneSql ¶
func (*Hood) FindSql ¶
FindSql performs a find using the specified custom sql query and arguments and writes the results to the specified out interface{}.
func (*Hood) IsTransaction ¶
IsTransaction returns wether the hood object represents an active transaction or not.
func (*Hood) Join ¶
Join performs a JOIN on tables, for example
Join(hood.InnerJoin, &User{}, "user.id", "order.id")
func (*Hood) Or ¶
Where adds a OR clause to the WHERE query. You can concatenate using the And and Or methods.
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) RemoveColumns ¶
RemoveColumns removes the specified columns from the table.
func (*Hood) RenameColumn ¶
RenameColumn renames the column in the specified table.
func (*Hood) RenameTable ¶
RenameTable renames a table. The arguments can either be a schema definition or plain strings.
func (*Hood) Select ¶
Select adds a SELECT clause to the query with the specified table and columns. The table can either be a string or it's name can be inferred from the passed interface{} type.
type Index ¶
Index represents a table index and is returned via the Indexed interface.
func (*Index) GoDeclaration ¶
type Indexed ¶
type Indexed interface {
Indexes(indexes *Indexes)
}
Indexed defines the indexes for a table. You can invoke Add on the passed instance.
type Model ¶
type Model struct { Pk *ModelField Table string Fields []*ModelField Indexes Indexes }
Model represents a parsed schema interface{}.
func (*Model) GoDeclaration ¶
type ModelField ¶
type ModelField 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 RawTag reflect.StructTag // The raw tag }
ModelField represents a schema field of a parsed model.
func (*ModelField) Default ¶
func (field *ModelField) Default() string
Default returns the default value for the field
func (*ModelField) GoDeclaration ¶
func (field *ModelField) GoDeclaration() string
func (*ModelField) Int ¶
func (field *ModelField) Int() (int64, bool)
Int returns the field int value and a bool flag indication if the conversion was successful
func (*ModelField) NotNull ¶
func (field *ModelField) NotNull() bool
NotNull tests if the field is declared as NOT NULL
func (*ModelField) PrimaryKey ¶
func (field *ModelField) PrimaryKey() bool
PrimaryKey tests if the field is declared using the sql tag "pk" or is of type Id
func (*ModelField) Size ¶
func (field *ModelField) Size() int
Size returns the field size, e.g. for varchars
func (*ModelField) String ¶
func (field *ModelField) String() (string, bool)
String returns the field string value and a bool flag indicating if the conversion was successful
func (*ModelField) Validate ¶
func (field *ModelField) Validate() error
Validate tests if the field conforms to it's validation constraints specified int the "validate" struct tag
func (*ModelField) Zero ¶
func (field *ModelField) Zero() bool
Zero tests wether or not the field is set
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
Validation error type
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Field ¶
func (e *ValidationError) Field() string
func (*ValidationError) Kind ¶
func (e *ValidationError) Kind() int