sqltypes

package
v0.27.3 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package sqltypes contains the types used to convert rego queries into SQL. The rego ast is converted into these types to better control the SQL generation. It allows writing the SQL generation for types in an easier to read way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPrimitive

func IsPrimitive(n Node) bool

func RegoVarPath

func RegoVarPath(path []string, terms []*ast.Term) ([]*ast.Term, error)

RegoVarPath will consume the following terms from the given rego Ref and return the remaining terms. If the path does not fully match, an error is returned. The first term must always be a Var.

Types

type ASTArray

type ASTArray struct {
	Source RegoSource
	Value  []Node
}

func (ASTArray) ContainsSQL

func (a ASTArray) ContainsSQL(cfg *SQLGenerator, needle Node) (string, error)

func (ASTArray) MyType

func (a ASTArray) MyType() Node

func (ASTArray) SQLString

func (a ASTArray) SQLString(cfg *SQLGenerator) string

func (ASTArray) UseAs

func (ASTArray) UseAs() Node

type AstBoolean

type AstBoolean struct {
	Source RegoSource
	Value  bool
}

AstBoolean is a literal true/false value.

func (AstBoolean) EqualsSQLString

func (b AstBoolean) EqualsSQLString(cfg *SQLGenerator, not bool, other Node) (string, error)

func (AstBoolean) IsBooleanNode

func (AstBoolean) IsBooleanNode()

func (AstBoolean) SQLString

func (b AstBoolean) SQLString(_ *SQLGenerator) string

func (AstBoolean) UseAs

func (AstBoolean) UseAs() Node

type AstNumber

type AstNumber struct {
	Source RegoSource
	// Value is intentionally vague as to if it's an integer or a float.
	// This defers that decision to the user. Rego keeps all numbers in this
	// type. If we were to source the type from something other than Rego,
	// we might want to make a Float and Int type which keep the original
	// precision.
	Value json.Number
}

func (AstNumber) EqualsSQLString

func (n AstNumber) EqualsSQLString(cfg *SQLGenerator, not bool, other Node) (string, error)

func (AstNumber) SQLString

func (n AstNumber) SQLString(_ *SQLGenerator) string

func (AstNumber) UseAs

func (AstNumber) UseAs() Node

type AstString

type AstString struct {
	Source RegoSource
	Value  string
}

func (AstString) EqualsSQLString

func (s AstString) EqualsSQLString(cfg *SQLGenerator, not bool, other Node) (string, error)

func (AstString) SQLString

func (s AstString) SQLString(_ *SQLGenerator) string

func (AstString) UseAs

func (AstString) UseAs() Node

type BooleanNode

type BooleanNode interface {
	Node
	IsBooleanNode()
}

BooleanNode is a node that returns a true/false when evaluated.

func And

func And(source RegoSource, terms ...BooleanNode) BooleanNode

func Bool

func Bool(t bool) BooleanNode

func BoolParenthesis

func BoolParenthesis(value BooleanNode) BooleanNode

BoolParenthesis wraps the given boolean node in parens. This is useful for grouping and avoiding ambiguity. This does not work for mathematical parenthesis to change order of operations.

func Equality

func Equality(notEquals bool, a, b Node) BooleanNode

func MemberOf

func MemberOf(needle, haystack Node) BooleanNode

func Or

func Or(source RegoSource, terms ...BooleanNode) BooleanNode

type Node

type Node interface {
	SQLString(cfg *SQLGenerator) string
	// UseAs is a helper function to allow a node to be used as a different
	// Node in operators. For example, a variable is really just a "string", so
	// having the Equality operator check for "String" or "StringVar" is just
	// excessive. Instead, we can just have the variable implement this function.
	UseAs() Node
}

func AlwaysFalseNode

func AlwaysFalseNode(n Node) Node

AlwaysFalseNode is mainly used for unit testing to make a Node immediately.

func Array

func Array(source RegoSource, nodes ...Node) (Node, error)

Array is typed to whatever the first element is. If there is not first element, the array element type is invalid.

func Number

func Number(source RegoSource, v json.Number) Node

func String

func String(v string) Node

type RegoSource

type RegoSource string

type SQLGenerator

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

func NewSQLGenerator

func NewSQLGenerator() *SQLGenerator

func (*SQLGenerator) AddError

func (g *SQLGenerator) AddError(err error)

func (*SQLGenerator) Errors

func (g *SQLGenerator) Errors() []error

type SupportsContainedIn

type SupportsContainedIn interface {
	ContainedInSQL(cfg *SQLGenerator, other Node) (string, error)
}

SupportsContainedIn is the inverse of SupportsContains. It is implemented from the "needle" rather than the haystack.

type SupportsContains

type SupportsContains interface {
	ContainsSQL(cfg *SQLGenerator, other Node) (string, error)
}

SupportsContains is an interface that can be implemented by types that support "me.Contains(other)". This is `internal_member2` in the rego.

type SupportsEquality

type SupportsEquality interface {
	// EqualsSQLString intentionally returns an error. This is so if
	// left = right is not supported, we can try right = left.
	EqualsSQLString(cfg *SQLGenerator, not bool, other Node) (string, error)
}

SupportsEquality is an interface that can be implemented by types that support equality with other types. We defer to other types to implement this as it is much easier to implement this in the context of the type.

type VariableConverter

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

func NewVariableConverter

func NewVariableConverter() *VariableConverter

func (*VariableConverter) ConvertVariable

func (vc *VariableConverter) ConvertVariable(rego ast.Ref) (Node, bool)

func (*VariableConverter) RegisterMatcher

func (vc *VariableConverter) RegisterMatcher(m ...VariableMatcher) *VariableConverter

type VariableMatcher

type VariableMatcher interface {
	ConvertVariable(rego ast.Ref) (Node, bool)
}

func AlwaysFalse

func AlwaysFalse(m VariableMatcher) VariableMatcher

AlwaysFalse overrides the inner node with a constant "false".

func StringVarMatcher

func StringVarMatcher(sqlString string, regoPath []string) VariableMatcher

Jump to

Keyboard shortcuts

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