expr

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: GPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

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 RegisterFunc(funcName string, fn func(col any, value []any) (sql string, args []any, err error), drivers ...driver.Driver)

func RegisterLookup

func RegisterLookup(lookup string, fn func(col string, value []any) (sql string, args []any, err error), drivers ...driver.Driver)

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) IsNot

func (g *ExprGroup) IsNot() bool

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

func (*ExprGroup) SQL

func (g *ExprGroup) SQL(sb *strings.Builder) []any

type ExprNode

type ExprNode struct {
	// contains filtered or unexported fields
}

func Expr

func Expr(field string, operation string, value ...any) *ExprNode

func Q

func Q(fieldLookup string, value ...any) *ExprNode

func (*ExprNode) And

func (e *ExprNode) And(exprs ...Expression) LogicalExpression

func (*ExprNode) Clone

func (e *ExprNode) Clone() Expression

func (*ExprNode) IsNot

func (e *ExprNode) IsNot() bool

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

func (*ExprNode) SQL

func (e *ExprNode) SQL(sb *strings.Builder) []any

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 ExpressionInfo struct {
	Driver   driver.Driver
	Model    attrs.Definer
	AliasGen *alias.Generator
	Quote    string
}

type Func

type Func struct {
	// contains filtered or unexported fields
}

func FuncAvg

func FuncAvg(expr ...any) *Func

func FuncCoalesce

func FuncCoalesce(expr ...any) *Func

func FuncConcat

func FuncConcat(expr ...any) *Func

func FuncCount

func FuncCount(expr ...any) *Func

func FuncLength

func FuncLength(expr any) *Func

func FuncLower

func FuncLower(expr any) *Func

func FuncMax

func FuncMax(expr ...any) *Func

func FuncMin

func FuncMin(expr ...any) *Func

func FuncNow

func FuncNow() *Func

func FuncSubstr

func FuncSubstr(expr any, start, length any) *Func

func FuncSum

func FuncSum(expr ...any) *Func

func FuncUpper

func FuncUpper(expr any) *Func

func (*Func) Clone

func (e *Func) Clone() Expression

func (*Func) FieldName

func (e *Func) FieldName() string

func (*Func) Resolve

func (e *Func) Resolve(inf *ExpressionInfo) Expression

func (*Func) SQL

func (e *Func) SQL(sb *strings.Builder) []any

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 LogicalOp

type LogicalOp string
const (
	OpAnd LogicalOp = "AND"
	OpOr  LogicalOp = "OR"
)

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

func (*RawNamedExpression) SQL

func (e *RawNamedExpression) SQL(sb *strings.Builder) []any

Jump to

Keyboard shortcuts

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