2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-08-06 10:29:04 +02:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
// Representation of an escaped literal
|
2019-08-06 10:29:04 +02:00
|
|
|
type LiteralExpression interface {
|
|
|
|
|
Expression
|
|
|
|
|
|
|
|
|
|
Value() interface{}
|
|
|
|
|
SetConstant(constant bool)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type literalExpressionImpl struct {
|
2019-08-11 09:52:02 +02:00
|
|
|
ExpressionInterfaceImpl
|
2019-07-28 14:57:02 +02:00
|
|
|
|
2019-06-03 14:41:39 +02:00
|
|
|
value interface{}
|
|
|
|
|
constant bool
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-06 10:29:04 +02:00
|
|
|
func literal(value interface{}, optionalConstant ...bool) *literalExpressionImpl {
|
|
|
|
|
exp := literalExpressionImpl{value: value}
|
2019-08-01 11:33:38 +02:00
|
|
|
|
|
|
|
|
if len(optionalConstant) > 0 {
|
|
|
|
|
exp.constant = optionalConstant[0]
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 09:52:02 +02:00
|
|
|
exp.ExpressionInterfaceImpl.Parent = &exp
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
return &exp
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 10:29:04 +02:00
|
|
|
func constLiteral(value interface{}) *literalExpressionImpl {
|
2019-06-04 11:52:37 +02:00
|
|
|
exp := literal(value)
|
2019-06-03 14:41:39 +02:00
|
|
|
exp.constant = true
|
|
|
|
|
|
|
|
|
|
return exp
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 10:29:04 +02:00
|
|
|
func (l *literalExpressionImpl) 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-08-06 10:29:04 +02:00
|
|
|
func (l *literalExpressionImpl) Value() interface{} {
|
|
|
|
|
return l.value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *literalExpressionImpl) SetConstant(constant bool) {
|
|
|
|
|
l.constant = constant
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type integerLiteralExpression struct {
|
|
|
|
|
literalExpressionImpl
|
|
|
|
|
integerInterfaceImpl
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Int is constructor for integer expressions literals.
|
2019-07-31 13:02:30 +02:00
|
|
|
func Int(value int64, constant ...bool) IntegerExpression {
|
2019-08-06 10:29:04 +02:00
|
|
|
numLiteral := &integerLiteralExpression{}
|
|
|
|
|
|
|
|
|
|
numLiteral.literalExpressionImpl = *literal(value)
|
|
|
|
|
if len(constant) > 0 && constant[0] == true {
|
|
|
|
|
numLiteral.constant = true
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 09:52:02 +02:00
|
|
|
numLiteral.literalExpressionImpl.Parent = numLiteral
|
2019-08-06 10:29:04 +02:00
|
|
|
numLiteral.integerInterfaceImpl.parent = numLiteral
|
|
|
|
|
|
|
|
|
|
return numLiteral
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type boolLiteralExpression struct {
|
|
|
|
|
boolInterfaceImpl
|
|
|
|
|
literalExpressionImpl
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
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-08-06 10:29:04 +02:00
|
|
|
boolLiteralExpression := boolLiteralExpression{}
|
|
|
|
|
|
|
|
|
|
boolLiteralExpression.literalExpressionImpl = *literal(value)
|
|
|
|
|
boolLiteralExpression.boolInterfaceImpl.parent = &boolLiteralExpression
|
|
|
|
|
|
|
|
|
|
return &boolLiteralExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type floatLiteral struct {
|
|
|
|
|
floatInterfaceImpl
|
|
|
|
|
literalExpressionImpl
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
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 {
|
2019-08-06 10:29:04 +02:00
|
|
|
floatLiteral := floatLiteral{}
|
|
|
|
|
floatLiteral.literalExpressionImpl = *literal(value)
|
|
|
|
|
|
|
|
|
|
floatLiteral.floatInterfaceImpl.parent = &floatLiteral
|
|
|
|
|
|
|
|
|
|
return &floatLiteral
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type stringLiteral struct {
|
|
|
|
|
stringInterfaceImpl
|
|
|
|
|
literalExpressionImpl
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// String creates new string literal expression
|
2019-08-06 10:29:04 +02:00
|
|
|
func String(value string, constant ...bool) StringExpression {
|
|
|
|
|
stringLiteral := stringLiteral{}
|
|
|
|
|
stringLiteral.literalExpressionImpl = *literal(value)
|
|
|
|
|
if len(constant) > 0 && constant[0] == true {
|
|
|
|
|
stringLiteral.constant = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stringLiteral.stringInterfaceImpl.parent = &stringLiteral
|
|
|
|
|
|
|
|
|
|
return &stringLiteral
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatMilliseconds(milliseconds ...int) string {
|
|
|
|
|
if len(milliseconds) > 0 {
|
|
|
|
|
if milliseconds[0] < 1000 {
|
|
|
|
|
return fmt.Sprintf(".%03d", milliseconds[0])
|
|
|
|
|
} else {
|
|
|
|
|
return "." + strconv.Itoa(milliseconds[0])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Time creates new time literal expression
|
2019-08-06 10:29:04 +02:00
|
|
|
func Time(hour, minute, second int, milliseconds ...int) TimeExpression {
|
|
|
|
|
timeStr := fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)
|
|
|
|
|
|
|
|
|
|
timeStr += formatMilliseconds(milliseconds...)
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-08-01 11:33:38 +02:00
|
|
|
return TimeExp(literal(timeStr))
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-06 11:41:45 +02:00
|
|
|
func TimeT(t time.Time) TimeExpression {
|
|
|
|
|
return TimeExp(literal(t))
|
|
|
|
|
}
|
|
|
|
|
|
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-05-30 14:49:36 +02:00
|
|
|
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d %+03d", hour, minute, second, milliseconds, timezone)
|
|
|
|
|
|
2019-08-01 11:33:38 +02:00
|
|
|
return TimezExp(literal(timeStr))
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-06 11:41:45 +02:00
|
|
|
func TimezT(t time.Time) TimezExpression {
|
|
|
|
|
return TimezExp(literal(t))
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// Timestamp creates new timestamp literal expression
|
2019-08-06 10:29:04 +02:00
|
|
|
func Timestamp(year int, month time.Month, day, hour, minute, second int, milliseconds ...int) TimestampExpression {
|
|
|
|
|
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second)
|
|
|
|
|
|
|
|
|
|
timeStr += formatMilliseconds(milliseconds...)
|
2019-05-30 14:49:36 +02:00
|
|
|
|
2019-08-01 11:33:38 +02:00
|
|
|
return TimestampExp(literal(timeStr))
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-06 11:41:45 +02:00
|
|
|
func TimestampT(t time.Time) TimestampExpression {
|
|
|
|
|
return TimestampExp(literal(t))
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %+04d",
|
|
|
|
|
year, month, day, hour, minute, second, milliseconds, timezone)
|
|
|
|
|
|
2019-08-01 11:33:38 +02:00
|
|
|
return TimestampzExp(literal(timeStr))
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-06 11:41:45 +02:00
|
|
|
func TimestampzT(t time.Time) TimestampzExpression {
|
|
|
|
|
return TimestampzExp(literal(t))
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
//Date creates new date expression
|
2019-08-06 10:29:04 +02:00
|
|
|
func Date(year int, month time.Month, day int) DateExpression {
|
2019-05-30 14:49:36 +02:00
|
|
|
timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
|
|
|
|
|
|
2019-08-01 11:33:38 +02:00
|
|
|
return DateExp(literal(timeStr))
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
2019-06-03 17:38:47 +02:00
|
|
|
|
2019-08-06 11:41:45 +02:00
|
|
|
func DateT(t time.Time) DateExpression {
|
|
|
|
|
return DateExp(literal(t))
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:38:47 +02:00
|
|
|
//--------------------------------------------------//
|
2019-06-03 18:28:16 +02:00
|
|
|
type nullLiteral struct {
|
2019-08-11 09:52:02 +02:00
|
|
|
ExpressionInterfaceImpl
|
2019-06-03 17:38:47 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2019-08-11 09:52:02 +02:00
|
|
|
nullExpression.ExpressionInterfaceImpl.Parent = nullExpression
|
2019-06-03 17:38:47 +02:00
|
|
|
|
|
|
|
|
return nullExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 14:10:47 +02:00
|
|
|
func (n *nullLiteral) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
|
|
|
|
|
out.WriteString("NULL")
|
2019-06-03 17:38:47 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
2019-06-03 18:28:16 +02:00
|
|
|
|
|
|
|
|
//--------------------------------------------------//
|
|
|
|
|
type starLiteral struct {
|
2019-08-11 09:52:02 +02:00
|
|
|
ExpressionInterfaceImpl
|
2019-06-03 18:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func newStarLiteral() Expression {
|
2019-06-03 18:28:16 +02:00
|
|
|
starExpression := &starLiteral{}
|
|
|
|
|
|
2019-08-11 09:52:02 +02:00
|
|
|
starExpression.ExpressionInterfaceImpl.Parent = starExpression
|
2019-06-03 18:28:16 +02:00
|
|
|
|
|
|
|
|
return starExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 14:10:47 +02:00
|
|
|
func (n *starLiteral) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
|
|
|
|
|
out.WriteString("*")
|
2019-06-03 18:28:16 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
2019-06-04 11:52:37 +02:00
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
|
|
|
|
|
type wrap struct {
|
2019-08-11 09:52:02 +02:00
|
|
|
ExpressionInterfaceImpl
|
2019-06-04 12:10:23 +02:00
|
|
|
expressions []Expression
|
2019-06-04 11:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-03 14:10:47 +02:00
|
|
|
func (n *wrap) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
|
|
|
|
|
out.WriteString("(")
|
2019-06-04 11:52:37 +02:00
|
|
|
err := serializeExpressionList(statement, n.expressions, ", ", out)
|
2019-08-03 14:10:47 +02:00
|
|
|
out.WriteString(")")
|
2019-06-04 11:52:37 +02:00
|
|
|
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}
|
2019-08-11 09:52:02 +02:00
|
|
|
wrap.ExpressionInterfaceImpl.Parent = wrap
|
2019-06-04 11:52:37 +02:00
|
|
|
|
|
|
|
|
return wrap
|
|
|
|
|
}
|
2019-06-04 12:10:23 +02:00
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
|
|
|
|
|
type rawExpression struct {
|
2019-08-11 09:52:02 +02:00
|
|
|
ExpressionInterfaceImpl
|
2019-07-28 14:57:02 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
raw string
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 14:10:47 +02:00
|
|
|
func (n *rawExpression) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
|
|
|
|
|
out.WriteString(n.raw)
|
2019-06-04 12:10:23 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 13:29:26 +02:00
|
|
|
// Raw can be used for any unsupported functions, operators or expressions.
|
|
|
|
|
// For example: Raw("current_database()")
|
|
|
|
|
func Raw(raw string) Expression {
|
2019-06-04 12:10:23 +02:00
|
|
|
rawExp := &rawExpression{raw: raw}
|
2019-08-11 09:52:02 +02:00
|
|
|
rawExp.ExpressionInterfaceImpl.Parent = rawExp
|
2019-06-04 12:10:23 +02:00
|
|
|
|
|
|
|
|
return rawExp
|
|
|
|
|
}
|