orm

package
v5.0.12+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2016 License: BSD-2-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrimaryKeyFlag = 1 << iota
	ForeignKeyFlag
	NotNullFlag
)
View Source
const (
	AfterQueryHookFlag = 1 << iota
	AfterSelectHookFlag
	BeforeInsertHookFlag
	AfterInsertHookFlag
	BeforeUpdateHookFlag
	AfterUpdateHookFlag
	BeforeDeleteHookFlag
	AfterDeleteHookFlag
)
View Source
const (
	HasOneRelation = 1 << iota
	BelongsToRelation
	HasManyRelation
	Many2ManyRelation
)

Variables

View Source
var Tables = newTables()

Functions

func CreateTable

func CreateTable(db DB, model interface{}, opt *CreateTableOptions) (*types.Result, error)

func Delete

func Delete(db DB, model interface{}) error

func Insert

func Insert(db DB, v ...interface{}) error

func Pager

func Pager(urlValues url.Values, defaultLimit int) func(*Query) (*Query, error)

Pager sets LIMIT and OFFSET from the URL values:

  • ?limit=10 - sets q.Limit(10), max limit is 1000.
  • ?page=5 - sets q.Offset((page - 1) * limit), max offset is 1000000.

func Scan

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

func Select

func Select(db DB, model interface{}) error

func URLValues

func URLValues(urlValues url.Values) func(*Query) (*Query, error)

func Update

func Update(db DB, model interface{}) error

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, b []byte) error
}

ColumnScanner is used to scan column values.

type CreateTableOptions

type CreateTableOptions struct{}

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

	Exec(query interface{}, params ...interface{}) (*types.Result, error)
	ExecOne(query interface{}, params ...interface{}) (*types.Result, error)
	Query(coll, query interface{}, params ...interface{}) (*types.Result, error)
	QueryOne(model, query interface{}, params ...interface{}) (*types.Result, error)

	QueryFormatter
}

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

type Discard

type Discard struct{}

func (Discard) AddModel

func (Discard) AddModel(_ ColumnScanner) error

func (Discard) AfterDelete

func (Discard) AfterDelete(DB) error

func (Discard) AfterInsert

func (Discard) AfterInsert(DB) error

func (Discard) AfterQuery

func (Discard) AfterQuery(DB) error

func (Discard) AfterSelect

func (Discard) AfterSelect(DB) error

func (Discard) AfterUpdate

func (Discard) AfterUpdate(DB) error

func (Discard) BeforeDelete

func (Discard) BeforeDelete(DB) error

func (Discard) BeforeInsert

func (Discard) BeforeInsert(DB) error

func (Discard) BeforeUpdate

func (Discard) BeforeUpdate(DB) error

func (Discard) NewModel

func (d Discard) NewModel() ColumnScanner

func (Discard) Reset

func (Discard) Reset() error

func (Discard) ScanColumn

func (Discard) ScanColumn(colIdx int, colName string, b []byte) error

type Field

type Field struct {
	Type reflect.Type

	GoName  string // struct field name, e.g. Id
	ColName types.Q
	SQLName string // SQL name, .e.g. id
	SQLType string
	Index   []int
	// contains filtered or unexported fields
}

func (*Field) AppendValue

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

func (*Field) Copy

func (f *Field) Copy() *Field

func (*Field) Has

func (f *Field) Has(flag uint8) bool

func (*Field) IsEmpty

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

func (*Field) OmitEmpty

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

func (*Field) ScanValue

func (f *Field) ScanValue(strct reflect.Value, b []byte) error

func (*Field) Value

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

type FormatAppender

type FormatAppender interface {
	AppendFormat([]byte, QueryFormatter) []byte
}

func Q

func Q(query string, params ...interface{}) FormatAppender

type Formatter

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

func (Formatter) Append

func (f Formatter) Append(dst []byte, src string, params ...interface{}) []byte

func (Formatter) AppendBytes

func (f Formatter) AppendBytes(dst, src []byte, params ...interface{}) []byte

func (*Formatter) Copy

func (f *Formatter) Copy() Formatter

func (Formatter) FormatQuery

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

func (*Formatter) SetParam

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

type Method

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

func (*Method) AppendValue

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

func (*Method) Has

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

func (*Method) Value

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

type Model

type Model interface {
	ColumnScanner

	// Reset resets model state.
	Reset() error

	// NewModel returns ColumnScanner that is used to scan columns
	// from the current row.
	NewModel() ColumnScanner

	// AddModel adds ColumnScanner to the Collection.
	AddModel(ColumnScanner) error

	AfterQuery(DB) error
	AfterSelect(DB) error

	BeforeInsert(DB) error
	AfterInsert(DB) error

	BeforeUpdate(DB) error
	AfterUpdate(DB) error

	BeforeDelete(DB) error
	AfterDelete(DB) error
}

func NewModel

func NewModel(values ...interface{}) (Model, error)

type Query

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

func NewQuery

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

func (*Query) Alias

func (q *Query) Alias(alias string) *Query

func (*Query) Apply

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

Apply calls the fn passing the Query as an argument.

func (*Query) Column

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

Column adds column to the Query quoting it according to PostgreSQL rules. ColumnExpr can be used to bypass quoting restriction.

func (*Query) ColumnExpr

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

ColumnExpr adds column expression to the Query.

func (*Query) Copy

func (q *Query) Copy() *Query

Copy returns copy of the Query.

func (*Query) Count

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

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

func (*Query) CountEstimate

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

CountEstimate uses EXPLAIN to get estimated number of rows matching 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) DB

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

func (*Query) Delete

func (q *Query) Delete() (*types.Result, error)

Delete deletes the model.

func (*Query) First

func (q *Query) First() error

First selects the first row.

func (*Query) FormatQuery

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

func (*Query) Group

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

func (*Query) GroupExpr

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

func (*Query) Having

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

func (*Query) Insert

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

Insert inserts the model.

func (*Query) Join

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

func (*Query) Last

func (q *Query) Last() error

Last selects the last row.

func (*Query) Limit

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

func (*Query) Model

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

func (*Query) New

func (q *Query) New() *Query

New returns new zero Query binded to the current db.

func (*Query) Offset

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

func (*Query) OnConflict

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

func (*Query) Order

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

Order adds sort order to the Query quoting column name. OrderExpr can be used to bypass quoting restriction.

func (*Query) OrderExpr

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

Order adds sort order to the Query.

func (*Query) Relation

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

func (*Query) Returning

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

func (*Query) Select

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

Select selects the model.

func (*Query) SelectAndCount

func (q *Query) SelectAndCount(values ...interface{}) (count int, err error)

SelectAndCount runs Select and Count in two separate goroutines, waits for them to finish and returns the result.

func (*Query) SelectAndCountEstimate

func (q *Query) SelectAndCountEstimate(threshold int) (count int, err error)

SelectAndCountEstimate runs Select and CountEstimate in two separate goroutines, waits for them to finish and returns the result.

func (*Query) SelectOrInsert

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

SelectOrInsert selects the model inserting one if it does not exist.

func (*Query) Set

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

func (*Query) Table

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

func (*Query) TableExpr

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

func (*Query) Update

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

Update updates the model.

func (*Query) Where

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

func (*Query) With

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

func (*Query) WrapWith

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

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

type QueryAppender

type QueryAppender interface {
	AppendQuery(dst []byte, params ...interface{}) ([]byte, error)
}

type QueryFormatter

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

type Relation

type Relation struct {
	Type        int
	Polymorphic bool
	Field       *Field
	JoinTable   *Table
	FKs         []*Field

	M2MTableName types.Q
	BasePrefix   string
	JoinPrefix   string
}

type Table

type Table struct {
	Type     reflect.Type
	TypeName string

	Name      types.Q
	Alias     types.Q
	ModelName string

	PKs       []*Field
	Fields    []*Field
	FieldsMap map[string]*Field

	Methods   map[string]*Method
	Relations map[string]*Relation
	// contains filtered or unexported fields
}

func (*Table) AddField

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

func (*Table) GetField

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

func (*Table) Has

func (t *Table) Has(flag int16) bool

func (*Table) HasField

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL