Add wiki Expressions page.

This commit is contained in:
go-jet 2019-06-27 19:55:21 +02:00
parent 3516d2861c
commit 6a6ee0561d
17 changed files with 399 additions and 109 deletions

View file

@ -3,20 +3,32 @@ package jet
type BoolExpression interface { type BoolExpression interface {
Expression Expression
EQ(expression BoolExpression) BoolExpression // Check if this expression is equal to rhs
NOT_EQ(expression BoolExpression) BoolExpression EQ(rhs BoolExpression) BoolExpression
// Check if this expression is not equal to rhs
NOT_EQ(rhs BoolExpression) BoolExpression
// Check if this expression is distinct to rhs
IS_DISTINCT_FROM(rhs BoolExpression) BoolExpression IS_DISTINCT_FROM(rhs BoolExpression) BoolExpression
// Check if this expression is not distinct to rhs
IS_NOT_DISTINCT_FROM(rhs BoolExpression) BoolExpression IS_NOT_DISTINCT_FROM(rhs BoolExpression) BoolExpression
// Check if this expression is true
IS_TRUE() BoolExpression IS_TRUE() BoolExpression
// Check if this expression is not true
IS_NOT_TRUE() BoolExpression IS_NOT_TRUE() BoolExpression
// Check if this expression is false
IS_FALSE() BoolExpression IS_FALSE() BoolExpression
// Check if this expression is not false
IS_NOT_FALSE() BoolExpression IS_NOT_FALSE() BoolExpression
// Check if this expression is unknown
IS_UNKNOWN() BoolExpression IS_UNKNOWN() BoolExpression
// Check if this expression is not unknown
IS_NOT_UNKNOWN() BoolExpression IS_NOT_UNKNOWN() BoolExpression
AND(expression BoolExpression) BoolExpression // expression AND operator rhs
OR(expression BoolExpression) BoolExpression AND(rhs BoolExpression) BoolExpression
// expression OR operator rhs
OR(rhs BoolExpression) BoolExpression
} }
type boolInterfaceImpl struct { type boolInterfaceImpl struct {
@ -24,19 +36,19 @@ type boolInterfaceImpl struct {
} }
func (b *boolInterfaceImpl) EQ(expression BoolExpression) BoolExpression { func (b *boolInterfaceImpl) EQ(expression BoolExpression) BoolExpression {
return EQ(b.parent, expression) return eq(b.parent, expression)
} }
func (b *boolInterfaceImpl) NOT_EQ(expression BoolExpression) BoolExpression { func (b *boolInterfaceImpl) NOT_EQ(expression BoolExpression) BoolExpression {
return NOT_EQ(b.parent, expression) return notEq(b.parent, expression)
} }
func (b *boolInterfaceImpl) IS_DISTINCT_FROM(rhs BoolExpression) BoolExpression { func (b *boolInterfaceImpl) IS_DISTINCT_FROM(rhs BoolExpression) BoolExpression {
return IS_DISTINCT_FROM(b.parent, rhs) return isDistinctFrom(b.parent, rhs)
} }
func (b *boolInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs BoolExpression) BoolExpression { func (b *boolInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs BoolExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(b.parent, rhs) return isNotDistinctFrom(b.parent, rhs)
} }
func (b *boolInterfaceImpl) AND(expression BoolExpression) BoolExpression { func (b *boolInterfaceImpl) AND(expression BoolExpression) BoolExpression {

View file

@ -12,7 +12,7 @@ import (
type serializeOption int type serializeOption int
const ( const (
NO_WRAP serializeOption = iota noWrap serializeOption = iota
) )
type clause interface { type clause interface {
@ -85,7 +85,7 @@ func (q *queryData) writeWhere(statement statementType, where Expression) error
q.writeString("WHERE") q.writeString("WHERE")
q.increaseIdent() q.increaseIdent()
err := where.serialize(statement, q, NO_WRAP) err := where.serialize(statement, q, noWrap)
q.decreaseIdent() q.decreaseIdent()
return err return err
@ -118,7 +118,7 @@ func (q *queryData) writeHaving(statement statementType, having Expression) erro
q.writeString("HAVING") q.writeString("HAVING")
q.increaseIdent() q.increaseIdent()
err := having.serialize(statement, q, NO_WRAP) err := having.serialize(statement, q, noWrap)
q.decreaseIdent() q.decreaseIdent()
return err return err

View file

@ -19,46 +19,46 @@ type dateInterfaceImpl struct {
} }
func (t *dateInterfaceImpl) EQ(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) EQ(rhs DateExpression) BoolExpression {
return EQ(t.parent, rhs) return eq(t.parent, rhs)
} }
func (t *dateInterfaceImpl) NOT_EQ(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) NOT_EQ(rhs DateExpression) BoolExpression {
return NOT_EQ(t.parent, rhs) return notEq(t.parent, rhs)
} }
func (t *dateInterfaceImpl) IS_DISTINCT_FROM(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) IS_DISTINCT_FROM(rhs DateExpression) BoolExpression {
return IS_DISTINCT_FROM(t.parent, rhs) return isDistinctFrom(t.parent, rhs)
} }
func (t *dateInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs DateExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(t.parent, rhs) return isNotDistinctFrom(t.parent, rhs)
} }
func (t *dateInterfaceImpl) LT(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) LT(rhs DateExpression) BoolExpression {
return LT(t.parent, rhs) return lt(t.parent, rhs)
} }
func (t *dateInterfaceImpl) LT_EQ(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) LT_EQ(rhs DateExpression) BoolExpression {
return LT_EQ(t.parent, rhs) return ltEq(t.parent, rhs)
} }
func (t *dateInterfaceImpl) GT(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) GT(rhs DateExpression) BoolExpression {
return GT(t.parent, rhs) return gt(t.parent, rhs)
} }
func (t *dateInterfaceImpl) GT_EQ(rhs DateExpression) BoolExpression { func (t *dateInterfaceImpl) GT_EQ(rhs DateExpression) BoolExpression {
return GT_EQ(t.parent, rhs) return gtEq(t.parent, rhs)
} }
//---------------------------------------------------// //---------------------------------------------------//
type DateExpressionWrapper struct { type dateExpressionWrapper struct {
dateInterfaceImpl dateInterfaceImpl
Expression Expression
} }
func newDateExpressionWrap(expression Expression) DateExpression { func newDateExpressionWrap(expression Expression) DateExpression {
dateExpressionWrap := DateExpressionWrapper{Expression: expression} dateExpressionWrap := dateExpressionWrapper{Expression: expression}
dateExpressionWrap.dateInterfaceImpl.parent = &dateExpressionWrap dateExpressionWrap.dateInterfaceImpl.parent = &dateExpressionWrap
return &dateExpressionWrap return &dateExpressionWrap
} }

View file

@ -5,38 +5,58 @@ import (
"fmt" "fmt"
) )
// An Expression // Common expression interface
type Expression interface { type Expression interface {
clause clause
projection projection
groupByClause groupByClause
OrderByClause OrderByClause
// Test expression whether it is a NULL value.
IS_NULL() BoolExpression IS_NULL() BoolExpression
// Test expression whether it is a non-NULL value.
IS_NOT_NULL() BoolExpression IS_NOT_NULL() BoolExpression
// Check if this expressions matches any in expressions list
IN(expressions ...Expression) BoolExpression IN(expressions ...Expression) BoolExpression
// Check if this expressions is different of all expressions in expressions list
NOT_IN(expressions ...Expression) BoolExpression NOT_IN(expressions ...Expression) BoolExpression
// The temporary alias name to assign to the expression
AS(alias string) projection AS(alias string) projection
// Expression will be used to sort query result in ascending order
ASC() OrderByClause ASC() OrderByClause
// Expression will be used to sort query result in ascending order
DESC() OrderByClause DESC() OrderByClause
// casts // Cast expression to dbType
TO(dbType string) Expression TO(dbType string) Expression
// Cast expression to bool type
TO_BOOL() BoolExpression TO_BOOL() BoolExpression
// Cast expression to smallint type
TO_SMALLINT() IntegerExpression TO_SMALLINT() IntegerExpression
// Cast expression to integer type
TO_INTEGER() IntegerExpression TO_INTEGER() IntegerExpression
// Cast expression to bigint type
TO_BIGINT() IntegerExpression TO_BIGINT() IntegerExpression
// Cast expression to numeric type, using precision and optionally scale
TO_NUMERIC(precision int, scale ...int) FloatExpression TO_NUMERIC(precision int, scale ...int) FloatExpression
// Cast expression to real type
TO_REAL() FloatExpression TO_REAL() FloatExpression
// Cast expression to double precision type
TO_DOUBLE() FloatExpression TO_DOUBLE() FloatExpression
// Cast expression to text type
TO_TEXT() StringExpression TO_TEXT() StringExpression
// Cast expression to date type
TO_DATE() DateExpression TO_DATE() DateExpression
// Cast expression to time type
TO_TIME() TimeExpression TO_TIME() TimeExpression
// Cast expression to time with time timezone type
TO_TIMEZ() TimezExpression TO_TIMEZ() TimezExpression
// Cast expression to timestamp type
TO_TIMESTAMP() TimestampExpression TO_TIMESTAMP() TimestampExpression
// Cast expression to timestamp with timezone type
TO_TIMESTAMPZ() TimestampzExpression TO_TIMESTAMPZ() TimestampzExpression
} }
@ -139,15 +159,15 @@ func (e *expressionInterfaceImpl) TO_TIMESTAMPZ() TimestampzExpression {
} }
func (e *expressionInterfaceImpl) serializeForGroupBy(statement statementType, out *queryData) error { func (e *expressionInterfaceImpl) serializeForGroupBy(statement statementType, out *queryData) error {
return e.parent.serialize(statement, out, NO_WRAP) return e.parent.serialize(statement, out, noWrap)
} }
func (e *expressionInterfaceImpl) serializeForProjection(statement statementType, out *queryData) error { func (e *expressionInterfaceImpl) serializeForProjection(statement statementType, out *queryData) error {
return e.parent.serialize(statement, out, NO_WRAP) return e.parent.serialize(statement, out, noWrap)
} }
func (e *expressionInterfaceImpl) serializeForOrderBy(statement statementType, out *queryData) error { func (e *expressionInterfaceImpl) serializeForOrderBy(statement statementType, out *queryData) error {
return e.parent.serialize(statement, out, NO_WRAP) return e.parent.serialize(statement, out, noWrap)
} }
// Representation of binary operations (e.g. comparisons, arithmetic) // Representation of binary operations (e.g. comparisons, arithmetic)
@ -177,7 +197,7 @@ func (c *binaryOpExpression) serialize(statement statementType, out *queryData,
return errors.New("nil rhs") return errors.New("nil rhs")
} }
wrap := !contains(options, NO_WRAP) wrap := !contains(options, noWrap)
if wrap { if wrap {
out.writeString("(") out.writeString("(")

View file

@ -1,4 +1,3 @@
// Query building functions for expressions components
package jet package jet
import ( import (

View file

@ -28,35 +28,35 @@ type floatInterfaceImpl struct {
} }
func (n *floatInterfaceImpl) EQ(rhs FloatExpression) BoolExpression { func (n *floatInterfaceImpl) EQ(rhs FloatExpression) BoolExpression {
return EQ(n.parent, rhs) return eq(n.parent, rhs)
} }
func (n *floatInterfaceImpl) NOT_EQ(rhs FloatExpression) BoolExpression { func (n *floatInterfaceImpl) NOT_EQ(rhs FloatExpression) BoolExpression {
return NOT_EQ(n.parent, rhs) return notEq(n.parent, rhs)
} }
func (n *floatInterfaceImpl) IS_DISTINCT_FROM(rhs FloatExpression) BoolExpression { func (n *floatInterfaceImpl) IS_DISTINCT_FROM(rhs FloatExpression) BoolExpression {
return IS_DISTINCT_FROM(n.parent, rhs) return isDistinctFrom(n.parent, rhs)
} }
func (n *floatInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs FloatExpression) BoolExpression { func (n *floatInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs FloatExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(n.parent, rhs) return isNotDistinctFrom(n.parent, rhs)
} }
func (n *floatInterfaceImpl) GT(rhs FloatExpression) BoolExpression { func (n *floatInterfaceImpl) GT(rhs FloatExpression) BoolExpression {
return GT(n.parent, rhs) return gt(n.parent, rhs)
} }
func (n *floatInterfaceImpl) GT_EQ(rhs FloatExpression) BoolExpression { func (n *floatInterfaceImpl) GT_EQ(rhs FloatExpression) BoolExpression {
return GT_EQ(n.parent, rhs) return gtEq(n.parent, rhs)
} }
func (n *floatInterfaceImpl) LT(expression FloatExpression) BoolExpression { func (n *floatInterfaceImpl) LT(expression FloatExpression) BoolExpression {
return LT(n.parent, expression) return lt(n.parent, expression)
} }
func (n *floatInterfaceImpl) LT_EQ(expression FloatExpression) BoolExpression { func (n *floatInterfaceImpl) LT_EQ(expression FloatExpression) BoolExpression {
return LT_EQ(n.parent, expression) return ltEq(n.parent, expression)
} }
func (n *floatInterfaceImpl) ADD(expression FloatExpression) FloatExpression { func (n *floatInterfaceImpl) ADD(expression FloatExpression) FloatExpression {

View file

@ -4,29 +4,49 @@ type IntegerExpression interface {
Expression Expression
numericExpression numericExpression
// Check if expression is equal to rhs
EQ(rhs IntegerExpression) BoolExpression EQ(rhs IntegerExpression) BoolExpression
// Check if expression is not equal to rhs
NOT_EQ(rhs IntegerExpression) BoolExpression NOT_EQ(rhs IntegerExpression) BoolExpression
// Check if expression is distinct from rhs
IS_DISTINCT_FROM(rhs IntegerExpression) BoolExpression IS_DISTINCT_FROM(rhs IntegerExpression) BoolExpression
// Check if expression is not distinct from rhs
IS_NOT_DISTINCT_FROM(rhs IntegerExpression) BoolExpression IS_NOT_DISTINCT_FROM(rhs IntegerExpression) BoolExpression
// Check if expression is less then rhs
LT(rhs IntegerExpression) BoolExpression LT(rhs IntegerExpression) BoolExpression
// Check if expression is less then equal rhs
LT_EQ(rhs IntegerExpression) BoolExpression LT_EQ(rhs IntegerExpression) BoolExpression
// Check if expression is greater then rhs
GT(rhs IntegerExpression) BoolExpression GT(rhs IntegerExpression) BoolExpression
// Check if expression is greater then equal rhs
GT_EQ(rhs IntegerExpression) BoolExpression GT_EQ(rhs IntegerExpression) BoolExpression
// expression + rhs
ADD(rhs IntegerExpression) IntegerExpression ADD(rhs IntegerExpression) IntegerExpression
// expression - rhs
SUB(rhs IntegerExpression) IntegerExpression SUB(rhs IntegerExpression) IntegerExpression
// expression * rhs
MUL(rhs IntegerExpression) IntegerExpression MUL(rhs IntegerExpression) IntegerExpression
// expression / rhs
DIV(rhs IntegerExpression) IntegerExpression DIV(rhs IntegerExpression) IntegerExpression
// expression % rhs
MOD(rhs IntegerExpression) IntegerExpression MOD(rhs IntegerExpression) IntegerExpression
// expression ^ rhs
POW(rhs IntegerExpression) IntegerExpression POW(rhs IntegerExpression) IntegerExpression
BIT_AND(expression IntegerExpression) IntegerExpression // expression & rhs
BIT_OR(expression IntegerExpression) IntegerExpression BIT_AND(rhs IntegerExpression) IntegerExpression
BIT_XOR(expression IntegerExpression) IntegerExpression // expression | rhs
BIT_OR(rhs IntegerExpression) IntegerExpression
// expression # rhs
BIT_XOR(rhs IntegerExpression) IntegerExpression
// ~expression
BIT_NOT() IntegerExpression BIT_NOT() IntegerExpression
BIT_SHIFT_LEFT(intExpression IntegerExpression) IntegerExpression // expression << rhs
BIT_SHIFT_RIGHT(intExpression IntegerExpression) IntegerExpression BIT_SHIFT_LEFT(shift IntegerExpression) IntegerExpression
// expression >> rhs
BIT_SHIFT_RIGHT(shift IntegerExpression) IntegerExpression
} }
type integerInterfaceImpl struct { type integerInterfaceImpl struct {
@ -35,35 +55,35 @@ type integerInterfaceImpl struct {
} }
func (i *integerInterfaceImpl) EQ(rhs IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) EQ(rhs IntegerExpression) BoolExpression {
return EQ(i.parent, rhs) return eq(i.parent, rhs)
} }
func (i *integerInterfaceImpl) NOT_EQ(rhs IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) NOT_EQ(rhs IntegerExpression) BoolExpression {
return NOT_EQ(i.parent, rhs) return notEq(i.parent, rhs)
} }
func (i *integerInterfaceImpl) IS_DISTINCT_FROM(rhs IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) IS_DISTINCT_FROM(rhs IntegerExpression) BoolExpression {
return IS_DISTINCT_FROM(i.parent, rhs) return isDistinctFrom(i.parent, rhs)
} }
func (i *integerInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs IntegerExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(i.parent, rhs) return isNotDistinctFrom(i.parent, rhs)
} }
func (i *integerInterfaceImpl) GT(rhs IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) GT(rhs IntegerExpression) BoolExpression {
return GT(i.parent, rhs) return gt(i.parent, rhs)
} }
func (i *integerInterfaceImpl) GT_EQ(rhs IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) GT_EQ(rhs IntegerExpression) BoolExpression {
return GT_EQ(i.parent, rhs) return gtEq(i.parent, rhs)
} }
func (i *integerInterfaceImpl) LT(expression IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) LT(expression IntegerExpression) BoolExpression {
return LT(i.parent, expression) return lt(i.parent, expression)
} }
func (i *integerInterfaceImpl) LT_EQ(expression IntegerExpression) BoolExpression { func (i *integerInterfaceImpl) LT_EQ(expression IntegerExpression) BoolExpression {
return LT_EQ(i.parent, expression) return ltEq(i.parent, expression)
} }
func (i *integerInterfaceImpl) ADD(expression IntegerExpression) IntegerExpression { func (i *integerInterfaceImpl) ADD(expression IntegerExpression) IntegerExpression {

View file

@ -11,48 +11,48 @@ func NOT(expr BoolExpression) BoolExpression {
//----------- Comparison operators ---------------// //----------- Comparison operators ---------------//
func EXISTS(subQuery SelectStatement) BoolExpression {
return newPrefixBoolExpression(subQuery, "EXISTS")
}
// Returns a representation of "a=b" // Returns a representation of "a=b"
func EQ(lhs, rhs Expression) BoolExpression { func eq(lhs, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, "=") return newBinaryBoolExpression(lhs, rhs, "=")
} }
// Returns a representation of "a!=b" // Returns a representation of "a!=b"
func NOT_EQ(lhs, rhs Expression) BoolExpression { func notEq(lhs, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, "!=") return newBinaryBoolExpression(lhs, rhs, "!=")
} }
func IS_DISTINCT_FROM(lhs, rhs Expression) BoolExpression { func isDistinctFrom(lhs, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, "IS DISTINCT FROM") return newBinaryBoolExpression(lhs, rhs, "IS DISTINCT FROM")
} }
func IS_NOT_DISTINCT_FROM(lhs, rhs Expression) BoolExpression { func isNotDistinctFrom(lhs, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, "IS NOT DISTINCT FROM") return newBinaryBoolExpression(lhs, rhs, "IS NOT DISTINCT FROM")
} }
// Returns a representation of "a<b" // Returns a representation of "a<b"
func LT(lhs Expression, rhs Expression) BoolExpression { func lt(lhs Expression, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, "<") return newBinaryBoolExpression(lhs, rhs, "<")
} }
// Returns a representation of "a<=b" // Returns a representation of "a<=b"
func LT_EQ(lhs, rhs Expression) BoolExpression { func ltEq(lhs, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, "<=") return newBinaryBoolExpression(lhs, rhs, "<=")
} }
// Returns a representation of "a>b" // Returns a representation of "a>b"
func GT(lhs, rhs Expression) BoolExpression { func gt(lhs, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, ">") return newBinaryBoolExpression(lhs, rhs, ">")
} }
// Returns a representation of "a>=b" // Returns a representation of "a>=b"
func GT_EQ(lhs, rhs Expression) BoolExpression { func gtEq(lhs, rhs Expression) BoolExpression {
return newBinaryBoolExpression(lhs, rhs, ">=") return newBinaryBoolExpression(lhs, rhs, ">=")
} }
func EXISTS(subQuery SelectStatement) BoolExpression {
return newPrefixBoolExpression(subQuery, "EXISTS")
}
// --------------- CASE operator -------------------// // --------------- CASE operator -------------------//
type CaseOperatorExpression interface { type CaseOperatorExpression interface {
@ -125,14 +125,14 @@ func (c *caseOperatorImpl) serialize(statement statementType, out *queryData, op
for i, when := range c.when { for i, when := range c.when {
out.writeString("WHEN") out.writeString("WHEN")
err := when.serialize(statement, out, NO_WRAP) err := when.serialize(statement, out, noWrap)
if err != nil { if err != nil {
return err return err
} }
out.writeString("THEN") out.writeString("THEN")
err = c.then[i].serialize(statement, out, NO_WRAP) err = c.then[i].serialize(statement, out, noWrap)
if err != nil { if err != nil {
return err return err
@ -141,7 +141,7 @@ func (c *caseOperatorImpl) serialize(statement statementType, out *queryData, op
if c.els != nil { if c.els != nil {
out.writeString("ELSE") out.writeString("ELSE")
err := c.els.serialize(statement, out, NO_WRAP) err := c.els.serialize(statement, out, noWrap)
if err != nil { if err != nil {
return err return err

View file

@ -5,7 +5,6 @@ type projection interface {
from(subQuery ExpressionTable) projection from(subQuery ExpressionTable) projection
} }
//------------------------------------------------------//
// Dummy type for projection list // Dummy type for projection list
type ProjectionList []projection type ProjectionList []projection

View file

@ -26,35 +26,35 @@ type stringInterfaceImpl struct {
} }
func (s *stringInterfaceImpl) EQ(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) EQ(rhs StringExpression) BoolExpression {
return EQ(s.parent, rhs) return eq(s.parent, rhs)
} }
func (s *stringInterfaceImpl) NOT_EQ(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) NOT_EQ(rhs StringExpression) BoolExpression {
return NOT_EQ(s.parent, rhs) return notEq(s.parent, rhs)
} }
func (s *stringInterfaceImpl) IS_DISTINCT_FROM(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) IS_DISTINCT_FROM(rhs StringExpression) BoolExpression {
return IS_DISTINCT_FROM(s.parent, rhs) return isDistinctFrom(s.parent, rhs)
} }
func (s *stringInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs StringExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(s.parent, rhs) return isNotDistinctFrom(s.parent, rhs)
} }
func (s *stringInterfaceImpl) GT(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) GT(rhs StringExpression) BoolExpression {
return GT(s.parent, rhs) return gt(s.parent, rhs)
} }
func (s *stringInterfaceImpl) GT_EQ(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) GT_EQ(rhs StringExpression) BoolExpression {
return GT_EQ(s.parent, rhs) return gtEq(s.parent, rhs)
} }
func (s *stringInterfaceImpl) LT(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) LT(rhs StringExpression) BoolExpression {
return LT(s.parent, rhs) return lt(s.parent, rhs)
} }
func (s *stringInterfaceImpl) LT_EQ(rhs StringExpression) BoolExpression { func (s *stringInterfaceImpl) LT_EQ(rhs StringExpression) BoolExpression {
return LT_EQ(s.parent, rhs) return ltEq(s.parent, rhs)
} }
func (s *stringInterfaceImpl) CONCAT(rhs Expression) StringExpression { func (s *stringInterfaceImpl) CONCAT(rhs Expression) StringExpression {

View file

@ -19,35 +19,35 @@ type timeInterfaceImpl struct {
} }
func (t *timeInterfaceImpl) EQ(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) EQ(rhs TimeExpression) BoolExpression {
return EQ(t.parent, rhs) return eq(t.parent, rhs)
} }
func (t *timeInterfaceImpl) NOT_EQ(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) NOT_EQ(rhs TimeExpression) BoolExpression {
return NOT_EQ(t.parent, rhs) return notEq(t.parent, rhs)
} }
func (t *timeInterfaceImpl) IS_DISTINCT_FROM(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) IS_DISTINCT_FROM(rhs TimeExpression) BoolExpression {
return IS_DISTINCT_FROM(t.parent, rhs) return isDistinctFrom(t.parent, rhs)
} }
func (t *timeInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimeExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(t.parent, rhs) return isNotDistinctFrom(t.parent, rhs)
} }
func (t *timeInterfaceImpl) LT(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) LT(rhs TimeExpression) BoolExpression {
return LT(t.parent, rhs) return lt(t.parent, rhs)
} }
func (t *timeInterfaceImpl) LT_EQ(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) LT_EQ(rhs TimeExpression) BoolExpression {
return LT_EQ(t.parent, rhs) return ltEq(t.parent, rhs)
} }
func (t *timeInterfaceImpl) GT(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) GT(rhs TimeExpression) BoolExpression {
return GT(t.parent, rhs) return gt(t.parent, rhs)
} }
func (t *timeInterfaceImpl) GT_EQ(rhs TimeExpression) BoolExpression { func (t *timeInterfaceImpl) GT_EQ(rhs TimeExpression) BoolExpression {
return GT_EQ(t.parent, rhs) return gtEq(t.parent, rhs)
} }
//---------------------------------------------------// //---------------------------------------------------//

View file

@ -19,35 +19,35 @@ type timestampInterfaceImpl struct {
} }
func (t *timestampInterfaceImpl) EQ(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) EQ(rhs TimestampExpression) BoolExpression {
return EQ(t.parent, rhs) return eq(t.parent, rhs)
} }
func (t *timestampInterfaceImpl) NOT_EQ(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) NOT_EQ(rhs TimestampExpression) BoolExpression {
return NOT_EQ(t.parent, rhs) return notEq(t.parent, rhs)
} }
func (t *timestampInterfaceImpl) IS_DISTINCT_FROM(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) IS_DISTINCT_FROM(rhs TimestampExpression) BoolExpression {
return IS_DISTINCT_FROM(t.parent, rhs) return isDistinctFrom(t.parent, rhs)
} }
func (t *timestampInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimestampExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(t.parent, rhs) return isNotDistinctFrom(t.parent, rhs)
} }
func (t *timestampInterfaceImpl) LT(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) LT(rhs TimestampExpression) BoolExpression {
return LT(t.parent, rhs) return lt(t.parent, rhs)
} }
func (t *timestampInterfaceImpl) LT_EQ(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) LT_EQ(rhs TimestampExpression) BoolExpression {
return LT_EQ(t.parent, rhs) return ltEq(t.parent, rhs)
} }
func (t *timestampInterfaceImpl) GT(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) GT(rhs TimestampExpression) BoolExpression {
return GT(t.parent, rhs) return gt(t.parent, rhs)
} }
func (t *timestampInterfaceImpl) GT_EQ(rhs TimestampExpression) BoolExpression { func (t *timestampInterfaceImpl) GT_EQ(rhs TimestampExpression) BoolExpression {
return GT_EQ(t.parent, rhs) return gtEq(t.parent, rhs)
} }
//------------------------------------------------- //-------------------------------------------------

View file

@ -19,35 +19,35 @@ type timestampzInterfaceImpl struct {
} }
func (t *timestampzInterfaceImpl) EQ(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) EQ(rhs TimestampzExpression) BoolExpression {
return EQ(t.parent, rhs) return eq(t.parent, rhs)
} }
func (t *timestampzInterfaceImpl) NOT_EQ(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) NOT_EQ(rhs TimestampzExpression) BoolExpression {
return NOT_EQ(t.parent, rhs) return notEq(t.parent, rhs)
} }
func (t *timestampzInterfaceImpl) IS_DISTINCT_FROM(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) IS_DISTINCT_FROM(rhs TimestampzExpression) BoolExpression {
return IS_DISTINCT_FROM(t.parent, rhs) return isDistinctFrom(t.parent, rhs)
} }
func (t *timestampzInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimestampzExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(t.parent, rhs) return isNotDistinctFrom(t.parent, rhs)
} }
func (t *timestampzInterfaceImpl) LT(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) LT(rhs TimestampzExpression) BoolExpression {
return LT(t.parent, rhs) return lt(t.parent, rhs)
} }
func (t *timestampzInterfaceImpl) LT_EQ(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) LT_EQ(rhs TimestampzExpression) BoolExpression {
return LT_EQ(t.parent, rhs) return ltEq(t.parent, rhs)
} }
func (t *timestampzInterfaceImpl) GT(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) GT(rhs TimestampzExpression) BoolExpression {
return GT(t.parent, rhs) return gt(t.parent, rhs)
} }
func (t *timestampzInterfaceImpl) GT_EQ(rhs TimestampzExpression) BoolExpression { func (t *timestampzInterfaceImpl) GT_EQ(rhs TimestampzExpression) BoolExpression {
return GT_EQ(t.parent, rhs) return gtEq(t.parent, rhs)
} }
//------------------------------------------------- //-------------------------------------------------

View file

@ -19,35 +19,35 @@ type timezInterfaceImpl struct {
} }
func (t *timezInterfaceImpl) EQ(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) EQ(rhs TimezExpression) BoolExpression {
return EQ(t.parent, rhs) return eq(t.parent, rhs)
} }
func (t *timezInterfaceImpl) NOT_EQ(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) NOT_EQ(rhs TimezExpression) BoolExpression {
return NOT_EQ(t.parent, rhs) return notEq(t.parent, rhs)
} }
func (t *timezInterfaceImpl) IS_DISTINCT_FROM(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) IS_DISTINCT_FROM(rhs TimezExpression) BoolExpression {
return IS_DISTINCT_FROM(t.parent, rhs) return isDistinctFrom(t.parent, rhs)
} }
func (t *timezInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) IS_NOT_DISTINCT_FROM(rhs TimezExpression) BoolExpression {
return IS_NOT_DISTINCT_FROM(t.parent, rhs) return isNotDistinctFrom(t.parent, rhs)
} }
func (t *timezInterfaceImpl) LT(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) LT(rhs TimezExpression) BoolExpression {
return LT(t.parent, rhs) return lt(t.parent, rhs)
} }
func (t *timezInterfaceImpl) LT_EQ(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) LT_EQ(rhs TimezExpression) BoolExpression {
return LT_EQ(t.parent, rhs) return ltEq(t.parent, rhs)
} }
func (t *timezInterfaceImpl) GT(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) GT(rhs TimezExpression) BoolExpression {
return GT(t.parent, rhs) return gt(t.parent, rhs)
} }
func (t *timezInterfaceImpl) GT_EQ(rhs TimezExpression) BoolExpression { func (t *timezInterfaceImpl) GT_EQ(rhs TimezExpression) BoolExpression {
return GT_EQ(t.parent, rhs) return gtEq(t.parent, rhs)
} }
//---------------------------------------------------// //---------------------------------------------------//

229
wiki/Expressions.md Normal file
View file

@ -0,0 +1,229 @@
Jet sql builder supports following expression types:
- Bool expressions
- Integer expressions
- Float expressions
- String expressions
- Date expressions
- Time expressions
- Timez expressions (with time zone)
- Timestamp expressions
- Timestampz expressions (with time zone)
_This list might be extended with feature Jet releases._
### Literal type
For every expression type there is a method to create one literal type expressions.
Literal type examples:
```
Bool(true)
Integer(11)
Float(23.44)
String("John Doe")
Date(2010, 12, 3)
Time(23, 6, 6, 1)
Timez(23, 6, 6, 222, +200)
Timestamp(2010, 10, 21, 15, 30, 12, 333)
Timestampz(2010, 10, 21, 15, 30, 12, 444, 0)
NULL
STAR (alias for *)
```
### Column types
Every sql builder table column belongs to one expression type. There are following column types:
```
ColumnBool
ColumnInteger
ColumnFloat
ColumnString
ColumnDate
ColumnTime
ColumnTimez
ColumnTimestamp
ColumnTimestampz
```
Columns and literals can form arbitrary expressions but have to follow valid SQL expression syntax.
For instance valid expressions are:
```
Bool(true).AND(Bool(false)).IS_FALSE()
(Film.Length.GT(Int(100)).AND(Film.Length.LT(Int(200))).IS_TRUE() // table 'film', integer column 'length'
```
Some of the invalid expressions. These expressions will cause go build to break.
```
Bool(true).AND(Int(11)) // can't compare bool and
Int(11).ADD(Float(22.2)) // can't add integer and floats, but
// using cast it is possible: Int(11).TO_FLOAT().ADD(Float(22.2))
```
## Common expression operators
```go
type Expression interface {
clause
projection
groupByClause
OrderByClause
// Test expression whether it is a NULL value.
IS_NULL() BoolExpression
// Test expression whether it is a non-NULL value.
IS_NOT_NULL() BoolExpression
// Check if this expressions matches any in expressions list
IN(expressions ...Expression) BoolExpression
// Check if this expressions is different of all expressions in expressions list
NOT_IN(expressions ...Expression) BoolExpression
// The temporary alias name to assign to the expression
AS(alias string) projection
// Expression will be used to sort query result in ascending order
ASC() OrderByClause
// Expression will be used to sort query result in ascending order
DESC() OrderByClause
// Cast expression to dbType
TO(dbType string) Expression
// Cast expression to bool type
TO_BOOL() BoolExpression
// Cast expression to smallint type
TO_SMALLINT() IntegerExpression
// Cast expression to integer type
TO_INTEGER() IntegerExpression
// Cast expression to bigint type
TO_BIGINT() IntegerExpression
// Cast expression to numeric type, using precision and optionally scale
TO_NUMERIC(precision int, scale ...int) FloatExpression
// Cast expression to real type
TO_REAL() FloatExpression
// Cast expression to double precision type
TO_DOUBLE() FloatExpression
// Cast expression to text type
TO_TEXT() StringExpression
// Cast expression to date type
TO_DATE() DateExpression
// Cast expression to time type
TO_TIME() TimeExpression
// Cast expression to time with time timezone type
TO_TIMEZ() TimezExpression
// Cast expression to timestamp type
TO_TIMESTAMP() TimestampExpression
// Cast expression to timestamp with timezone type
TO_TIMESTAMPZ() TimestampzExpression
}
```
Examples:
```go
Film.Description.IS_NULL()
Int(1).ADD(Int(2)).AS("1+2")
(Film.Duration.ADD(Int(3))).ASC()
String("1999-01-08 13:05:06 +0100 CET").TO_TIMESTAMPZ()
```
### Bool expression operators:
```go
type BoolExpression interface {
Expression
// Check if this expression is equal to rhs
EQ(rhs BoolExpression) BoolExpression
// Check if this expression is not equal to rhs
NOT_EQ(rhs BoolExpression) BoolExpression
// Check if this expression is distinct to rhs
IS_DISTINCT_FROM(rhs BoolExpression) BoolExpression
// Check if this expression is not distinct to rhs
IS_NOT_DISTINCT_FROM(rhs BoolExpression) BoolExpression
// Check if this expression is true
IS_TRUE() BoolExpression
// Check if this expression is not true
IS_NOT_TRUE() BoolExpression
// Check if this expression is false
IS_FALSE() BoolExpression
// Check if this expression is not false
IS_NOT_FALSE() BoolExpression
// Check if this expression is unknown
IS_UNKNOWN() BoolExpression
// Check if this expression is not unknown
IS_NOT_UNKNOWN() BoolExpression
// expression AND operator rhs
AND(rhs BoolExpression) BoolExpression
// expression OR operator rhs
OR(rhs BoolExpression) BoolExpression
}
```
Examples:
```
Staff.Active.EQ(Bool(true))
Staff.Active.IS_TRUE()
Bool(true).AND(Staff.Active).OR(Bool(false))
```
### Integer expression operators
```go
type IntegerExpression interface {
Expression
numericExpression
// Check if expression is equal to rhs
EQ(rhs IntegerExpression) BoolExpression
// Check if expression is not equal to rhs
NOT_EQ(rhs IntegerExpression) BoolExpression
// Check if expression is distinct from rhs
IS_DISTINCT_FROM(rhs IntegerExpression) BoolExpression
// Check if expression is not distinct from rhs
IS_NOT_DISTINCT_FROM(rhs IntegerExpression) BoolExpression
// Check if expression is less then rhs
LT(rhs IntegerExpression) BoolExpression
// Check if expression is less then equal rhs
LT_EQ(rhs IntegerExpression) BoolExpression
// Check if expression is greater then rhs
GT(rhs IntegerExpression) BoolExpression
// Check if expression is greater then equal rhs
GT_EQ(rhs IntegerExpression) BoolExpression
// expression + rhs
ADD(rhs IntegerExpression) IntegerExpression
// expression - rhs
SUB(rhs IntegerExpression) IntegerExpression
// expression * rhs
MUL(rhs IntegerExpression) IntegerExpression
// expression / rhs
DIV(rhs IntegerExpression) IntegerExpression
// expression % rhs
MOD(rhs IntegerExpression) IntegerExpression
// expression ^ rhs
POW(rhs IntegerExpression) IntegerExpression
// expression & rhs
BIT_AND(rhs IntegerExpression) IntegerExpression
// expression | rhs
BIT_OR(rhs IntegerExpression) IntegerExpression
// expression # rhs
BIT_XOR(rhs IntegerExpression) IntegerExpression
// ~expression
BIT_NOT() IntegerExpression
// expression << rhs
BIT_SHIFT_LEFT(shift IntegerExpression) IntegerExpression
// expression >> rhs
BIT_SHIFT_RIGHT(shift IntegerExpression) IntegerExpression
}
```
Examples:
```go
(Film.Length.ADD(Int(20))).LT(Int(200))
Film.LanguageID.EQ(Int(2))
Film.LanguageID.BIT_SHIFT_LEFT(Int(2))
```

View file

@ -4,7 +4,7 @@ To generate files we need running database instance containing already defined d
This files can be generated in two ways: This files can be generated in two ways:
#### Generating from command line #### 1) Generating from command line
Install jetgen to GOPATH bin folder. This will allow generating jet files from the command line. Install jetgen to GOPATH bin folder. This will allow generating jet files from the command line.
@ -53,7 +53,7 @@ Generating enum sql builder files...
Generating enum model files... Generating enum model files...
Done Done
``` ```
#### Generating from code #### 2) Generating from code
``` ```
import "github.com/go-jet/jet/generator/postgresgen" import "github.com/go-jet/jet/generator/postgresgen"
@ -77,7 +77,7 @@ In both ways, generator will:
- generate sql builder and model Go files for each schema tables and enums into destination folder `./gen`. - generate sql builder and model Go files for each schema tables and enums into destination folder `./gen`.
Generated files folder structure will look like this: Generated files folder structure will look like this:
```sh ```
|-- gen # destination folder |-- gen # destination folder
| `-- jetdb # database name | `-- jetdb # database name
| `-- dvds # schema name | `-- dvds # schema name
@ -252,7 +252,7 @@ const (
For a reference SQL table definition of table `film`: For a reference SQL table definition of table `film`:
```sql ```
CREATE TABLE dvds.film ( CREATE TABLE dvds.film (
film_id integer DEFAULT nextval('dvds.film_film_id_seq'::regclass) NOT NULL, film_id integer DEFAULT nextval('dvds.film_film_id_seq'::regclass) NOT NULL,
title character varying(255) NOT NULL, title character varying(255) NOT NULL,

11
wiki/_Sidebar.md Normal file
View file

@ -0,0 +1,11 @@
* [Installation](https://github.com/go-jet/jet/wiki/Installation)
* [Generator](https://github.com/go-jet/jet/wiki/Generator)
* [Writing SQL in Go]()
* [Expressions](https://github.com/go-jet/jet/wiki/Expressions)
* [Statements]()
* [SELECT]()
* [INSERT]()
* [UPDATE]()
* [DELETE]()
* [LOCK]()