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 Base
- func (d *Base) AddColumn(hood *Hood, table, column string, typ interface{}, size int) error
- func (d *Base) AddColumnSql(table, column string, typ interface{}, size int) string
- func (d *Base) ChangeColumn(hood *Hood, table, column string, typ interface{}, size int) error
- func (d *Base) ChangeColumnSql(table, column string, typ interface{}, size int) string
- func (d *Base) CreateIndex(hood *Hood, table, column string, unique bool) error
- func (d *Base) CreateIndexSql(table, column string, unique bool) string
- func (d *Base) CreateTable(hood *Hood, model *Model) error
- func (d *Base) CreateTableIfNotExists(hood *Hood, model *Model) error
- func (d *Base) CreateTableSql(model *Model, ifNotExists bool) string
- func (d *Base) Delete(hood *Hood, model *Model) (Id, error)
- func (d *Base) DeleteSql(model *Model) (string, []interface{})
- func (d *Base) DropColumn(hood *Hood, table, column string) error
- func (d *Base) DropColumnSql(table, column string) string
- func (d *Base) DropIndex(hood *Hood, column string) error
- func (d *Base) DropIndexSql(column string) string
- func (d *Base) DropTable(hood *Hood, table string) error
- func (d *Base) DropTableIfExists(hood *Hood, table string) error
- func (d *Base) DropTableSql(table string, ifExists bool) string
- func (d *Base) Insert(hood *Hood, model *Model) (Id, error)
- func (d *Base) InsertSql(model *Model) (string, []interface{})
- func (d *Base) KeywordAutoIncrement() string
- func (d *Base) KeywordDefault(s string) string
- func (d *Base) KeywordNotNull() string
- func (d *Base) KeywordPrimaryKey() string
- func (d *Base) NextMarker(pos *int) string
- func (d *Base) QuerySql(hood *Hood) (string, []interface{})
- func (d *Base) RenameColumn(hood *Hood, table, from, to string) error
- func (d *Base) RenameColumnSql(table, from, to string) string
- func (d *Base) RenameTable(hood *Hood, from, to string) error
- func (d *Base) RenameTableSql(from, to string) string
- func (d *Base) SqlType(f interface{}, size int) string
- func (d *Base) Update(hood *Hood, model *Model) (Id, error)
- func (d *Base) UpdateSql(model *Model) (string, []interface{})
- func (d *Base) ValueToField(value reflect.Value, field reflect.Value) error
- type Dialect
- type Field
- type Hood
- func (hood *Hood) AddColumns(schema interface{}) error
- func (hood *Hood) Begin() *Hood
- func (hood *Hood) ChangeColumn(table, column interface{}) error
- func (hood *Hood) Commit() error
- func (hood *Hood) CreateIndex(table interface{}, column string, unique bool) 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) DropIndex(column 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) 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) RemoveColumn(table, column 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(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
- type Sqlite3
- func (d *Sqlite3) ChangeColumn(hood *Hood, table, column string, typ interface{}, size int) error
- func (d *Sqlite3) ChangeColumnSql(table, column string, typ interface{}, size int) string
- func (d *Sqlite3) DropColumn(hood *Hood, table, column string) error
- func (d *Sqlite3) DropColumnSql(table, column string) string
- func (d *Sqlite3) RenameColumn(hood *Hood, table, from, to string) error
- func (d *Sqlite3) RenameColumnSql(table, from, to string) 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 Base ¶
type Base struct {
Dialect Dialect
}
func (*Base) AddColumnSql ¶
func (*Base) ChangeColumn ¶
func (*Base) ChangeColumnSql ¶
func (*Base) CreateIndex ¶
func (*Base) CreateIndexSql ¶
func (*Base) CreateTableIfNotExists ¶
func (*Base) DropColumnSql ¶
func (*Base) DropIndexSql ¶
func (*Base) KeywordAutoIncrement ¶
func (*Base) KeywordDefault ¶
func (*Base) KeywordNotNull ¶
func (*Base) KeywordPrimaryKey ¶
func (*Base) NextMarker ¶
func (*Base) RenameColumnSql ¶
func (*Base) RenameTableSql ¶
type Dialect ¶
type Dialect interface { // Marker returns the dialect specific markers for prepared statements, // for instance $1, $2, ... and increments the position by one. // The position starts at 0. NextMarker(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 // 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{}) // 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, table, column string, unique bool) error // CreateIndexSql returns the sql for creating an index on the specified column. CreateIndexSql(table, column string, unique bool) string // DropIndex drops the index for the specified column. DropIndex(hood *Hood, column string) error // DropIndexSql returns the sql for dropping the index on the specified column. DropIndexSql(column 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 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) AddColumns ¶
AddColumns adds the columns in the specified schema to the table.
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) ChangeColumn ¶
ChangeColumn changes the data type of the specified column.
func (*Hood) CreateIndex ¶
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) 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) 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) RemoveColumn ¶
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 columsn and table. The table can either be a string or it's name can be inferred from the passed interface{} type.
type Sqlite3 ¶
type Sqlite3 struct {
Base
}
func (*Sqlite3) ChangeColumn ¶
func (*Sqlite3) ChangeColumnSql ¶
func (*Sqlite3) DropColumnSql ¶
func (*Sqlite3) RenameColumn ¶
func (*Sqlite3) RenameColumnSql ¶
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