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-07-28 14:57:02 +02:00
|
|
|
noOpVisitorImpl
|
|
|
|
|
|
2019-06-03 14:41:39 +02:00
|
|
|
value interface{}
|
|
|
|
|
constant bool
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-01 11:33:38 +02:00
|
|
|
func literal(value interface{}, optionalConstant ...bool) *literalExpression {
|
2019-04-29 14:39:48 +02:00
|
|
|
exp := literalExpression{value: value}
|
2019-08-01 11:33:38 +02:00
|
|
|
|
|
|
|
|
if len(optionalConstant) > 0 {
|
|
|
|
|
exp.constant = optionalConstant[0]
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
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-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-01 11:33:38 +02:00
|
|
|
return IntExp(literal(value, constant...))
|
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-01 11:33:38 +02:00
|
|
|
return BoolExp(literal(value))
|
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-01 11:33:38 +02:00
|
|
|
return FloatExp(literal(value))
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
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-08-01 11:33:38 +02:00
|
|
|
return StringExp(literal(value))
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
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-05-30 14:49:36 +02:00
|
|
|
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d", hour, minute, second, 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-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-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 {
|
|
|
|
|
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, milliseconds)
|
|
|
|
|
|
2019-08-01 11:33:38 +02:00
|
|
|
return TimestampExp(literal(timeStr))
|
2019-05-30 14:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
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-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 {
|
|
|
|
|
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-06-03 18:28:16 +02:00
|
|
|
type nullLiteral struct {
|
2019-06-03 17:38:47 +02:00
|
|
|
expressionInterfaceImpl
|
2019-07-28 14:57:02 +02:00
|
|
|
noOpVisitorImpl
|
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
|
|
|
|
|
|
|
|
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-07-28 14:57:02 +02:00
|
|
|
noOpVisitorImpl
|
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{}
|
|
|
|
|
|
|
|
|
|
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-28 14:57:02 +02:00
|
|
|
func (n *wrap) accept(visitor visitor) {
|
|
|
|
|
for _, exp := range n.expressions {
|
|
|
|
|
exp.accept(visitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2019-07-28 14:57:02 +02:00
|
|
|
noOpVisitorImpl
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
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
|
|
|
|
|
}
|