Documentation
¶
Index ¶
- Constants
- Variables
- type Config
- type CustomBuildFn
- type CustomConditionFn
- type Dialect
- type InvalidCond
- type ModValueFn
- type Operator
- type Plan
- func (p *Plan) And(cond interface{}, vars ...interface{}) *Plan
- func (p *Plan) Build() (rp *Plan)
- func (p *Plan) Not(cond interface{}, vars ...interface{}) *Plan
- func (p *Plan) Or(cond interface{}, vars ...interface{}) *Plan
- func (p *Plan) SQL() string
- func (p *Plan) SetColumnAliases(aliases map[string]string, mode ...rune) *Plan
- func (p *Plan) SetCustomConditions(aliases map[string]CustomConditionFn, mode ...rune) *Plan
- func (p *Plan) SetTable(value string) *Plan
- func (p *Plan) Vars() []interface{}
- func (p *Plan) Where(cond interface{}, vars ...interface{}) *Plan
Constants ¶
const ( OverwriteMode = 'o' AppendMode = 'a' WriteMode = 'w' )
Modes to set configurations
const ( // DialectMySQLName defines the MySQL dialect name DialectMySQLName = "mysql" // DialectPostgreSQLName defines the PostgreSQL dialect name DialectPostgreSQLName = "postgres" )
Variables ¶
var ( // DialectMySQL predefines the MySQL dialect DialectMySQL = &mysqlDialect{} // DialectPostgreSQL predefines the PostgreSQL dialect DialectPostgreSQL = &postgresqlDialect{} )
var ( // DefaultConfig is the default configuration of the planner DefaultConfig = Config{ Separator: "__", Dialect: DialectPostgreSQL, ColumnAliases: make(map[string]string), CustomConditions: make(map[string]CustomConditionFn), } )
var ( // OperatorsList defines the list of built-in operators OperatorsList = map[string]*Operator{ "exact": defaultOperator, "iexact": &Operator{Template: "LOWER(%s) %s LOWER(?)"}, "notexact": &Operator{Operator: "<>"}, "inotexact": &Operator{Operator: "<>", Template: "LOWER(%s) %s LOWER(?)"}, "gt": &Operator{Operator: ">"}, "lt": &Operator{Operator: "<"}, "gte": &Operator{Operator: ">="}, "lte": &Operator{Operator: "<="}, "startswith": &Operator{ Operator: "LIKE", ModValue: func(value interface{}) interface{} { return Utils.ToString(value) + "%" }, }, "istartswith": &Operator{ Operator: "LIKE", Template: "LOWER(%s) %s LOWER(?)", ModValue: func(value interface{}) interface{} { return Utils.ToString(value) + "%" }, }, "endswith": &Operator{ Operator: "LIKE", ModValue: func(value interface{}) interface{} { return "%" + Utils.ToString(value) }, }, "iendswith": &Operator{ Operator: "LIKE", Template: "LOWER(%s) %s LOWER(?)", ModValue: func(value interface{}) interface{} { return "%" + Utils.ToString(value) }, }, "contains": &Operator{ Operator: "LIKE", ModValue: func(value interface{}) interface{} { return "%" + Utils.ToString(value) + "%" }, }, "icontains": &Operator{ Operator: "LIKE", Template: "LOWER(%s) %s LOWER(?)", ModValue: func(value interface{}) interface{} { return "%" + Utils.ToString(value) + "%" }, }, "in": &Operator{ Operator: "IN", Template: "%s %s (?)", ModValue: func(value interface{}) interface{} { return Utils.ToSlice(value) }, }, "date": &Operator{ Template: "DATE(%s) %s ?", ModValue: func(value interface{}) interface{} { return Utils.ToDate(value) }, }, "between": &Operator{ CustomBuild: func(field string, value interface{}, cfg Config) (string, []interface{}) { var from interface{} var to interface{} if vi, ok := value.([]interface{}); ok && len(vi) >= 2 { from = vi[0] to = vi[1] } else if vs, ok := value.([]string); ok && len(vs) >= 2 { from = vs[0] to = vs[1] } else if vt, ok := value.([]time.Time); ok && len(vt) >= 2 { from = vt[0] to = vt[1] } else { return "", []interface{}{} } return fmt.Sprintf("%s BETWEEN ? AND ?", field), []interface{}{Utils.ToDateTime(from), Utils.ToDateTime(to)} }, }, "isnull": &Operator{ CustomBuild: func(field string, value interface{}, cfg Config) (string, []interface{}) { operator := "IS NULL" if null, ok := value.(bool); ok && !null { operator = "IS NOT NULL" } return fmt.Sprintf("%s %s", field, operator), []interface{}{} }, }, "datebetween": &Operator{ CustomBuild: func(field string, value interface{}, cfg Config) (string, []interface{}) { var from interface{} var to interface{} if vi, ok := value.([]interface{}); ok && len(vi) >= 2 { from = vi[0] to = vi[1] } else if vs, ok := value.([]string); ok && len(vs) >= 2 { from = vs[0] to = vs[1] } else if vt, ok := value.([]time.Time); ok && len(vt) >= 2 { from = vt[0] to = vt[1] } else { return "", []interface{}{} } return fmt.Sprintf("DATE(%s) BETWEEN ? AND ?", field), []interface{}{Utils.ToDate(from), Utils.ToDate(to)} }, }, } )
var Utils = utilsCollection{}
Utils list of predefined functions to make our life easier
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // The separator between field and operation. Default to "__" which requires the condition format as: field__operator Separator string // Collections of methods to build correct SQL clause for specific dialect. Only support MySQL and PostgreSQL (default) for the moment Dialect Dialect // Whether to report error or silently skip anomalies in the conditions schema. Default to false Strict bool // Table name to add before the columns in SQL clause, i.e: table_name.column_name. Default to empty which will keep the column unchanged Table string // The map of column aliases to be replaced when build the SQL clause. Use cases: // Example: {"name": "foo.name", "bname": "bar.name"} ColumnAliases map[string]string // Custom conditions allow full access on the condition generating CustomConditions map[string]CustomConditionFn // contains filtered or unexported fields }
Config defines the config for planner
type CustomBuildFn ¶
CustomBuildFn represents the function to build SQL string for the operator
type CustomConditionFn ¶ added in v1.1.0
CustomConditionFn represents the func signature which provide full access on the condition generating. Return value should be in form of condition, i.e a map or slice Return nil will exclude the condition from result
type InvalidCond ¶
type InvalidCond struct {
// contains filtered or unexported fields
}
InvalidCond represents the error when invalid condition is given
func (*InvalidCond) Error ¶
func (e *InvalidCond) Error() string
type ModValueFn ¶
type ModValueFn func(value interface{}) interface{}
ModValueFn represents the function to modify only the value before actually build the SQL
type Operator ¶
type Operator struct { // Reference to an existing operator AliasOf string // The actual SQL operator. Default to "=" if empty Operator string // The SQL template. Default to "%s %s ?" // Note: This must contains 2 "%s" placeholders for the column & operator, and 1 "?" for the value Template string // The function to build the SQL condition in your own way. Ignored if AliasOf is provided, ignores Operator & Template. CustomBuild CustomBuildFn // Instead of customize the whole build func, you probably only want to modify the value a litle bit ModValue ModValueFn }
Operator represents an alias for the SQL operator
type Plan ¶
type Plan struct { Error error // contains filtered or unexported fields }
Plan contains information to build WHERE clause
func Where ¶
func Where(cond interface{}, vars ...interface{}) *Plan
Where is shortcut to create new plan with default configurations
func WithConfig ¶
WithConfig returns an empty plan using the given configs. Zero value will be replaced by default config.
func (*Plan) SetColumnAliases ¶ added in v1.1.0
SetColumnAliases updates the `ColumnAliases` config value
func (*Plan) SetCustomConditions ¶ added in v1.1.0
func (p *Plan) SetCustomConditions(aliases map[string]CustomConditionFn, mode ...rune) *Plan
SetCustomConditions updates the `CustomConditions` config values