Literal constructors simplified.

This commit is contained in:
go-jet 2019-08-01 11:33:38 +02:00
parent dfb72c06a7
commit 449cd9fd8f

View file

@ -11,8 +11,13 @@ type literalExpression struct {
constant bool constant bool
} }
func literal(value interface{}) *literalExpression { func literal(value interface{}, optionalConstant ...bool) *literalExpression {
exp := literalExpression{value: value} exp := literalExpression{value: value}
if len(optionalConstant) > 0 {
exp.constant = optionalConstant[0]
}
exp.expressionInterfaceImpl.parent = &exp exp.expressionInterfaceImpl.parent = &exp
return &exp return &exp
@ -35,159 +40,60 @@ func (l literalExpression) serialize(statement statementType, out *sqlBuilder, o
return nil return nil
} }
type integerLiteralExpression struct {
literalExpression
integerInterfaceImpl
}
// Int is constructor for integer expressions literals. // Int is constructor for integer expressions literals.
func Int(value int64, constant ...bool) IntegerExpression { func Int(value int64, constant ...bool) IntegerExpression {
numLiteral := &integerLiteralExpression{} return IntExp(literal(value, constant...))
numLiteral.literalExpression = *literal(value)
if len(constant) > 0 && constant[0] == true {
numLiteral.constant = true
}
numLiteral.literalExpression.parent = numLiteral
numLiteral.integerInterfaceImpl.parent = numLiteral
return numLiteral
}
//---------------------------------------------------//
type boolLiteralExpression struct {
boolInterfaceImpl
literalExpression
} }
// Bool creates new bool literal expression // Bool creates new bool literal expression
func Bool(value bool) BoolExpression { func Bool(value bool) BoolExpression {
boolLiteralExpression := boolLiteralExpression{} return BoolExp(literal(value))
boolLiteralExpression.literalExpression = *literal(value)
boolLiteralExpression.boolInterfaceImpl.parent = &boolLiteralExpression
return &boolLiteralExpression
}
//---------------------------------------------------//
type floatLiteral struct {
floatInterfaceImpl
literalExpression
} }
// Float creates new float literal expression // Float creates new float literal expression
func Float(value float64) FloatExpression { func Float(value float64) FloatExpression {
floatLiteral := floatLiteral{} return FloatExp(literal(value))
floatLiteral.literalExpression = *literal(value)
floatLiteral.floatInterfaceImpl.parent = &floatLiteral
return &floatLiteral
}
//---------------------------------------------------//
type stringLiteral struct {
stringInterfaceImpl
literalExpression
} }
// String creates new string literal expression // String creates new string literal expression
func String(value string) StringExpression { func String(value string) StringExpression {
stringLiteral := stringLiteral{} return StringExp(literal(value))
stringLiteral.literalExpression = *literal(value)
stringLiteral.stringInterfaceImpl.parent = &stringLiteral
return &stringLiteral
}
//---------------------------------------------------//
type timeLiteral struct {
timeInterfaceImpl
literalExpression
} }
// Time creates new time literal expression // Time creates new time literal expression
func Time(hour, minute, second, milliseconds int) TimeExpression { func Time(hour, minute, second, milliseconds int) TimeExpression {
timeLiteral := &timeLiteral{}
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d", hour, minute, second, milliseconds) timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d", hour, minute, second, milliseconds)
timeLiteral.literalExpression = *literal(timeStr)
timeLiteral.timeInterfaceImpl.parent = timeLiteral return TimeExp(literal(timeStr))
return timeLiteral
}
//---------------------------------------------------//
type timezLiteral struct {
timezInterfaceImpl
literalExpression
} }
// Timez creates new time with time zone literal expression // Timez creates new time with time zone literal expression
func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression { func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression {
timezLiteral := &timezLiteral{}
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d %+03d", hour, minute, second, milliseconds, timezone) timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d %+03d", hour, minute, second, milliseconds, timezone)
timezLiteral.literalExpression = *literal(timeStr)
timezLiteral.timezInterfaceImpl.parent = timezLiteral return TimezExp(literal(timeStr))
return timezLiteral
}
//---------------------------------------------------//
type timestampLiteral struct {
timestampInterfaceImpl
literalExpression
} }
// Timestamp creates new timestamp literal expression // Timestamp creates new timestamp literal expression
func Timestamp(year, month, day, hour, minute, second, milliseconds int) TimestampExpression { func Timestamp(year, month, day, hour, minute, second, milliseconds int) TimestampExpression {
timestampLiteral := &timestampLiteral{}
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, milliseconds) timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, milliseconds)
timestampLiteral.literalExpression = *literal(timeStr)
timestampLiteral.timestampInterfaceImpl.parent = timestampLiteral return TimestampExp(literal(timeStr))
return timestampLiteral
}
//---------------------------------------------------//
type timestampzLiteral struct {
timestampzInterfaceImpl
literalExpression
} }
// Timestampz creates new timestamp with time zone literal expression // Timestampz creates new timestamp with time zone literal expression
func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone int) TimestampzExpression { func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone int) TimestampzExpression {
timestampzLiteral := &timestampzLiteral{}
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %+04d", timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %+04d",
year, month, day, hour, minute, second, milliseconds, timezone) year, month, day, hour, minute, second, milliseconds, timezone)
timestampzLiteral.literalExpression = *literal(timeStr) return TimestampzExp(literal(timeStr))
timestampzLiteral.timestampzInterfaceImpl.parent = timestampzLiteral
return timestampzLiteral
}
//---------------------------------------------------//
type dateLiteral struct {
dateInterfaceImpl
literalExpression
} }
//Date creates new date expression //Date creates new date expression
func Date(year, month, day int) DateExpression { func Date(year, month, day int) DateExpression {
dateLiteral := &dateLiteral{}
timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day) timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
dateLiteral.literalExpression = *literal(timeStr)
dateLiteral.dateInterfaceImpl.parent = dateLiteral
return dateLiteral return DateExp(literal(timeStr))
} }
//--------------------------------------------------// //--------------------------------------------------//