Documentation ¶
Index ¶
- Constants
- Variables
- type Condition
- type Const
- type Dialect
- type Expression
- type Function
- type OrCondition
- type Param
- type Translator
- func (tr Translator) Condition2Sql(sb *strings.Builder, c *Condition) error
- func (tr Translator) Expression2Sql(sb *strings.Builder, e *Expression) error
- func (tr Translator) OrCondition2Sql(sb *strings.Builder, oc *OrCondition) error
- func (tr Translator) Param2Sql(sb *strings.Builder, p *Param) error
- func (tr Translator) Params2Sql(sb *strings.Builder, prms []*Param) error
- func (tr Translator) Translate(sb *strings.Builder, expr string) error
- func (tr Translator) XCondition2Sql(sb *strings.Builder, xc *XCondition) error
- type XCondition
Constants ¶
const ( StringParamID = "__string__" NumberParamID = "__number__" ArrayParamID = "__array__" // PfLValue the parameter can be a lvalue in the condition PfLValue = 1 << 0 // PfRValue the parameter can be a rvalue in the condition PfRValue = 1 << 1 // PfNop the parameter cannot have any operation PfNop = 1 << 2 // PfComparable the parameter can be compared: <, >, !=, =, >=, <= PfComparable = 1 << 3 // PfInArray the IN operation is allowed for the param PfInArray = 1 << 4 )
Variables ¶
var ( // PqFilterConditionsDialect is a set of specific dialects for // translating filter conditions into Postgres where condition. PqFilterConditionsDialect = map[string]Dialect{ StringParamID: { Flags: PfRValue | PfComparable, Translate: func(tr Translator, sb *strings.Builder, p Param) error { sb.WriteString("'") sb.WriteString(p.Const.String) sb.WriteString("'") return nil }, }, NumberParamID: {Flags: PfRValue | PfComparable}, ArrayParamID: {Flags: PfRValue}, "path": { Flags: PfLValue | PfComparable | PfInArray, Translate: func(tr Translator, sb *strings.Builder, p Param) error { sb.WriteString("concat(n.path, n.name)") return nil }, }, "format": { Flags: PfLValue | PfComparable | PfInArray, }, "tag": { Flags: PfLValue | PfComparable | PfRValue | PfInArray, Translate: func(tr Translator, sb *strings.Builder, p Param) error { if p.Function == nil { return fmt.Errorf("tag must be a function: %w", errors.ErrInvalid) } if len(p.Function.Params) != 1 { return fmt.Errorf("tag() function expects only one parameter - the name of the tag: %w", errors.ErrInvalid) } if p.Function.Params[0].Const == nil { return fmt.Errorf("tag() function expects the tag name (string) as the parameter: %w", errors.ErrInvalid) } if p.Function.Params[0].Const.String == "" { return fmt.Errorf("tag() expects the tag name, which cannot be empty: %w", errors.ErrInvalid) } sb.WriteString("n.tags ->> ") _ = tr.Param2Sql(sb, p.Function.Params[0]) return nil }, }, "prefix": { Flags: PfLValue | PfNop, Translate: func(tr Translator, sb *strings.Builder, p Param) error { if p.Function == nil { return fmt.Errorf("prefix must be a function: %w", errors.ErrInvalid) } if len(p.Function.Params) != 2 { return fmt.Errorf("prefix(s, p) function expects two parameters: %w", errors.ErrInvalid) } sb.WriteString(" position(") _ = tr.Param2Sql(sb, p.Function.Params[1]) sb.WriteString(" in ") _ = tr.Param2Sql(sb, p.Function.Params[0]) sb.WriteString(") = 1") return nil }, }, } )
Functions ¶
This section is empty.
Types ¶
type Condition ¶
type Condition struct { FirstParam Param ` @@` Op string ` {@("<"|">"|">="|"<="|"!="|"="|"IN")` SecondParam *Param ` @@}` }
Condition is a unary or binary logical operation which has First mandatory param and optional operation and second param (optional)
type Dialect ¶
type Dialect struct { // Flags defines how the parameter can be treated. Is it Unary or binary operation, can the parameter // be a lvalue etc. Flags int // Translate is the function (can be nil), which is called for translation the parameter p to the dialect. For example, // on the ql level the function hasPrefix(path, "abc") may be translated to the Postgres SQL: // position('abc' in record.path) = 1 Translate func(tr Translator, sb *strings.Builder, p Param) error }
Dialect describes how the parameter of specific type or name should be treated. Dialect has a name, either predefined for String(StringParamID), Number(NumberParamID) or the Array(ArrayParamID) or it is defined for the specific identifier or function name.
type Expression ¶
type Expression struct {
Or []*OrCondition `@@ { "OR" @@ }`
}
Expression is an AST element which describes a series of OR conditions
type OrCondition ¶
type OrCondition struct {
And []*XCondition `@@ { "AND" @@ }`
}
OrCondition is an AST element which describes a series of AND conditions
type Param ¶
type Param struct { Const *Const ` @@` Function *Function ` | @@` Identifier string ` | @Ident` Array []*Const `|"[" (@@ {"," @@})?"]"` }
Param describes a parameter either a constant (string or number), function, identifier or an array of constants
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator struct allows to turn AST objects (Expression, Condition etc.) to the SQL statements according to the dialect provided
func NewTranslator ¶
func NewTranslator(dialects map[string]Dialect) Translator
NewTranslator creates new Translator with dialects provided
func (Translator) Condition2Sql ¶
func (tr Translator) Condition2Sql(sb *strings.Builder, c *Condition) error
Condition2Sql turns the AST object c to the query string according to the dialect of the translator
func (Translator) Expression2Sql ¶
func (tr Translator) Expression2Sql(sb *strings.Builder, e *Expression) error
Expression2Sql turns the AST object e to the query string according to the dialect of the translator
func (Translator) OrCondition2Sql ¶
func (tr Translator) OrCondition2Sql(sb *strings.Builder, oc *OrCondition) error
OrCondition2Sql turns the AST object oc to the query string according to the dialect of the translator
func (Translator) Param2Sql ¶
func (tr Translator) Param2Sql(sb *strings.Builder, p *Param) error
Param2Sql turns the AST object p to the query string according to the dialect of the translator
func (Translator) Params2Sql ¶
func (tr Translator) Params2Sql(sb *strings.Builder, prms []*Param) error
Params2Sql turns the AST object prms to the query string according to the dialect of the translator
func (Translator) Translate ¶ added in v0.94.0
func (tr Translator) Translate(sb *strings.Builder, expr string) error
Translate translates the expression string to string according to the dialect of the translator
func (Translator) XCondition2Sql ¶
func (tr Translator) XCondition2Sql(sb *strings.Builder, xc *XCondition) error
XCondition2Sql turns the AST object xc to the query string according to the dialect of the translator
type XCondition ¶
type XCondition struct { Not bool ` [@"NOT"] ` Cond *Condition `( @@` Expr *Expression `| "(" @@ ")")` }
XCondition is an AST element which groups either a Condition object or an Expression object