Documentation
¶
Overview ¶
Package scope defines functions that operates on engine.Engine and enables operating on model values easily.
Scope adds a layer of encapsulation on the model on which we are using to compose Queries or interact with the database
Index ¶
- func AddIndex(e *engine.Engine, unique bool, value interface{}, indexName string, ...) error
- func AddJoinRelation(table string, s *model.JoinTableHandler, e *engine.Engine, source interface{}, ...) (*model.Expr, error)
- func AddToVars(e *engine.Engine, value interface{}) string
- func AutoIndex(e *engine.Engine, value interface{}) error
- func Automigrate(e *engine.Engine, value interface{}) error
- func ChangeableField(e *engine.Engine, field *model.Field) bool
- func ConvertInterfaceToMap(e *engine.Engine, values interface{}, withIgnoredField bool) map[string]interface{}
- func CreateJoinTable(e *engine.Engine, field *model.StructField) error
- func CreateTable(e *engine.Engine, value interface{}) error
- func DropTable(e *engine.Engine, value interface{}) error
- func FieldByName(e *engine.Engine, value interface{}, name string) (*model.Field, error)
- func Fields(e *engine.Engine, value interface{}) ([]*model.Field, error)
- func GetForeignField(column string, fields []*model.StructField) *model.StructField
- func GetModelStruct(e *engine.Engine, value interface{}) (*model.Struct, error)
- func GetSearchMap(e *engine.Engine, s *model.JoinTableHandler, sources ...interface{}) map[string]interface{}
- func HasColumn(e *engine.Engine, modelValue interface{}, column string) bool
- func HasConditions(e *engine.Engine, modelValue interface{}) bool
- func Initialize(e *engine.Engine)
- func PrimaryField(e *engine.Engine, value interface{}) (*model.Field, error)
- func PrimaryFields(e *engine.Engine, value interface{}) ([]*model.Field, error)
- func PrimaryKey(e *engine.Engine, value interface{}) (string, error)
- func Quote(e *engine.Engine, str string) string
- func QuotedTableName(e *engine.Engine, value interface{}) string
- func SaveFieldAsAssociation(e *engine.Engine, field *model.Field) (bool, *model.Relationship)
- func Scan(rows *sql.Rows, columns []string, fields []*model.Field)
- func SelectAttrs(e *engine.Engine) []string
- func SetColumn(e *engine.Engine, column interface{}, value interface{}) error
- func SetupJoinTable(s *model.JoinTableHandler, relationship *model.Relationship, tableName string, ...)
- func ShouldSaveAssociation(e *engine.Engine) bool
- func TableName(e *engine.Engine, value interface{}) string
- func UpdatedAttrsWithValues(e *engine.Engine, value interface{}) (results map[string]interface{}, hasUpdate bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddIndex ¶
func AddIndex(e *engine.Engine, unique bool, value interface{}, indexName string, column ...string) error
AddIndex add extra queries fo creating database index. The indexes are packed on e.Sope.Exprs and it sets the e.Scope.MultiExpr to true signaling that there are additional multiple SQL queries bundled in the e.Scope.
if unique is true this will generate CREATE UNIQUE INDEX and in case of false it generates CREATE INDEX.
func AddJoinRelation ¶
func AddJoinRelation(table string, s *model.JoinTableHandler, e *engine.Engine, source interface{}, destination interface{}) (*model.Expr, error)
AddJoinRelation create relationship in join table for source and destination
func AddToVars ¶
AddToVars add value to e.Scope.SQLVars it returns the positional binding of the values.
The way positional arguments are handled inthe database/sql package relies on database specific setting.
For instance in ql
$1 will bind the value of the first argument.
The returned string depends on implementation provided by the Dialect.BindVar, the number that is passed to BindVar is based on the number of items stored in e.Scope.SQLVars. So if the length is 4 it might be $4 for the ql dialect.
It is possible to supply *model.Expr as value. The expression will be evaluated accordingly by replacing each occurrence of ? in *model.Expr.Q with the positional binding of the *model.Expr.Arg item.
func Automigrate ¶
Automigrate generates sql for creting database table for model value if the table doesnt exist yet. It also alters fields if the model has been updated.
NOTE For the case of an updated model which will need to alter the table to reflect the new changes, the SQL is stored under e.Scope.Exprs. The caller must be aware of this, and remember to chceck if e.Scope.MultiExpr is true so as to get the additional SQL.
func ChangeableField ¶
ChangeableField returns true if the field's value can be changed.
func ConvertInterfaceToMap ¶
func ConvertInterfaceToMap(e *engine.Engine, values interface{}, withIgnoredField bool) map[string]interface{}
ConvertInterfaceToMap tries to convert value into a map[string]interface{}
The map keys are field names, and the values are the supposed field values. This cunction only supports maps, []interface{} and structs.
For [interface{}, if the first value is a string, then it must be a succession of key pair values like
["name","gernest","age",1000]
That user case arises when the optional argument slice like args... is used where the type is string.
// Provided you have a function func some(args...string){} //and you use to pass the following argumens. some("name?","gernest") //passsing args to this function will yield map["name?"]="gernest"
func CreateJoinTable ¶
func CreateJoinTable(e *engine.Engine, field *model.StructField) error
CreateJoinTable creates a join table that handles many to many relationship.
For instance if users have many to many relation to languages then the join table will be users_language and containing keys that point to both users and languages table.
func CreateTable ¶
CreateTable generates CREATE TABLE SQL
func DropTable ¶
DropTable generates SQL query for DROP TABLE.
The Generated SQL is not wrapped in a transaction. All state altering queries must be wrapped in a transaction block.
We don't need to rap the transaction block at this level so as to enable flexibility of combining multiple querries that will be wrapped under the same transaction.
func FieldByName ¶
FieldByName returns the field in the model struct value with name name.
TODO:(gernest) return an error when the field is not found.
func Fields ¶
Fields extracts []*model.Fields from value, value is obvously a struct or something. This is only done when e.Scope.Fields is nil, for the case of non nil value then *e.Scope.Fiedls is returned without computing anything.
func GetForeignField ¶
func GetForeignField(column string, fields []*model.StructField) *model.StructField
GetForeignField return the foreign field among the supplied fields.
func GetModelStruct ¶
GetModelStruct construct a *model.Struct from value. This does not set the e.Scope.Value to value, you must set this value manually if you want to set the scope value.
value must be a go struct or a slict of go struct. The computed *model.Struct is cached , so multiple calls to this function with the same value won't compute anything and return the cached copy. It is less unlikely that the structs will be changine at runtime.
The value can implement engine.Tabler interface to help easily identify the table name for the model.
func GetSearchMap ¶
func GetSearchMap(e *engine.Engine, s *model.JoinTableHandler, sources ...interface{}) map[string]interface{}
GetSearchMap return a map of fields that are related as in foreign keys between the source model and destination model.
func HasConditions ¶
HasConditions return true if engine e has any relevant condition for narrowing down queries.
This goes down like this
- the primary field is not zero or
- there is WHERE condition or
- there is OR condition or
- there is NOT condition
func Initialize ¶
Initialize initializes value for e.Scope.Value There are three areas where we look for values to initialize the model with.
e.Seach.WhereConditions e.Search.InitAttrs e.Search.AssignAttr
func PrimaryField ¶
PrimaryField returns the field with name id, or any primary field that happens to be the one defined by the model value.
func PrimaryFields ¶
PrimaryFields returns fields that have PRIMARY_KEY tag from the struct value.
func PrimaryKey ¶
PrimaryKey returns the name of the primary key for the model value
func Quote ¶
Quote quotes the str into an SQL string. This makes sure sql strings have "" around them.
For the case of a str which has a dot in it example one.two the string is quoted and becomes "one"."two" and the quote implementation is called from the e.Parent.Dialect.
In case of a string without a dot example one it will be quoted using the current dialect e.Dialect
func QuotedTableName ¶
QuotedTableName returns a quoted table name.
func SaveFieldAsAssociation ¶
SaveFieldAsAssociation saves associations.
This returns relationship that can be saved, the relationship is taken from field provided that the field is not blank,is changeable and is not an ignored field.
Only works if the field has tag SAVE_ASSOCIATION
func SelectAttrs ¶
SelectAttrs returns the attributes in the select query.
func SetupJoinTable ¶
func SetupJoinTable(s *model.JoinTableHandler, relationship *model.Relationship, tableName string, source reflect.Type, destination reflect.Type)
SetupJoinTable initialize a default join table handler
func ShouldSaveAssociation ¶
ShouldSaveAssociation return true if indeed we want the association to me model to be saved.
This relies on a context value that is set at scope level with key model.SaveAssociation.This key must store a boolean value. It is possible to store the value as string "skip" if you want to skip the saving.
TODO: There is really no need for the skip string, a boolean false is enough since it will make the return value false and skip saving associations.
func TableName ¶
TableName returns a string representation of the possible name of the table that is mapped to the model value.
If it happens that the model value implements engine.Tabler interface then we go with it.
In case we are in search mode, the Tablename inside the e.Search.TableName is what we use.
func UpdatedAttrsWithValues ¶
func UpdatedAttrsWithValues(e *engine.Engine, value interface{}) (results map[string]interface{}, hasUpdate bool)
UpdatedAttrsWithValues returns a map of field names with value. This takes value, and updates any field that needts to be updated by adding the field name mapped to the new field value to the returned map results.
That applies if the value is a struct. Any other type of values are handeled by ConvertInterfaceToMap function.
Types ¶
This section is empty.