2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-30 14:49:36 +02:00
|
|
|
import "fmt"
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
// Representation of an escaped literal
|
|
|
|
|
type literalExpression struct {
|
|
|
|
|
expressionInterfaceImpl
|
2019-06-03 14:41:39 +02:00
|
|
|
value interface{}
|
|
|
|
|
constant bool
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 11:52:37 +02:00
|
|
|
func literal(value interface{}) *literalExpression {
|
2019-04-29 14:39:48 +02:00
|
|
|
exp := literalExpression{value: value}
|
|
|
|
|
exp.expressionInterfaceImpl.parent = &exp
|
|
|
|
|
|
|
|
|
|
return &exp
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 11:52:37 +02:00
|
|
|
func constLiteral(value interface{}) *literalExpression {
|
|
|
|
|
exp := literal(value)
|
2019-06-03 14:41:39 +02:00
|
|
|
exp.constant = true
|
|
|
|
|
|
|
|
|
|
return exp
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 10:48:03 +02:00
|
|
|
func (l literalExpression) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
|
2019-06-03 14:41:39 +02:00
|
|
|
if l.constant {
|
|
|
|
|
out.insertConstantArgument(l.value)
|
|
|
|
|
} else {
|
2019-07-18 17:43:11 +02:00
|
|
|
out.insertParametrizedArgument(l.value)
|
2019-06-03 14:41:39 +02:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-05-06 12:42:15 +02:00
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
type integerLiteralExpression struct {
|
2019-05-06 12:42:15 +02:00
|
|
|
literalExpression
|
2019-05-31 12:59:57 +02:00
|
|
|
integerInterfaceImpl
|
2019-05-06 12:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Int is constructor for integer expressions literals.
|
2019-06-01 15:00:37 +02:00
|
|
|
func Int(value int64) IntegerExpression {
|
2019-05-31 12:59:57 +02:00
|
|
|
numLiteral := &integerLiteralExpression{}
|
2019-05-06 12:42:15 +02:00
|
|
|
|
2019-06-04 11:52:37 +02:00
|
|
|
numLiteral.literalExpression = *literal(value)
|
2019-05-06 12:42:15 +02:00
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
numLiteral.literalExpression.parent = numLiteral
|
|
|
|
|
numLiteral.integerInterfaceImpl.parent = numLiteral
|
2019-05-06 12:42:15 +02:00
|
|
|
|
|
|
|
|
return numLiteral
|
|
|
|
|
}
|
2019-05-29 14:03:38 +02:00
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type boolLiteralExpression struct {
|
|
|
|
|
boolInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Bool creates new bool literal expression
|
2019-05-31 12:59:57 +02:00
|
|
|
func Bool(value bool) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
boolLiteralExpression := boolLiteralExpression{}
|
|
|
|
|
|
2019-06-04 11:52:37 +02:00
|
|
|
boolLiteralExpression.literalExpression = *literal(value)
|
2019-05-29 14:03:38 +02:00
|
|
|
boolLiteralExpression.boolInterfaceImpl.parent = &boolLiteralExpression
|
|
|
|
|
|
|
|
|
|
return &boolLiteralExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
2019-05-31 12:59:57 +02:00
|
|
|
type floatLiteral struct {
|
|
|
|
|
floatInterfaceImpl
|
2019-05-29 14:03:38 +02:00
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Float creates new float literal expression
|
2019-05-31 12:59:57 +02:00
|
|
|
func Float(value float64) FloatExpression {
|
|
|
|
|
floatLiteral := floatLiteral{}
|
2019-06-04 11:52:37 +02:00
|
|
|
floatLiteral.literalExpression = *literal(value)
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
floatLiteral.floatInterfaceImpl.parent = &floatLiteral
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
return &floatLiteral
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type stringLiteral struct {
|
|
|
|
|
stringInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// String creates new string literal expression
|
2019-06-02 12:45:46 +02:00
|
|
|
func String(value string) StringExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
stringLiteral := stringLiteral{}
|
2019-06-04 11:52:37 +02:00
|
|
|
stringLiteral.literalExpression = *literal(value)
|
2019-05-29 14:03:38 +02:00
|
|
|
|
|
|
|
|
stringLiteral.stringInterfaceImpl.parent = &stringLiteral
|
|
|
|
|
|
|
|
|
|
return &stringLiteral
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type timeLiteral struct {
|
|
|
|
|
timeInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Time creates new time literal expression
|
2019-06-02 18:12:44 +02:00
|
|
|
func Time(hour, minute, second, milliseconds int) TimeExpression {
|
2019-07-07 12:19:05 +02:00
|
|
|
timeLiteral := &timeLiteral{}
|
2019-05-30 14:49:36 +02:00
|
|
|
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d", hour, minute, second, milliseconds)
|
2019-06-04 11:52:37 +02:00
|
|
|
timeLiteral.literalExpression = *literal(timeStr)
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
timeLiteral.timeInterfaceImpl.parent = timeLiteral
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
return CAST(timeLiteral).AS_TIME()
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
2019-05-30 14:49:36 +02:00
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type timezLiteral struct {
|
|
|
|
|
timezInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Timez creates new time with time zone literal expression
|
2019-06-02 18:12:44 +02:00
|
|
|
func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression {
|
2019-07-07 12:19:05 +02:00
|
|
|
timezLiteral := &timezLiteral{}
|
2019-05-30 14:49:36 +02:00
|
|
|
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d %+03d", hour, minute, second, milliseconds, timezone)
|
2019-06-04 11:52:37 +02:00
|
|
|
timezLiteral.literalExpression = *literal(timeStr)
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
timezLiteral.timezInterfaceImpl.parent = timezLiteral
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
return CAST(timezLiteral).AS_TIMEZ()
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type timestampLiteral struct {
|
|
|
|
|
timestampInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Timestamp creates new timestamp literal expression
|
2019-05-30 14:49:36 +02:00
|
|
|
func Timestamp(year, month, day, hour, minute, second, milliseconds int) TimestampExpression {
|
2019-07-07 12:19:05 +02:00
|
|
|
timestampLiteral := ×tampLiteral{}
|
2019-05-30 14:49:36 +02:00
|
|
|
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, milliseconds)
|
2019-06-04 11:52:37 +02:00
|
|
|
timestampLiteral.literalExpression = *literal(timeStr)
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
timestampLiteral.timestampInterfaceImpl.parent = timestampLiteral
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
return CAST(timestampLiteral).AS_TIMESTAMP()
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type timestampzLiteral struct {
|
|
|
|
|
timestampzInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Timestampz creates new timestamp with time zone literal expression
|
2019-05-30 14:49:36 +02:00
|
|
|
func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone int) TimestampzExpression {
|
2019-07-07 12:19:05 +02:00
|
|
|
timestampzLiteral := ×tampzLiteral{}
|
2019-05-30 14:49:36 +02:00
|
|
|
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %+04d",
|
|
|
|
|
year, month, day, hour, minute, second, milliseconds, timezone)
|
|
|
|
|
|
2019-06-04 11:52:37 +02:00
|
|
|
timestampzLiteral.literalExpression = *literal(timeStr)
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
timestampzLiteral.timestampzInterfaceImpl.parent = timestampzLiteral
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
return CAST(timestampzLiteral).AS_TIMESTAMPZ()
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type dateLiteral struct {
|
|
|
|
|
dateInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
//Date creates new date expression
|
2019-05-30 14:49:36 +02:00
|
|
|
func Date(year, month, day int) DateExpression {
|
2019-07-07 12:19:05 +02:00
|
|
|
dateLiteral := &dateLiteral{}
|
2019-05-30 14:49:36 +02:00
|
|
|
|
|
|
|
|
timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
|
2019-06-04 11:52:37 +02:00
|
|
|
dateLiteral.literalExpression = *literal(timeStr)
|
2019-07-07 12:19:05 +02:00
|
|
|
dateLiteral.dateInterfaceImpl.parent = dateLiteral
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-07-07 12:19:05 +02:00
|
|
|
return CAST(dateLiteral).AS_DATE()
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
2019-06-03 17:38:47 +02:00
|
|
|
|
|
|
|
|
//--------------------------------------------------//
|
2019-06-03 18:28:16 +02:00
|
|
|
type nullLiteral struct {
|
2019-06-03 17:38:47 +02:00
|
|
|
expressionInterfaceImpl
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func newNullLiteral() Expression {
|
2019-06-03 18:28:16 +02:00
|
|
|
nullExpression := &nullLiteral{}
|
2019-06-03 17:38:47 +02:00
|
|
|
|
|
|
|
|
nullExpression.expressionInterfaceImpl.parent = nullExpression
|
|
|
|
|
|
|
|
|
|
return nullExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 10:48:03 +02:00
|
|
|
func (n *nullLiteral) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
|
2019-06-03 17:38:47 +02:00
|
|
|
out.writeString("NULL")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-06-03 18:28:16 +02:00
|
|
|
|
|
|
|
|
//--------------------------------------------------//
|
|
|
|
|
type starLiteral struct {
|
|
|
|
|
expressionInterfaceImpl
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func newStarLiteral() Expression {
|
2019-06-03 18:28:16 +02:00
|
|
|
starExpression := &starLiteral{}
|
|
|
|
|
|
|
|
|
|
starExpression.expressionInterfaceImpl.parent = starExpression
|
|
|
|
|
|
|
|
|
|
return starExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 10:48:03 +02:00
|
|
|
func (n *starLiteral) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
|
2019-06-03 18:28:16 +02:00
|
|
|
out.writeString("*")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-06-04 11:52:37 +02:00
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
|
|
|
|
|
type wrap struct {
|
|
|
|
|
expressionInterfaceImpl
|
2019-06-04 12:10:23 +02:00
|
|
|
expressions []Expression
|
2019-06-04 11:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-08 10:48:03 +02:00
|
|
|
func (n *wrap) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
|
2019-06-04 11:52:37 +02:00
|
|
|
out.writeString("(")
|
|
|
|
|
err := serializeExpressionList(statement, n.expressions, ", ", out)
|
|
|
|
|
out.writeString(")")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// WRAP wraps list of expressions with brackets '(' and ')'
|
2019-06-04 12:10:23 +02:00
|
|
|
func WRAP(expression ...Expression) Expression {
|
2019-06-04 11:52:37 +02:00
|
|
|
wrap := &wrap{expressions: expression}
|
|
|
|
|
wrap.expressionInterfaceImpl.parent = wrap
|
|
|
|
|
|
|
|
|
|
return wrap
|
|
|
|
|
}
|
2019-06-04 12:10:23 +02:00
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
|
|
|
|
|
type rawExpression struct {
|
|
|
|
|
expressionInterfaceImpl
|
|
|
|
|
raw string
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 10:48:03 +02:00
|
|
|
func (n *rawExpression) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
|
2019-06-04 12:10:23 +02:00
|
|
|
out.writeString(n.raw)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// RAW can be used for any unsupported functions, operators or expressions.
|
|
|
|
|
// For example: RAW("current_database()")
|
2019-06-04 12:10:23 +02:00
|
|
|
func RAW(raw string) Expression {
|
|
|
|
|
rawExp := &rawExpression{raw: raw}
|
|
|
|
|
rawExp.expressionInterfaceImpl.parent = rawExp
|
|
|
|
|
|
|
|
|
|
return rawExp
|
|
|
|
|
}
|