ql

package
v0.104.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
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
	// PfInLike the IN or LIKE operations are allowed for the param
	PfInLike = 1 << 4
)

Variables

View Source
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 | PfInLike,
			Translate: func(tr Translator, sb *strings.Builder, p Param) error {
				sb.WriteString("n.path")
				return nil
			},
		},

		"node": {
			Flags: PfLValue | PfComparable,
			Translate: func(tr Translator, sb *strings.Builder, p Param) error {
				sb.WriteString("n.name")
				return nil
			},
		},

		"format": {
			Flags: PfLValue | PfComparable | PfInLike,
			Translate: func(tr Translator, sb *strings.Builder, p Param) error {
				sb.WriteString("ir.format")
				return nil
			},
		},

		"tag": {
			Flags: PfLValue | PfComparable | PfRValue | PfInLike,
			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].id() != StringParamID {
					return fmt.Errorf("tag() function expects the tag name (string) as the parameter: %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"|"LIKE")`
	SecondParam *Param ` @@}`
}

Condition is a unary or binary logical operation which has First mandatory param and optional operation and second param (optional)

type Const

type Const struct {
	Number float32 ` @Number`
	String string  ` | @String`
}

Const contains the constant either string or float32 value

func (Const) Value

func (c Const) Value() string

Value returns string value of the constant

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 Function

type Function struct {
	Name   string   ` @Ident `
	Params []*Param ` "(" (@@ {"," @@})? ")"`
}

Function is a functional parameter

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

func (Param) Name

func (p Param) Name(full bool) string

Name returns "value" of the constants (strings, numbers and the arrays) and names for the functions and identifiers

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

Jump to

Keyboard shortcuts

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