Documentation ¶
Index ¶
- Constants
- Variables
- func Reflect(model interface{}) (reflect.Type, reflect.Value)
- type AND
- type BINARY
- type ColumnValue
- type EQ
- type FieldList
- func (fl *FieldList) BelongsToFieldsFor(relation, tableref, tablename string) ([]string, []string, error)
- func (fl *FieldList) FieldsFor() ([]QueryField, error)
- func (fl *FieldList) HasNFieldsFor(relation, tableref string, field FieldListItem, ...) ([]string, []string, error)
- func (fl *FieldList) RelatedFieldsFor(relation, tableref string, cb func(reflect.Type) *FieldList) (joins []string, selects []string, err error)
- func (fl *FieldList) TraversalsByName(columns []string) (Traversals, error)
- type FieldListItem
- type GroupOperator
- type Grouper
- type Mapper
- func (m *Mapper) All(models interface{}) error
- func (m *Mapper) Create(model interface{}) error
- func (m *Mapper) Delete(model interface{}, id int64) error
- func (m *Mapper) Exec(query string, args ...interface{}) (sql.Result, error)
- func (m *Mapper) FieldList(t reflect.Type) *FieldList
- func (m *Mapper) FieldMap(model interface{}) map[string]reflect.Value
- func (m *Mapper) Find(model interface{}, id int64) error
- func (m *Mapper) FindBy(model interface{}, column string, needle string) error
- func (m *Mapper) Get(dest interface{}, query string, args ...interface{}) error
- func (m *Mapper) HealthReport(ctx context.Context) (healthy bool, errors map[string]string)
- func (m *Mapper) Logger(logger *logrus.Logger)
- func (m *Mapper) Name() string
- func (m *Mapper) NamedExec(query string, arg interface{}) (sql.Result, error)
- func (m *Mapper) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
- func (m *Mapper) NewSelect(model interface{}) (*SelectQuery, error)
- func (m *Mapper) Open() error
- func (m *Mapper) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
- func (m *Mapper) Select(dest interface{}, query string, args ...interface{}) error
- func (m *Mapper) Update(model interface{}) error
- type NOT
- type NULL
- type OR
- type Operator
- func BinaryOp(col string, value interface{}, truthy, falsy string) Operator
- func Eq(col string, value interface{}) Operator
- func Ge(col string, value interface{}) Operator
- func Gt(col string, value interface{}) Operator
- func Le(col string, value interface{}) Operator
- func Lt(col string, value interface{}) Operator
- type QueryField
- type SelectQuery
- type Traversal
- type Traversals
Constants ¶
const ( // OptRelatedTo is an internal struct tag option mapper inserts for all columns, which is a subfield of an embedded table. Its value is the name of the embedded table. OptRelatedTo = "_related_to_" OptUnrelated = "_unrelated_" // OptBelongs is a struct tag option marking a "belongs-to" relation. OptBelongs = "belongs" // OptRelation is a struct tag option marking a "has-one", "has-many", or a "many-to-many" relation, containing the other end's ref stub for the struct's ID. // // Example tag: `db:"posts,relation=author"`: you can reference this join as `posts`, and the other end will have a field called `author_id` to reference this table. OptRelation = "relation" // OptReverse is a struct tag option marking a "many-to-many" relation, // containing the stub how the other end is referenced in the linker table (see OptThrough). // OptReverse is taken into consideration only if OptRelation and OptThrough // are provided. // // Example tag: `db:"groups,relation=user,reverse=group,through=user_groups"`: // you can reference this join as `groups`, and there must be a `user_groups` // table with a `user_id` and a `group_id` field. `user_id` references to this // table, `group_id` references to the joined table. OptReverse = "reverse" // OptThrough is a struct tag option marking a "many-to-many" relation, // containing the linker table's name. See OptReverse for an example. // OptThrough is taken into consideration only if OptRelation and OptReverse // are also provided. OptThrough = "through" )
Variables ¶
var ErrInvalidType = errors.New("Invalid Model Type")
ErrInvalidType is an error returning where the model is invalid (currently, when it is a null pointer)
Functions ¶
Types ¶
type AND ¶
type AND struct {
*GroupOperator
}
AND is an operator struct, with AND relation between each item
type BINARY ¶ added in v0.2.0
type BINARY struct { ColumnValue TruthyRel string FalsyRel string }
BINARY implements a 2-parameter operator
type ColumnValue ¶
type ColumnValue struct {
// contains filtered or unexported fields
}
ColumnValue is a standard struct representing a database column and its desierd value. This is the base struct of column-based operators.
func (*ColumnValue) Column ¶
func (c *ColumnValue) Column() string
Column returns returns the object's column name
func (*ColumnValue) Keys ¶
func (c *ColumnValue) Keys() []string
Keys returns keys the object returns, in order
func (*ColumnValue) Values ¶
func (c *ColumnValue) Values() map[string]interface{}
Values returns the object's value
type EQ ¶
type EQ struct {
ColumnValue
}
EQ implements equivalence Operator. It is based on Column struct.
type FieldList ¶
type FieldList struct { Fields []FieldListItem Type reflect.Type Joins map[string]*FieldList }
FieldList stores fields of a reflectx.StructMap's Index (from sqlx), with the structure's type
func (*FieldList) BelongsToFieldsFor ¶
func (fl *FieldList) BelongsToFieldsFor(relation, tableref, tablename string) ([]string, []string, error)
BelongsToFieldsFor converts FieldListItems to JOIN and SELECTs query substrings SQL query buildders can use directly
func (*FieldList) FieldsFor ¶
func (fl *FieldList) FieldsFor() ([]QueryField, error)
FieldsFor converts FieldListItems to query fields SQL query builders can use. It doesn't include related fields.
func (*FieldList) HasNFieldsFor ¶
func (fl *FieldList) HasNFieldsFor(relation, tableref string, field FieldListItem, typeMapper func(reflect.Type) *FieldList) ([]string, []string, error)
HasNFieldsFor queries related model to build JOIN and SELECTs query substrings SQL query buildders can use directly. It uses a callback, which can provide a *FieldList from the referenced type.
func (*FieldList) RelatedFieldsFor ¶
func (fl *FieldList) RelatedFieldsFor(relation, tableref string, cb func(reflect.Type) *FieldList) (joins []string, selects []string, err error)
RelatedFieldsFor converts FieldListItems to JOINs and SELECTs SQL query builders can use directly
func (*FieldList) TraversalsByName ¶
func (fl *FieldList) TraversalsByName(columns []string) (Traversals, error)
TraversalsByName provides a traversal index for SELECT query results, to map result rows' columns with model's entry positions
type FieldListItem ¶
type FieldListItem struct { reflect.Type Field reflect.StructField Index []int Name string Options map[string]string Path string Traversed bool }
FieldListItem is a line item of a model's field list
func (FieldListItem) QField ¶
func (fi FieldListItem) QField() *QueryField
QField returns a query field based on a FieldListItem
type GroupOperator ¶
type GroupOperator struct {
// contains filtered or unexported fields
}
GroupOperator is an iternal data structure for an operator with multiple sub-operators
func NewGroupOp ¶
func NewGroupOp(op string, items ...Operator) *GroupOperator
NewGroupOp returns a new group of operators
func (*GroupOperator) Add ¶
func (op *GroupOperator) Add(ops ...Operator)
Add adds more operatorn to an existing GroupOperator
func (*GroupOperator) Keys ¶
func (op *GroupOperator) Keys() []string
Keys returns all the keys found in its sub-operators in order
func (*GroupOperator) Values ¶
func (op *GroupOperator) Values() map[string]interface{}
Values returns all the values found in its sub-operators
func (*GroupOperator) Where ¶
func (op *GroupOperator) Where(truthy bool) string
Where is a helper function for implementer structs to provide all where clauses
type Grouper ¶
type Grouper interface {
Add(...Operator)
}
Grouper interface denotes a group operator, where multiple sub-operators can be added into
type Mapper ¶
Mapper is our PGSQL connection struct
func (*Mapper) FieldList ¶ added in v0.2.0
FieldList returns a map of types in the form of a StructMap, from the original model's type
func (*Mapper) FieldMap ¶
FieldMap returns a map of fields for a model. It handles pointer of model.
func (*Mapper) HealthReport ¶
HealthReport returns healthy status, or map of issues. Currently, a closed database is reported as an error.
func (*Mapper) NamedExec ¶
NamedExec runs sqlx.NamedExec nicely. It opens database if needed, and logs the query.
func (*Mapper) NamedQuery ¶
NamedQuery runs sqlx.NamedQuery nicely. It opens database if needed, and logs the query.
func (*Mapper) NewSelect ¶
func (m *Mapper) NewSelect(model interface{}) (*SelectQuery, error)
NewSelect returns a new SelectQuery with the provided model attached
func (*Mapper) Open ¶
Open opens connection to the database. It is implicitly called by db calls defined in Mapper, but sometimes it's desirable to make sure the database is available at start time.
func (*Mapper) Queryx ¶
Queryx runs sqlx.Queryx nicely. It opens database if needed, and logs the query.
type NOT ¶
type NOT struct {
Operator
}
NOT is a simple negate operator struct
type NULL ¶
type NULL struct {
ColumnValue
}
NULL implements IS NULL operator. It is based on Column struct.
type OR ¶
type OR struct {
*GroupOperator
}
OR is an operator struct, with OR relation between each item
type Operator ¶
Operator describes an operator, in which queries can build their WHERE clauses.
func BinaryOp ¶ added in v0.2.0
BinaryOp Returns a 2-parameter operator, with a truthy or falsy operator between column and value
Example: BinaryOp("column", 4, ">=", "<") yields `column >= 4` in normal query, or `column < 4` in negated form.
type QueryField ¶ added in v0.2.0
type QueryField struct {
// contains filtered or unexported fields
}
QueryField is a conversion struct for building INSERT or UPDATE queries
type SelectQuery ¶
type SelectQuery struct {
// contains filtered or unexported fields
}
SelectQuery represent a new SELECT query builder
func (*SelectQuery) All ¶
func (q *SelectQuery) All() error
All executes SELECT query, returning all the items selected. This call evaluates provided parameters, builds SQL query, and populates model slice the SelectQuery is created with.
func (*SelectQuery) Join ¶
func (q *SelectQuery) Join(selectors ...string) *SelectQuery
Join prepares query for joining tables, which will populate referenced submodels. This supports "has one," "has many," and "many to many" relations. Parameters have to reference db field names, which are also some sort of relations. Examples:
```golang
type Table struct { ID int WeBelongToID int `db:"we_belong_to_id"` WeBelongTo TableWeBelongTo `db:"we_belong_to,belongs"` HasOne OtherTable `db:"has_one,relation=table"` HasMany []*ManyTable `db:"has_many,relation=table"` ManyToMany []*M2MTable `db:"many_to_many,relation=table,reverse=mtm,through=table_m2ms"` }
```
represents four join possibilities: "we_belong_to," "has_one," and "has_many." In the first example, we expect the other item's ID stored with the name of the relation + "_id" ("we_belong_to_id"), In the other two examples, we expect our ID to be stored at the other end with the name of the value of "relation" optional tag + "_id" ("table_id"). In the last example, we also expect that the relation is set up in "table_m2ms" table, and it also has a has many relationship with M2MTable, referencing its ID as "reverse" optional tag + "_id" ("mtm_id") in the "through" table ("table_m2ms") too.
func (*SelectQuery) Select ¶
func (q *SelectQuery) Select(selectors ...string) *SelectQuery
Select sets columns to be selected into model. By default, all fields in the model and its joined relations are populated.
func (*SelectQuery) Where ¶
func (q *SelectQuery) Where(op Operator) *SelectQuery
Where sets where clauses to the SELECT query, using Operator interface. Calling it multiple times will yield an AND relationship among operators.
type Traversal ¶ added in v0.2.0
type Traversal struct { Name string Index []int Relation reflect.StructField }
Traversal stores dial information for each column into the resulting model instance