Documentation
¶
Index ¶
- func ParseExprStatement(statement string, value []any) (newStatement string, fields []string, values []any)
- func RegisterFunc(funcName string, ...)
- func RegisterLookup(lookup string, ...)
- func ResolveExpressionArgs(inf *ExpressionInfo, arguments []any) []any
- func ResolveExpressionField(inf *ExpressionInfo, field string, forUpdate bool) string
- type ExprGroup
- func (g *ExprGroup) And(exprs ...Expression) LogicalExpression
- func (g *ExprGroup) Clone() Expression
- func (g *ExprGroup) IsNot() bool
- func (g *ExprGroup) Not(not bool) LogicalExpression
- func (g *ExprGroup) Or(exprs ...Expression) LogicalExpression
- func (g *ExprGroup) Resolve(inf *ExpressionInfo) Expression
- func (g *ExprGroup) SQL(sb *strings.Builder) []any
- type ExprNode
- func (e *ExprNode) And(exprs ...Expression) LogicalExpression
- func (e *ExprNode) Clone() Expression
- func (e *ExprNode) IsNot() bool
- func (e *ExprNode) Not(not bool) LogicalExpression
- func (e *ExprNode) Or(exprs ...Expression) LogicalExpression
- func (e *ExprNode) Resolve(inf *ExpressionInfo) Expression
- func (e *ExprNode) SQL(sb *strings.Builder) []any
- type Expression
- type ExpressionInfo
- type Func
- func FuncAvg(expr ...any) *Func
- func FuncCoalesce(expr ...any) *Func
- func FuncConcat(expr ...any) *Func
- func FuncCount(expr ...any) *Func
- func FuncLength(expr any) *Func
- func FuncLower(expr any) *Func
- func FuncMax(expr ...any) *Func
- func FuncMin(expr ...any) *Func
- func FuncNow() *Func
- func FuncSubstr(expr any, start, length any) *Func
- func FuncSum(expr ...any) *Func
- func FuncUpper(expr any) *Func
- type LogicalExpression
- type LogicalOp
- type NamedExpression
- type RawExpr
- type RawNamedExpression
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseExprStatement ¶
func ParseExprStatement(statement string, value []any) (newStatement string, fields []string, values []any)
The statement should contain placeholders for the fields and values, which will be replaced with the actual values.
The placeholders for fields should be in the format ![FieldName], and the placeholders for values should be in the format ?[Index], or the values should use the regular SQL placeholder directly (database driver dependent).
Example usage:
# sets the field name to the first field found in the statement, I.E. ![Field1]: stmt, fields, values := ParseExprStatement("![Field1] = ![Age] + ?[1] + ![Height] + ?[2] * ?[1]", 3, 4)
func RegisterFunc ¶
func RegisterLookup ¶
func ResolveExpressionArgs ¶
func ResolveExpressionArgs(inf *ExpressionInfo, arguments []any) []any
func ResolveExpressionField ¶
func ResolveExpressionField(inf *ExpressionInfo, field string, forUpdate bool) string
Types ¶
type ExprGroup ¶
type ExprGroup struct {
// contains filtered or unexported fields
}
ExprGroup
func (*ExprGroup) And ¶
func (g *ExprGroup) And(exprs ...Expression) LogicalExpression
func (*ExprGroup) Clone ¶
func (g *ExprGroup) Clone() Expression
func (*ExprGroup) Not ¶
func (g *ExprGroup) Not(not bool) LogicalExpression
func (*ExprGroup) Or ¶
func (g *ExprGroup) Or(exprs ...Expression) LogicalExpression
func (*ExprGroup) Resolve ¶
func (g *ExprGroup) Resolve(inf *ExpressionInfo) Expression
type ExprNode ¶
type ExprNode struct {
// contains filtered or unexported fields
}
func (*ExprNode) And ¶
func (e *ExprNode) And(exprs ...Expression) LogicalExpression
func (*ExprNode) Clone ¶
func (e *ExprNode) Clone() Expression
func (*ExprNode) Not ¶
func (e *ExprNode) Not(not bool) LogicalExpression
func (*ExprNode) Or ¶
func (e *ExprNode) Or(exprs ...Expression) LogicalExpression
func (*ExprNode) Resolve ¶
func (e *ExprNode) Resolve(inf *ExpressionInfo) Expression
type Expression ¶
type Expression interface { SQL(sb *strings.Builder) []any Clone() Expression Resolve(inf *ExpressionInfo) Expression }
func And ¶
func And(exprs ...Expression) Expression
func Or ¶
func Or(exprs ...Expression) Expression
func Raw ¶
func Raw(statement string, value ...any) Expression
type ExpressionInfo ¶
type Func ¶
type Func struct {
// contains filtered or unexported fields
}
func FuncCoalesce ¶
func FuncConcat ¶
func FuncLength ¶
func FuncSubstr ¶
func (*Func) Clone ¶
func (e *Func) Clone() Expression
func (*Func) Resolve ¶
func (e *Func) Resolve(inf *ExpressionInfo) Expression
type LogicalExpression ¶
type LogicalExpression interface { Expression IsNot() bool Not(b bool) LogicalExpression And(...Expression) LogicalExpression Or(...Expression) LogicalExpression }
func Express ¶
func Express(key interface{}, vals ...interface{}) []LogicalExpression
type NamedExpression ¶
type NamedExpression interface { Expression FieldName() string }
func F ¶
func F(statement string, value ...any) NamedExpression
# sets the field name to the first field found in the statement, I.E. ![Height]: expr := F("? + ? + ![Height] + ? * ?", 4, 5, 6, 7) fmt.Println(expr.SQL()) // prints: "? + ? + table.height + ? * ?" fmt.Println(expr.Args()) // prints: [4, 5, 6, 7]
func NamedF ¶
func NamedF(fieldName, stmt string, value ...any) NamedExpression
NamedF creates a new RawNamedExpression with the given statement and values. It parses the statement to extract the fields and values, and returns a pointer to the new RawNamedExpression.
The statement should contain placeholders for the fields and values, which will be replaced with the actual values.
The placeholders for fields should be in the format ![FieldName], and the placeholders for values should be in the format ?[Index], or the values should use the regular SQL placeholder directly (database driver dependent).
Example usage:
expr := NamedF("Field1", "![Age] + ?[1] + ![Height] + ?[2] * ?[1]", 3, 4) fmt.Println(expr.SQL()) // prints: "table.age + ? + table.height + ?" fmt.Println(expr.Args()) // prints: [3, 4] expr := NamedF("Field1", "? + ? + ![Height] + ? * ?", 4, 5, 6, 7) fmt.Println(expr.SQL()) // prints: "? + ? + table.height + ? * ?" fmt.Println(expr.Args()) // prints: [4, 5, 6, 7]
func U ¶
func U(statement string, value ...any) NamedExpression
U creates a new RawNamedExpression with the given statement and values. It parses the statement to extract the fields and values, and returns a pointer to the new RawNamedExpression.
It is meant to be used for create & update statements, where there should be no alias used for the field name.
The first field in the statement is used as the field name for the expression, and the rest of the fields are used as placeholders for the values.
The statement should contain placeholders for the fields and values, which will be replaced with the actual values.
The placeholders for fields should be in the format ![FieldName], and the placeholders for values should be in the format ?[Index], or the values should use the regular SQL placeholder directly (database driver dependent).
Example usage:
# sets the field name to the first field found in the statement, I.E. ![Field1]: expr := U("![Field1] = ![Age] + ?[1] + ![Height] + ?[2] * ?[1]", 3, 4) fmt.Println(expr.SQL()) // prints: "field1 = age + ? + height + ?" fmt.Println(expr.Args()) // prints: [3, 4] # sets the field name to the first field found in the statement, I.E. ![Field1]: expr := U("![Field1] = ? + ? + ![Height] + ? * ?", 4, 5, 6, 7) fmt.Println(expr.SQL()) // prints: "field1 = ? + ? + height + ? * ?" fmt.Println(expr.Args()) // prints: [4, 5, 6, 7]
type RawExpr ¶
type RawExpr = RawNamedExpression
RawExpr is a function expression for SQL queries. It is used to represent a function call in SQL queries.
It can be used like so:
RawExpr{ // Represent the SQL function call, with each %s being replaced by the corresponding field in fields. Statement: `SUBSTR(TRIM(%s, " "), 0, 2) = ?``, // The fields to be used in the SQL function call. Each field will be replaced by the corresponding value in args. Fields: []string{"myField"}, // The arguments to be used in the SQL function call. Each argument will be replaced by the corresponding value in args. Params: []any{"ab"}, }
type RawNamedExpression ¶
type RawNamedExpression struct { Statement string Params []any Fields []string Field string // contains filtered or unexported fields }
func (*RawNamedExpression) Clone ¶
func (e *RawNamedExpression) Clone() Expression
func (*RawNamedExpression) FieldName ¶
func (e *RawNamedExpression) FieldName() string
func (*RawNamedExpression) Resolve ¶
func (e *RawNamedExpression) Resolve(inf *ExpressionInfo) Expression