Documentation ¶
Overview ¶
The query package primarily contains code used internally by the framework to build queries. You will primarily interact with Nodes through the code generated ORM structures. Other than working with Nodes, you will not likely need to use the query package, unless you are creating a database driver.
Index ¶
- Constants
- func ColumnNodeDbName(n *ColumnNode) string
- func ExpandNode(n NodeI)
- func LogNode(n NodeI, level int)
- func ManyManyNodeDbColumn(n *ManyManyNode) string
- func ManyManyNodeDbTable(n *ManyManyNode) string
- func ManyManyNodeIsArray(n *ManyManyNode) bool
- func ManyManyNodeIsTypeTable(n *ManyManyNode) bool
- func ManyManyNodeRefColumn(n *ManyManyNode) string
- func ManyManyNodeRefTable(n *ManyManyNode) string
- func NodeGoName(n NodeI) string
- func NodeIsConditioner(n NodeI) bool
- func NodeIsExpanded(n NodeI) bool
- func NodeIsExpander(n NodeI) bool
- func NodeSetCondition(n NodeI, condition NodeI)
- func NodeSorterSortDesc(n NodeSorter) bool
- func NodeTableName(n NodeI) string
- func OperationNodeDistinct(n *OperationNode) bool
- func OperationNodeFunction(n *OperationNode) string
- func ReferenceNodeDbColumnName(n *ReferenceNode) string
- func ReferenceNodeRefColumn(n *ReferenceNode) string
- func ReferenceNodeRefTable(n *ReferenceNode) string
- func ReverseReferenceNodeDbColumnName(n *ReverseReferenceNode) string
- func ReverseReferenceNodeIsArray(n *ReverseReferenceNode) bool
- func ReverseReferenceNodeRefColumn(n *ReverseReferenceNode) string
- func ReverseReferenceNodeRefTable(n *ReverseReferenceNode) string
- func SetParentNode(child NodeI, parent NodeI)
- func ValueNodeGetValue(n *ValueNode) interface{}
- type AliasNode
- type AliasNodeI
- type AliasValue
- type ColumnNode
- type Expander
- type GoColumnType
- type ManyManyNode
- type Node
- type NodeI
- func ChildNodes(n nodeLinkI) []NodeI
- func ContainedNodes(n NodeI) (nodes []NodeI)
- func NewValueNode(i interface{}) NodeI
- func NodeCondition(n NodeI) NodeI
- func OperationNodeOperands(n *OperationNode) []NodeI
- func ParentNode(n NodeI) NodeI
- func RelatedColumnNode(n NodeI) NodeI
- func RootNode(n nodeLinkI) NodeI
- func Value(i interface{}) NodeI
- type NodeSorter
- type NodeType
- type OperationNode
- type Operator
- type QueryBuilderI
- type ReferenceNode
- type ReverseReferenceNode
- type SubqueryNode
- type TableNode
- type TableNodeI
- type ValueNode
Constants ¶
const ( OpEqual Operator = "=" OpNotEqual Operator = "<>" OpAnd = "AND" OpOr = "OR" OpXor = "XOR" OpGreater = ">" OpGreaterEqual = ">=" OpLess = "<" OpLessEqual = "<=" // Unary logical OpNot = "NOT" OpAll = "1=1" OpNone = "1=0" // Math operators OpAdd = "+" OpSubtract = "-" OpMultiply = "*" OpDivide = "/" OpModulo = "%" // Unary math OpNegate = " -" // Bit operators OpBitAnd = "&" OpBitOr = "|" OpBitXor = "^" OpShiftLeft = "<<" OpShiftRight = ">>" // Unary bit OpBitInvert = "~" // Function operator // The function name is followed by the operators in parenthesis OpFunc = "func" // SQL functions that act like operators in that the operator is put in between the operands OpLike = "LIKE" OpIn = "IN" OpNotIn = "NOT IN" // Special NULL tests OpNull = "NULL" OpNotNull = "NOT NULL" )
const AliasResults = "aliases_"
The special item to use for named aliases in the result set
Variables ¶
This section is empty.
Functions ¶
func ColumnNodeDbName ¶
func ColumnNodeDbName(n *ColumnNode) string
ColumnNodeDbName is used internally by the framework to return the name of the column in the database.
func ExpandNode ¶
func ExpandNode(n NodeI)
ExpandNode is used internally by the framework to expand a many-many relationship.
func ManyManyNodeDbColumn ¶
func ManyManyNodeDbColumn(n *ManyManyNode) string
ManyManyNodeDbColumn is used internally by the framework to return the column name in the table the node belongs to
func ManyManyNodeDbTable ¶
func ManyManyNodeDbTable(n *ManyManyNode) string
ManyManyNodeDbTable is used internally by the framework to return the table name of the table the node belongs to
func ManyManyNodeIsArray ¶
func ManyManyNodeIsArray(n *ManyManyNode) bool
ManyManyNodeIsArray is used internally by the framework to return whether the node creates an array, or just a link to a single item.
func ManyManyNodeIsTypeTable ¶
func ManyManyNodeIsTypeTable(n *ManyManyNode) bool
ManyManyNodeIsTypeTable is used internally by the framework to return whether the node points to a type table
func ManyManyNodeRefColumn ¶
func ManyManyNodeRefColumn(n *ManyManyNode) string
ManyManyNodeRefColumn is used internally by the framework to return the column name on the other end of the link
func ManyManyNodeRefTable ¶
func ManyManyNodeRefTable(n *ManyManyNode) string
ManyManyNodeRefTable is used internally by the framework to return the table name on the other end of the link
func NodeGoName ¶
NodeGoName is used internally by the framework to return the go name of the item the node refers to.
func NodeIsConditioner ¶
NodeIsConditioner is used internally by the framework to determine if the node has a condition.
func NodeIsExpanded ¶
NodeIsExpanded is used internally by the framework to detect if the node is an expanded many-many relationship.
func NodeIsExpander ¶
NodeIsExpander is used internally by the framework to detect if the node can be an expanded many-many relationship.
func NodeSetCondition ¶
NodeSetCondition is used internally by the framework to set a condition on a node.
func NodeSorterSortDesc ¶
func NodeSorterSortDesc(n NodeSorter) bool
NodeSorterSortDesc is used internally by the framework to determine if the NodeSorter is descending.
func NodeTableName ¶
NodeTableName is used internally by the framework to return the table associated with a node.
func OperationNodeDistinct ¶
func OperationNodeDistinct(n *OperationNode) bool
OperationNodeDistinct is used internally by the framework to get the distinct value.
func OperationNodeFunction ¶
func OperationNodeFunction(n *OperationNode) string
OperationNodeFunction is used internally by the framework to get the function.
func ReferenceNodeDbColumnName ¶
func ReferenceNodeDbColumnName(n *ReferenceNode) string
ReferenceNodeDbColumnName is used internally by the framework to get the column name for this side of the relationship.
func ReferenceNodeRefColumn ¶
func ReferenceNodeRefColumn(n *ReferenceNode) string
ReferenceNodeRefColumn is used internally by the framework to get the column name for the other side of the relationship.
func ReferenceNodeRefTable ¶
func ReferenceNodeRefTable(n *ReferenceNode) string
ReferenceNodeRefTable is used internally by the framework to get the table name for the other side of the relationship.
func ReverseReferenceNodeDbColumnName ¶
func ReverseReferenceNodeDbColumnName(n *ReverseReferenceNode) string
ReverseReferenceNodeDbColumnName is used internally by the framework to get the database column on this side of the relationship, which is most likely the primary key.
func ReverseReferenceNodeIsArray ¶
func ReverseReferenceNodeIsArray(n *ReverseReferenceNode) bool
ReverseReferenceNodeIsArray is used internally by the framework to determine if a node should create an array
func ReverseReferenceNodeRefColumn ¶
func ReverseReferenceNodeRefColumn(n *ReverseReferenceNode) string
ReverseReferenceNodeRefColumn is used internally by the framework to get the referenced column
func ReverseReferenceNodeRefTable ¶
func ReverseReferenceNodeRefTable(n *ReverseReferenceNode) string
ReverseReferenceNodeRefTable is used internally by the framework to get the referenced table
func SetParentNode ¶
SetParentNode is used internally by the framework. It is used by the query builder to build a join tree, and by the codegenerator to initialize nodes
func ValueNodeGetValue ¶
func ValueNodeGetValue(n *ValueNode) interface{}
ValueNodeGetValue is used internally by the framework to get the node's internal value.
Types ¶
type AliasNode ¶
type AliasNode struct {
Node
}
An AliasNode allows reference to a prior aliased operation later in a query. An alias is a name given to a computed value.
type AliasNodeI ¶
type AliasNodeI interface { NodeI }
type AliasValue ¶
type AliasValue struct {
// contains filtered or unexported fields
}
An AliasValue is returned by the GetAlias function that is generated for each type. You then convert the alias to a particular type to use it.
func NewAliasValue ¶
func NewAliasValue(a interface{}) AliasValue
NewAliasValue is used by the ORM to wrap an aliased operation or computed value that was returned by a query. You would not normally call this function.
func (AliasValue) DateTime ¶
func (a AliasValue) DateTime() datetime.DateTime
DateTime returns the value as a datetime.DateTime value.
func (AliasValue) IsNil ¶
func (a AliasValue) IsNil() bool
IsNil returns true if the value returned was a NULL value from the database.
func (AliasValue) IsNull ¶
func (a AliasValue) IsNull() bool
IsNull is the same as IsNil, and returns true if the operation return a NULL value from the database.
func (AliasValue) String ¶
func (a AliasValue) String() string
String returns the value as a string. A NULL value will be an empty string.
type ColumnNode ¶
type ColumnNode struct { Node // contains filtered or unexported fields }
A Column represents a table or field in a database structure, and is the leaf of a node tree or chain.
func NewColumnNode ¶
func NewColumnNode(dbKey string, dbTable string, dbName string, goName string, goType GoColumnType) *ColumnNode
func (*ColumnNode) Ascending ¶
func (n *ColumnNode) Ascending() NodeI
Ascending is used in an OrderBy query builder function to sort the column in ascending order.
func (*ColumnNode) Descending ¶
func (n *ColumnNode) Descending() NodeI
Descending is used in an OrderBy query builder function to sort the column in descending order.
func (*ColumnNode) Equals ¶
func (n *ColumnNode) Equals(n2 NodeI) bool
Equals is used internally by the framework to determine if two nodes are equal.
type Expander ¶
type Expander interface { Expand() // contains filtered or unexported methods }
Expander is the interface a node must satisfy to be able to be expanded upon, making a many-* relationship create multiple versions of the original object.
type GoColumnType ¶
type GoColumnType int
GoColumnType represents the GO type that corresponds to a database column
const ( ColTypeUnknown GoColumnType = iota ColTypeBytes ColTypeString ColTypeInteger ColTypeUnsigned ColTypeInteger64 ColTypeUnsigned64 ColTypeDateTime ColTypeFloat ColTypeDouble ColTypeBool )
func ColumnNodeGoType ¶
func ColumnNodeGoType(n *ColumnNode) GoColumnType
GoColumnType is used internally by the framework to return the go type corresponding to the given column.
func (GoColumnType) DefaultValue ¶
func (g GoColumnType) DefaultValue() string
DefaultValue returns a string that represents the GO default value for the corresponding type
func (GoColumnType) GoType ¶ added in v0.0.3
func (g GoColumnType) GoType() string
GoType returns the actual GO type as go code
func (GoColumnType) String ¶
func (g GoColumnType) String() string
String returns the constant type name as a string
type ManyManyNode ¶
type ManyManyNode struct { Node // contains filtered or unexported fields }
A ManyManyNode is an association node that links one table to another table with a many-to-many relationship. Some of the columns have overloaded meanings depending on SQL or NoSQL mode.
func NewManyManyNode ¶
func NewManyManyNode( dbKey string, dbTable string, dbColumn string, goName string, refTableName string, refColumn string, isType bool, ) *ManyManyNode
NewManyManyNode is used internally by the framework to return a new ManyMany node.
func (*ManyManyNode) Equals ¶
func (n *ManyManyNode) Equals(n2 NodeI) bool
Equals is used internally by the framework to test if the node is the same as another node.
func (*ManyManyNode) Expand ¶
func (n *ManyManyNode) Expand()
Expand tells this node to create multiple original objects with a single link for each joined item, rather than to create one original with an array of joined items
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is the base mixin for all node structures. A node is a representation of an object or a relationship between objects in a database that we use to create a query. It lets us abstract the structure of a database to be able to query any kind of database. Obviously, this doesn't work for all possible database structures, but it generally works well enough to solve most, if not all, of the situations that you will come across.
type NodeI ¶
type NodeI interface { // Equals returns true if the given node is equal to this node Equals(NodeI) bool // SetAlias sets a unique name for the node as used in a database query SetAlias(string) // GetAlias returns the alias that was used in a database query GetAlias() string // contains filtered or unexported methods }
NodeI is the interface that all nodes must satisfy
func ChildNodes ¶
func ChildNodes(n nodeLinkI) []NodeI
ChildNodes is used internally by the framework to get the child nodes of a ndoe
func ContainedNodes ¶
ContainedNodes is used internally by the framework to return the contained nodes.
func NewValueNode ¶
func NewValueNode(i interface{}) NodeI
NewValueNode returns a new ValueNode that wraps the given value.
func NodeCondition ¶
NodeCondition is used internally by the framework to get a condition node.
func OperationNodeOperands ¶
func OperationNodeOperands(n *OperationNode) []NodeI
OperationNodeOperands is used internally by the framework to get the operands.
func ParentNode ¶
ParentNode is used internally by the framework to return a node's parent.
func RelatedColumnNode ¶
RelatedColumnNode is used internally by the framework to create a new node for the other side of the relationship.
type NodeSorter ¶
type NodeSorter interface { Ascending() NodeI Descending() NodeI // contains filtered or unexported methods }
NodeSorter is the interface a node must satisfy to be able to be used in an OrderBy statement.
type NodeType ¶
type NodeType int
NodeType indicates the type of node, which saves us from having to use reflection to determine this
func GetNodeType ¶
GetNodeType is used internally by the framework to get the type of node without using reflection.
type OperationNode ¶
type OperationNode struct { Node // contains filtered or unexported fields }
An OperationNode is a general purpose structure that specifies an operation on a node or group of nodes. The operation could be arithmetic, boolean, or a function.
func NewCountNode ¶
func NewCountNode(operands ...NodeI) *OperationNode
NewCountNode creates a Count function node. If no operands are given, it will use * as the parameter to the function which means it will count nulls. To NOT count nulls, at least one table name needs to be specified.
func NewFunctionNode ¶
func NewFunctionNode(functionName string, operands ...interface{}) *OperationNode
NewFunctionNode returns an operation node that executes a database function.
func NewOperationNode ¶
func NewOperationNode(op Operator, operands ...interface{}) *OperationNode
NewOperationNode returns a new operation.
func (*OperationNode) Distinct ¶
func (n *OperationNode) Distinct() *OperationNode
Distinct sets the operation to return distinct results
func (*OperationNode) Equals ¶
func (n *OperationNode) Equals(n2 NodeI) bool
Equals is used internally by the framework to tell if two nodes are equal
type Operator ¶
type Operator string
Operator is used internally by the framework to specify an operation to be performed by the database. Not all databases can perform all the operations. It will be up to the database driver to sort this out.
func OperationNodeOperator ¶
func OperationNodeOperator(n *OperationNode) Operator
OperationNodeOperator is used internally by the framework to get the operator.
type QueryBuilderI ¶
type QueryBuilderI interface { Join(n NodeI, condition NodeI) QueryBuilderI Expand(n NodeI) QueryBuilderI Condition(c NodeI) QueryBuilderI Having(c NodeI) QueryBuilderI OrderBy(nodes ...NodeI) QueryBuilderI GroupBy(nodes ...NodeI) QueryBuilderI Limit(maxRowCount int, offset int) QueryBuilderI Select(nodes ...NodeI) QueryBuilderI Distinct() QueryBuilderI Alias(name string, n NodeI) QueryBuilderI Load(ctx context.Context) []map[string]interface{} Delete(ctx context.Context) Count(ctx context.Context, distinct bool, nodes ...NodeI) uint Subquery() *SubqueryNode }
The query builder is the primary aid in creating cross-platform, portable queries to the database(s) The code-generated ORM classes call these functions to build a query. The query will eventually get sent to the database for processing, and then unpacked into one of the ORM generated objects. You generally will not call these functions directly, but rather will call the matching functions in each of the codegenerated ORM classes located in your project directory.
If you are creating a database driver, you will implement these functions in the query builder that you provide.
func SubqueryBuilder ¶
func SubqueryBuilder(n *SubqueryNode) QueryBuilderI
SubqueryBuilder is used internally by the framework to return the internal query builder of the subquery
type ReferenceNode ¶
type ReferenceNode struct { Node // contains filtered or unexported fields }
A ReferenceNode is a forward-pointing foreign key relationship, and can define a one-to-one or one-to-many relationship, depending on whether it is unique. If the other side of the relationship is not a type table, then the other table will have a matching ReverseReferenceNode.
func NewReferenceNode ¶
func NewReferenceNode( dbKey string, dbTableName string, dbColumnName string, goColumnName string, goName string, refTableName string, refColumn string, isType bool, ) *ReferenceNode
NewReferenceNode creates a forward reference node.
func (*ReferenceNode) Equals ¶
func (n *ReferenceNode) Equals(n2 NodeI) bool
Equals is used internally by the framework to determine if two nodes are equal.
type ReverseReferenceNode ¶
type ReverseReferenceNode struct { Node // contains filtered or unexported fields }
ReverseReferenceNode creates a reverse reference node representing a one to many relationship or one-to-one relationship, depending on whether the foreign key is unique. The other side of the relationship will have a matching forward ReferenceNode.
For SQL databases, a ReverseReferenceNode will not have anything in its table to indicate the relationship. It is a kind of virtual placeholder to indicate that a foreign key in another table is pointing to this table, and therefore that relationship can be used to build a query.
func NewReverseReferenceNode ¶
func (*ReverseReferenceNode) Equals ¶
func (n *ReverseReferenceNode) Equals(n2 NodeI) bool
Equals is used internally by the framework to determine if two nodes are equal.
func (*ReverseReferenceNode) Expand ¶
func (n *ReverseReferenceNode) Expand()
Expand tells the node to expand its results into multiple records, one for each item found in this relationship, rather than have this relationship create an array of items within an individual record.
type SubqueryNode ¶
type SubqueryNode struct { Node // contains filtered or unexported fields }
A SubqueryNode represents a "select" subquery. Subqueries are not always portable to other databases, and are not easily checked for syntax errors, since a subquery can return a scalar, vector, or even an entire table. You generally do not create a subquery node directly, but rather you use the codegenerated models to start a query on a table, and then end the query with "Subquery()" which will turn the query into a usable subquery node that you can embed in other queries.
func NewSubqueryNode ¶
func NewSubqueryNode(b QueryBuilderI) *SubqueryNode
NewSubqueryNode creates a new subquery
func (*SubqueryNode) Equals ¶
func (n *SubqueryNode) Equals(n2 NodeI) bool
Equals is used internally by the framework to determine if two nodes are equal
type TableNode ¶
type TableNode struct { Node // contains filtered or unexported fields }
A TableNode is a representation of the top level of a chain of nodes that point to a particular field in a query, even after aliases and joins are taken into account. TableNodes are create by the code generation process, one for each table in the database.
func NewTableNode ¶
NewTableNode creates a table node, which is always the starting node in a node chain
type TableNodeI ¶
type TableNodeI interface { NodeI SelectNodes_() []*ColumnNode PrimaryKeyNode_() *ColumnNode EmbeddedNode_() NodeI }
TableNodeI is the interface that all table nodes must implement. TableNodes are create by the code generation process, one for each table in the database.