diff --git a/mysql/mysql_types.go b/mysql/mysql_types.go index acf9e07..b556003 100644 --- a/mysql/mysql_types.go +++ b/mysql/mysql_types.go @@ -74,3 +74,8 @@ var MINUSf = jet.MINUSf var BIT_NOT = jet.BIT_NOT var SELECT = jet.SELECT + +//-----------------literals----------------------// + +var STAR = jet.STAR +var NULL = jet.NULL diff --git a/tests/mysql/alltypes_test.go b/tests/mysql/alltypes_test.go index d877601..9f5ac12 100644 --- a/tests/mysql/alltypes_test.go +++ b/tests/mysql/alltypes_test.go @@ -27,6 +27,67 @@ func TestAllTypes(t *testing.T) { testutils.AssertJSON(t, dest, allTypesJson) } +func TestExpressionOperators(t *testing.T) { + query := AllTypes.SELECT( + AllTypes.Integer.IS_NULL().AS("result.is_null"), + AllTypes.DatePtr.IS_NOT_NULL().AS("result.is_not_null"), + AllTypes.SmallIntPtr.IN(Int(11), Int(22)).AS("result.in"), + AllTypes.SmallIntPtr.IN(AllTypes.SELECT(AllTypes.Integer)).AS("result.in_select"), + AllTypes.SmallIntPtr.NOT_IN(Int(11), Int(22), NULL).AS("result.not_in"), + AllTypes.SmallIntPtr.NOT_IN(AllTypes.SELECT(AllTypes.Integer)).AS("result.not_in_select"), + ).LIMIT(2) + + //fmt.Println(query.Sql()) + + testutils.AssertStatementSql(t, query, ` +SELECT all_types.integer IS NULL AS "result.is_null", + all_types.date_ptr IS NOT NULL AS "result.is_not_null", + (all_types.small_int_ptr IN (?, ?)) AS "result.in", + (all_types.small_int_ptr IN (( + SELECT all_types.integer AS "all_types.integer" + FROM test_sample.all_types + ))) AS "result.in_select", + (all_types.small_int_ptr NOT IN (?, ?, NULL)) AS "result.not_in", + (all_types.small_int_ptr NOT IN (( + SELECT all_types.integer AS "all_types.integer" + FROM test_sample.all_types + ))) AS "result.not_in_select" +FROM test_sample.all_types +LIMIT ?; +`, int64(11), int64(22), int64(11), int64(22), int64(2)) + + var dest []struct { + common.ExpressionTestResult `alias:"result.*"` + } + + err := query.Query(db, &dest) + + assert.NilError(t, err) + + //testutils.JsonPrint(dest) + + testutils.AssertJSON(t, dest, ` +[ + { + "IsNull": false, + "IsNotNull": true, + "In": false, + "InSelect": false, + "NotIn": null, + "NotInSelect": true + }, + { + "IsNull": false, + "IsNotNull": false, + "In": null, + "InSelect": null, + "NotIn": null, + "NotInSelect": null + } +] +`) +} + func TestBoolOperators(t *testing.T) { query := AllTypes.SELECT( AllTypes.Boolean.EQ(AllTypes.BooleanPtr).AS("EQ1"), diff --git a/tests/postgres/alltypes_test.go b/tests/postgres/alltypes_test.go index 558a5d0..73df9b7 100644 --- a/tests/postgres/alltypes_test.go +++ b/tests/postgres/alltypes_test.go @@ -58,13 +58,68 @@ func TestAllTypesInsertQuery(t *testing.T) { func TestExpressionOperators(t *testing.T) { query := AllTypes.SELECT( - AllTypes.Integer.IS_NULL(), - AllTypes.Timestamp.IS_NOT_NULL(), - AllTypes.SmallIntPtr.IN(Int(11), Int(22), NULL), - AllTypes.SmallIntPtr.IN(AllTypes.SELECT(AllTypes.IntegerPtr)), - AllTypes.SmallIntPtr.NOT_IN(Int(11), Int(22), NULL), - AllTypes.SmallIntPtr.NOT_IN(AllTypes.SELECT(AllTypes.IntegerPtr)), + AllTypes.Integer.IS_NULL().AS("result.is_null"), + AllTypes.DatePtr.IS_NOT_NULL().AS("result.is_not_null"), + AllTypes.SmallIntPtr.IN(Int(11), Int(22)).AS("result.in"), + AllTypes.SmallIntPtr.IN(AllTypes.SELECT(AllTypes.Integer)).AS("result.in_select"), + AllTypes.SmallIntPtr.NOT_IN(Int(11), Int(22), NULL).AS("result.not_in"), + AllTypes.SmallIntPtr.NOT_IN(AllTypes.SELECT(AllTypes.Integer)).AS("result.not_in_select"), + ).LIMIT(2) + //fmt.Println(query.Sql()) + + testutils.AssertStatementSql(t, query, ` +SELECT all_types.integer IS NULL AS "result.is_null", + all_types.date_ptr IS NOT NULL AS "result.is_not_null", + (all_types.small_int_ptr IN ($1, $2)) AS "result.in", + (all_types.small_int_ptr IN (( + SELECT all_types.integer AS "all_types.integer" + FROM test_sample.all_types + ))) AS "result.in_select", + (all_types.small_int_ptr NOT IN ($3, $4, NULL)) AS "result.not_in", + (all_types.small_int_ptr NOT IN (( + SELECT all_types.integer AS "all_types.integer" + FROM test_sample.all_types + ))) AS "result.not_in_select" +FROM test_sample.all_types +LIMIT $5; +`, int64(11), int64(22), int64(11), int64(22), int64(2)) + + var dest []struct { + common.ExpressionTestResult `alias:"result.*"` + } + + err := query.Query(db, &dest) + + assert.NilError(t, err) + + //testutils.JsonPrint(dest) + + testutils.AssertJSON(t, dest, ` +[ + { + "IsNull": false, + "IsNotNull": true, + "In": false, + "InSelect": false, + "NotIn": null, + "NotInSelect": true + }, + { + "IsNull": false, + "IsNotNull": false, + "In": null, + "InSelect": null, + "NotIn": null, + "NotInSelect": null + } +] +`) +} + +func TestExpressionCast(t *testing.T) { + + query := AllTypes.SELECT( postgres.CAST(Int(150)).AS_CHAR(12), postgres.CAST(String("TRUE")).AS_BOOL(), postgres.CAST(String("111")).AS_SMALLINT(), diff --git a/tests/testdata/common/common.go b/tests/testdata/common/common.go index 55f6554..60ebf7c 100644 --- a/tests/testdata/common/common.go +++ b/tests/testdata/common/common.go @@ -1,5 +1,14 @@ package common +type ExpressionTestResult struct { + IsNull *bool + IsNotNull *bool + In *bool + InSelect *bool + NotIn *bool + NotInSelect *bool +} + type EqualityExpResult struct { Eq1 *bool Eq2 *bool