Documentation ¶
Overview ¶
The API in this package is not stable and may change without any notice.
Index ¶
- Constants
- func RegisterTable(strct interface{})
- func Scan(values ...interface{}) scanValuesModel
- func SetTableNameInflector(fn func(string) string)
- type AfterDeleteHook
- type AfterInsertHook
- type AfterScanHook
- type AfterSelectHook
- type AfterUpdateHook
- type BeforeDeleteHook
- type BeforeInsertHook
- type BeforeScanHook
- type BeforeUpdateHook
- type ColumnScanner
- type ColumnsAppender
- type CreateCompositeOptions
- type CreateCompositeQuery
- func (q *CreateCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
- func (q *CreateCompositeQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *CreateCompositeQuery) Clone() QueryCommand
- func (q *CreateCompositeQuery) Operation() QueryOp
- func (q *CreateCompositeQuery) Query() *Query
- func (q *CreateCompositeQuery) String() string
- type CreateTableOptions
- type CreateTableQuery
- func (q *CreateTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *CreateTableQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *CreateTableQuery) Clone() QueryCommand
- func (q *CreateTableQuery) Operation() QueryOp
- func (q *CreateTableQuery) Query() *Query
- func (q *CreateTableQuery) String() string
- type DB
- type DeleteQuery
- func (q *DeleteQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *DeleteQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *DeleteQuery) Clone() QueryCommand
- func (q *DeleteQuery) Operation() QueryOp
- func (q *DeleteQuery) Query() *Query
- func (q *DeleteQuery) String() string
- type Discard
- func (m Discard) AddColumnScanner(ColumnScanner) error
- func (Discard) AfterDelete(ctx context.Context) error
- func (Discard) AfterInsert(ctx context.Context) error
- func (Discard) AfterScan(ctx context.Context) error
- func (Discard) AfterSelect(ctx context.Context) error
- func (Discard) AfterUpdate(ctx context.Context) error
- func (Discard) BeforeDelete(ctx context.Context) (context.Context, error)
- func (Discard) BeforeInsert(ctx context.Context) (context.Context, error)
- func (Discard) BeforeUpdate(ctx context.Context) (context.Context, error)
- func (Discard) Init() error
- func (m Discard) NextColumnScanner() ColumnScanner
- func (m Discard) ScanColumn(col types.ColumnInfo, rd types.Reader, n int) error
- type DropCompositeOptions
- type DropCompositeQuery
- func (q *DropCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
- func (q *DropCompositeQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *DropCompositeQuery) Clone() QueryCommand
- func (q *DropCompositeQuery) Operation() QueryOp
- func (q *DropCompositeQuery) Query() *Query
- func (q *DropCompositeQuery) String() string
- type DropTableOptions
- type DropTableQuery
- func (q *DropTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *DropTableQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *DropTableQuery) Clone() QueryCommand
- func (q *DropTableQuery) Operation() QueryOp
- func (q *DropTableQuery) Query() *Query
- func (q *DropTableQuery) String() string
- type Field
- func (f *Field) AppendValue(b []byte, strct reflect.Value, quote int) []byte
- func (f *Field) Clone() *Field
- func (f *Field) HasZeroValue(strct reflect.Value) bool
- func (f *Field) NullZero() bool
- func (f *Field) ScanValue(strct reflect.Value, rd types.Reader, n int) error
- func (f *Field) Value(strct reflect.Value) reflect.Value
- type Formatter
- func (f *Formatter) FormatQuery(dst []byte, query string, params ...interface{}) []byte
- func (f *Formatter) FormatQueryBytes(dst, query []byte, params ...interface{}) []byte
- func (f *Formatter) Param(param string) interface{}
- func (f *Formatter) String() string
- func (f *Formatter) WithModel(model interface{}) *Formatter
- func (f *Formatter) WithParam(param string, value interface{}) *Formatter
- func (f *Formatter) WithTableModel(model TableModel) *Formatter
- type HooklessModel
- type InsertQuery
- func (q *InsertQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *InsertQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *InsertQuery) Clone() QueryCommand
- func (q *InsertQuery) Operation() QueryOp
- func (q *InsertQuery) Query() *Query
- func (q *InsertQuery) String() string
- type Method
- type Model
- type Query
- func (q *Query) AllWithDeleted() *Query
- func (q *Query) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
- func (q *Query) Apply(fn func(*Query) *Query) *Query
- func (q *Query) Clone() *Query
- func (q *Query) Column(columns ...string) *Query
- func (q *Query) ColumnExpr(expr string, params ...interface{}) *Query
- func (q *Query) CopyFrom(ctx context.Context, r io.Reader, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) CopyTo(ctx context.Context, w io.Writer, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) Count(ctx context.Context) (int, error)
- func (q *Query) CountEstimate(ctx context.Context, threshold int) (int, error)
- func (q *Query) CreateComposite(ctx context.Context, opt *CreateCompositeOptions) error
- func (q *Query) CreateTable(ctx context.Context, opt *CreateTableOptions) error
- func (q *Query) DB(db DB) *Query
- func (q *Query) Delete(ctx context.Context, values ...interface{}) (Result, error)
- func (q *Query) Deleted() *Query
- func (q *Query) Distinct() *Query
- func (q *Query) DistinctOn(expr string, params ...interface{}) *Query
- func (q *Query) DropComposite(ctx context.Context, opt *DropCompositeOptions) error
- func (q *Query) DropTable(ctx context.Context, opt *DropTableOptions) error
- func (q *Query) Err(err error) *Query
- func (q *Query) Except(other *Query) *Query
- func (q *Query) ExceptAll(other *Query) *Query
- func (q *Query) ExcludeColumn(columns ...string) *Query
- func (q *Query) Exec(ctx context.Context, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) ExecOne(ctx context.Context, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) Exists(ctx context.Context) (bool, error)
- func (q *Query) First(ctx context.Context) error
- func (q *Query) For(s string, params ...interface{}) *Query
- func (q *Query) ForEach(ctx context.Context, fn interface{}) error
- func (q *Query) ForceDelete(ctx context.Context, values ...interface{}) (Result, error)
- 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(ctx context.Context, values ...interface{}) (Result, error)
- func (q *Query) Intersect(other *Query) *Query
- func (q *Query) IntersectAll(other *Query) *Query
- 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(ctx context.Context) error
- func (q *Query) Limit(n int) *Query
- func (q *Query) Model(model ...interface{}) *Query
- func (q *Query) ModelTableExpr(expr string, params ...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(ctx context.Context, model, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) QueryOne(ctx context.Context, model, query interface{}, params ...interface{}) (Result, error)
- func (q *Query) Relation(name string, apply ...func(*Query) *Query) *Query
- func (q *Query) Returning(s string, params ...interface{}) *Query
- func (q *Query) Select(ctx context.Context, values ...interface{}) error
- func (q *Query) SelectAndCount(ctx context.Context, values ...interface{}) (count int, firstErr error)
- func (q *Query) SelectAndCountEstimate(ctx context.Context, threshold int, values ...interface{}) (count int, firstErr error)
- func (q *Query) SelectOrInsert(ctx context.Context, 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) TableModel() TableModel
- func (q *Query) Union(other *Query) *Query
- func (q *Query) UnionAll(other *Query) *Query
- func (q *Query) Update(ctx context.Context, scan ...interface{}) (Result, error)
- func (q *Query) UpdateNotZero(ctx context.Context, 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) *Query
- func (q *Query) WhereIn(where string, slice interface{}) *Query
- func (q *Query) WhereNotGroup(fn func(*Query) *Query) *Query
- func (q *Query) WhereOr(condition string, params ...interface{}) *Query
- func (q *Query) WhereOrGroup(fn func(*Query) *Query) *Query
- func (q *Query) WhereOrNotGroup(fn func(*Query) *Query) *Query
- func (q *Query) WherePK(cols ...string) *Query
- func (q *Query) WithDelete(name string, subq *Query) *Query
- func (q *Query) WithInsert(name string, subq *Query) *Query
- func (q *Query) WithSelect(name string, subq *Query) *Query
- func (q *Query) WithUpdate(name string, subq *Query) *Query
- func (q *Query) WithValues(name string, subq *Query) *Query
- func (q *Query) WrapWith(name string) *Query
- type QueryAppender
- type QueryCommand
- type QueryFormatter
- type QueryOp
- type Relation
- type Result
- type SafeQueryAppender
- type SelectQuery
- func (q *SelectQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *SelectQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *SelectQuery) Clone() QueryCommand
- func (q *SelectQuery) Operation() QueryOp
- func (q *SelectQuery) Query() *Query
- func (q *SelectQuery) String() string
- type Table
- type TableModel
- type TemplateAppender
- type UpdateQuery
- func (q *UpdateQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *UpdateQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *UpdateQuery) Clone() QueryCommand
- func (q *UpdateQuery) Operation() QueryOp
- func (q *UpdateQuery) Query() *Query
- func (q *UpdateQuery) String() string
- type ValuesQuery
- func (q *ValuesQuery) AppendColumns(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *ValuesQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
- func (q *ValuesQuery) AppendTemplate(b []byte) ([]byte, error)
- func (q *ValuesQuery) Clone() QueryCommand
- func (q *ValuesQuery) Operation() QueryOp
- func (q *ValuesQuery) Query() *Query
- func (q *ValuesQuery) String() string
Constants ¶
const ( PrimaryKeyFlag = uint8(1) << iota ForeignKeyFlag NotNullFlag UseZeroFlag UniqueFlag ArrayFlag )
const ( InvalidRelation = iota HasOneRelation BelongsToRelation HasManyRelation Many2ManyRelation )
Variables ¶
This section is empty.
Functions ¶
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 AfterDeleteHook ¶
type AfterInsertHook ¶
type AfterScanHook ¶
type AfterSelectHook ¶
type AfterUpdateHook ¶
type BeforeDeleteHook ¶
type BeforeInsertHook ¶
type BeforeScanHook ¶
type BeforeUpdateHook ¶
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(col types.ColumnInfo, rd types.Reader, n int) error }
ColumnScanner is used to scan column values.
type ColumnsAppender ¶
type ColumnsAppender interface {
AppendColumns(fmter QueryFormatter, b []byte) ([]byte, error)
}
type CreateCompositeOptions ¶
type CreateCompositeOptions struct {
Varchar int // replaces PostgreSQL data type `text` with `varchar(n)`
}
type CreateCompositeQuery ¶
type CreateCompositeQuery struct {
// contains filtered or unexported fields
}
func NewCreateCompositeQuery ¶
func NewCreateCompositeQuery(q *Query, opt *CreateCompositeOptions) *CreateCompositeQuery
func (*CreateCompositeQuery) AppendQuery ¶
func (q *CreateCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
func (*CreateCompositeQuery) AppendTemplate ¶
func (q *CreateCompositeQuery) AppendTemplate(b []byte) ([]byte, error)
func (*CreateCompositeQuery) Clone ¶
func (q *CreateCompositeQuery) Clone() QueryCommand
func (*CreateCompositeQuery) Operation ¶
func (q *CreateCompositeQuery) Operation() QueryOp
func (*CreateCompositeQuery) Query ¶
func (q *CreateCompositeQuery) Query() *Query
func (*CreateCompositeQuery) String ¶
func (q *CreateCompositeQuery) String() string
type CreateTableOptions ¶
type CreateTableOptions struct { Varchar int // replaces PostgreSQL data type `text` with `varchar(n)` Temp bool IfNotExists bool // FKConstraints causes CreateTable to create foreign key constraints // for has one relations. ON DELETE hook can be added using tag // `pg:"on_delete:RESTRICT"` on foreign key field. ON UPDATE hook can be added using tag // `pg:"on_update:CASCADE"` FKConstraints bool }
type CreateTableQuery ¶
type CreateTableQuery struct {
// contains filtered or unexported fields
}
func NewCreateTableQuery ¶
func NewCreateTableQuery(q *Query, opt *CreateTableOptions) *CreateTableQuery
func (*CreateTableQuery) AppendQuery ¶
func (q *CreateTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*CreateTableQuery) AppendTemplate ¶
func (q *CreateTableQuery) AppendTemplate(b []byte) ([]byte, error)
func (*CreateTableQuery) Clone ¶
func (q *CreateTableQuery) Clone() QueryCommand
func (*CreateTableQuery) Operation ¶
func (q *CreateTableQuery) Operation() QueryOp
func (*CreateTableQuery) Query ¶
func (q *CreateTableQuery) Query() *Query
func (*CreateTableQuery) String ¶
func (q *CreateTableQuery) String() string
type DB ¶
type DB interface { Model(model ...interface{}) *Query Exec(ctx context.Context, query interface{}, params ...interface{}) (Result, error) ExecOne(ctx context.Context, query interface{}, params ...interface{}) (Result, error) Query(ctx context.Context, model, query interface{}, params ...interface{}) (Result, error) QueryOne(ctx context.Context, model, query interface{}, params ...interface{}) (Result, error) CopyFrom(ctx context.Context, r io.Reader, query interface{}, params ...interface{}) (Result, error) CopyTo(ctx context.Context, w io.Writer, query interface{}, params ...interface{}) (Result, error) Formatter() QueryFormatter }
DB is a common interface for pg.DB and pg.Tx types.
type DeleteQuery ¶
type DeleteQuery struct {
// contains filtered or unexported fields
}
func NewDeleteQuery ¶
func NewDeleteQuery(q *Query) *DeleteQuery
func (*DeleteQuery) AppendQuery ¶
func (q *DeleteQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*DeleteQuery) AppendTemplate ¶
func (q *DeleteQuery) AppendTemplate(b []byte) ([]byte, error)
func (*DeleteQuery) Clone ¶
func (q *DeleteQuery) Clone() QueryCommand
func (*DeleteQuery) Operation ¶
func (q *DeleteQuery) Operation() QueryOp
func (*DeleteQuery) Query ¶
func (q *DeleteQuery) Query() *Query
func (*DeleteQuery) String ¶
func (q *DeleteQuery) String() string
type Discard ¶
type Discard struct {
// contains filtered or unexported fields
}
func (Discard) AddColumnScanner ¶
func (m Discard) AddColumnScanner(ColumnScanner) error
func (Discard) AfterDelete ¶
func (Discard) AfterInsert ¶
func (Discard) AfterSelect ¶
func (Discard) AfterUpdate ¶
func (Discard) BeforeDelete ¶
func (Discard) BeforeInsert ¶
func (Discard) BeforeUpdate ¶
func (Discard) NextColumnScanner ¶
func (m Discard) NextColumnScanner() ColumnScanner
func (Discard) ScanColumn ¶
type DropCompositeOptions ¶
type DropCompositeQuery ¶
type DropCompositeQuery struct {
// contains filtered or unexported fields
}
func NewDropCompositeQuery ¶
func NewDropCompositeQuery(q *Query, opt *DropCompositeOptions) *DropCompositeQuery
func (*DropCompositeQuery) AppendQuery ¶
func (q *DropCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
func (*DropCompositeQuery) AppendTemplate ¶
func (q *DropCompositeQuery) AppendTemplate(b []byte) ([]byte, error)
func (*DropCompositeQuery) Clone ¶
func (q *DropCompositeQuery) Clone() QueryCommand
func (*DropCompositeQuery) Operation ¶
func (q *DropCompositeQuery) Operation() QueryOp
func (*DropCompositeQuery) Query ¶
func (q *DropCompositeQuery) Query() *Query
func (*DropCompositeQuery) String ¶
func (q *DropCompositeQuery) String() string
type DropTableOptions ¶
type DropTableQuery ¶
type DropTableQuery struct {
// contains filtered or unexported fields
}
func NewDropTableQuery ¶
func NewDropTableQuery(q *Query, opt *DropTableOptions) *DropTableQuery
func (*DropTableQuery) AppendQuery ¶
func (q *DropTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*DropTableQuery) AppendTemplate ¶
func (q *DropTableQuery) AppendTemplate(b []byte) ([]byte, error)
func (*DropTableQuery) Clone ¶
func (q *DropTableQuery) Clone() QueryCommand
func (*DropTableQuery) Operation ¶
func (q *DropTableQuery) Operation() QueryOp
func (*DropTableQuery) Query ¶
func (q *DropTableQuery) Query() *Query
func (*DropTableQuery) String ¶
func (q *DropTableQuery) String() string
type Field ¶
type Field struct { Field reflect.StructField Type reflect.Type Index []int GoName string // struct field name, e.g. Id SQLName string // SQL name, .e.g. id Column types.Safe // escaped SQL name, e.g. "id" SQLType string UserSQLType string Default types.Safe OnDelete string OnUpdate string // contains filtered or unexported fields }
func (*Field) AppendValue ¶
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
func NewFormatter ¶
func NewFormatter() *Formatter
func (*Formatter) FormatQuery ¶
func (*Formatter) FormatQueryBytes ¶
func (*Formatter) WithTableModel ¶
func (f *Formatter) WithTableModel(model TableModel) *Formatter
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. Init() error // NextColumnScanner returns a ColumnScanner that is used to scan columns // from the current row. It is called once for every row. NextColumnScanner() ColumnScanner // AddColumnScanner adds the ColumnScanner to the model. AddColumnScanner(ColumnScanner) error }
type InsertQuery ¶
type InsertQuery struct {
// contains filtered or unexported fields
}
func NewInsertQuery ¶
func NewInsertQuery(q *Query) *InsertQuery
func (*InsertQuery) AppendQuery ¶
func (q *InsertQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*InsertQuery) AppendTemplate ¶
func (q *InsertQuery) AppendTemplate(b []byte) ([]byte, error)
func (*InsertQuery) Clone ¶
func (q *InsertQuery) Clone() QueryCommand
func (*InsertQuery) Operation ¶
func (q *InsertQuery) Operation() QueryOp
func (*InsertQuery) Query ¶
func (q *InsertQuery) Query() *Query
func (*InsertQuery) String ¶
func (q *InsertQuery) String() string
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) AllWithDeleted ¶
AllWithDeleted changes query to return all rows including soft deleted ones.
func (*Query) AppendQuery ¶
func (q *Query) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
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) CopyFrom ¶
func (q *Query) CopyFrom( ctx context.Context, r io.Reader, query interface{}, params ...interface{}, ) (Result, error)
CopyFrom is an alias from DB.CopyFrom.
func (*Query) CopyTo ¶
func (q *Query) CopyTo( ctx context.Context, w io.Writer, query interface{}, params ...interface{}, ) (Result, error)
CopyTo is an alias from DB.CopyTo.
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) CreateComposite ¶
func (q *Query) CreateComposite(ctx context.Context, opt *CreateCompositeOptions) error
func (*Query) CreateTable ¶
func (q *Query) CreateTable(ctx context.Context, opt *CreateTableOptions) error
func (*Query) Delete ¶
Delete deletes the model. When model has deleted_at column the row is soft deleted instead.
func (*Query) DistinctOn ¶
func (*Query) DropComposite ¶
func (q *Query) DropComposite(ctx context.Context, opt *DropCompositeOptions) error
func (*Query) DropTable ¶
func (q *Query) DropTable(ctx context.Context, opt *DropTableOptions) error
func (*Query) ExcludeColumn ¶
ExcludeColumn excludes a column from the list of to be selected columns.
func (*Query) Exec ¶
func (q *Query) Exec( ctx context.Context, query interface{}, params ...interface{}, ) (Result, error)
Exec is an alias for DB.Exec.
func (*Query) ExecOne ¶
func (q *Query) ExecOne( ctx context.Context, query interface{}, params ...interface{}, ) (Result, error)
ExecOne is an alias for DB.ExecOne.
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 can accept a struct, a pointer to a struct, an orm.Model, or values for the columns in a row. Function must return an error.
func (*Query) ForceDelete ¶
Delete forces delete of the model with deleted_at column.
func (*Query) IntersectAll ¶
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) ModelTableExpr ¶
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) Query ¶
func (q *Query) Query( ctx context.Context, model, query interface{}, params ...interface{}, ) (Result, error)
Query is an alias for DB.Query.
func (*Query) QueryOne ¶
func (q *Query) QueryOne( ctx context.Context, model, query interface{}, params ...interface{}, ) (Result, error)
QueryOne is an alias for DB.QueryOne.
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) Returning ¶
Returning adds a RETURNING clause to the query.
`Returning("NULL")` can be used to suppress default returning clause generated by go-pg for INSERT queries to get values for null columns.
func (*Query) SelectAndCount ¶
func (q *Query) SelectAndCount( ctx context.Context, values ...interface{}, ) (count int, firstErr error)
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( ctx context.Context, 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) TableModel ¶
func (q *Query) TableModel() TableModel
func (*Query) UpdateNotZero ¶
Update updates the model omitting fields with zero values such as:
- empty string,
- 0,
- zero time,
- empty map or slice,
- byte array with all zeroes,
- nil ptr,
- types with method `IsZero() == true`.
func (*Query) WhereGroup ¶
WhereGroup encloses conditions added in the function in parentheses.
q.Where("TRUE"). WhereGroup(func(q *orm.Query) *orm.Query { return q.WhereOr("FALSE").WhereOr("TRUE"). })
generates
WHERE TRUE AND (FALSE OR TRUE)
func (*Query) WhereNotGroup ¶
WhereGroup encloses conditions added in the function in parentheses.
q.Where("TRUE"). WhereNotGroup(func(q *orm.Query) *orm.Query { return q.WhereOr("FALSE").WhereOr("TRUE"). })
generates
WHERE TRUE AND NOT (FALSE OR TRUE)
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) WhereOrNotGroup ¶
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 NOT (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) WithSelect ¶
With adds subq as common table expression with the given name.
type QueryAppender ¶
type QueryAppender interface {
AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
}
type QueryCommand ¶
type QueryCommand interface { QueryAppender TemplateAppender String() string Operation() QueryOp Clone() QueryCommand Query() *Query }
type QueryFormatter ¶
type QueryOp ¶
type QueryOp string
const ( SelectOp QueryOp = "SELECT" InsertOp QueryOp = "INSERT" UpdateOp QueryOp = "UPDATE" DeleteOp QueryOp = "DELETE" CreateTableOp QueryOp = "CREATE TABLE" DropTableOp QueryOp = "DROP TABLE" CreateCompositeOp QueryOp = "CREATE COMPOSITE" DropCompositeOp QueryOp = "DROP COMPOSITE" ValuesOp QueryOp = "VALUES" )
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 }
Result summarizes an executed SQL command.
type SafeQueryAppender ¶
type SafeQueryAppender struct {
// contains filtered or unexported fields
}
func (*SafeQueryAppender) AppendQuery ¶
func (q *SafeQueryAppender) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
func (*SafeQueryAppender) AppendValue ¶
func (q *SafeQueryAppender) AppendValue(b []byte, quote int) ([]byte, error)
func (*SafeQueryAppender) Value ¶
func (q *SafeQueryAppender) Value() types.Safe
type SelectQuery ¶
type SelectQuery struct {
// contains filtered or unexported fields
}
func NewSelectQuery ¶
func NewSelectQuery(q *Query) *SelectQuery
func (*SelectQuery) AppendQuery ¶
func (q *SelectQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*SelectQuery) AppendTemplate ¶
func (q *SelectQuery) AppendTemplate(b []byte) ([]byte, error)
func (*SelectQuery) Clone ¶
func (q *SelectQuery) Clone() QueryCommand
func (*SelectQuery) Operation ¶
func (q *SelectQuery) Operation() QueryOp
func (*SelectQuery) Query ¶
func (q *SelectQuery) Query() *Query
func (*SelectQuery) String ¶
func (q *SelectQuery) String() string
type Table ¶
type Table struct { Type reflect.Type TypeName string Alias types.Safe ModelName string SQLName types.Safe SQLNameForSelects types.Safe Tablespace types.Safe PartitionBy string 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 SetSoftDeleteField func(fv reflect.Value) error // 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(QueryFormatter, []byte, string) ([]byte, bool) Join(string, func(*Query) *Query) *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 }
type TemplateAppender ¶
type UpdateQuery ¶
type UpdateQuery struct {
// contains filtered or unexported fields
}
func NewUpdateQuery ¶
func NewUpdateQuery(q *Query, omitZero bool) *UpdateQuery
func (*UpdateQuery) AppendQuery ¶
func (q *UpdateQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*UpdateQuery) AppendTemplate ¶
func (q *UpdateQuery) AppendTemplate(b []byte) ([]byte, error)
func (*UpdateQuery) Clone ¶
func (q *UpdateQuery) Clone() QueryCommand
func (*UpdateQuery) Operation ¶
func (q *UpdateQuery) Operation() QueryOp
func (*UpdateQuery) Query ¶
func (q *UpdateQuery) Query() *Query
func (*UpdateQuery) String ¶
func (q *UpdateQuery) String() string
type ValuesQuery ¶
type ValuesQuery struct {
// contains filtered or unexported fields
}
func NewValuesQuery ¶
func NewValuesQuery(q *Query) *ValuesQuery
func (*ValuesQuery) AppendColumns ¶
func (q *ValuesQuery) AppendColumns(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*ValuesQuery) AppendQuery ¶
func (q *ValuesQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)
func (*ValuesQuery) AppendTemplate ¶
func (q *ValuesQuery) AppendTemplate(b []byte) ([]byte, error)
func (*ValuesQuery) Clone ¶
func (q *ValuesQuery) Clone() QueryCommand
func (*ValuesQuery) Operation ¶
func (q *ValuesQuery) Operation() QueryOp
func (*ValuesQuery) Query ¶
func (q *ValuesQuery) Query() *Query
func (*ValuesQuery) String ¶
func (q *ValuesQuery) String() string
Source Files ¶
- composite.go
- composite_create.go
- composite_drop.go
- composite_parser.go
- count_estimate.go
- field.go
- format.go
- hook.go
- join.go
- model.go
- model_discard.go
- model_func.go
- model_map.go
- model_map_slice.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
- msgpack.go
- orm.go
- query.go
- query_delete.go
- query_insert.go
- query_select.go
- query_table_create.go
- query_table_drop.go
- query_update.go
- query_values.go
- relation.go
- result.go
- table.go
- table_params.go
- tables.go
- types.go
- util.go