Documentation ¶
Index ¶
- Constants
- func CreateComposite(db DB, model interface{}, opt *CreateCompositeOptions) error
- func CreateTable(db DB, model interface{}, opt *CreateTableOptions) error
- func Delete(db DB, model interface{}) error
- func DropComposite(db DB, model interface{}, opt *DropCompositeOptions) error
- func DropTable(db DB, model interface{}, opt *DropTableOptions) error
- func ForceDelete(db DB, model interface{}) error
- func Insert(db DB, model ...interface{}) error
- func Q(query string, params ...interface{}) *queryParamsAppender
- func RegisterTable(strct interface{})
- func Scan(values ...interface{}) scanValuesModel
- func Select(db DB, model interface{}) error
- func SetTableNameInflector(fn func(string) string)
- func Update(db DB, model interface{}) error
- type ColumnScanner
- type CreateCompositeOptions
- type CreateTableOptions
- type DB
- type Discard
- func (m Discard) AddModel(ColumnScanner) error
- func (Discard) AfterDelete(_ DB) error
- func (Discard) AfterInsert(_ DB) error
- func (Discard) AfterQuery(_ DB) error
- func (Discard) AfterSelect(_ DB) error
- func (Discard) AfterUpdate(_ DB) error
- func (Discard) BeforeDelete(_ DB) error
- func (Discard) BeforeInsert(_ DB) error
- func (Discard) BeforeSelectQuery(db DB, q *Query) (*Query, error)
- func (Discard) BeforeUpdate(_ DB) error
- func (Discard) Init() error
- func (m Discard) NewModel() ColumnScanner
- func (m Discard) ScanColumn(colIdx int, colName string, rd types.Reader, n int) error
- type DropCompositeOptions
- type DropTableOptions
- type Field
- func (f *Field) AppendValue(b []byte, strct reflect.Value, quote int) []byte
- func (f *Field) Copy() *Field
- func (f *Field) HasFlag(flag uint8) bool
- func (f *Field) IsZero(strct reflect.Value) bool
- func (f *Field) OmitZero() bool
- func (f *Field) ScanValue(strct reflect.Value, rd types.Reader, n int) error
- func (f *Field) SetFlag(flag uint8)
- func (f *Field) Value(strct reflect.Value) reflect.Value
- type FormatAppender
- type Formatter
- func (f Formatter) Append(dst []byte, src string, params ...interface{}) []byte
- func (f Formatter) AppendBytes(dst, src []byte, params ...interface{}) []byte
- func (f Formatter) FormatQuery(dst []byte, query string, params ...interface{}) []byte
- func (f Formatter) Param(param string) interface{}
- func (f *Formatter) SetParam(param string, value interface{})
- func (f Formatter) String() string
- func (f Formatter) WithParam(param string, value interface{}) Formatter
- type HooklessModel
- type Method
- type Model
- type Query
- func (q *Query) AppendFormat(b []byte, f QueryFormatter) []byte
- func (q *Query) Apply(fn func(*Query) (*Query, error)) *Query
- func (q *Query) Column(columns ...string) *Query
- func (q *Query) ColumnExpr(expr string, params ...interface{}) *Query
- func (q *Query) Copy() *Query
- func (q *Query) CopyFrom(r io.Reader, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) CopyTo(w io.Writer, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) Count() (int, error)
- func (q *Query) CountEstimate(threshold int) (int, error)
- func (q *Query) CreateTable(opt *CreateTableOptions) error
- func (q *Query) DB(db DB) *Query
- func (q *Query) Delete(values ...interface{}) (Result, error)
- func (q *Query) Deleted() *Query
- func (q *Query) DropTable(opt *DropTableOptions) error
- func (q *Query) ExcludeColumn(columns ...string) *Query
- func (q *Query) Exec(query interface{}, params ...interface{}) (Result, error)
- func (q *Query) ExecOne(query interface{}, params ...interface{}) (Result, error)
- func (q *Query) Exists() (bool, error)
- func (q *Query) First() error
- func (q *Query) For(s string, params ...interface{}) *Query
- func (q *Query) ForEach(fn interface{}) error
- func (q *Query) ForceDelete(values ...interface{}) (Result, error)
- func (q *Query) FormatQuery(b []byte, query string, params ...interface{}) []byte
- func (q *Query) GetModel() TableModel
- func (q *Query) Group(columns ...string) *Query
- func (q *Query) GroupExpr(group string, params ...interface{}) *Query
- func (q *Query) Having(having string, params ...interface{}) *Query
- func (q *Query) Insert(values ...interface{}) (Result, error)
- func (q *Query) Join(join string, params ...interface{}) *Query
- func (q *Query) JoinOn(condition string, params ...interface{}) *Query
- func (q *Query) JoinOnOr(condition string, params ...interface{}) *Query
- func (q *Query) Last() error
- func (q *Query) Limit(n int) *Query
- func (q *Query) Model(model ...interface{}) *Query
- func (q *Query) New() *Query
- func (q *Query) Offset(n int) *Query
- func (q *Query) OnConflict(s string, params ...interface{}) *Query
- func (q *Query) Order(orders ...string) *Query
- func (q *Query) OrderExpr(order string, params ...interface{}) *Query
- func (q *Query) Query(model, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) QueryOne(model, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) Relation(name string, apply ...func(*Query) (*Query, error)) *Query
- func (q *Query) Returning(s string, params ...interface{}) *Query
- func (q *Query) Select(values ...interface{}) error
- func (q *Query) SelectAndCount(values ...interface{}) (count int, firstErr error)
- func (q *Query) SelectAndCountEstimate(threshold int, values ...interface{}) (count int, firstErr error)
- func (q *Query) SelectOrInsert(values ...interface{}) (inserted bool, _ error)
- func (q *Query) Set(set string, params ...interface{}) *Query
- func (q *Query) Table(tables ...string) *Query
- func (q *Query) TableExpr(expr string, params ...interface{}) *Query
- func (q *Query) Update(scan ...interface{}) (Result, error)
- func (q *Query) UpdateNotNull(scan ...interface{}) (Result, error)
- func (q *Query) Value(column string, value string, params ...interface{}) *Query
- func (q *Query) Where(condition string, params ...interface{}) *Query
- func (q *Query) WhereGroup(fn func(*Query) (*Query, error)) *Query
- func (q *Query) WhereIn(where string, values ...interface{}) *Query
- func (q *Query) WhereOr(condition string, params ...interface{}) *Query
- func (q *Query) WhereOrGroup(fn func(*Query) (*Query, error)) *Query
- func (q *Query) WherePK() *Query
- func (q *Query) WhereStruct(strct interface{}) *Query
- func (q *Query) With(name string, subq *Query) *Query
- func (q *Query) WrapWith(name string) *Query
- type QueryAppender
- type QueryFormatter
- type Relation
- type Result
- type Table
- func (t *Table) AddField(field *Field)
- func (t *Table) AppendParam(b []byte, strct reflect.Value, name string) ([]byte, bool)
- func (t *Table) GetField(fieldName string) (*Field, error)
- func (t *Table) HasField(field string) bool
- func (t *Table) HasFlag(flag uint16) bool
- func (t *Table) RemoveField(field *Field)
- func (t *Table) SetFlag(flag uint16)
- func (t *Table) String() string
- type TableModel
Constants ¶
const ( PrimaryKeyFlag = uint8(1) << iota ForeignKeyFlag NotNullFlag UniqueFlag ArrayFlag )
const ( HasOneRelation = 1 << iota BelongsToRelation HasManyRelation Many2ManyRelation )
const ( AfterQueryHookFlag = uint16(1) << iota BeforeSelectQueryHookFlag AfterSelectHookFlag BeforeInsertHookFlag AfterInsertHookFlag BeforeUpdateHookFlag AfterUpdateHookFlag BeforeDeleteHookFlag AfterDeleteHookFlag )
Variables ¶
This section is empty.
Functions ¶
func CreateComposite ¶
func CreateComposite(db DB, model interface{}, opt *CreateCompositeOptions) error
func CreateTable ¶
func CreateTable(db DB, model interface{}, opt *CreateTableOptions) error
func DropComposite ¶
func DropComposite(db DB, model interface{}, opt *DropCompositeOptions) error
func DropTable ¶
func DropTable(db DB, model interface{}, opt *DropTableOptions) error
func ForceDelete ¶
func RegisterTable ¶
func RegisterTable(strct interface{})
RegisterTable registers a struct as SQL table. It is usually used to register intermediate table in many to many relationship.
func SetTableNameInflector ¶
SetTableNameInflector overrides the default func that pluralizes model name to get table name, e.g. my_article becomes my_articles.
Types ¶
type ColumnScanner ¶
type ColumnScanner interface { // Scan assigns a column value from a row. // // An error should be returned if the value can not be stored // without loss of information. ScanColumn(colIdx int, colName string, rd types.Reader, n int) error }
ColumnScanner is used to scan column values.
type CreateCompositeOptions ¶
type CreateCompositeOptions struct {
Varchar int // replaces PostgreSQL data type `text` with `varchar(n)`
}
type CreateTableOptions ¶
type CreateTableOptions struct { Temp bool IfNotExists bool Varchar int // replaces PostgreSQL data type `text` with `varchar(n)` // FKConstraints causes CreateTable to create foreign key constraints // for has one relations. ON DELETE hook can be added using tag // `sql:"on_delete:RESTRICT"` on foreign key field. FKConstraints bool }
type DB ¶
type DB interface { Model(model ...interface{}) *Query Select(model interface{}) error Insert(model ...interface{}) error Update(model interface{}) error Delete(model interface{}) error ForceDelete(model interface{}) error Exec(query interface{}, params ...interface{}) (Result, error) ExecOne(query interface{}, params ...interface{}) (Result, error) Query(coll, query interface{}, params ...interface{}) (Result, error) QueryOne(model, query interface{}, params ...interface{}) (Result, error) CopyFrom(r io.Reader, query interface{}, params ...interface{}) (Result, error) CopyTo(w io.Writer, query interface{}, params ...interface{}) (Result, error) Context() context.Context QueryFormatter }
DB is a common interface for pg.DB and pg.Tx types.
type Discard ¶
type Discard struct {
// contains filtered or unexported fields
}
func (Discard) AddModel ¶
func (m Discard) AddModel(ColumnScanner) error
func (Discard) AfterDelete ¶
func (Discard) AfterInsert ¶
func (Discard) AfterQuery ¶
func (Discard) AfterSelect ¶
func (Discard) AfterUpdate ¶
func (Discard) BeforeDelete ¶
func (Discard) BeforeInsert ¶
func (Discard) BeforeSelectQuery ¶
func (Discard) BeforeUpdate ¶
func (Discard) NewModel ¶
func (m Discard) NewModel() ColumnScanner
type DropCompositeOptions ¶
type DropTableOptions ¶
type Field ¶
type Field struct { Field reflect.StructField Type reflect.Type GoName string // struct field name, e.g. Id SQLName string // SQL name, .e.g. id Column types.Q // escaped SQL name, e.g. "id" SQLType string Index []int Default types.Q OnDelete string // contains filtered or unexported fields }
func (*Field) AppendValue ¶
type FormatAppender ¶
type FormatAppender interface {
AppendFormat([]byte, QueryFormatter) []byte
}
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
func (Formatter) AppendBytes ¶
func (Formatter) FormatQuery ¶
type HooklessModel ¶
type HooklessModel interface { // Init is responsible to initialize/reset model state. // It is called only once no matter how many rows // were returned by database. Init() error // NewModel returns ColumnScanner that is used to scan columns // from the current row. It is called once for every row. NewModel() ColumnScanner // AddModel adds ColumnScanner created by NewModel to the Collection. AddModel(ColumnScanner) error }
type Method ¶
type Method struct { Index int // contains filtered or unexported fields }
func (*Method) AppendValue ¶
type Model ¶
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
func (*Query) AppendFormat ¶
func (q *Query) AppendFormat(b []byte, f QueryFormatter) []byte
func (*Query) Column ¶
Column adds a column to the Query quoting it according to PostgreSQL rules. Does not expand params like ?TableAlias etc. ColumnExpr can be used to bypass quoting restriction or for params expansion. Column name can be:
- column_name,
- table_alias.column_name,
- table_alias.*.
func (*Query) ColumnExpr ¶
ColumnExpr adds column expression to the Query.
func (*Query) Count ¶
Count returns number of rows matching the query using count aggregate function.
func (*Query) CountEstimate ¶
CountEstimate uses EXPLAIN to get estimated number of rows returned the query. If that number is bigger than the threshold it returns the estimation. Otherwise it executes another query using count aggregate function and returns the result.
func (*Query) CreateTable ¶
func (q *Query) CreateTable(opt *CreateTableOptions) error
func (*Query) Delete ¶
Delete deletes the model. When model has deleted_at column the row is soft deleted instead.
func (*Query) DropTable ¶
func (q *Query) DropTable(opt *DropTableOptions) error
func (*Query) ExcludeColumn ¶
ExcludeColumn excludes a column from the list of to be selected columns.
func (*Query) Exists ¶
Exists returns true or false depending if there are any rows matching the query.
func (*Query) First ¶
First sorts rows by primary key and selects the first row. It is a shortcut for:
q.OrderExpr("id ASC").Limit(1)
func (*Query) ForEach ¶
ForEach calls the function for each row returned by the query without loading all rows into the memory. Function accepts a struct, pointer to a struct, orm.Model, or values for columns in a row. Function must return an error.
func (*Query) ForceDelete ¶
Delete forces delete of the model with deleted_at column.
func (*Query) FormatQuery ¶
func (*Query) GetModel ¶
func (q *Query) GetModel() TableModel
func (*Query) Last ¶
Last sorts rows by primary key and selects the last row. It is a shortcut for:
q.OrderExpr("id DESC").Limit(1)
func (*Query) OnConflict ¶
func (*Query) Order ¶
Order adds sort order to the Query quoting column name. Does not expand params like ?TableAlias etc. OrderExpr can be used to bypass quoting restriction or for params expansion.
func (*Query) Relation ¶
Relation adds a relation to the query. Relation name can be:
- RelationName to select all columns,
- RelationName.column_name,
- RelationName._ to join relation without selecting relation columns.
func (*Query) SelectAndCount ¶
SelectAndCount runs Select and Count in two goroutines, waits for them to finish and returns the result. If query limit is -1 it does not select any data and only counts the results.
func (*Query) SelectAndCountEstimate ¶
func (q *Query) SelectAndCountEstimate(threshold int, values ...interface{}) (count int, firstErr error)
SelectAndCountEstimate runs Select and CountEstimate in two goroutines, waits for them to finish and returns the result. If query limit is -1 it does not select any data and only counts the results.
func (*Query) SelectOrInsert ¶
SelectOrInsert selects the model inserting one if it does not exist. It returns true when model was inserted.
func (*Query) UpdateNotNull ¶
Update updates the model omitting null columns.
func (*Query) WhereGroup ¶
WhereGroup encloses conditions added in the function in parentheses.
q.Where("TRUE"). WhereGroup(func(q *orm.Query) (*orm.Query, error)) { q = q.WhereOr("FALSE").WhereOr("TRUE"). return q, nil })
generates
WHERE TRUE AND (FALSE OR TRUE)
func (*Query) WhereIn ¶
WhereIn is a shortcut for Where and pg.In to work with IN operator:
WhereIn("id IN (?)", 1, 2, 3)
func (*Query) WhereOrGroup ¶
WhereOrGroup encloses conditions added in the function in parentheses.
q.Where("TRUE"). WhereOrGroup(func(q *orm.Query) (*orm.Query, error)) { q = q.Where("FALSE").Where("TRUE"). return q, nil })
generates
WHERE TRUE OR (FALSE AND TRUE)
func (*Query) WherePK ¶
WherePK adds condition based on the model primary keys. Usually it is the same as:
Where("id = ?id")
func (*Query) WhereStruct ¶
WhereStruct generates conditions for the struct fields with non-zero values:
- Foo int - Where("foo = ?", strct.Foo)
- Foo []int - Where("foo = ANY(?)", pg.Array(strct.Foo))
- FooNEQ int - Where("foo != ?", strct.Foo)
- FooExclude int - Where("foo != ?", strct.Foo)
- FooGT int - Where("foo > ?", strct.Foo)
- FooGTE int - Where("foo >= ?", strct.Foo)
- FooLT int - Where("foo < ?", strct.Foo)
- FooLTE int - Where("foo <= ?", strct.Foo)
urlvalues.Decode can be used to decode url.Values into the struct.
Following field tags are recognized:
- pg:"-" - field is ignored.
- pg:",nowhere" - field is decoded but is ignored by WhereStruct.
- pg:",nodecode" - field is not decoded but is used by WhereStruct.
- pg:",required" - condition is added for zero values as well.
type QueryAppender ¶
type QueryAppender interface { Copy() QueryAppender Query() *Query AppendQuery(dst []byte) ([]byte, error) }
type QueryFormatter ¶
type Relation ¶
type Result ¶
type Result interface { Model() Model // RowsAffected returns the number of rows affected by SELECT, INSERT, UPDATE, // or DELETE queries. It returns -1 if query can't possibly affect any rows, // e.g. in case of CREATE or SHOW queries. RowsAffected() int // RowsReturned returns the number of rows returned by the query. RowsReturned() int }
A Result summarizes an executed SQL command.
type Table ¶
type Table struct { Type reflect.Type TypeName string Alias types.Q ModelName string Name string FullName types.Q FullNameForSelects types.Q Fields []*Field // PKs + DataFields PKs []*Field DataFields []*Field FieldsMap map[string]*Field Methods map[string]*Method Relations map[string]*Relation Unique map[string][]*Field SoftDeleteField *Field // contains filtered or unexported fields }
Table represents a SQL table created from Go struct.
func (*Table) AppendParam ¶
func (*Table) RemoveField ¶
type TableModel ¶
type TableModel interface { Model IsNil() bool Table() *Table Relation() *Relation AppendParam([]byte, QueryFormatter, string) ([]byte, bool) Join(string, func(*Query) (*Query, error)) *join GetJoin(string) *join GetJoins() []join AddJoin(join) *join Root() reflect.Value Index() []int ParentIndex() []int Mount(reflect.Value) Kind() reflect.Kind Value() reflect.Value // contains filtered or unexported methods }
Source Files ¶
- composite.go
- composite_create.go
- composite_drop.go
- composite_parser.go
- count_estimate.go
- delete.go
- field.go
- format.go
- hook.go
- inflection.go
- insert.go
- join.go
- model.go
- model_discard.go
- model_func.go
- model_scan.go
- model_slice.go
- model_table.go
- model_table_m2m.go
- model_table_many.go
- model_table_slice.go
- model_table_struct.go
- orm.go
- query.go
- relation.go
- result.go
- select.go
- struct_filter.go
- table.go
- table_create.go
- table_drop.go
- table_params.go
- tables.go
- update.go
- util.go