Explicit sqlbuilder cast.
This commit is contained in:
parent
4d7fbf8f49
commit
3c5553b3dc
21 changed files with 293 additions and 104 deletions
|
|
@ -124,3 +124,20 @@ func newPostifxBoolExpression(expression Expression, operator string) BoolExpres
|
||||||
|
|
||||||
return &exp
|
return &exp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------//
|
||||||
|
|
||||||
|
type boolExpressionWrapper struct {
|
||||||
|
boolInterfaceImpl
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBoolExpressionWrap(expression Expression) BoolExpression {
|
||||||
|
boolExpressionWrap := boolExpressionWrapper{Expression: expression}
|
||||||
|
boolExpressionWrap.boolInterfaceImpl.parent = &boolExpressionWrap
|
||||||
|
return &boolExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func BoolExp(expression Expression) BoolExpression {
|
||||||
|
return newBoolExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ func TestBinaryBoolExpression(t *testing.T) {
|
||||||
assertClauseSerialize(t, boolExpression.OR(Int(4).EQ(Int(5))),
|
assertClauseSerialize(t, boolExpression.OR(Int(4).EQ(Int(5))),
|
||||||
"(($1 = $2) OR ($3 = $4))", int64(2), int64(3), int64(4), int64(5))
|
"(($1 = $2) OR ($3 = $4))", int64(2), int64(3), int64(4), int64(5))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBoolLiteral(t *testing.T) {
|
func TestBoolLiteral(t *testing.T) {
|
||||||
assertClauseSerialize(t, Bool(true), "$1", true)
|
assertClauseSerialize(t, Bool(true), "$1", true)
|
||||||
assertClauseSerialize(t, Bool(false), "$1", false)
|
assertClauseSerialize(t, Bool(false), "$1", false)
|
||||||
|
|
@ -71,3 +72,8 @@ func TestExists(t *testing.T) {
|
||||||
WHERE table1.col1 = table2.col3
|
WHERE table1.col1 = table2.col3
|
||||||
)`, int64(1))
|
)`, int64(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBoolExp(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, BoolExp(String("true")), "$1", "true")
|
||||||
|
assertClauseSerialize(t, BoolExp(String("true")).IS_TRUE(), "$1 IS TRUE", "true")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,8 @@ type integerCast struct {
|
||||||
cast
|
cast
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIntegerCast(expression Expression) IntegerExpression {
|
func newIntegerCast(expression Expression, intType string) IntegerExpression {
|
||||||
integerCast := &integerCast{cast: *newCast(expression, "integer")}
|
integerCast := &integerCast{cast: *newCast(expression, intType)}
|
||||||
|
|
||||||
integerCast.integerInterfaceImpl.parent = integerCast
|
integerCast.integerInterfaceImpl.parent = integerCast
|
||||||
integerCast.expressionInterfaceImpl.parent = integerCast
|
integerCast.expressionInterfaceImpl.parent = integerCast
|
||||||
|
|
@ -54,8 +54,8 @@ type floatCast struct {
|
||||||
cast
|
cast
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDoubleCast(expression Expression) FloatExpression {
|
func newFloatCast(expression Expression, floatType string) FloatExpression {
|
||||||
floatCast := &floatCast{cast: *newCast(expression, "double precision")}
|
floatCast := &floatCast{cast: *newCast(expression, floatType)}
|
||||||
|
|
||||||
floatCast.floatInterfaceImpl.parent = floatCast
|
floatCast.floatInterfaceImpl.parent = floatCast
|
||||||
floatCast.expressionInterfaceImpl.parent = floatCast
|
floatCast.expressionInterfaceImpl.parent = floatCast
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,20 @@ func (t *dateInterfaceImpl) GT(rhs DateExpression) BoolExpression {
|
||||||
func (t *dateInterfaceImpl) GT_EQ(rhs DateExpression) BoolExpression {
|
func (t *dateInterfaceImpl) GT_EQ(rhs DateExpression) BoolExpression {
|
||||||
return GT_EQ(t.parent, rhs)
|
return GT_EQ(t.parent, rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------//
|
||||||
|
|
||||||
|
type DateExpressionWrapper struct {
|
||||||
|
dateInterfaceImpl
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDateExpressionWrap(expression Expression) DateExpression {
|
||||||
|
dateExpressionWrap := DateExpressionWrapper{Expression: expression}
|
||||||
|
dateExpressionWrap.dateInterfaceImpl.parent = &dateExpressionWrap
|
||||||
|
return &dateExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func DateExp(expression Expression) DateExpression {
|
||||||
|
return newDateExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package sqlbuilder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// An Expression
|
// An Expression
|
||||||
|
|
@ -22,16 +23,20 @@ type Expression interface {
|
||||||
ASC() OrderByClause
|
ASC() OrderByClause
|
||||||
DESC() OrderByClause
|
DESC() OrderByClause
|
||||||
|
|
||||||
CAST_TO(dbType string) Expression
|
TO(dbType string) Expression
|
||||||
CAST_TO_BOOL() BoolExpression
|
TO_BOOL() BoolExpression
|
||||||
CAST_TO_INTEGER() IntegerExpression
|
TO_SMALLINT() IntegerExpression
|
||||||
CAST_TO_DOUBLE() FloatExpression
|
TO_INTEGER() IntegerExpression
|
||||||
CAST_TO_TEXT() StringExpression
|
TO_BIGINT() IntegerExpression
|
||||||
CAST_TO_DATE() DateExpression
|
TO_NUMERIC(precision int, scale ...int) FloatExpression
|
||||||
CAST_TO_TIME() TimeExpression
|
TO_REAL() FloatExpression
|
||||||
CAST_TO_TIMEZ() TimezExpression
|
TO_DOUBLE() FloatExpression
|
||||||
CAST_TO_TIMESTAMP() TimestampExpression
|
TO_TEXT() StringExpression
|
||||||
CAST_TO_TIMESTAMPZ() TimestampzExpression
|
TO_DATE() DateExpression
|
||||||
|
TO_TIME() TimeExpression
|
||||||
|
TO_TIMEZ() TimezExpression
|
||||||
|
TO_TIMESTAMP() TimestampExpression
|
||||||
|
TO_TIMESTAMPZ() TimestampzExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
type expressionInterfaceImpl struct {
|
type expressionInterfaceImpl struct {
|
||||||
|
|
@ -66,43 +71,65 @@ func (e *expressionInterfaceImpl) DESC() OrderByClause {
|
||||||
return &orderByClauseImpl{expression: e.parent, ascent: false}
|
return &orderByClauseImpl{expression: e.parent, ascent: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO(dbType string) Expression {
|
func (e *expressionInterfaceImpl) TO(dbType string) Expression {
|
||||||
return newCast(e.parent, dbType)
|
return newCast(e.parent, dbType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_BOOL() BoolExpression {
|
func (e *expressionInterfaceImpl) TO_BOOL() BoolExpression {
|
||||||
return newBoolCast(e.parent)
|
return newBoolCast(e.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_INTEGER() IntegerExpression {
|
func (e *expressionInterfaceImpl) TO_SMALLINT() IntegerExpression {
|
||||||
return newIntegerCast(e.parent)
|
return newIntegerCast(e.parent, "smallint")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_DOUBLE() FloatExpression {
|
func (e *expressionInterfaceImpl) TO_INTEGER() IntegerExpression {
|
||||||
return newDoubleCast(e.parent)
|
return newIntegerCast(e.parent, "integer")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_TEXT() StringExpression {
|
func (e *expressionInterfaceImpl) TO_BIGINT() IntegerExpression {
|
||||||
|
return newIntegerCast(e.parent, "bigint")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *expressionInterfaceImpl) TO_NUMERIC(precision int, scale ...int) FloatExpression {
|
||||||
|
var castType string
|
||||||
|
if len(scale) > 0 {
|
||||||
|
castType = fmt.Sprintf("numeric(%d, %d)", precision, scale[0])
|
||||||
|
} else {
|
||||||
|
castType = fmt.Sprintf("numeric(%d)", precision)
|
||||||
|
}
|
||||||
|
return newFloatCast(e.parent, castType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *expressionInterfaceImpl) TO_REAL() FloatExpression {
|
||||||
|
return newFloatCast(e.parent, "real")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *expressionInterfaceImpl) TO_DOUBLE() FloatExpression {
|
||||||
|
return newFloatCast(e.parent, "double precision")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *expressionInterfaceImpl) TO_TEXT() StringExpression {
|
||||||
return newTextCast(e.parent)
|
return newTextCast(e.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_DATE() DateExpression {
|
func (e *expressionInterfaceImpl) TO_DATE() DateExpression {
|
||||||
return newDateCast(e.parent)
|
return newDateCast(e.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_TIME() TimeExpression {
|
func (e *expressionInterfaceImpl) TO_TIME() TimeExpression {
|
||||||
return newTimeCast(e.parent)
|
return newTimeCast(e.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_TIMEZ() TimezExpression {
|
func (e *expressionInterfaceImpl) TO_TIMEZ() TimezExpression {
|
||||||
return newTimezCast(e.parent)
|
return newTimezCast(e.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_TIMESTAMP() TimestampExpression {
|
func (e *expressionInterfaceImpl) TO_TIMESTAMP() TimestampExpression {
|
||||||
return newTimestampCast(e.parent)
|
return newTimestampCast(e.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) CAST_TO_TIMESTAMPZ() TimestampzExpression {
|
func (e *expressionInterfaceImpl) TO_TIMESTAMPZ() TimestampzExpression {
|
||||||
return newTimestampzCast(e.parent)
|
return newTimestampzCast(e.parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,40 +25,57 @@ func TestExpressionIS_NOT_DISTINCT_FROM(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_BOOL(t *testing.T) {
|
func TestExpressionCAST_TO_BOOL(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_BOOL(), "table2.col3::boolean")
|
assertClauseSerialize(t, table2Col3.TO_BOOL(), "table2.col3::boolean")
|
||||||
assertClauseSerialize(t, table2Col3.ADD(table2Col3).CAST_TO_BOOL(), "(table2.col3 + table2.col3)::boolean")
|
assertClauseSerialize(t, table2Col3.ADD(table2Col3).TO_BOOL(), "(table2.col3 + table2.col3)::boolean")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_TO_SMALLINT(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, table2Col3.TO_SMALLINT(), "table2.col3::smallint")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_INTEGER(t *testing.T) {
|
func TestExpressionCAST_TO_INTEGER(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_INTEGER(), "table2.col3::integer")
|
assertClauseSerialize(t, table2Col3.TO_INTEGER(), "table2.col3::integer")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_TO_BIGINT(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, table2Col3.TO_BIGINT(), "table2.col3::bigint")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_TO_NUMERIC(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, table2Col3.TO_NUMERIC(11, 11), "table2.col3::numeric(11, 11)")
|
||||||
|
assertClauseSerialize(t, table2Col3.TO_NUMERIC(11), "table2.col3::numeric(11)")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_TO_REAL(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, table2Col3.TO_REAL(), "table2.col3::real")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_DOUBLE(t *testing.T) {
|
func TestExpressionCAST_TO_DOUBLE(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_DOUBLE(), "table2.col3::double precision")
|
assertClauseSerialize(t, table2Col3.TO_DOUBLE(), "table2.col3::double precision")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TEXT(t *testing.T) {
|
func TestExpressionCAST_TO_TEXT(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_TEXT(), "table2.col3::text")
|
assertClauseSerialize(t, table2Col3.TO_TEXT(), "table2.col3::text")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_DATE(t *testing.T) {
|
func TestExpressionCAST_TO_DATE(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_DATE(), "table2.col3::date")
|
assertClauseSerialize(t, table2Col3.TO_DATE(), "table2.col3::date")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIME(t *testing.T) {
|
func TestExpressionCAST_TO_TIME(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_TIME(), "table2.col3::time without time zone")
|
assertClauseSerialize(t, table2Col3.TO_TIME(), "table2.col3::time without time zone")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIMEZ(t *testing.T) {
|
func TestExpressionCAST_TO_TIMEZ(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_TIMEZ(), "table2.col3::time with time zone")
|
assertClauseSerialize(t, table2Col3.TO_TIMEZ(), "table2.col3::time with time zone")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIMESTAMP(t *testing.T) {
|
func TestExpressionCAST_TO_TIMESTAMP(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_TIMESTAMP(), "table2.col3::timestamp without time zone")
|
assertClauseSerialize(t, table2Col3.TO_TIMESTAMP(), "table2.col3::timestamp without time zone")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIMESTAMPZ(t *testing.T) {
|
func TestExpressionCAST_TO_TIMESTAMPZ(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.CAST_TO_TIMESTAMPZ(), "table2.col3::timestamp with time zone")
|
assertClauseSerialize(t, table2Col3.TO_TIMESTAMPZ(), "table2.col3::timestamp with time zone")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIN(t *testing.T) {
|
func TestIN(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package sqlbuilder
|
package sqlbuilder
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
type FloatExpression interface {
|
type FloatExpression interface {
|
||||||
Expression
|
Expression
|
||||||
|
|
||||||
|
|
@ -102,32 +100,19 @@ func newBinaryFloatExpression(lhs, rhs FloatExpression, operator string) FloatEx
|
||||||
return &floatExpression
|
return &floatExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
////---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
type floatExpressionWrapper struct {
|
|
||||||
expressionInterfaceImpl
|
|
||||||
floatInterfaceImpl
|
|
||||||
|
|
||||||
expression Expression
|
type floatExpressionWrapper struct {
|
||||||
|
floatInterfaceImpl
|
||||||
|
Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFloatExpressionWrap(expression Expression) FloatExpression {
|
func newFloatExpressionWrap(expression Expression) FloatExpression {
|
||||||
floatExpressionWrap := floatExpressionWrapper{}
|
floatExpressionWrap := floatExpressionWrapper{Expression: expression}
|
||||||
|
|
||||||
floatExpressionWrap.expression = expression
|
|
||||||
|
|
||||||
floatExpressionWrap.expressionInterfaceImpl.parent = &floatExpressionWrap
|
|
||||||
floatExpressionWrap.floatInterfaceImpl.parent = &floatExpressionWrap
|
floatExpressionWrap.floatInterfaceImpl.parent = &floatExpressionWrap
|
||||||
|
|
||||||
return &floatExpressionWrap
|
return &floatExpressionWrap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *floatExpressionWrapper) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
func FloatExp(expression Expression) FloatExpression {
|
||||||
if n == nil {
|
return newFloatExpressionWrap(expression)
|
||||||
return errors.New("Float expressions wrapper is nil. ")
|
|
||||||
}
|
|
||||||
//out.writeString("(")
|
|
||||||
err := n.expression.serialize(statement, out)
|
|
||||||
//out.writeString(")")
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,3 +63,10 @@ func TestFloatExpressionPOW(t *testing.T) {
|
||||||
assertClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "(table1.colFloat ^ table2.colFloat)")
|
assertClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "(table1.colFloat ^ table2.colFloat)")
|
||||||
assertClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "(table1.colFloat ^ $1)", float64(2.11))
|
assertClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "(table1.colFloat ^ $1)", float64(2.11))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFloatExp(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, FloatExp(table1ColInt), "table1.colInt")
|
||||||
|
assertClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)), "(table1.colInt + table3.colInt)")
|
||||||
|
assertClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)).ADD(Float(11.11)),
|
||||||
|
"((table1.colInt + table3.colInt) + $1)", float64(11.11))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -148,3 +148,22 @@ func newPrefixIntegerOpExpression(expression IntegerExpression, operator string)
|
||||||
|
|
||||||
return &integerExpression
|
return &integerExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------//
|
||||||
|
type integerExpressionWrapper struct {
|
||||||
|
integerInterfaceImpl
|
||||||
|
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newIntExpressionWrap(expression Expression) IntegerExpression {
|
||||||
|
intExpressionWrap := integerExpressionWrapper{Expression: expression}
|
||||||
|
|
||||||
|
intExpressionWrap.integerInterfaceImpl.parent = &intExpressionWrap
|
||||||
|
|
||||||
|
return &intExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func IntExp(expression Expression) IntegerExpression {
|
||||||
|
return newIntExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,3 +73,9 @@ func TestIntExpressionBIT_SHIFT_RIGHT(t *testing.T) {
|
||||||
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(table2ColInt), "(table1.colInt >> table2.colInt)")
|
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(table2ColInt), "(table1.colInt >> table2.colInt)")
|
||||||
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(Int(11)), "(table1.colInt >> $1)", int64(11))
|
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(Int(11)), "(table1.colInt >> $1)", int64(11))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIntExpressionIntExp(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, IntExp(table1ColFloat), "table1.colFloat")
|
||||||
|
assertClauseSerialize(t, IntExp(table1ColFloat.ADD(table2ColFloat)).ADD(Int(11)),
|
||||||
|
"((table1.colFloat + table2.colFloat) + $1)", int64(11))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ func Time(hour, minute, second, milliseconds int) TimeExpression {
|
||||||
|
|
||||||
timeLiteral.timeInterfaceImpl.parent = &timeLiteral
|
timeLiteral.timeInterfaceImpl.parent = &timeLiteral
|
||||||
|
|
||||||
return timeLiteral.CAST_TO_TIME()
|
return timeLiteral.TO_TIME()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -123,7 +123,7 @@ func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression {
|
||||||
|
|
||||||
timezLiteral.timezInterfaceImpl.parent = &timezLiteral
|
timezLiteral.timezInterfaceImpl.parent = &timezLiteral
|
||||||
|
|
||||||
return timezLiteral.CAST_TO_TIMEZ()
|
return timezLiteral.TO_TIMEZ()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -139,7 +139,7 @@ func Timestamp(year, month, day, hour, minute, second, milliseconds int) Timesta
|
||||||
|
|
||||||
timestampLiteral.timestampInterfaceImpl.parent = ×tampLiteral
|
timestampLiteral.timestampInterfaceImpl.parent = ×tampLiteral
|
||||||
|
|
||||||
return timestampLiteral.CAST_TO_TIMESTAMP()
|
return timestampLiteral.TO_TIMESTAMP()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -157,7 +157,7 @@ func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone i
|
||||||
|
|
||||||
timestampzLiteral.timestampzInterfaceImpl.parent = ×tampzLiteral
|
timestampzLiteral.timestampzInterfaceImpl.parent = ×tampzLiteral
|
||||||
|
|
||||||
return timestampzLiteral.CAST_TO_TIMESTAMPZ()
|
return timestampzLiteral.TO_TIMESTAMPZ()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -173,7 +173,7 @@ func Date(year, month, day int) DateExpression {
|
||||||
dateLiteral.literalExpression = *literal(timeStr)
|
dateLiteral.literalExpression = *literal(timeStr)
|
||||||
dateLiteral.dateInterfaceImpl.parent = &dateLiteral
|
dateLiteral.dateInterfaceImpl.parent = &dateLiteral
|
||||||
|
|
||||||
return dateLiteral.CAST_TO_DATE()
|
return dateLiteral.TO_DATE()
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------//
|
//--------------------------------------------------//
|
||||||
|
|
|
||||||
|
|
@ -257,7 +257,3 @@ func (s *selectStatementImpl) Query(db execution.Db, destination interface{}) er
|
||||||
func (s *selectStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
func (s *selectStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
||||||
return Execute(s, db)
|
return Execute(s, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NumExp(expression Expression) FloatExpression {
|
|
||||||
return newFloatExpressionWrap(expression)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -94,3 +94,20 @@ func newBinaryStringExpression(lhs, rhs Expression, operator string) StringExpre
|
||||||
|
|
||||||
return &boolExpression
|
return &boolExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------//
|
||||||
|
|
||||||
|
type stringExpressionWrapper struct {
|
||||||
|
stringInterfaceImpl
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStringExpressionWrap(expression Expression) StringExpression {
|
||||||
|
stringExpressionWrap := stringExpressionWrapper{Expression: expression}
|
||||||
|
stringExpressionWrap.stringInterfaceImpl.parent = &stringExpressionWrap
|
||||||
|
return &stringExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func StringExp(expression Expression) StringExpression {
|
||||||
|
return newStringExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,3 +65,8 @@ func TestStringNOT_SIMILAR_TO(t *testing.T) {
|
||||||
assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(table2ColStr), "(table3.col2 NOT SIMILAR TO table2.colStr)")
|
assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(table2ColStr), "(table3.col2 NOT SIMILAR TO table2.colStr)")
|
||||||
assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(String("JOHN")), "(table3.col2 NOT SIMILAR TO $1)", "JOHN")
|
assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(String("JOHN")), "(table3.col2 NOT SIMILAR TO $1)", "JOHN")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringExp(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, StringExp(table2ColFloat), "table2.colFloat")
|
||||||
|
assertClauseSerialize(t, StringExp(table2ColFloat).NOT_LIKE(String("abc")), "(table2.colFloat NOT LIKE $1)", "abc")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,3 +71,20 @@ func newPrefixTimeExpression(operator string, expression Expression) TimeExpress
|
||||||
func INTERVAL(interval string) Expression {
|
func INTERVAL(interval string) Expression {
|
||||||
return newPrefixTimeExpression("INTERVAL", literal(interval))
|
return newPrefixTimeExpression("INTERVAL", literal(interval))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------//
|
||||||
|
|
||||||
|
type timeExpressionWrapper struct {
|
||||||
|
timeInterfaceImpl
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTimeExpressionWrap(expression Expression) TimeExpression {
|
||||||
|
timeExpressionWrap := timeExpressionWrapper{Expression: expression}
|
||||||
|
timeExpressionWrap.timeInterfaceImpl.parent = &timeExpressionWrap
|
||||||
|
return &timeExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimeExp(expression Expression) TimeExpression {
|
||||||
|
return newTimeExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,3 +33,9 @@ func TestTimeExpressionGT_EQ(t *testing.T) {
|
||||||
assertClauseSerialize(t, table1ColTime.GT_EQ(table2ColTime), "(table1.colTime >= table2.colTime)")
|
assertClauseSerialize(t, table1ColTime.GT_EQ(table2ColTime), "(table1.colTime >= table2.colTime)")
|
||||||
assertClauseSerialize(t, table1ColTime.GT_EQ(Time(10, 20, 0, 0)), "(table1.colTime >= $1::time without time zone)", "10:20:00.000")
|
assertClauseSerialize(t, table1ColTime.GT_EQ(Time(10, 20, 0, 0)), "(table1.colTime >= $1::time without time zone)", "10:20:00.000")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTimeExp(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, TimeExp(table1ColFloat), "table1.colFloat")
|
||||||
|
assertClauseSerialize(t, TimeExp(table1ColFloat).LT(Time(1, 1, 1, 1)),
|
||||||
|
"(table1.colFloat < $1::time without time zone)", string("01:01:01.001"))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,20 @@ func (t *timestampInterfaceImpl) GT(rhs TimestampExpression) BoolExpression {
|
||||||
func (t *timestampInterfaceImpl) GT_EQ(rhs TimestampExpression) BoolExpression {
|
func (t *timestampInterfaceImpl) GT_EQ(rhs TimestampExpression) BoolExpression {
|
||||||
return GT_EQ(t.parent, rhs)
|
return GT_EQ(t.parent, rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------
|
||||||
|
|
||||||
|
type timestampExpressionWrapper struct {
|
||||||
|
timestampInterfaceImpl
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTimestampExpressionWrap(expression Expression) TimestampExpression {
|
||||||
|
timestampExpressionWrap := timestampExpressionWrapper{Expression: expression}
|
||||||
|
timestampExpressionWrap.timestampInterfaceImpl.parent = ×tampExpressionWrap
|
||||||
|
return ×tampExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimestampExp(expression Expression) TimestampExpression {
|
||||||
|
return newTimestampExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,20 @@ func (t *timestampzInterfaceImpl) GT(rhs TimestampzExpression) BoolExpression {
|
||||||
func (t *timestampzInterfaceImpl) GT_EQ(rhs TimestampzExpression) BoolExpression {
|
func (t *timestampzInterfaceImpl) GT_EQ(rhs TimestampzExpression) BoolExpression {
|
||||||
return GT_EQ(t.parent, rhs)
|
return GT_EQ(t.parent, rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------
|
||||||
|
|
||||||
|
type timestampzExpressionWrapper struct {
|
||||||
|
timestampzInterfaceImpl
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTimestampzExpressionWrap(expression Expression) TimestampzExpression {
|
||||||
|
timestampzExpressionWrap := timestampzExpressionWrapper{Expression: expression}
|
||||||
|
timestampzExpressionWrap.timestampzInterfaceImpl.parent = ×tampzExpressionWrap
|
||||||
|
return ×tampzExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimestampzExp(expression Expression) TimestampExpression {
|
||||||
|
return newTimestampExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,3 +67,20 @@ func newPrefixTimezExpression(operator string, expression Expression) TimezExpre
|
||||||
|
|
||||||
return &timeExpr
|
return &timeExpr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------//
|
||||||
|
|
||||||
|
type timezExpressionWrapper struct {
|
||||||
|
timezInterfaceImpl
|
||||||
|
Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTimezExpressionWrap(expression Expression) TimezExpression {
|
||||||
|
timezExpressionWrap := timezExpressionWrapper{Expression: expression}
|
||||||
|
timezExpressionWrap.timezInterfaceImpl.parent = &timezExpressionWrap
|
||||||
|
return &timezExpressionWrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimezExp(expression Expression) TimeExpression {
|
||||||
|
return newTimeExpressionWrap(expression)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -700,27 +700,6 @@ LIMIT 1000;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
func TestSubQuery(t *testing.T) {
|
func TestSubQuery(t *testing.T) {
|
||||||
|
|
||||||
//selectStmtTable := Actor.SELECT(Actor.FirstName, Actor.LastName).AsTable("table_expression")
|
|
||||||
//
|
|
||||||
//query := selectStmtTable.SELECT(
|
|
||||||
// selectStmtTable.RefStringColumn(Actor.FirstName).AS("nesto"),
|
|
||||||
// selectStmtTable.RefIntColumnName("actor.last_name").AS("nesto2"),
|
|
||||||
// )
|
|
||||||
//
|
|
||||||
//queryStr, args, err := query.Sql()
|
|
||||||
//
|
|
||||||
//assert.NilError(t, err)
|
|
||||||
//
|
|
||||||
//fmt.Println(queryStr)
|
|
||||||
//
|
|
||||||
//avrgCustomer := NumExp(Customer.SELECT(Customer.LastName).LIMIT(1))
|
|
||||||
//
|
|
||||||
//Customer.
|
|
||||||
// innerJoin(selectStmtTable, Customer.LastName.EQ(selectStmtTable.RefStringColumn(Actor.FirstName))).
|
|
||||||
// SELECT(Customer.AllColumns, selectStmtTable.RefIntColumnName("first_name")).
|
|
||||||
// WHERE(Actor.LastName.Neq(avrgCustomer))
|
|
||||||
|
|
||||||
expectedQuery := `
|
expectedQuery := `
|
||||||
SELECT actor.actor_id AS "actor.actor_id",
|
SELECT actor.actor_id AS "actor.actor_id",
|
||||||
actor.first_name AS "actor.first_name",
|
actor.first_name AS "actor.first_name",
|
||||||
|
|
@ -742,12 +721,20 @@ FROM dvds.actor
|
||||||
) AS films ON (film_actor.film_id = films."film.film_id");
|
) AS films ON (film_actor.film_id = films."film.film_id");
|
||||||
`
|
`
|
||||||
|
|
||||||
rFilmsOnly := Film.SELECT(Film.FilmID, Film.Title, Film.Rating).
|
rFilmsOnly := Film.
|
||||||
|
SELECT(
|
||||||
|
Film.FilmID,
|
||||||
|
Film.Title,
|
||||||
|
Film.Rating,
|
||||||
|
).
|
||||||
WHERE(Film.Rating.EQ(enum.MpaaRating.R)).
|
WHERE(Film.Rating.EQ(enum.MpaaRating.R)).
|
||||||
AsTable("films")
|
AsTable("films")
|
||||||
|
|
||||||
query := Actor.INNER_JOIN(FilmActor, Actor.ActorID.EQ(FilmActor.FilmID)).
|
rFilmId := rFilmsOnly.RefIntColumn(Film.FilmID)
|
||||||
INNER_JOIN(rFilmsOnly, FilmActor.FilmID.EQ(rFilmsOnly.RefIntColumn(Film.FilmID))).
|
|
||||||
|
query := Actor.
|
||||||
|
INNER_JOIN(FilmActor, Actor.ActorID.EQ(FilmActor.FilmID)).
|
||||||
|
INNER_JOIN(rFilmsOnly, FilmActor.FilmID.EQ(rFilmId)).
|
||||||
SELECT(
|
SELECT(
|
||||||
Actor.AllColumns,
|
Actor.AllColumns,
|
||||||
FilmActor.AllColumns,
|
FilmActor.AllColumns,
|
||||||
|
|
@ -806,11 +793,11 @@ FROM dvds.film
|
||||||
WHERE film.rental_rate = (
|
WHERE film.rental_rate = (
|
||||||
SELECT MAX(film.rental_rate)
|
SELECT MAX(film.rental_rate)
|
||||||
FROM dvds.film
|
FROM dvds.film
|
||||||
)
|
)::double precision
|
||||||
ORDER BY film.film_id ASC;
|
ORDER BY film.film_id ASC;
|
||||||
`
|
`
|
||||||
|
|
||||||
maxFilmRentalRate := NumExp(Film.SELECT(MAXf(Film.RentalRate)))
|
maxFilmRentalRate := Film.SELECT(MAXf(Film.RentalRate)).TO_DOUBLE()
|
||||||
|
|
||||||
query := Film.
|
query := Film.
|
||||||
SELECT(Film.AllColumns).
|
SELECT(Film.AllColumns).
|
||||||
|
|
@ -936,10 +923,14 @@ ORDER BY customer_payment_sum.amount_sum ASC;
|
||||||
|
|
||||||
customersPaymentTable := customersPaymentSubQuery.AsTable("customer_payment_sum")
|
customersPaymentTable := customersPaymentSubQuery.AsTable("customer_payment_sum")
|
||||||
amountSumColumn := customersPaymentTable.RefIntColumnName("amount_sum")
|
amountSumColumn := customersPaymentTable.RefIntColumnName("amount_sum")
|
||||||
|
customerId := customersPaymentTable.RefIntColumn(Payment.CustomerID)
|
||||||
|
|
||||||
query := Customer.
|
query := Customer.
|
||||||
INNER_JOIN(customersPaymentTable, Customer.CustomerID.EQ(customersPaymentTable.RefIntColumn(Payment.CustomerID))).
|
INNER_JOIN(customersPaymentTable, Customer.CustomerID.EQ(customerId)).
|
||||||
SELECT(Customer.AllColumns, amountSumColumn.AS("customer_with_amounts.amount_sum")).
|
SELECT(
|
||||||
|
Customer.AllColumns,
|
||||||
|
amountSumColumn.AS("customer_with_amounts.amount_sum"),
|
||||||
|
).
|
||||||
ORDER_BY(amountSumColumn.ASC())
|
ORDER_BY(amountSumColumn.ASC())
|
||||||
|
|
||||||
assertQuery(t, query, expectedSql)
|
assertQuery(t, query, expectedSql)
|
||||||
|
|
|
||||||
|
|
@ -34,15 +34,20 @@ func TestExpressionOperators(t *testing.T) {
|
||||||
AllTypes.SmallintPtr.NOT_IN(Int(11), Int(22), NULL),
|
AllTypes.SmallintPtr.NOT_IN(Int(11), Int(22), NULL),
|
||||||
AllTypes.SmallintPtr.NOT_IN(AllTypes.SELECT(AllTypes.IntegerPtr)),
|
AllTypes.SmallintPtr.NOT_IN(AllTypes.SELECT(AllTypes.IntegerPtr)),
|
||||||
|
|
||||||
String("TRUE").CAST_TO_BOOL(),
|
String("TRUE").TO_BOOL(),
|
||||||
String("111").CAST_TO_INTEGER(),
|
String("111").TO_SMALLINT(),
|
||||||
String("11.23").CAST_TO_DOUBLE(),
|
String("111").TO_INTEGER(),
|
||||||
Int(234).CAST_TO_TEXT(),
|
String("111").TO_BIGINT(),
|
||||||
String("1/8/1999").CAST_TO_DATE(),
|
String("11.23").TO_NUMERIC(30, 10),
|
||||||
String("04:05:06.789").CAST_TO_TIME(),
|
String("11.23").TO_NUMERIC(30),
|
||||||
String("04:05:06 PST").CAST_TO_TIMEZ(),
|
String("11.23").TO_REAL(),
|
||||||
String("1999-01-08 04:05:06").CAST_TO_TIMESTAMP(),
|
String("11.23").TO_DOUBLE(),
|
||||||
String("January 8 04:05:06 1999 PST").CAST_TO_TIMESTAMPZ(),
|
Int(234).TO_TEXT(),
|
||||||
|
String("1/8/1999").TO_DATE(),
|
||||||
|
String("04:05:06.789").TO_TIME(),
|
||||||
|
String("04:05:06 PST").TO_TIMEZ(),
|
||||||
|
String("1999-01-08 04:05:06").TO_TIMESTAMP(),
|
||||||
|
String("January 8 04:05:06 1999 PST").TO_TIMESTAMPZ(),
|
||||||
|
|
||||||
TO_CHAR(AllTypes.Timestamp, String("HH12:MI:SS")),
|
TO_CHAR(AllTypes.Timestamp, String("HH12:MI:SS")),
|
||||||
TO_CHAR(AllTypes.Integer, String("999")),
|
TO_CHAR(AllTypes.Integer, String("999")),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue