builder

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package builder contains functions for SQL building. Most of the functions builds the SQL from the engine.Engine, and updates 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddIndex

func AddIndex(e *engine.Engine, unique bool, indexName string, column ...string) error

AddIndex builds SQL to add index for columns with given name

func CombinedCondition

func CombinedCondition(e *engine.Engine, modelValue interface{}) (string, error)

CombinedCondition combines all conditions to build a single SQL query.

func GroupSQL

func GroupSQL(e *engine.Engine) string

GroupSQL generates GROUP BY SQL. This returns an empty string when engine.Engine.Search.Group is empty.

func HavingSQL

func HavingSQL(e *engine.Engine, modelValue interface{}) (string, error)

HavingSQL builds HAVING SQL clause

func JoinSQL

func JoinSQL(e *engine.Engine, modelValue interface{}) (string, error)

JoinSQL builds JOIN SQL clause for modelValue using engine e as context.

func LimitAndOffsetSQL

func LimitAndOffsetSQL(e *engine.Engine) string

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 OrderSQL

func OrderSQL(e *engine.Engine, modelValue interface{}) string

OrderSQL builds ORDER BY SQL clause

func PrepareQuery

func PrepareQuery(e *engine.Engine, modelValue interface{}) error

PrepareQuery sets the e.Scope.SQL by generating the whole sql query inside engine.

func PrepareQuerySQL

func PrepareQuerySQL(e *engine.Engine, modelValue interface{}) (string, error)

PrepareQuerySQL returns SQL that has been built on the engine e for the modelValue.

func PrimaryCondition

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

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 integer 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 Select

func Select(e *engine.Engine, modelValue interface{}, clause map[string]interface{}) (str string)

Select builds select query

func SelectSQL

func SelectSQL(e *engine.Engine, modelValue interface{}) string

SelectSQL builds SELECT clause for modelValue using engine e as context.

func Where

func Where(e *engine.Engine, modelValue interface{}, clause map[string]interface{}) (str string, err error)

Where builds 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 query,

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 query 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.

func WhereSQL

func WhereSQL(e *engine.Engine, modelValue interface{}) (sql string, err error)

WhereSQL builds WHERE SQL clause of modelValue using the given engine e as context.

Types

This section is empty.

Jump to

Keyboard shortcuts

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