Documentation
¶
Overview ¶
Package builder contains functions for SQL building. Most of the functions builds the SQL from the enine.Engine, and uptates the struct in a convenient manner.
Be aware that, you should not pass a raw engine.Engine as some of the functions assumes engine.Engine.Search or engine.Engine.Scope is properly set.
Index ¶
- func AddIndex(e *engine.Engine, unique bool, indexName string, column ...string) error
- func CombinedCondition(e *engine.Engine, modelValue interface{}) (string, error)
- func GroupSQL(e *engine.Engine) string
- func HavingSQL(e *engine.Engine, modelValue interface{}) (string, error)
- func JoinSQL(e *engine.Engine, modelValue interface{}) (string, error)
- func LimitAndOffsetSQL(e *engine.Engine) string
- func Not(e *engine.Engine, modelValue interface{}, clause map[string]interface{}) (str string, err error)
- func OrderSQL(e *engine.Engine, modelValue interface{}) string
- func PrepareQuery(e *engine.Engine, modelValue interface{}) error
- func PrepareQuerySQL(e *engine.Engine, modelValue interface{}) (string, error)
- func PrimaryCondition(e *engine.Engine, modelValue, value interface{}) (string, error)
- func Select(e *engine.Engine, modelValue interface{}, clause map[string]interface{}) (str string)
- func SelectSQL(e *engine.Engine, modelValue interface{}) string
- func Where(e *engine.Engine, modelValue interface{}, clause map[string]interface{}) (str string, err error)
- func WhereSQL(e *engine.Engine, modelValue interface{}) (sql string, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CombinedCondition ¶
CombinedCondition combines all conditions to build a single SQL query.
func GroupSQL ¶
GroupSQL generates GROUP BY SQL. This returns an empty string when engine.Engine.Search.Group is empty.
func LimitAndOffsetSQL ¶
LimitAndOffsetSQL generates SQL for LIMIT and OFFSET. This relies on the implementation defined by the engine.Engine.Dialect.
func Not ¶
func Not(e *engine.Engine, modelValue interface{}, clause map[string]interface{}) (str string, err error)
Not generates sql for NOT condition. This accepts clause with two keys, one for query and the other for args( positional arguments)
query value can be of several types.
string, int int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, sql.Nu[]int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16,[]uint32,[]uint64, []string, []interface{} map[string]interface{}: struct
func PrepareQuery ¶
PrepareQuery sets the e.Scope.SQL by generating the whole sql query isnide engine.
func PrepareQuerySQL ¶
PrepareQuerySQL returns SQL that has been built on the engine e for the modelValue.
func PrimaryCondition ¶
PrimaryCondition generates WHERE clause with the value set for primary key. This will return an error if the modelValue doesn't have primary key, the reason for modelValue not to have a primary key might be due to the modelValue not being a valid ngorm model, please check scope.PrimaryKey for more details.
So, if the modelValue has primary key field id, and the value supplied is an integrer 14.The string generated will be id=$1 provided value is the first positional argument. Practically speaking it is the same as id=14.
func Where ¶
func Where(e *engine.Engine, modelValue interface{}, clause map[string]interface{}) (str string, err error)
Where buiilds the sql where condition. The clause is a map of two important keys, one is query and the second is args. It is possible to use a struct instead of a map for clause, but for now we can stick with this else we will need to do a giant refactoring.
query value can be of several types.
string, int int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, sql.Nu[]int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16,[]uint32,[]uint64, []string, []interface{} map[string]interface{}: struct
Note that if you supply a query as a struct then it should be a model. Example of a clause is,
map[string]interface{}{"query": query, "args": values}
Where query can be anything of the above types and values is possibly a slice of positional values. Positional values are values which will be inserted in place of a placeholder e.g ?. For instance s querry,
select * from home where item=? && importance =?
Then we can pass
[]interface}{"milk", "critical"}
The args slice has "milk" as the first thing and "critical" as the second. Now we can reconstruct the querry after appling the positional argument and get the following.
select * from home where item="milk" && importance="critical"
In real case, the way the positional arguments are bound is database specific. For example ql uses $1,$2,$3 etc but also supports ?. You don't have to worry about this, it is automatically handled by the supported database dialects.
Types ¶
This section is empty.