diff --git a/sqlbuilder/func_expression.go b/sqlbuilder/func_expression.go index 25e0646..c4ed1a8 100644 --- a/sqlbuilder/func_expression.go +++ b/sqlbuilder/func_expression.go @@ -86,6 +86,20 @@ func newStringFunc(name string, expressions ...expression) StringExpression { return stringFunc } +type dateFunc struct { + funcExpressionImpl + dateInterfaceImpl +} + +func newDateFunc(name string, expressions ...expression) DateExpression { + dateFunc := &dateFunc{} + + dateFunc.funcExpressionImpl = *newFunc(name, expressions, dateFunc) + dateFunc.dateInterfaceImpl.parent = dateFunc + + return dateFunc +} + type boolFunc struct { funcExpressionImpl boolInterfaceImpl @@ -100,6 +114,20 @@ func newBoolFunc(name string, expressions ...expression) BoolExpression { return boolFunc } +type timestampzFunc struct { + funcExpressionImpl + timestampzInterfaceImpl +} + +func newTimestampzFunc(name string, expressions ...expression) TimestampzExpression { + timestampzFunc := ×tampzFunc{} + + timestampzFunc.funcExpressionImpl = *newFunc(name, expressions, timestampzFunc) + timestampzFunc.timestampzInterfaceImpl.parent = timestampzFunc + + return timestampzFunc +} + // ------------------ Mathematical functions ---------------// func ABSf(floatExpression FloatExpression) FloatExpression { @@ -336,3 +364,21 @@ func TO_ASCII(str StringExpression, encoding ...StringExpression) StringExpressi func TO_HEX(number IntegerExpression) StringExpression { return newStringFunc("TO_HEX", number) } + +//----------Data Type Formatting Functions ----------------------// + +func TO_CHAR(expression expression, text StringExpression) StringExpression { + return newStringFunc("TO_CHAR", expression, text) +} + +func TO_DATE(dateStr, format StringExpression) DateExpression { + return newDateFunc("TO_DATE", dateStr, format) +} + +func TO_NUMBER(floatStr, format StringExpression) FloatExpression { + return newFloatFunc("TO_NUMBER", floatStr, format) +} + +func TO_TIMESTAMP(timestampzStr, format StringExpression) TimestampzExpression { + return newTimestampzFunc("TO_TIMESTAMP", timestampzStr, format) +} diff --git a/tests/types_test.go b/tests/types_test.go index 0e894fd..de43514 100644 --- a/tests/types_test.go +++ b/tests/types_test.go @@ -25,6 +25,28 @@ func TestAllTypesSelect(t *testing.T) { assert.DeepEqual(t, dest[1], allTypesRow1) } +func TestExpressionOperators(t *testing.T) { + query := AllTypes.SELECT( + AllTypes.Integer.IS_NULL(), + AllTypes.Timestamp.IS_NOT_NULL(), + + TO_CHAR(AllTypes.Timestamp, String("HH12:MI:SS")), + TO_CHAR(AllTypes.Integer, String("999")), + TO_CHAR(AllTypes.DoublePrecision, String("999D9")), + TO_CHAR(AllTypes.Numeric, String("999D99S")), + + TO_DATE(String("05 Dec 2000"), String("DD Mon YYYY")), + TO_NUMBER(String("12,454"), String("99G999D9S")), + TO_TIMESTAMP(String("05 Dec 2000"), String("DD Mon YYYY")), + ) + + fmt.Println(query.DebugSql()) + + err := query.Query(db, &struct{}{}) + + assert.NilError(t, err) +} + func TestStringOperators(t *testing.T) { query := AllTypes.SELECT( AllTypes.Text.EQ(AllTypes.Character), @@ -87,19 +109,6 @@ func TestStringOperators(t *testing.T) { assert.NilError(t, err) } -func TestExpressionOperators(t *testing.T) { - query := AllTypes.SELECT( - AllTypes.Integer.IS_NULL(), - AllTypes.Timestamp.IS_NOT_NULL(), - ) - - fmt.Println(query.DebugSql()) - - err := query.Query(db, &struct{}{}) - - assert.NilError(t, err) -} - func TestBoolOperators(t *testing.T) { query := AllTypes.SELECT( AllTypes.Boolean.EQ(AllTypes.BooleanPtr),