ql

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 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
	// PfGreaterLess the parameter can be compared with < or > only
	PfGreaterLess = 1 << 5
	// The ParamDialect ValueF() is a constant value and should not be re-calculated every time when called
	// it also means that ValueF will return a value for the *new(T)
	PfConstValue = 1 << 6
)
View Source
const (
	StringParamID = "__string__"
	NumberParamID = "__number__"
	ArrayParamID  = "__array__"
)

Variables

View Source
var (
	LogsCondValueDialect = Dialect[*solaris.Log]{
		StringParamID: {
			Flags: PfRValue | PfComparable | PfConstValue,
			ValueF: func(p *Param, _ *solaris.Log) (any, error) {
				return p.Const.Value(), nil
			},
			Type: VTString,
		},
		ArrayParamID: {
			Flags: PfRValue | PfConstValue,
			ValueF: func(p *Param, _ *solaris.Log) (any, error) {
				var strArr []string
				for _, elem := range p.Array {
					strArr = append(strArr, elem.Value())
				}
				return strArr, nil
			},
			Type: VTStrings,
		},
		"logID": {
			Flags: PfLValue | PfComparable | PfInLike,
			ValueF: func(p *Param, log *solaris.Log) (any, error) {
				return log.ID, nil
			},
			Type: VTString,
		},
		"tag": {
			Flags: PfLValue | PfComparable | PfRValue | PfInLike,
			CheckF: func(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)
				}
				return nil
			},
			ValueF: func(p *Param, log *solaris.Log) (any, error) {
				if len(log.Tags) == 0 {
					return "", nil
				}
				return log.Tags[p.Function.Params[0].Name(true)], nil
			},
			Type: VTString,
		},
	}
	LogsCondTranslateDialect = Dialect[*solaris.Log]{
		StringParamID: {
			Flags: PfRValue | PfComparable,
			TranslateF: func(tr Translator[*solaris.Log], sb *strings.Builder, p Param) error {

				sb.WriteString("'")
				sb.WriteString(*p.Const.String)
				sb.WriteString("'")
				return nil
			},
		},
		ArrayParamID: {
			Flags: PfRValue,
			TranslateF: func(tr Translator[*solaris.Log], sb *strings.Builder, p Param) error {
				sb.WriteString("(")
				for i, c := range p.Array {
					if i > 0 {
						sb.WriteString(", ")
					}
					if c.String == nil {
						return fmt.Errorf("array must contain only strings: %w", errors.ErrInvalid)
					}
					sb.WriteString("'")
					sb.WriteString(*c.String)
					sb.WriteString("'")
				}
				sb.WriteString(")")
				return nil
			},
		},
		"logID": {
			Flags: PfLValue | PfComparable | PfInLike,
			TranslateF: func(tr Translator[*solaris.Log], sb *strings.Builder, p Param) error {
				sb.WriteString("id")
				return nil
			},
		},
		"tag": {
			Flags: PfLValue | PfComparable | PfRValue | PfInLike,
			TranslateF: func(tr Translator[*solaris.Log], 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("tags ->> ")
				_ = tr.Param2Sql(sb, p.Function.Params[0])
				return nil
			},
		},
	}
	RecordsCondValueDialect = Dialect[*solaris.Record]{
		StringParamID: {
			Flags: PfRValue | PfComparable | PfConstValue,
			ValueF: func(p *Param, _ *solaris.Record) (any, error) {
				return p.Const.Value(), nil
			},
			Type: VTString,
		},
		"ctime": {
			Flags: PfLValue | PfComparable,
			ValueF: func(p *Param, r *solaris.Record) (any, error) {
				if r.CreatedAt != nil {
					return r.CreatedAt.AsTime(), nil
				}
				return time.Time{}, nil
			},
			Type: VTTime,
		},
	}
)
View Source
var (
	OpsAll  = []string{"<", ">", "<=", ">=", "=", "!="}
	OpsGtLt = []string{"<", ">"}
)

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

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 added in v0.9.0

type Dialect[T any] map[string]ParamDialect[T]

type ExprF added in v0.9.0

type ExprF[T any] func(t T) bool

ExprF represents the Expression function to evaluate the expression for the type T

func BuildExprF added in v0.9.0

func BuildExprF[T any](expr *Expression, dialect Dialect[T]) (ExprF[T], error)

BuildExprF allows to build the ExprF[T] function by the expression and the dialect provided. The result function may be used for testing a value of T either it matches the expression or not.

type Expression

type Expression struct {
	Or []*OrCondition `@@ { "OR" @@ }`
}

Expression is an AST element which describes a series of OR conditions

func Parse

func Parse(expr string) (*Expression, error)

Parse parses the expr and in case of success returns AST

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

func (p Param) ID() string

ID returns the param id by its type: - string: StringParamID - number: NumberParamID - function: the function name - identifier: the identifier name - array: ArrayParamID

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 ParamDialect

type ParamDialect[T any] struct {
	// Flags contains flags associated with the parameter, please see Pf constants above
	Flags int
	// CheckF is used for calculating the parameter value while building an evaluator function
	// If not specified, then the parameter is always correct
	CheckF func(p *Param) error
	// TranslateF is the function (can be nil), which is called for translation the parameter p to the dialect.
	TranslateF func(tr Translator[T], sb *strings.Builder, p Param) error
	// ValueF is the function which allows to get the parameter value. The function MUST NOT be called
	// if the CheckF returns an error
	ValueF valueF[T]
	// Define the type for the ValueF result
	Type ValueType
}

ParamDialect is used for describing grama objects in the dialect

type ParamIntervalBuilder added in v0.15.0

type ParamIntervalBuilder[T, K any] struct {
	// contains filtered or unexported fields
}

ParamIntervalBuilder allows to build value intervals from the AST expression for a given parameter and comparison operations specified.

func NewParamIntervalBuilder added in v0.15.0

func NewParamIntervalBuilder[T, K any](basis intervals.Basis[T], dialect Dialect[K], param string, ops []string) ParamIntervalBuilder[T, K]

NewParamIntervalBuilder returns new ParamIntervalBuilder.

func (*ParamIntervalBuilder[T, K]) Build added in v0.15.0

func (ib *ParamIntervalBuilder[T, K]) Build(expr *Expression) ([]intervals.Interval[T], error)

Build returns a list of intervals built from the AST expression. Returned intervals are sorted by the L border.

type Translator added in v0.17.0

type Translator[T any] struct {
	// contains filtered or unexported fields
}

Translator struct allows to turn AST objects (Expression, Condition etc.) to the statements according to the dialect provided

func NewTranslator added in v0.17.0

func NewTranslator[T any](dialect Dialect[T]) Translator[T]

NewTranslator creates new Translator with dialects provided

func (Translator[T]) Condition2Sql added in v0.17.0

func (tr Translator[T]) 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[T]) Expression2Sql added in v0.17.0

func (tr Translator[T]) 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[T]) OrCondition2Sql added in v0.17.0

func (tr Translator[T]) 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[T]) Param2Sql added in v0.17.0

func (tr Translator[T]) 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[T]) Params2Sql added in v0.17.0

func (tr Translator[T]) Params2Sql(sb *strings.Builder, pp []*Param) error

Params2Sql turns the AST object pp to the query string according to the dialect of the translator

func (Translator[T]) Translate added in v0.17.0

func (tr Translator[T]) Translate(sb *strings.Builder, expr string) error

Translate translates the expression string to string according to the dialect of the translator

func (Translator[T]) XCondition2Sql added in v0.17.0

func (tr Translator[T]) 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 ValueType added in v0.9.0

type ValueType int

ValueType defines the type returned by a parameter value function (see below)

const (
	VTNA      ValueType = 0
	VTString  ValueType = 1
	VTTime    ValueType = 2
	VTBool    ValueType = 3
	VTStrings ValueType = 4
)

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