jet/literal_expression.go

254 lines
6.2 KiB
Go
Raw Normal View History

2019-06-21 13:56:57 +02:00
package jet
import "fmt"
2019-05-29 14:03:38 +02:00
// Representation of an escaped literal
type literalExpression struct {
expressionInterfaceImpl
2019-06-03 14:41:39 +02:00
value interface{}
constant bool
}
func literal(value interface{}) *literalExpression {
exp := literalExpression{value: value}
exp.expressionInterfaceImpl.parent = &exp
return &exp
}
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 {
out.insertPreparedArgument(l.value)
}
return nil
}
2019-05-06 12:42:15 +02:00
type integerLiteralExpression struct {
2019-05-06 12:42:15 +02:00
literalExpression
integerInterfaceImpl
2019-05-06 12:42:15 +02:00
}
func Int(value int64) IntegerExpression {
numLiteral := &integerLiteralExpression{}
2019-05-06 12:42:15 +02:00
numLiteral.literalExpression = *literal(value)
2019-05-06 12:42:15 +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
}
func Bool(value bool) BoolExpression {
2019-05-29 14:03:38 +02:00
boolLiteralExpression := boolLiteralExpression{}
boolLiteralExpression.literalExpression = *literal(value)
2019-05-29 14:03:38 +02:00
boolLiteralExpression.boolInterfaceImpl.parent = &boolLiteralExpression
return &boolLiteralExpression
}
//---------------------------------------------------//
type floatLiteral struct {
floatInterfaceImpl
2019-05-29 14:03:38 +02:00
literalExpression
}
func Float(value float64) FloatExpression {
floatLiteral := floatLiteral{}
floatLiteral.literalExpression = *literal(value)
2019-05-29 14:03:38 +02:00
floatLiteral.floatInterfaceImpl.parent = &floatLiteral
2019-05-29 14:03:38 +02:00
return &floatLiteral
2019-05-29 14:03:38 +02:00
}
//---------------------------------------------------//
type stringLiteral struct {
stringInterfaceImpl
literalExpression
}
func String(value string) StringExpression {
2019-05-29 14:03:38 +02:00
stringLiteral := stringLiteral{}
stringLiteral.literalExpression = *literal(value)
2019-05-29 14:03:38 +02:00
stringLiteral.stringInterfaceImpl.parent = &stringLiteral
return &stringLiteral
}
//---------------------------------------------------//
type timeLiteral struct {
timeInterfaceImpl
literalExpression
}
func Time(hour, minute, second, milliseconds int) TimeExpression {
2019-07-07 12:19:05 +02:00
timeLiteral := &timeLiteral{}
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d", hour, minute, second, milliseconds)
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
}
//---------------------------------------------------//
type timezLiteral struct {
timezInterfaceImpl
literalExpression
}
func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression {
2019-07-07 12:19:05 +02:00
timezLiteral := &timezLiteral{}
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d %+03d", hour, minute, second, milliseconds, timezone)
timezLiteral.literalExpression = *literal(timeStr)
2019-07-07 12:19:05 +02:00
timezLiteral.timezInterfaceImpl.parent = timezLiteral
2019-07-07 12:19:05 +02:00
return CAST(timezLiteral).AS_TIMEZ()
}
//---------------------------------------------------//
type timestampLiteral struct {
timestampInterfaceImpl
literalExpression
}
func Timestamp(year, month, day, hour, minute, second, milliseconds int) TimestampExpression {
2019-07-07 12:19:05 +02:00
timestampLiteral := &timestampLiteral{}
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, milliseconds)
timestampLiteral.literalExpression = *literal(timeStr)
2019-07-07 12:19:05 +02:00
timestampLiteral.timestampInterfaceImpl.parent = timestampLiteral
2019-07-07 12:19:05 +02:00
return CAST(timestampLiteral).AS_TIMESTAMP()
}
//---------------------------------------------------//
type timestampzLiteral struct {
timestampzInterfaceImpl
literalExpression
}
func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone int) TimestampzExpression {
2019-07-07 12:19:05 +02:00
timestampzLiteral := &timestampzLiteral{}
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %+04d",
year, month, day, hour, minute, second, milliseconds, timezone)
timestampzLiteral.literalExpression = *literal(timeStr)
2019-07-07 12:19:05 +02:00
timestampzLiteral.timestampzInterfaceImpl.parent = timestampzLiteral
2019-07-07 12:19:05 +02:00
return CAST(timestampzLiteral).AS_TIMESTAMPZ()
}
//---------------------------------------------------//
type dateLiteral struct {
dateInterfaceImpl
literalExpression
}
func Date(year, month, day int) DateExpression {
2019-07-07 12:19:05 +02:00
dateLiteral := &dateLiteral{}
timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
dateLiteral.literalExpression = *literal(timeStr)
2019-07-07 12:19:05 +02:00
dateLiteral.dateInterfaceImpl.parent = dateLiteral
2019-07-07 12:19:05 +02:00
return CAST(dateLiteral).AS_DATE()
}
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
}
//---------------------------------------------------//
type wrap struct {
expressionInterfaceImpl
2019-06-04 12:10:23 +02:00
expressions []Expression
}
2019-07-08 10:48:03 +02:00
func (n *wrap) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
out.writeString("(")
err := serializeExpressionList(statement, n.expressions, ", ", out)
out.writeString(")")
return err
}
2019-06-04 12:10:23 +02:00
func WRAP(expression ...Expression) Expression {
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
}
func RAW(raw string) Expression {
rawExp := &rawExpression{raw: raw}
rawExp.expressionInterfaceImpl.parent = rawExp
return rawExp
}