scope

package
v0.0.0-...-9ed695e Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2017 License: MIT Imports: 13 Imported by: 0

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

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

func AddToVars(e *engine.Engine, value interface{}) string

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 AutoIndex

func AutoIndex(e *engine.Engine, value interface{}) error

AutoIndex generates CREATE INDEX SQL

func Automigrate

func Automigrate(e *engine.Engine, value interface{}) error

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

func ChangeableField(e *engine.Engine, field *model.Field) bool

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

func CreateTable(e *engine.Engine, value interface{}) error

CreateTable generates CREATE TABLE SQL

func DropTable

func DropTable(e *engine.Engine, value interface{}) error

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

func FieldByName(e *engine.Engine, value interface{}, name string) (*model.Field, error)

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

func Fields(e *engine.Engine, value interface{}) ([]*model.Field, error)

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

func GetModelStruct(e *engine.Engine, value interface{}) (*model.Struct, error)

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 HasColumn

func HasColumn(e *engine.Engine, modelValue interface{}, column string) bool

HasColumn returns true if the modelValue has column of name column.

func HasConditions

func HasConditions(e *engine.Engine, modelValue interface{}) bool

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

func Initialize(e *engine.Engine)

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

func PrimaryField(e *engine.Engine, value interface{}) (*model.Field, error)

PrimaryField returns the field with name id, or any primary field that happens to be the one defined by the model value.

func PrimaryFields

func PrimaryFields(e *engine.Engine, value interface{}) ([]*model.Field, error)

PrimaryFields returns fields that have PRIMARY_KEY tag from the struct value.

func PrimaryKey

func PrimaryKey(e *engine.Engine, value interface{}) (string, error)

PrimaryKey returns the name of the primary key for the model value

func Quote

func Quote(e *engine.Engine, str string) string

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

func QuotedTableName(e *engine.Engine, value interface{}) string

QuotedTableName returns a quoted table name.

func SaveFieldAsAssociation

func SaveFieldAsAssociation(e *engine.Engine, field *model.Field) (bool, *model.Relationship)

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 Scan

func Scan(rows *sql.Rows, columns []string, fields []*model.Field)

Scan scans restult from the rows into fields.

func SelectAttrs

func SelectAttrs(e *engine.Engine) []string

SelectAttrs returns the attributes in the select query.

func SetColumn

func SetColumn(e *engine.Engine, column interface{}, value interface{}) error

SetColumn sets the column value.

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

func ShouldSaveAssociation(e *engine.Engine) bool

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

func TableName(e *engine.Engine, value interface{}) string

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.

Jump to

Keyboard shortcuts

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