jet/internal/jet/literal_expression.go

283 lines
6.8 KiB
Go
Raw Normal View History

2019-06-21 13:56:57 +02:00
package jet
import (
"fmt"
"strconv"
"time"
)
2019-05-29 14:03:38 +02:00
// Representation of an escaped literal
type LiteralExpression interface {
Expression
Value() interface{}
SetConstant(constant bool)
}
type literalExpressionImpl struct {
2019-08-11 09:52:02 +02:00
ExpressionInterfaceImpl
2019-06-03 14:41:39 +02:00
value interface{}
constant bool
}
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
return &exp
}
func constLiteral(value interface{}) *literalExpressionImpl {
exp := literal(value)
2019-06-03 14:41:39 +02:00
exp.constant = true
return exp
}
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
}
return nil
}
2019-05-06 12:42:15 +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 {
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
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
func Bool(value bool) BoolExpression {
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
func Float(value float64) FloatExpression {
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
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
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))
}
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
func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression {
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))
}
func TimezT(t time.Time) TimezExpression {
return TimezExp(literal(t))
}
2019-07-18 17:43:11 +02:00
// Timestamp creates new timestamp literal expression
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-08-01 11:33:38 +02:00
return TimestampExp(literal(timeStr))
}
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
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))
}
func TimestampzT(t time.Time) TimestampzExpression {
return TimestampzExp(literal(t))
}
2019-07-18 17:43:11 +02:00
//Date creates new date expression
func Date(year int, month time.Month, day int) DateExpression {
timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
2019-08-01 11:33:38 +02:00
return DateExp(literal(timeStr))
}
2019-06-03 17:38:47 +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
}
//---------------------------------------------------//
type wrap struct {
2019-08-11 09:52:02 +02:00
ExpressionInterfaceImpl
2019-06-04 12:10:23 +02:00
expressions []Expression
}
2019-08-03 14:10:47 +02:00
func (n *wrap) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
out.WriteString("(")
err := serializeExpressionList(statement, n.expressions, ", ", out)
2019-08-03 14:10:47 +02:00
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 {
wrap := &wrap{expressions: expression}
2019-08-11 09:52:02 +02:00
wrap.ExpressionInterfaceImpl.Parent = wrap
return wrap
}
2019-06-04 12:10:23 +02:00
//---------------------------------------------------//
type rawExpression struct {
2019-08-11 09:52:02 +02:00
ExpressionInterfaceImpl
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
}