pg: github.com/go-pg/pg/orm Index | Files

package orm

import "github.com/go-pg/pg/orm"

The API in this package is not stable and may change without any notice.

Index

Package Files

composite.go composite_create.go composite_drop.go composite_parser.go count_estimate.go delete.go field.go format.go hook.go insert.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 relation.go result.go select.go table.go table_create.go table_drop.go table_params.go tables.go types.go update.go util.go

Constants

const (
    PrimaryKeyFlag = uint8(1) << iota
    ForeignKeyFlag
    NotNullFlag
    UseZeroFlag
    UniqueFlag
    ArrayFlag
)
const (
    InvalidRelation = iota
    HasOneRelation
    BelongsToRelation
    HasManyRelation
    Many2ManyRelation
)

func RegisterTable Uses

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 Scan Uses

func Scan(values ...interface{}) scanValuesModel

nolint

func SetTableNameInflector Uses

func SetTableNameInflector(fn func(string) string)

SetTableNameInflector overrides the default func that pluralizes model name to get table name, e.g. my_article becomes my_articles.

type AfterDeleteHook Uses

type AfterDeleteHook interface {
    AfterDelete(context.Context) error
}

type AfterInsertHook Uses

type AfterInsertHook interface {
    AfterInsert(context.Context) error
}

type AfterScanHook Uses

type AfterScanHook interface {
    AfterScan(context.Context) error
}

type AfterSelectHook Uses

type AfterSelectHook interface {
    AfterSelect(context.Context) error
}

type AfterUpdateHook Uses

type AfterUpdateHook interface {
    AfterUpdate(context.Context) error
}

type BeforeDeleteHook Uses

type BeforeDeleteHook interface {
    BeforeDelete(context.Context) (context.Context, error)
}

type BeforeInsertHook Uses

type BeforeInsertHook interface {
    BeforeInsert(context.Context) (context.Context, error)
}

type BeforeScanHook Uses

type BeforeScanHook interface {
    BeforeScan(context.Context) error
}

type BeforeUpdateHook Uses

type BeforeUpdateHook interface {
    BeforeUpdate(context.Context) (context.Context, error)
}

type ColumnScanner Uses

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 CreateCompositeOptions Uses

type CreateCompositeOptions struct {
    Varchar int // replaces PostgreSQL data type `text` with `varchar(n)`
}

type CreateCompositeQuery Uses

type CreateCompositeQuery struct {
    // contains filtered or unexported fields
}

func NewCreateCompositeQuery Uses

func NewCreateCompositeQuery(q *Query, opt *CreateCompositeOptions) *CreateCompositeQuery

func (*CreateCompositeQuery) AppendQuery Uses

func (q *CreateCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)

func (*CreateCompositeQuery) AppendTemplate Uses

func (q *CreateCompositeQuery) AppendTemplate(b []byte) ([]byte, error)

func (*CreateCompositeQuery) Clone Uses

func (q *CreateCompositeQuery) Clone() QueryCommand

func (*CreateCompositeQuery) Operation Uses

func (q *CreateCompositeQuery) Operation() QueryOp

func (*CreateCompositeQuery) Query Uses

func (q *CreateCompositeQuery) Query() *Query

func (*CreateCompositeQuery) String Uses

func (q *CreateCompositeQuery) String() string

type CreateTableOptions Uses

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 Uses

type CreateTableQuery struct {
    // contains filtered or unexported fields
}

func NewCreateTableQuery Uses

func NewCreateTableQuery(q *Query, opt *CreateTableOptions) *CreateTableQuery

func (*CreateTableQuery) AppendQuery Uses

func (q *CreateTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)

func (*CreateTableQuery) AppendTemplate Uses

func (q *CreateTableQuery) AppendTemplate(b []byte) ([]byte, error)

func (*CreateTableQuery) Clone Uses

func (q *CreateTableQuery) Clone() QueryCommand

func (*CreateTableQuery) Operation Uses

func (q *CreateTableQuery) Operation() QueryOp

func (*CreateTableQuery) Query Uses

func (q *CreateTableQuery) Query() *Query

func (*CreateTableQuery) String Uses

func (q *CreateTableQuery) String() string

type DB Uses

type DB interface {
    Model(model ...interface{}) *Query
    ModelContext(c context.Context, model ...interface{}) *Query

    Exec(query interface{}, params ...interface{}) (Result, error)
    ExecContext(c context.Context, query interface{}, params ...interface{}) (Result, error)
    ExecOne(query interface{}, params ...interface{}) (Result, error)
    ExecOneContext(c context.Context, query interface{}, params ...interface{}) (Result, error)
    Query(model, query interface{}, params ...interface{}) (Result, error)
    QueryContext(c context.Context, model, query interface{}, params ...interface{}) (Result, error)
    QueryOne(model, query interface{}, params ...interface{}) (Result, error)
    QueryOneContext(c context.Context, 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
    Formatter() QueryFormatter
}

DB is a common interface for pg.DB and pg.Tx types.

type DeleteQuery Uses

type DeleteQuery struct {
    // contains filtered or unexported fields
}

func NewDeleteQuery Uses

func NewDeleteQuery(q *Query) *DeleteQuery

func (*DeleteQuery) AppendQuery Uses

func (q *DeleteQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)

func (*DeleteQuery) AppendTemplate Uses

func (q *DeleteQuery) AppendTemplate(b []byte) ([]byte, error)

func (*DeleteQuery) Clone Uses

func (q *DeleteQuery) Clone() QueryCommand

func (*DeleteQuery) Operation Uses

func (q *DeleteQuery) Operation() QueryOp

func (*DeleteQuery) Query Uses

func (q *DeleteQuery) Query() *Query

func (*DeleteQuery) String Uses

func (q *DeleteQuery) String() string

type Discard Uses

type Discard struct {
    // contains filtered or unexported fields
}

func (Discard) AddColumnScanner Uses

func (m Discard) AddColumnScanner(ColumnScanner) error

func (Discard) AfterDelete Uses

func (Discard) AfterDelete(ctx context.Context) error

func (Discard) AfterInsert Uses

func (Discard) AfterInsert(ctx context.Context) error

func (Discard) AfterScan Uses

func (Discard) AfterScan(ctx context.Context) error

func (Discard) AfterSelect Uses

func (Discard) AfterSelect(ctx context.Context) error

func (Discard) AfterUpdate Uses

func (Discard) AfterUpdate(ctx context.Context) error

func (Discard) BeforeDelete Uses

func (Discard) BeforeDelete(ctx context.Context) (context.Context, error)

func (Discard) BeforeInsert Uses

func (Discard) BeforeInsert(ctx context.Context) (context.Context, error)

func (Discard) BeforeUpdate Uses

func (Discard) BeforeUpdate(ctx context.Context) (context.Context, error)

func (Discard) Init Uses

func (Discard) Init() error

func (Discard) NextColumnScanner Uses

func (m Discard) NextColumnScanner() ColumnScanner

func (Discard) ScanColumn Uses

func (m Discard) ScanColumn(col types.ColumnInfo, rd types.Reader, n int) error

type DropCompositeOptions Uses

type DropCompositeOptions struct {
    IfExists bool
    Cascade  bool
}

type DropCompositeQuery Uses

type DropCompositeQuery struct {
    // contains filtered or unexported fields
}

func NewDropCompositeQuery Uses

func NewDropCompositeQuery(q *Query, opt *DropCompositeOptions) *DropCompositeQuery

func (*DropCompositeQuery) AppendQuery Uses

func (q *DropCompositeQuery) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)

func (*DropCompositeQuery) AppendTemplate Uses

func (q *DropCompositeQuery) AppendTemplate(b []byte) ([]byte, error)

func (*DropCompositeQuery) Clone Uses

func (q *DropCompositeQuery) Clone() QueryCommand

func (*DropCompositeQuery) Operation Uses

func (q *DropCompositeQuery) Operation() QueryOp

func (*DropCompositeQuery) Query Uses

func (q *DropCompositeQuery) Query() *Query

func (*DropCompositeQuery) String Uses

func (q *DropCompositeQuery) String() string

type DropTableOptions Uses

type DropTableOptions struct {
    IfExists bool
    Cascade  bool
}

type DropTableQuery Uses

type DropTableQuery struct {
    // contains filtered or unexported fields
}

func NewDropTableQuery Uses

func NewDropTableQuery(q *Query, opt *DropTableOptions) *DropTableQuery

func (*DropTableQuery) AppendQuery Uses

func (q *DropTableQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)

func (*DropTableQuery) AppendTemplate Uses

func (q *DropTableQuery) AppendTemplate(b []byte) ([]byte, error)

func (*DropTableQuery) Clone Uses

func (q *DropTableQuery) Clone() QueryCommand

func (*DropTableQuery) Operation Uses

func (q *DropTableQuery) Operation() QueryOp

func (*DropTableQuery) Query Uses

func (q *DropTableQuery) Query() *Query

func (*DropTableQuery) String Uses

func (q *DropTableQuery) String() string

type Field Uses

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 Uses

func (f *Field) AppendValue(b []byte, strct reflect.Value, quote int) []byte

func (*Field) Clone Uses

func (f *Field) Clone() *Field

func (*Field) HasZeroValue Uses

func (f *Field) HasZeroValue(strct reflect.Value) bool

func (*Field) NullZero Uses

func (f *Field) NullZero() bool

func (*Field) ScanValue Uses

func (f *Field) ScanValue(strct reflect.Value, rd types.Reader, n int) error

func (*Field) Value Uses

func (f *Field) Value(strct reflect.Value) reflect.Value

type Formatter Uses

type Formatter struct {
    // contains filtered or unexported fields
}

func NewFormatter Uses

func NewFormatter() *Formatter

func (*Formatter) FormatQuery Uses

func (f *Formatter) FormatQuery(dst []byte, query string, params ...interface{}) []byte

func (*Formatter) FormatQueryBytes Uses

func (f *Formatter) FormatQueryBytes(dst, query []byte, params ...interface{}) []byte

func (*Formatter) Param Uses

func (f *Formatter) Param(param string) interface{}

func (*Formatter) String Uses

func (f *Formatter) String() string

func (*Formatter) WithModel Uses

func (f *Formatter) WithModel(model interface{}) *Formatter

func (*Formatter) WithParam Uses

func (f *Formatter) WithParam(param string, value interface{}) *Formatter

func (*Formatter) WithTableModel Uses

func (f *Formatter) WithTableModel(model TableModel) *Formatter

type HooklessModel Uses

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 Uses

type InsertQuery struct {
    // contains filtered or unexported fields
}

func NewInsertQuery Uses

func NewInsertQuery(q *Query) *InsertQuery

func (*InsertQuery) AppendQuery Uses

func (q *InsertQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)

func (*InsertQuery) AppendTemplate Uses

func (q *InsertQuery) AppendTemplate(b []byte) ([]byte, error)

func (*InsertQuery) Clone Uses

func (q *InsertQuery) Clone() QueryCommand

func (*InsertQuery) Operation Uses

func (q *InsertQuery) Operation() QueryOp

func (*InsertQuery) Query Uses

func (q *InsertQuery) Query() *Query

func (*InsertQuery) String Uses

func (q *InsertQuery) String() string

type Method Uses

type Method struct {
    Index int
    // contains filtered or unexported fields
}

func (*Method) AppendValue Uses

func (m *Method) AppendValue(dst []byte, strct reflect.Value, quote int) []byte

func (*Method) Has Uses

func (m *Method) Has(flag int8) bool

func (*Method) Value Uses

func (m *Method) Value(strct reflect.Value) reflect.Value

type Model Uses

type Model interface {
    HooklessModel

    AfterScanHook
    AfterSelectHook

    BeforeInsertHook
    AfterInsertHook

    BeforeUpdateHook
    AfterUpdateHook

    BeforeDeleteHook
    AfterDeleteHook
}

func NewModel Uses

func NewModel(value interface{}) (Model, error)

type Query Uses

type Query struct {
    // contains filtered or unexported fields
}

func NewQuery Uses

func NewQuery(db DB, model ...interface{}) *Query

func NewQueryContext Uses

func NewQueryContext(ctx context.Context, db DB, model ...interface{}) *Query

func (*Query) AllWithDeleted Uses

func (q *Query) AllWithDeleted() *Query

AllWithDeleted changes query to return all rows including soft deleted ones.

func (*Query) AppendQuery Uses

func (q *Query) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)

func (*Query) Apply Uses

func (q *Query) Apply(fn func(*Query) (*Query, error)) *Query

Apply calls the fn passing the Query as an argument.

func (*Query) Clone Uses

func (q *Query) Clone() *Query

Clone clones the Query.

func (*Query) Column Uses

func (q *Query) Column(columns ...string) *Query

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 Uses

func (q *Query) ColumnExpr(expr string, params ...interface{}) *Query

ColumnExpr adds column expression to the Query.

func (*Query) Context Uses

func (q *Query) Context(c context.Context) *Query

func (*Query) CopyFrom Uses

func (q *Query) CopyFrom(r io.Reader, query interface{}, params ...interface{}) (Result, error)

CopyFrom is an alias from DB.CopyFrom.

func (*Query) CopyTo Uses

func (q *Query) CopyTo(w io.Writer, query interface{}, params ...interface{}) (Result, error)

CopyTo is an alias from DB.CopyTo.

func (*Query) Count Uses

func (q *Query) Count() (int, error)

Count returns number of rows matching the query using count aggregate function.

func (*Query) CountEstimate Uses

func (q *Query) CountEstimate(threshold int) (int, error)

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.

Based on https://wiki.postgresql.org/wiki/Count_estimate

func (*Query) CreateComposite Uses

func (q *Query) CreateComposite(opt *CreateCompositeOptions) error

func (*Query) CreateTable Uses

func (q *Query) CreateTable(opt *CreateTableOptions) error

func (*Query) DB Uses

func (q *Query) DB(db DB) *Query

func (*Query) Delete Uses

func (q *Query) Delete(values ...interface{}) (Result, error)

Delete deletes the model. When model has deleted_at column the row is soft deleted instead.

func (*Query) Deleted Uses

func (q *Query) Deleted() *Query

Deleted adds `WHERE deleted_at IS NOT NULL` clause for soft deleted models.

func (*Query) Distinct Uses

func (q *Query) Distinct() *Query

func (*Query) DistinctOn Uses

func (q *Query) DistinctOn(expr string, params ...interface{}) *Query

func (*Query) DropComposite Uses

func (q *Query) DropComposite(opt *DropCompositeOptions) error

func (*Query) DropTable Uses

func (q *Query) DropTable(opt *DropTableOptions) error

func (*Query) Except Uses

func (q *Query) Except(other *Query) *Query

func (*Query) ExceptAll Uses

func (q *Query) ExceptAll(other *Query) *Query

func (*Query) ExcludeColumn Uses

func (q *Query) ExcludeColumn(columns ...string) *Query

ExcludeColumn excludes a column from the list of to be selected columns.

func (*Query) Exec Uses

func (q *Query) Exec(query interface{}, params ...interface{}) (Result, error)

Exec is an alias for DB.Exec.

func (*Query) ExecOne Uses

func (q *Query) ExecOne(query interface{}, params ...interface{}) (Result, error)

ExecOne is an alias for DB.ExecOne.

func (*Query) Exists Uses

func (q *Query) Exists() (bool, error)

Exists returns true or false depending if there are any rows matching the query.

func (*Query) First Uses

func (q *Query) First() error

First sorts rows by primary key and selects the first row. It is a shortcut for:

q.OrderExpr("id ASC").Limit(1)

func (*Query) For Uses

func (q *Query) For(s string, params ...interface{}) *Query

func (*Query) ForEach Uses

func (q *Query) ForEach(fn interface{}) error

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 Uses

func (q *Query) ForceDelete(values ...interface{}) (Result, error)

Delete forces delete of the model with deleted_at column.

func (*Query) Group Uses

func (q *Query) Group(columns ...string) *Query

func (*Query) GroupExpr Uses

func (q *Query) GroupExpr(group string, params ...interface{}) *Query

func (*Query) Having Uses

func (q *Query) Having(having string, params ...interface{}) *Query

func (*Query) Insert Uses

func (q *Query) Insert(values ...interface{}) (Result, error)

Insert inserts the model.

func (*Query) Intersect Uses

func (q *Query) Intersect(other *Query) *Query

func (*Query) IntersectAll Uses

func (q *Query) IntersectAll(other *Query) *Query

func (*Query) Join Uses

func (q *Query) Join(join string, params ...interface{}) *Query

func (*Query) JoinOn Uses

func (q *Query) JoinOn(condition string, params ...interface{}) *Query

JoinOn appends join condition to the last join.

func (*Query) JoinOnOr Uses

func (q *Query) JoinOnOr(condition string, params ...interface{}) *Query

func (*Query) Last Uses

func (q *Query) Last() error

Last sorts rows by primary key and selects the last row. It is a shortcut for:

q.OrderExpr("id DESC").Limit(1)

func (*Query) Limit Uses

func (q *Query) Limit(n int) *Query

func (*Query) Model Uses

func (q *Query) Model(model ...interface{}) *Query

func (*Query) New Uses

func (q *Query) New() *Query

New returns new zero Query bound to the current db.

func (*Query) Offset Uses

func (q *Query) Offset(n int) *Query

func (*Query) OnConflict Uses

func (q *Query) OnConflict(s string, params ...interface{}) *Query

func (*Query) Order Uses

func (q *Query) Order(orders ...string) *Query

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) OrderExpr Uses

func (q *Query) OrderExpr(order string, params ...interface{}) *Query

Order adds sort order to the Query.

func (*Query) Query Uses

func (q *Query) Query(model, query interface{}, params ...interface{}) (Result, error)

Query is an alias for DB.Query.

func (*Query) QueryOne Uses

func (q *Query) QueryOne(model, query interface{}, params ...interface{}) (Result, error)

QueryOne is an alias for DB.QueryOne.

func (*Query) Relation Uses

func (q *Query) Relation(name string, apply ...func(*Query) (*Query, error)) *Query

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 Uses

func (q *Query) Returning(s string, params ...interface{}) *Query

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) Select Uses

func (q *Query) Select(values ...interface{}) error

Select selects the model.

func (*Query) SelectAndCount Uses

func (q *Query) SelectAndCount(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 Uses

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 Uses

func (q *Query) SelectOrInsert(values ...interface{}) (inserted bool, _ error)

SelectOrInsert selects the model inserting one if it does not exist. It returns true when model was inserted.

func (*Query) Set Uses

func (q *Query) Set(set string, params ...interface{}) *Query

func (*Query) Table Uses

func (q *Query) Table(tables ...string) *Query

func (*Query) TableExpr Uses

func (q *Query) TableExpr(expr string, params ...interface{}) *Query

func (*Query) TableModel Uses

func (q *Query) TableModel() TableModel

func (*Query) Union Uses

func (q *Query) Union(other *Query) *Query

func (*Query) UnionAll Uses

func (q *Query) UnionAll(other *Query) *Query

func (*Query) Update Uses

func (q *Query) Update(scan ...interface{}) (Result, error)

Update updates the model.

func (*Query) UpdateNotZero Uses

func (q *Query) UpdateNotZero(scan ...interface{}) (Result, error)

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) Value Uses

func (q *Query) Value(column string, value string, params ...interface{}) *Query

Value overwrites model value for the column in INSERT and UPDATE queries.

func (*Query) Where Uses

func (q *Query) Where(condition string, params ...interface{}) *Query

func (*Query) WhereGroup Uses

func (q *Query) WhereGroup(fn func(*Query) (*Query, error)) *Query

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 Uses

func (q *Query) WhereIn(where string, slice interface{}) *Query

WhereIn is a shortcut for Where and pg.In.

func (*Query) WhereInMulti Uses

func (q *Query) WhereInMulti(where string, values ...interface{}) *Query

WhereInMulti is a shortcut for Where and pg.InMulti.

func (*Query) WhereNotGroup Uses

func (q *Query) WhereNotGroup(fn func(*Query) (*Query, error)) *Query

WhereGroup encloses conditions added in the function in parentheses.

q.Where("TRUE").
	WhereNotGroup(func(q *orm.Query) (*orm.Query, error) {
		q = q.WhereOr("FALSE").WhereOr("TRUE").
		return q, nil
	})

generates

WHERE TRUE AND NOT (FALSE OR TRUE)

func (*Query) WhereOr Uses

func (q *Query) WhereOr(condition string, params ...interface{}) *Query

func (*Query) WhereOrGroup Uses

func (q *Query) WhereOrGroup(fn func(*Query) (*Query, error)) *Query

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 Uses

func (q *Query) WhereOrNotGroup(fn func(*Query) (*Query, error)) *Query

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 Uses

func (q *Query) WherePK() *Query

WherePK adds condition based on the model primary keys. Usually it is the same as:

Where("id = ?id")

func (*Query) With Uses

func (q *Query) With(name string, subq *Query) *Query

With adds subq as common table expression with the given name.

func (*Query) WithDelete Uses

func (q *Query) WithDelete(name string, subq *Query) *Query

func (*Query) WithInsert Uses

func (q *Query) WithInsert(name string, subq *Query) *Query

func (*Query) WithUpdate Uses

func (q *Query) WithUpdate(name string, subq *Query) *Query

func (*Query) WrapWith Uses

func (q *Query) WrapWith(name string) *Query

WrapWith creates new Query and adds to it current query as common table expression with the given name.

type QueryAppender Uses

type QueryAppender interface {
    AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)
}

type QueryCommand Uses

type QueryCommand interface {
    QueryAppender
    TemplateAppender
    String() string
    Operation() QueryOp
    Clone() QueryCommand
    Query() *Query
}

type QueryFormatter Uses

type QueryFormatter interface {
    FormatQuery(b []byte, query string, params ...interface{}) []byte
}

type QueryOp Uses

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"
)

type Relation Uses

type Relation struct {
    Type        int
    Field       *Field
    JoinTable   *Table
    BaseFKs     []*Field
    JoinFKs     []*Field
    Polymorphic *Field

    M2MTableName  types.Safe
    M2MTableAlias types.Safe
    M2MBaseFKs    []string
    M2MJoinFKs    []string
}

func (*Relation) String Uses

func (r *Relation) String() string

type Result Uses

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 Uses

type SafeQueryAppender struct {
    // contains filtered or unexported fields
}

func SafeQuery Uses

func SafeQuery(query string, params ...interface{}) *SafeQueryAppender

nolint

func (*SafeQueryAppender) AppendQuery Uses

func (q *SafeQueryAppender) AppendQuery(fmter QueryFormatter, b []byte) ([]byte, error)

func (*SafeQueryAppender) AppendValue Uses

func (q *SafeQueryAppender) AppendValue(b []byte, quote int) ([]byte, error)

func (*SafeQueryAppender) Value Uses

func (q *SafeQueryAppender) Value() types.Safe

type SelectQuery Uses

type SelectQuery struct {
    // contains filtered or unexported fields
}

func NewSelectQuery Uses

func NewSelectQuery(q *Query) *SelectQuery

func (*SelectQuery) AppendQuery Uses

func (q *SelectQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)

func (*SelectQuery) AppendTemplate Uses

func (q *SelectQuery) AppendTemplate(b []byte) ([]byte, error)

func (*SelectQuery) Clone Uses

func (q *SelectQuery) Clone() QueryCommand

func (*SelectQuery) Operation Uses

func (q *SelectQuery) Operation() QueryOp

func (*SelectQuery) Query Uses

func (q *SelectQuery) Query() *Query

func (*SelectQuery) String Uses

func (q *SelectQuery) String() string

type Table Uses

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 GetTable Uses

func GetTable(typ reflect.Type) *Table

GetTable returns a Table for a struct type.

func (*Table) AddField Uses

func (t *Table) AddField(field *Field)

func (*Table) AppendParam Uses

func (t *Table) AppendParam(b []byte, strct reflect.Value, name string) ([]byte, bool)

func (*Table) GetField Uses

func (t *Table) GetField(name string) (*Field, error)

func (*Table) HasField Uses

func (t *Table) HasField(name string) bool

func (*Table) RemoveField Uses

func (t *Table) RemoveField(field *Field)

func (*Table) String Uses

func (t *Table) String() string

type TableModel Uses

type TableModel interface {
    Model

    IsNil() bool
    Table() *Table
    Relation() *Relation
    AppendParam(QueryFormatter, []byte, 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
}

type TemplateAppender Uses

type TemplateAppender interface {
    AppendTemplate(b []byte) ([]byte, error)
}

type UpdateQuery Uses

type UpdateQuery struct {
    // contains filtered or unexported fields
}

func NewUpdateQuery Uses

func NewUpdateQuery(q *Query, omitZero bool) *UpdateQuery

func (*UpdateQuery) AppendQuery Uses

func (q *UpdateQuery) AppendQuery(fmter QueryFormatter, b []byte) (_ []byte, err error)

func (*UpdateQuery) AppendTemplate Uses

func (q *UpdateQuery) AppendTemplate(b []byte) ([]byte, error)

func (*UpdateQuery) Clone Uses

func (q *UpdateQuery) Clone() QueryCommand

func (*UpdateQuery) Operation Uses

func (q *UpdateQuery) Operation() QueryOp

func (*UpdateQuery) Query Uses

func (q *UpdateQuery) Query() *Query

func (*UpdateQuery) String Uses

func (q *UpdateQuery) String() string

Package orm imports 24 packages (graph) and is imported by 111 packages. Updated 2020-09-23. Refresh now. Tools for package owners.