Documentation ¶
Index ¶
- Variables
- func StringListContains(list []string, value string) bool
- type AfterDelete
- type AfterInsert
- type AfterUpdate
- type BeforeDelete
- type BeforeInsert
- type BeforeUpdate
- type CallbackDef
- type CallbackFunc
- type CallbackList
- type Callbacks
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) BeginContext(ctx context.Context) (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) Delete(s MappedStruct) error
- func (db *DB) DeleteContext(ctx context.Context, s MappedStruct) error
- func (db DB) GetEngine() Engine
- func (db *DB) Insert(s MappedStruct) error
- func (db *DB) InsertContext(ctx context.Context, s MappedStruct) error
- func (db *DB) Query(mp MapperProvider) Query
- func (db *DB) Update(s MappedStruct, fields ...string) error
- func (db *DB) UpdateContext(ctx context.Context, s MappedStruct, fields ...string) error
- type Engine
- type IDB
- type MappedStruct
- type Mapper
- type MapperProvider
- type MarshaledScalarField
- func (f MarshaledScalarField) Eq(value interface{}) qb.Clause
- func (f MarshaledScalarField) Gt(value interface{}) qb.Clause
- func (f MarshaledScalarField) Gte(value interface{}) qb.Clause
- func (f MarshaledScalarField) In(values ...interface{}) qb.Clause
- func (f MarshaledScalarField) Lt(value interface{}) qb.Clause
- func (f MarshaledScalarField) Lte(value interface{}) qb.Clause
- func (f MarshaledScalarField) NotEq(value interface{}) qb.Clause
- func (f MarshaledScalarField) NotIn(values ...interface{}) qb.Clause
- type Metadata
- type Model
- type Query
- func (q Query) All(value interface{}) error
- func (q Query) AllContext(ctx context.Context, value interface{}) error
- func (q Query) Count(count interface{}) error
- func (q Query) CountContext(ctx context.Context, count interface{}) error
- func (q Query) Exists() (exists bool, err error)
- func (q Query) ExistsContext(ctx context.Context) (exists bool, err error)
- func (q Query) Filter(clauses ...qb.Clause) Query
- func (q Query) ForUpdate(mps ...MapperProvider) Query
- func (q Query) Get(s MappedStruct, pkey ...interface{}) error
- func (q Query) GetContext(ctx context.Context, s MappedStruct, pkey ...interface{}) error
- func (q Query) InnerJoin(mp MapperProvider, clause ...qb.Clause) Query
- func (q Query) LeftJoin(mp MapperProvider, clause ...qb.Clause) Query
- func (q Query) Limit(limit int) Query
- func (q Query) LimitOffset(limit int, offset int) Query
- func (q Query) Offset(offset int) Query
- func (q Query) One(s MappedStruct) error
- func (q Query) OneContext(ctx context.Context, s MappedStruct) error
- func (q Query) OrderBy(clauses ...qb.Clause) Query
- func (q Query) RightJoin(mp MapperProvider, clause ...qb.Clause) Query
- func (q Query) SQLQuery() (*sql.Rows, error)
- func (q Query) SQLQueryContext(ctx context.Context) (*sql.Rows, error)
- func (q Query) SQLQueryRow() qb.Row
- func (q Query) SQLQueryRowContext(ctx context.Context) qb.Row
- func (q Query) Scalar(value interface{}) error
- func (q Query) ScalarContext(ctx context.Context, value interface{}) error
- func (q Query) Select(clause ...qb.Clause) Query
- func (q Query) SelectStmt() qb.SelectStmt
- func (q Query) Where(clauses ...qb.Clause) Query
- type ScalarField
- func (f ScalarField) Accept(context *qb.CompilerContext) string
- func (f ScalarField) Eq(value interface{}) qb.Clause
- func (f ScalarField) Gt(value interface{}) qb.Clause
- func (f ScalarField) Gte(value interface{}) qb.Clause
- func (f ScalarField) In(values ...interface{}) qb.Clause
- func (f ScalarField) Like(pattern string) qb.Clause
- func (f ScalarField) Lt(value interface{}) qb.Clause
- func (f ScalarField) Lte(value interface{}) qb.Clause
- func (f ScalarField) NotEq(value interface{}) qb.Clause
- func (f ScalarField) NotIn(values ...interface{}) qb.Clause
- type Tx
- func (tx Tx) Commit() error
- func (tx Tx) Delete(s MappedStruct) error
- func (tx Tx) DeleteContext(ctx context.Context, s MappedStruct) error
- func (tx Tx) GetEngine() Engine
- func (tx Tx) Insert(s MappedStruct) error
- func (tx Tx) InsertContext(ctx context.Context, s MappedStruct) error
- func (tx Tx) Query(mp MapperProvider) Query
- func (tx Tx) Rollback() error
- func (tx Tx) Update(s MappedStruct, fields ...string) error
- func (tx Tx) UpdateContext(ctx context.Context, s MappedStruct, fields ...string) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRecordNotFound is returned by Update, Delete, or One() if // not matching record were found ErrRecordNotFound = errors.New("yago.RecordNotFound") // ErrMultipleRecords is returned by Update or One() if too many // records matched the statement ErrMultipleRecords = errors.New("yago.MultipleRecords") // ErrInvalidColumns is returned by Scalar if the query returned // a number of columns != 1 ErrInvalidColumns = errors.New("yago.InvalidColumns") )
Functions ¶
func StringListContains ¶
StringListContains returns true if the list containts the passed value, false otherwise
Types ¶
type AfterDelete ¶
type AfterDelete interface {
AfterDelete(db *DB)
}
AfterDelete can be implemented by structs that need a after delete callback
type AfterInsert ¶
type AfterInsert interface {
AfterInsert(db *DB)
}
AfterInsert can be implemented by structs that need a after insert callback
type AfterUpdate ¶
type AfterUpdate interface {
AfterUpdate(db *DB)
}
AfterUpdate can be implemented by structs that need a after update callback
type BeforeDelete ¶
type BeforeDelete interface {
BeforeDelete(db *DB)
}
BeforeDelete can be implemented by structs that need a before delete callback
type BeforeInsert ¶
type BeforeInsert interface {
BeforeInsert(db *DB)
}
BeforeInsert can be implemented by structs that need a before insert callback
type BeforeUpdate ¶
type BeforeUpdate interface {
BeforeUpdate(db *DB)
}
BeforeUpdate can be implemented by structs that need a before update callback
type CallbackDef ¶
type CallbackDef struct {
// contains filtered or unexported fields
}
CallbackDef is a single callback definition
func AddCallbackSorted ¶
func AddCallbackSorted(defs []CallbackDef, def CallbackDef) []CallbackDef
AddCallbackSorted insert a CallbackDef respecting the before/after args
func Callback ¶
func Callback(name string, callback CallbackFunc) CallbackDef
Callback returns a new CallbackDef
func (CallbackDef) After ¶
func (c CallbackDef) After(name string) CallbackDef
After which callback to be called
func (CallbackDef) Before ¶
func (c CallbackDef) Before(name string) CallbackDef
Before which callback to be called
type CallbackFunc ¶
type CallbackFunc func(db *DB, s MappedStruct)
CallbackFunc is the type of the callback functions
type CallbackList ¶
type CallbackList struct {
// contains filtered or unexported fields
}
CallbackList containts a list of same-type callbacks
func (CallbackList) Call ¶
func (c CallbackList) Call(db *DB, s MappedStruct)
Call runs all the callbacks with the passed arguments
func (*CallbackList) Get ¶
func (c *CallbackList) Get(name string) *CallbackDef
Get a callback by name
type Callbacks ¶
type Callbacks struct { BeforeInsert CallbackList AfterInsert CallbackList BeforeUpdate CallbackList AfterUpdate CallbackList BeforeDelete CallbackList AfterDelete CallbackList }
Callbacks containts a set of callbacks
var DefaultCallbacks Callbacks
DefaultCallbacks contains the default callbacks every database will be initialized with.
type DB ¶
DB is a database handle with callbacks that links a Metadata and a qb.Engine
func (*DB) BeginContext ¶
BeginContext start a new transaction
func (*DB) DeleteContext ¶
func (db *DB) DeleteContext(ctx context.Context, s MappedStruct) error
Delete a struct in the database
func (*DB) InsertContext ¶
func (db *DB) InsertContext(ctx context.Context, s MappedStruct) error
InsertContext a struct in the database
func (*DB) Query ¶
func (db *DB) Query(mp MapperProvider) Query
Query returns a new Query for the struct
func (*DB) Update ¶
func (db *DB) Update(s MappedStruct, fields ...string) error
Update the struct attributes in DB
func (*DB) UpdateContext ¶
UpdateContext the struct attributes in DB
type Engine ¶
type Engine interface { Exec(builder qb.Builder) (sql.Result, error) Query(builder qb.Builder) (*sql.Rows, error) QueryRow(builder qb.Builder) qb.Row ExecContext(ctx context.Context, builder qb.Builder) (sql.Result, error) QueryContext(ctx context.Context, builder qb.Builder) (*sql.Rows, error) QueryRowContext(ctx context.Context, builder qb.Builder) qb.Row }
Engine is the common interface of qb.Engine and qb.Tx
type IDB ¶
type IDB interface { Insert(MappedStruct) error Update(MappedStruct, ...string) error Delete(MappedStruct) error Query(MapperProvider) Query InsertContext(context.Context, MappedStruct) error UpdateContext(context.Context, MappedStruct, ...string) error DeleteContext(context.Context, MappedStruct) error GetEngine() Engine }
IDB is the common interface of DB and Tx
type MappedStruct ¶
MappedStruct is implemented by all mapped structures
type Mapper ¶
type Mapper interface { Name() string Table() *qb.TableElem StructType() reflect.Type FieldList() []qb.Clause AutoIncrementPKey() bool LoadAutoIncrementPKeyValue(instance MappedStruct, value int64) SQLValues(instance MappedStruct, fields ...string) map[string]interface{} PKey(instance MappedStruct) []interface{} PKeyClause(values []interface{}) qb.Clause ScanPKey(rows *sql.Rows, instance MappedStruct) error Scan(rows *sql.Rows, instance MappedStruct) error }
Mapper links a mapped struct and table definition
type MapperProvider ¶
type MapperProvider interface {
GetMapper() Mapper
}
MapperProvider is implemented by any struct that can provide a single mapper
type MarshaledScalarField ¶
type MarshaledScalarField struct {
ScalarField
}
MarshaledScalarField A text marshaled scalar field
func NewMarshaledScalarField ¶
func NewMarshaledScalarField(column qb.ColumnElem) MarshaledScalarField
NewMarshaledScalarField returns a new ScalarField
func (MarshaledScalarField) Eq ¶
func (f MarshaledScalarField) Eq(value interface{}) qb.Clause
Eq returns a = clause
func (MarshaledScalarField) Gt ¶
func (f MarshaledScalarField) Gt(value interface{}) qb.Clause
Gt returns a > clause
func (MarshaledScalarField) Gte ¶
func (f MarshaledScalarField) Gte(value interface{}) qb.Clause
Gte returns a >= clause
func (MarshaledScalarField) In ¶
func (f MarshaledScalarField) In(values ...interface{}) qb.Clause
In returns a IN clause
func (MarshaledScalarField) Lt ¶
func (f MarshaledScalarField) Lt(value interface{}) qb.Clause
Lt returns a < clause
func (MarshaledScalarField) Lte ¶
func (f MarshaledScalarField) Lte(value interface{}) qb.Clause
Lte returns a <= clause
func (MarshaledScalarField) NotEq ¶
func (f MarshaledScalarField) NotEq(value interface{}) qb.Clause
NotEq returns a != clause
func (MarshaledScalarField) NotIn ¶
func (f MarshaledScalarField) NotIn(values ...interface{}) qb.Clause
NotIn returns a NOT IN clause
type Metadata ¶
type Metadata struct {
// contains filtered or unexported fields
}
Metadata holds the table defs & mappers of a db
func NewMetadataFromQbMetadata ¶
func NewMetadataFromQbMetadata(qbMeta *qb.MetaDataElem) *Metadata
NewMetadataFromQbMetadata returns a Metadata from a qb.Metadata
func (*Metadata) GetMapper ¶
func (m *Metadata) GetMapper(s MappedStruct) Mapper
GetMapper returns the default mapper of a mapped struct
func (*Metadata) GetQbMetadata ¶
func (m *Metadata) GetQbMetadata() *qb.MetaDataElem
GetQbMetadata returns the underlying
type Model ¶
type Model interface {
GetMapper() Mapper
}
A Model provides handy access to a struct definition.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query helps querying structs from the database
func (Query) AllContext ¶
AllContext load all the structs matching the query
func (Query) CountContext ¶
CountContext change the columns to COUNT(*), execute the query and returns the result
func (Query) ExistsContext ¶
ExistsContext return true if any record matches the current query
func (Query) ForUpdate ¶
func (q Query) ForUpdate(mps ...MapperProvider) Query
ForUpdate add a FOR UPDATE clause
func (Query) Get ¶
func (q Query) Get(s MappedStruct, pkey ...interface{}) error
Get returns a record from its primary key values
func (Query) GetContext ¶
func (q Query) GetContext(ctx context.Context, s MappedStruct, pkey ...interface{}) error
GetContext returns a record from its primary key values
func (Query) InnerJoin ¶
func (q Query) InnerJoin(mp MapperProvider, clause ...qb.Clause) Query
InnerJoin joins a table
func (Query) LeftJoin ¶
func (q Query) LeftJoin(mp MapperProvider, clause ...qb.Clause) Query
LeftJoin joins a table
func (Query) LimitOffset ¶
LimitOffset add a LIMIT/OFFSET clause
func (Query) One ¶
func (q Query) One(s MappedStruct) error
One returns one and only one struct from the query. If query has no result or more than one, an error is returned
func (Query) OneContext ¶
func (q Query) OneContext(ctx context.Context, s MappedStruct) error
OneContext returns one and only one struct from the query. If query has no result or more than one, an error is returned
func (Query) RightJoin ¶
func (q Query) RightJoin(mp MapperProvider, clause ...qb.Clause) Query
RightJoin joins a table
func (Query) SQLQueryContext ¶
SQLQueryContext runs the query
func (Query) SQLQueryRow ¶
SQLQueryRow runs the query and expects at most one row in the result
func (Query) SQLQueryRowContext ¶
SQLQueryRowContext runs the query and expects at most one row in the result
func (Query) ScalarContext ¶
ScalarContext execute the query and retrieve a single value from it
func (Query) SelectStmt ¶
func (q Query) SelectStmt() qb.SelectStmt
SelectStmt returns the builded SelectStmt
type ScalarField ¶
type ScalarField struct {
Column qb.ColumnElem
}
ScalarField A simple scalar field
func NewScalarField ¶
func NewScalarField(column qb.ColumnElem) ScalarField
NewScalarField returns a new ScalarField
func (ScalarField) Accept ¶
func (f ScalarField) Accept(context *qb.CompilerContext) string
Accept calls the underlying column 'Accept'.
func (ScalarField) Gte ¶
func (f ScalarField) Gte(value interface{}) qb.Clause
Gte returns a >= clause
func (ScalarField) In ¶
func (f ScalarField) In(values ...interface{}) qb.Clause
In returns a IN clause
func (ScalarField) Like ¶
func (f ScalarField) Like(pattern string) qb.Clause
Like returns a LIKE clause
func (ScalarField) Lte ¶
func (f ScalarField) Lte(value interface{}) qb.Clause
Lte returns a <= clause
func (ScalarField) NotEq ¶
func (f ScalarField) NotEq(value interface{}) qb.Clause
NotEq returns a != clause
func (ScalarField) NotIn ¶
func (f ScalarField) NotIn(values ...interface{}) qb.Clause
NotIn returns a NOT IN clause
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is an on-going database transaction
func (Tx) DeleteContext ¶
func (tx Tx) DeleteContext(ctx context.Context, s MappedStruct) error
DeleteContext drop a struct from the database
func (Tx) InsertContext ¶
func (tx Tx) InsertContext(ctx context.Context, s MappedStruct) error
InsertContext a new struct to the database
func (Tx) Update ¶
func (tx Tx) Update(s MappedStruct, fields ...string) error
Update write struct values to the database If fields is provided, only theses fields are written
func (Tx) UpdateContext ¶
UpdateContext write struct values to the database If fields is provided, only theses fields are written