MySQL int expressions.

This commit is contained in:
go-jet 2019-07-31 13:02:30 +02:00
parent 163ecf4c42
commit fcce8d4262
19 changed files with 654 additions and 168 deletions

View file

@ -28,6 +28,9 @@ func newMySQLDialect() Dialect {
mySQLDialect.SerializeOverrides["IS DISTINCT FROM"] = mysql_IS_DISTINCT_FROM mySQLDialect.SerializeOverrides["IS DISTINCT FROM"] = mysql_IS_DISTINCT_FROM
mySQLDialect.SerializeOverrides["IS NOT DISTINCT FROM"] = mysql_IS_NOT_DISTINCT_FROM mySQLDialect.SerializeOverrides["IS NOT DISTINCT FROM"] = mysql_IS_NOT_DISTINCT_FROM
mySQLDialect.SerializeOverrides["/"] = mysql_DIVISION
mySQLDialect.SerializeOverrides["#"] = mysql_BIT_XOR
mySQLDialect.AliasQuoteChar = '"' mySQLDialect.AliasQuoteChar = '"'
mySQLDialect.IdentifierQuoteChar = '"' mySQLDialect.IdentifierQuoteChar = '"'
mySQLDialect.ArgumentPlaceholder = func(int) string { mySQLDialect.ArgumentPlaceholder = func(int) string {
@ -63,6 +66,57 @@ func newDialect(name, packageName string) Dialect {
return newDialect return newDialect
} }
func mysql_BIT_XOR(expressions ...Expression) serializeFunc {
return func(statement statementType, out *sqlBuilder, options ...serializeOption) error {
if len(expressions) != 2 {
return errors.New("Invalid number of expressions for operator")
}
lhs := expressions[0]
rhs := expressions[1]
if err := lhs.serialize(statement, out, options...); err != nil {
return err
}
out.writeString("^")
if err := rhs.serialize(statement, out, options...); err != nil {
return err
}
return nil
}
}
func mysql_DIVISION(expressions ...Expression) serializeFunc {
return func(statement statementType, out *sqlBuilder, options ...serializeOption) error {
if len(expressions) != 2 {
return errors.New("Invalid number of expressions for operator")
}
lhs := expressions[0]
rhs := expressions[1]
if err := lhs.serialize(statement, out, options...); err != nil {
return err
}
_, isLhsInt := lhs.(IntegerExpression)
_, isRhsInt := rhs.(IntegerExpression)
if isLhsInt && isRhsInt {
out.writeString("DIV")
} else {
out.writeString("/")
}
if err := rhs.serialize(statement, out, options...); err != nil {
return err
}
return nil
}
}
func mysql_IS_NOT_DISTINCT_FROM(expressions ...Expression) serializeFunc { func mysql_IS_NOT_DISTINCT_FROM(expressions ...Expression) serializeFunc {
return func(statement statementType, out *sqlBuilder, options ...serializeOption) error { return func(statement statementType, out *sqlBuilder, options ...serializeOption) error {
if len(expressions) != 2 { if len(expressions) != 2 {

View file

@ -171,7 +171,8 @@ func (p *prefixOpExpression) serialize(statement statementType, out *sqlBuilder,
return errors.New("jet: Prefix Expression is nil") return errors.New("jet: Prefix Expression is nil")
} }
out.writeString(p.operator + " ") out.writeString("(")
out.writeString(p.operator)
if p.expression == nil { if p.expression == nil {
return errors.New("jet: nil prefix Expression") return errors.New("jet: nil prefix Expression")
@ -180,6 +181,8 @@ func (p *prefixOpExpression) serialize(statement statementType, out *sqlBuilder,
return err return err
} }
out.writeString(")")
return nil return nil
} }

View file

@ -81,7 +81,7 @@ func (n *floatInterfaceImpl) MOD(expression NumericExpression) FloatExpression {
} }
func (n *floatInterfaceImpl) POW(expression NumericExpression) FloatExpression { func (n *floatInterfaceImpl) POW(expression NumericExpression) FloatExpression {
return POWER(n.parent, expression) return POW(n.parent, expression)
} }
//---------------------------------------------------// //---------------------------------------------------//

View file

@ -62,6 +62,9 @@ func TestFloatExpressionMUL(t *testing.T) {
func TestFloatExpressionDIV(t *testing.T) { func TestFloatExpressionDIV(t *testing.T) {
assertPostgreClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)") assertPostgreClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)")
assertPostgreClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / $1)", float64(2.11)) assertPostgreClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / $1)", float64(2.11))
assertMySQLClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)")
assertMySQLClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / ?)", float64(2.11))
} }
func TestFloatExpressionMOD(t *testing.T) { func TestFloatExpressionMOD(t *testing.T) {
@ -70,8 +73,8 @@ func TestFloatExpressionMOD(t *testing.T) {
} }
func TestFloatExpressionPOW(t *testing.T) { func TestFloatExpressionPOW(t *testing.T) {
assertPostgreClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "POWER(table1.col_float, table2.col_float)") assertPostgreClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "POW(table1.col_float, table2.col_float)")
assertPostgreClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "POWER(table1.col_float, $1)", float64(2.11)) assertPostgreClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "POW(table1.col_float, $1)", float64(2.11))
} }
func TestFloatExp(t *testing.T) { func TestFloatExp(t *testing.T) {

View file

@ -19,6 +19,10 @@ func ABSi(integerExpression IntegerExpression) IntegerExpression {
return newIntegerFunc("ABS", integerExpression) return newIntegerFunc("ABS", integerExpression)
} }
func POW(base, exponent NumericExpression) FloatExpression {
return NewFloatFunc("POW", base, exponent)
}
func POWER(base, exponent NumericExpression) FloatExpression { func POWER(base, exponent NumericExpression) FloatExpression {
return NewFloatFunc("POWER", base, exponent) return NewFloatFunc("POWER", base, exponent)
} }

View file

@ -106,7 +106,7 @@ func (i *integerInterfaceImpl) MOD(expression IntegerExpression) IntegerExpressi
} }
func (i *integerInterfaceImpl) POW(expression IntegerExpression) IntegerExpression { func (i *integerInterfaceImpl) POW(expression IntegerExpression) IntegerExpression {
return newBinaryIntegerExpression(i.parent, expression, "^") return IntExp(POW(i.parent, expression))
} }
func (i *integerInterfaceImpl) BIT_AND(expression IntegerExpression) IntegerExpression { func (i *integerInterfaceImpl) BIT_AND(expression IntegerExpression) IntegerExpression {
@ -166,6 +166,24 @@ func newPrefixIntegerOperator(expression IntegerExpression, operator string) Int
return &integerExpression return &integerExpression
} }
//---------------------------------------------------//
type prefixFloatOpExpression struct {
expressionInterfaceImpl
floatInterfaceImpl
prefixOpExpression
}
func newPrefixFloatOperator(expression FloatExpression, operator string) FloatExpression {
floatOpExpression := prefixFloatOpExpression{}
floatOpExpression.prefixOpExpression = newPrefixExpression(expression, operator)
floatOpExpression.expressionInterfaceImpl.parent = &floatOpExpression
floatOpExpression.floatInterfaceImpl.parent = &floatOpExpression
return &floatOpExpression
}
//---------------------------------------------------// //---------------------------------------------------//
type integerExpressionWrapper struct { type integerExpressionWrapper struct {
integerInterfaceImpl integerInterfaceImpl

View file

@ -52,6 +52,9 @@ func TestIntegerExpressionMUL(t *testing.T) {
func TestIntegerExpressionDIV(t *testing.T) { func TestIntegerExpressionDIV(t *testing.T) {
assertPostgreClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int / table2.col_int)") assertPostgreClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int / table2.col_int)")
assertPostgreClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int / $1)", int64(11)) assertPostgreClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int / $1)", int64(11))
assertMySQLClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int DIV table2.col_int)")
assertMySQLClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int DIV ?)", int64(11))
} }
func TestIntExpressionMOD(t *testing.T) { func TestIntExpressionMOD(t *testing.T) {
@ -60,13 +63,37 @@ func TestIntExpressionMOD(t *testing.T) {
} }
func TestIntExpressionPOW(t *testing.T) { func TestIntExpressionPOW(t *testing.T) {
assertPostgreClauseSerialize(t, table1ColInt.POW(table2ColInt), "(table1.col_int ^ table2.col_int)") assertPostgreClauseSerialize(t, table1ColInt.POW(table2ColInt), "POW(table1.col_int, table2.col_int)")
assertPostgreClauseSerialize(t, table1ColInt.POW(Int(11)), "(table1.col_int ^ $1)", int64(11)) assertPostgreClauseSerialize(t, table1ColInt.POW(Int(11)), "POW(table1.col_int, $1)", int64(11))
} }
func TestIntExpressionBIT_NOT(t *testing.T) { func TestIntExpressionBIT_NOT(t *testing.T) {
assertPostgreClauseSerialize(t, BIT_NOT(table2ColInt), "~ table2.col_int") assertPostgreClauseSerialize(t, BIT_NOT(table2ColInt), "(~ table2.col_int)")
assertPostgreClauseSerialize(t, BIT_NOT(Int(11)), "~ $1", int64(11)) assertPostgreClauseSerialize(t, BIT_NOT(Int(11)), "(~ $1)", int64(11))
}
func TestIntExpressionBIT_AND(t *testing.T) {
assertPostgreClauseSerialize(t, table1ColInt.BIT_AND(table2ColInt), "(table1.col_int & table2.col_int)")
assertPostgreClauseSerialize(t, table1ColInt.BIT_AND(Int(11)), "(table1.col_int & $1)", int64(11))
assertMySQLClauseSerialize(t, table1ColInt.BIT_AND(table2ColInt), "(table1.col_int & table2.col_int)")
assertMySQLClauseSerialize(t, table1ColInt.BIT_AND(Int(11)), "(table1.col_int & ?)", int64(11))
}
func TestIntExpressionBIT_OR(t *testing.T) {
assertPostgreClauseSerialize(t, table1ColInt.BIT_OR(table2ColInt), "(table1.col_int | table2.col_int)")
assertPostgreClauseSerialize(t, table1ColInt.BIT_OR(Int(11)), "(table1.col_int | $1)", int64(11))
assertMySQLClauseSerialize(t, table1ColInt.BIT_OR(table2ColInt), "(table1.col_int | table2.col_int)")
assertMySQLClauseSerialize(t, table1ColInt.BIT_OR(Int(11)), "(table1.col_int | ?)", int64(11))
}
func TestIntExpressionBIT_XOR(t *testing.T) {
assertPostgreClauseSerialize(t, table1ColInt.BIT_XOR(table2ColInt), "(table1.col_int # table2.col_int)")
assertPostgreClauseSerialize(t, table1ColInt.BIT_XOR(Int(11)), "(table1.col_int # $1)", int64(11))
assertMySQLClauseSerialize(t, table1ColInt.BIT_XOR(table2ColInt), "(table1.col_int ^ table2.col_int)")
assertMySQLClauseSerialize(t, table1ColInt.BIT_XOR(Int(11)), "(table1.col_int ^ ?)", int64(11))
} }
func TestIntExpressionBIT_SHIFT_LEFT(t *testing.T) { func TestIntExpressionBIT_SHIFT_LEFT(t *testing.T) {
@ -84,3 +111,8 @@ func TestIntExpressionIntExp(t *testing.T) {
assertPostgreClauseSerialize(t, IntExp(table1ColFloat.ADD(table2ColFloat)).ADD(Int(11)), assertPostgreClauseSerialize(t, IntExp(table1ColFloat.ADD(table2ColFloat)).ADD(Int(11)),
"((table1.col_float + table2.col_float) + $1)", int64(11)) "((table1.col_float + table2.col_float) + $1)", int64(11))
} }
func TestIntExpression_MINUSi(t *testing.T) {
assertPostgreClauseSerialize(t, MINUSi(table2ColInt), "(- table2.col_int)")
assertPostgreClauseSerialize(t, MINUSi(Int(3)), "(- $1)", int64(3))
}

View file

@ -46,8 +46,8 @@ func AssertJSONFile(t *testing.T, jsonFilePath string, data interface{}) {
jsonData, err := json.MarshalIndent(data, "", "\t") jsonData, err := json.MarshalIndent(data, "", "\t")
assert.NilError(t, err) assert.NilError(t, err)
assert.Assert(t, string(fileJSONData) == string(jsonData)) //assert.Assert(t, string(fileJSONData) == string(jsonData))
//assert.DeepEqual(t, string(fileJSONData), string(jsonData)) assert.DeepEqual(t, string(fileJSONData), string(jsonData))
} }
func AssertStatementSql(t *testing.T, query jet.Statement, expectedQuery string, expectedArgs ...interface{}) { func AssertStatementSql(t *testing.T, query jet.Statement, expectedQuery string, expectedArgs ...interface{}) {
@ -55,6 +55,9 @@ func AssertStatementSql(t *testing.T, query jet.Statement, expectedQuery string,
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, queryStr, expectedQuery) assert.Equal(t, queryStr, expectedQuery)
if len(expectedArgs) == 0 {
return
}
assert.DeepEqual(t, args, expectedArgs) assert.DeepEqual(t, args, expectedArgs)
} }

View file

@ -41,10 +41,13 @@ type integerLiteralExpression struct {
} }
// Int is constructor for integer expressions literals. // Int is constructor for integer expressions literals.
func Int(value int64) IntegerExpression { func Int(value int64, constant ...bool) IntegerExpression {
numLiteral := &integerLiteralExpression{} numLiteral := &integerLiteralExpression{}
numLiteral.literalExpression = *literal(value) numLiteral.literalExpression = *literal(value)
if len(constant) > 0 && constant[0] == true {
numLiteral.constant = true
}
numLiteral.literalExpression.parent = numLiteral numLiteral.literalExpression.parent = numLiteral
numLiteral.integerInterfaceImpl.parent = numLiteral numLiteral.integerInterfaceImpl.parent = numLiteral

View file

@ -59,3 +59,7 @@ var TRUNC = TRUNCATE
var TRUNCATE = func(floatExpression jet.FloatExpression, precision jet.IntegerExpression) jet.FloatExpression { var TRUNCATE = func(floatExpression jet.FloatExpression, precision jet.IntegerExpression) jet.FloatExpression {
return jet.NewFloatFunc("TRUNCATE", floatExpression, precision) return jet.NewFloatFunc("TRUNCATE", floatExpression, precision)
} }
var MINUSi = jet.MINUSi
var MINUSf = jet.MINUSf
var BIT_NOT = jet.BIT_NOT

View file

@ -2,6 +2,18 @@ package jet
import "errors" import "errors"
// --------- Arithmetic operators -------------//
// MINUSi changes the sign of the intExp.
func MINUSi(intExp IntegerExpression) IntegerExpression {
return newPrefixIntegerOperator(intExp, "-")
}
// MINUSi changes the sign of the intExp.
func MINUSf(floatExp FloatExpression) FloatExpression {
return newPrefixFloatOperator(floatExp, "-")
}
//----------- Logical operators ---------------// //----------- Logical operators ---------------//
// NOT returns negation of bool expression result // NOT returns negation of bool expression result

View file

@ -5,10 +5,10 @@ import "testing"
func TestOperatorNOT(t *testing.T) { func TestOperatorNOT(t *testing.T) {
notExpression := NOT(Int(2).EQ(Int(1))) notExpression := NOT(Int(2).EQ(Int(1)))
assertPostgreClauseSerialize(t, NOT(table1ColBool), "NOT table1.col_bool") assertPostgreClauseSerialize(t, NOT(table1ColBool), "(NOT table1.col_bool)")
assertPostgreClauseSerialize(t, notExpression, "NOT ($1 = $2)", int64(2), int64(1)) assertPostgreClauseSerialize(t, notExpression, "(NOT ($1 = $2))", int64(2), int64(1))
assertProjectionSerialize(t, notExpression.AS("alias_not_expression"), `NOT ($1 = $2) AS "alias_not_expression"`, int64(2), int64(1)) assertProjectionSerialize(t, notExpression.AS("alias_not_expression"), `(NOT ($1 = $2)) AS "alias_not_expression"`, int64(2), int64(1))
assertPostgreClauseSerialize(t, notExpression.AND(Int(4).EQ(Int(5))), `(NOT ($1 = $2) AND ($3 = $4))`, int64(2), int64(1), int64(4), int64(5)) assertPostgreClauseSerialize(t, notExpression.AND(Int(4).EQ(Int(5))), `((NOT ($1 = $2)) AND ($3 = $4))`, int64(2), int64(1), int64(4), int64(5))
} }
func TestCase1(t *testing.T) { func TestCase1(t *testing.T) {

View file

@ -11,37 +11,37 @@ CREATE TABLE `all_types` (
`boolean` BOOLEAN NOT NULL, `boolean` BOOLEAN NOT NULL,
`boolean_ptr` BOOLEAN, `boolean_ptr` BOOLEAN,
`tiny_int` TINYINT NOT NULL, `tiny_int` TINYINT NOT NULL,
`utiny_int` TINYINT unsigned NOT NULL, `u_tiny_int` TINYINT unsigned NOT NULL,
`small_int` SMALLINT NOT NULL, `small_int` SMALLINT NOT NULL,
`usmall_int` SMALLINT unsigned NOT NULL, `u_small_int` SMALLINT unsigned NOT NULL,
`medium_int` MEDIUMINT NOT NULL, `medium_int` MEDIUMINT NOT NULL,
`umedium_int` MEDIUMINT unsigned NOT NULL, `u_medium_int` MEDIUMINT unsigned NOT NULL,
`integer` INT NOT NULL, `integer` INT NOT NULL,
`uinteger` INT unsigned NOT NULL, `u_integer` INT unsigned NOT NULL,
`big_int` bigint(20) NOT NULL, `big_int` bigint(20) NOT NULL,
`ubig_int` bigint(20) unsigned NOT NULL, `u_big_int` bigint(20) unsigned NOT NULL,
-- ptr -- ptr
`tiny_int_ptr` TINYINT, `tiny_int_ptr` TINYINT,
`utiny_int_ptr` TINYINT unsigned, `u_tiny_int_ptr` TINYINT unsigned,
`small_int_ptr` SMALLINT, `small_int_ptr` SMALLINT,
`usmall_int_ptr` SMALLINT unsigned, `u_small_int_ptr` SMALLINT unsigned,
`medium_int_ptr` MEDIUMINT, `medium_int_ptr` MEDIUMINT,
`umedium_int_ptr` MEDIUMINT unsigned, `u_medium_int_ptr` MEDIUMINT unsigned,
`int_ptr` INT, `integer_ptr` INT,
`uint_ptr` INT unsigned, `u_integer_ptr` INT unsigned,
`big_int_ptr` bigint(20), `big_int_ptr` bigint(20),
`ubig_int_ptr` bigint(20) unsigned, `u_big_int_ptr` bigint(20) unsigned,
-- floats -- floats
@ -114,12 +114,12 @@ CREATE TABLE `all_types` (
INSERT INTO `all_types` VALUES INSERT INTO `all_types` VALUES
(false, true, (false, true,
-3,3,-14,14,-150,150,-1600,1600,-17000,17000, -3,3,14,14,-150,150,-1600,1600,5000,50000,
-3,3,-14,14,-150,150,-1600,1600,-17000,17000, -3,3,14,14,-150,150,-1600,1600,50000,50000,
1.11,1.11,2.22,2.22,3.33,3.33,4.44,4.44,5.55,5.55, 1.11,1.11,2.22,2.22,3.33,3.33,4.44,4.44,5.55,5.55,
_binary '\0',_binary '\0','2008-07-04','2008-07-04','2011-12-18 13:17:17','2011-12-18 13:17:17','2007-12-31 23:00:01','2007-12-31 23:00:01',2004,2004,'char','char','varchar','varchar',_binary 'binary\0\0\0\0\0\0\0\0\0\0\0\0\0\0',_binary 'binary\0\0\0\0\0\0\0\0\0\0\0\0\0\0',_binary 'varbinary',_binary 'varbinary',_binary 'blob',_binary 'blob','text','text','value1','value1','s1','s2','{\"key1\": \"value1\", \"key2\": \"value2\"}','{\"key1\": \"value1\", \"key2\": \"value2\"}'), _binary '\0',_binary '\0','2008-07-04','2008-07-04','2011-12-18 13:17:17','2011-12-18 13:17:17','2007-12-31 23:00:01','2007-12-31 23:00:01',2004,2004,'char','char','varchar','varchar',_binary 'binary\0\0\0\0\0\0\0\0\0\0\0\0\0\0',_binary 'binary\0\0\0\0\0\0\0\0\0\0\0\0\0\0',_binary 'varbinary',_binary 'varbinary',_binary 'blob',_binary 'blob','text','text','value1','value1','s1','s2','{\"key1\": \"value1\", \"key2\": \"value2\"}','{\"key1\": \"value1\", \"key2\": \"value2\"}'),
(false, NULL, (false, NULL,
-3,3,-14,14,-150,150,-1600,1600,-17000,17000, -3,3,14,14,-150,150,-1600,1600,5000,50000,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
1.11,NULL,2.22,NULL,3.33,NULL,4.44,NULL,5.55,NULL, 1.11,NULL,2.22,NULL,3.33,NULL,4.44,NULL,5.55,NULL,
_binary '\0',NULL,'2008-07-04',NULL,'2011-12-18 13:17:17',NULL,'2007-12-31 23:00:01',NULL,2004,NULL,'char',NULL,'varchar',NULL,_binary 'binary\0\0\0\0\0\0\0\0\0\0\0\0\0\0',NULL,_binary 'varbinary',NULL,_binary 'blob',NULL,'text',NULL,'value1',NULL,'s1',NULL,'{\"key1\": \"value1\", \"key2\": \"value2\"}',NULL); _binary '\0',NULL,'2008-07-04',NULL,'2011-12-18 13:17:17',NULL,'2007-12-31 23:00:01',NULL,2004,NULL,'char',NULL,'varchar',NULL,_binary 'binary\0\0\0\0\0\0\0\0\0\0\0\0\0\0',NULL,_binary 'varbinary',NULL,_binary 'blob',NULL,'text',NULL,'value1',NULL,'s1',NULL,'{\"key1\": \"value1\", \"key2\": \"value2\"}',NULL);

View file

@ -8,12 +8,12 @@ DROP TABLE IF EXISTS test_sample.all_types;
CREATE TABLE test_sample.ALL_TYPES CREATE TABLE test_sample.ALL_TYPES
( (
-- numeric -- numeric
smallint_ptr smallint, small_int_ptr smallint,
smallint smallint NOT NULL, small_int smallint NOT NULL,
integer_ptr integer, integer_ptr integer,
integer integer NOT NULL, integer integer NOT NULL,
bigint_ptr bigint, big_int_ptr bigint,
bigint bigint NOT NULL, big_int bigint NOT NULL,
decimal_ptr decimal(10, 2), decimal_ptr decimal(10, 2),
decimal decimal(10, 2) NOT NULL, decimal decimal(10, 2) NOT NULL,
numeric_ptr numeric(20, 3), numeric_ptr numeric(20, 3),
@ -97,7 +97,7 @@ CREATE TABLE test_sample.ALL_TYPES
); );
INSERT INTO test_sample.ALL_types( INSERT INTO test_sample.ALL_types(
smallint_ptr, "smallint", integer_ptr, "integer", bigint_ptr, "bigint", decimal_ptr, "decimal", numeric_ptr, "numeric", real_ptr, "real", double_precision_ptr, double_precision, smallserial, serial, bigserial, small_int_ptr, "small_int", integer_ptr, "integer", big_int_ptr, "big_int", decimal_ptr, "decimal", numeric_ptr, "numeric", real_ptr, "real", double_precision_ptr, double_precision, smallserial, serial, bigserial,
-- money_ptr, money, -- money_ptr, money,
character_varying_ptr, character_varying, character_ptr, "character", text_ptr, text, character_varying_ptr, character_varying, character_ptr, "character", text_ptr, text,
bytea_ptr, bytea, bytea_ptr, bytea,
@ -110,7 +110,7 @@ INSERT INTO test_sample.ALL_types(
xml_ptr, xml, xml_ptr, xml,
json_ptr, json, jsonb_ptr, jsonb, json_ptr, json, jsonb_ptr, jsonb,
integer_array_ptr, integer_array, text_array_ptr, text_array, jsonb_array, text_multi_dim_array_ptr, text_multi_dim_array) integer_array_ptr, integer_array, text_array_ptr, text_array, jsonb_array, text_multi_dim_array_ptr, text_multi_dim_array)
VALUES (1, 1, 300, 300, 50000, 5000, 1.11, 1.11, 2.22, 2.22, 5.55, 5.55, 11111111.22, 11111111.22, DEFAULT, DEFAULT, DEFAULT, VALUES (14, 14, 300, 300, 50000, 5000, 1.11, 1.11, 2.22, 2.22, 5.55, 5.55, 11111111.22, 11111111.22, DEFAULT, DEFAULT, DEFAULT,
-- 100000, 100000, -- 100000, 100000,
'ABBA', 'ABBA', 'JOHN', 'JOHN', 'Some text', 'Some text', 'ABBA', 'ABBA', 'JOHN', 'JOHN', 'Some text', 'Some text',
'bytea', 'bytea', 'bytea', 'bytea',
@ -124,7 +124,7 @@ VALUES (1, 1, 300, 300, 50000, 5000, 1.11, 1.11, 2.22, 2.22, 5.55, 5.55, 1111111
'{"a": 1, "b": 3}', '{"a": 1, "b": 3}', '{"a": 1, "b": 3}', '{"a": 1, "b": 3}', '{"a": 1, "b": 3}', '{"a": 1, "b": 3}', '{"a": 1, "b": 3}', '{"a": 1, "b": 3}',
'{1, 2, 3}', '{1, 2, 3}', '{"breakfast", "consulting"}', '{"breakfast", "consulting"}', ARRAY['{"a": 1, "b": 2}'::jsonb, '{"a":3, "b": 4}'::jsonb], '{{"meeting", "lunch"}, {"training", "presentation"}}', '{{"meeting", "lunch"}, {"training", "presentation"}}') '{1, 2, 3}', '{1, 2, 3}', '{"breakfast", "consulting"}', '{"breakfast", "consulting"}', ARRAY['{"a": 1, "b": 2}'::jsonb, '{"a":3, "b": 4}'::jsonb], '{{"meeting", "lunch"}, {"training", "presentation"}}', '{{"meeting", "lunch"}, {"training", "presentation"}}')
, ,
(NULL, 1, NULL, 300, NULL, 5000, NULL, 1.11, NULL, 2.22, NULL, 5.55, NULL, 11111111.22, DEFAULT, DEFAULT, DEFAULT, (NULL, 14, NULL, 300, NULL, 5000, NULL, 1.11, NULL, 2.22, NULL, 5.55, NULL, 11111111.22, DEFAULT, DEFAULT, DEFAULT,
-- NULL, 100000, -- NULL, 100000,
NULL, 'ABBA', NULL, 'JOHN', NULL, 'Some text', NULL, 'ABBA', NULL, 'JOHN', NULL, 'Some text',
NULL, 'bytea', NULL, 'bytea',

View file

@ -167,8 +167,8 @@ SELECT (all_types.numeric = all_types.numeric) AS "eq1",
TRUNCATE((all_types.decimal / ?), ?) AS "div2", TRUNCATE((all_types.decimal / ?), ?) AS "div2",
TRUNCATE((all_types.decimal % all_types.decimal_ptr), ?) AS "mod1", TRUNCATE((all_types.decimal % all_types.decimal_ptr), ?) AS "mod1",
TRUNCATE((all_types.decimal % ?), ?) AS "mod2", TRUNCATE((all_types.decimal % ?), ?) AS "mod2",
TRUNCATE(POWER(all_types.decimal, all_types.decimal_ptr), ?) AS "pow1", TRUNCATE(POW(all_types.decimal, all_types.decimal_ptr), ?) AS "pow1",
TRUNCATE(POWER(all_types.decimal, ?), ?) AS "pow2", TRUNCATE(POW(all_types.decimal, ?), ?) AS "pow2",
TRUNCATE(ABS(all_types.decimal), ?) AS "abs", TRUNCATE(ABS(all_types.decimal), ?) AS "abs",
TRUNCATE(POWER(all_types.decimal, ?), ?) AS "power", TRUNCATE(POWER(all_types.decimal, ?), ?) AS "power",
TRUNCATE(SQRT(all_types.decimal), ?) AS "sqrt", TRUNCATE(SQRT(all_types.decimal), ?) AS "sqrt",
@ -194,31 +194,170 @@ LIMIT ?;
testutils.AssertJSONFile(t, "./testdata/common/float_operators.json", dest) testutils.AssertJSONFile(t, "./testdata/common/float_operators.json", dest)
} }
func TestIntegerOperators(t *testing.T) {
query := AllTypes.SELECT(
AllTypes.BigInt,
AllTypes.BigIntPtr,
AllTypes.SmallInt,
AllTypes.SmallIntPtr,
AllTypes.BigInt.EQ(AllTypes.BigInt).AS("eq1"),
AllTypes.BigInt.EQ(Int(12)).AS("eq2"),
AllTypes.BigInt.NOT_EQ(AllTypes.BigIntPtr).AS("neq1"),
AllTypes.BigInt.NOT_EQ(Int(12)).AS("neq2"),
AllTypes.BigInt.IS_DISTINCT_FROM(AllTypes.BigInt).AS("distinct1"),
AllTypes.BigInt.IS_DISTINCT_FROM(Int(12)).AS("distinct2"),
AllTypes.BigInt.IS_NOT_DISTINCT_FROM(AllTypes.BigInt).AS("not distinct1"),
AllTypes.BigInt.IS_NOT_DISTINCT_FROM(Int(12)).AS("not distinct2"),
AllTypes.BigInt.LT(AllTypes.BigIntPtr).AS("lt1"),
AllTypes.BigInt.LT(Int(65)).AS("lt2"),
AllTypes.BigInt.LT_EQ(AllTypes.BigIntPtr).AS("lte1"),
AllTypes.BigInt.LT_EQ(Int(65)).AS("lte2"),
AllTypes.BigInt.GT(AllTypes.BigIntPtr).AS("gt1"),
AllTypes.BigInt.GT(Int(65)).AS("gt2"),
AllTypes.BigInt.GT_EQ(AllTypes.BigIntPtr).AS("gte1"),
AllTypes.BigInt.GT_EQ(Int(65)).AS("gte2"),
AllTypes.BigInt.ADD(AllTypes.BigInt).AS("add1"),
AllTypes.BigInt.ADD(Int(11)).AS("add2"),
AllTypes.BigInt.SUB(AllTypes.BigInt).AS("sub1"),
AllTypes.BigInt.SUB(Int(11)).AS("sub2"),
AllTypes.BigInt.MUL(AllTypes.BigInt).AS("mul1"),
AllTypes.BigInt.MUL(Int(11)).AS("mul2"),
AllTypes.BigInt.DIV(AllTypes.BigInt).AS("div1"),
AllTypes.BigInt.DIV(Int(11)).AS("div2"),
AllTypes.BigInt.MOD(AllTypes.BigInt).AS("mod1"),
AllTypes.BigInt.MOD(Int(11)).AS("mod2"),
AllTypes.SmallInt.POW(AllTypes.SmallInt.DIV(Int(3))).AS("pow1"),
AllTypes.SmallInt.POW(Int(6)).AS("pow2"),
AllTypes.SmallInt.BIT_AND(AllTypes.SmallInt).AS("bit_and1"),
AllTypes.SmallInt.BIT_AND(AllTypes.SmallInt).AS("bit_and2"),
AllTypes.SmallInt.BIT_OR(AllTypes.SmallInt).AS("bit or 1"),
AllTypes.SmallInt.BIT_OR(Int(22)).AS("bit or 2"),
AllTypes.SmallInt.BIT_XOR(AllTypes.SmallInt).AS("bit xor 1"),
AllTypes.SmallInt.BIT_XOR(Int(11)).AS("bit xor 2"),
BIT_NOT(MINUSi(AllTypes.SmallInt)).AS("bit_not_1"),
BIT_NOT(MINUSi(Int(11, true))).AS("bit_not_2"),
AllTypes.SmallInt.BIT_SHIFT_LEFT(AllTypes.SmallInt.DIV(Int(2))).AS("bit shift left 1"),
AllTypes.SmallInt.BIT_SHIFT_LEFT(Int(4)).AS("bit shift left 2"),
AllTypes.SmallInt.BIT_SHIFT_RIGHT(AllTypes.SmallInt.DIV(Int(5))).AS("bit shift right 1"),
AllTypes.SmallInt.BIT_SHIFT_RIGHT(Int(1)).AS("bit shift right 2"),
ABSi(AllTypes.BigInt).AS("abs"),
SQRT(ABSi(AllTypes.BigInt)).AS("sqrt"),
CBRT(ABSi(AllTypes.BigInt)).AS("cbrt"),
).LIMIT(2)
//fmt.Println(query.Sql())
testutils.AssertStatementSql(t, query, `
SELECT all_types.big_int AS "all_types.big_int",
all_types.big_int_ptr AS "all_types.big_int_ptr",
all_types.small_int AS "all_types.small_int",
all_types.small_int_ptr AS "all_types.small_int_ptr",
(all_types.big_int = all_types.big_int) AS "eq1",
(all_types.big_int = ?) AS "eq2",
(all_types.big_int != all_types.big_int_ptr) AS "neq1",
(all_types.big_int != ?) AS "neq2",
(NOT all_types.big_int <=> all_types.big_int) AS "distinct1",
(NOT all_types.big_int <=> ?) AS "distinct2",
(all_types.big_int <=> all_types.big_int) AS "not distinct1",
(all_types.big_int <=> ?) AS "not distinct2",
(all_types.big_int < all_types.big_int_ptr) AS "lt1",
(all_types.big_int < ?) AS "lt2",
(all_types.big_int <= all_types.big_int_ptr) AS "lte1",
(all_types.big_int <= ?) AS "lte2",
(all_types.big_int > all_types.big_int_ptr) AS "gt1",
(all_types.big_int > ?) AS "gt2",
(all_types.big_int >= all_types.big_int_ptr) AS "gte1",
(all_types.big_int >= ?) AS "gte2",
(all_types.big_int + all_types.big_int) AS "add1",
(all_types.big_int + ?) AS "add2",
(all_types.big_int - all_types.big_int) AS "sub1",
(all_types.big_int - ?) AS "sub2",
(all_types.big_int * all_types.big_int) AS "mul1",
(all_types.big_int * ?) AS "mul2",
(all_types.big_int DIV all_types.big_int) AS "div1",
(all_types.big_int DIV ?) AS "div2",
(all_types.big_int % all_types.big_int) AS "mod1",
(all_types.big_int % ?) AS "mod2",
POW(all_types.small_int, (all_types.small_int DIV ?)) AS "pow1",
POW(all_types.small_int, ?) AS "pow2",
(all_types.small_int & all_types.small_int) AS "bit_and1",
(all_types.small_int & all_types.small_int) AS "bit_and2",
(all_types.small_int | all_types.small_int) AS "bit or 1",
(all_types.small_int | ?) AS "bit or 2",
(all_types.small_int ^ all_types.small_int) AS "bit xor 1",
(all_types.small_int ^ ?) AS "bit xor 2",
(~ (- all_types.small_int)) AS "bit_not_1",
(~ (- 11)) AS "bit_not_2",
(all_types.small_int << (all_types.small_int DIV ?)) AS "bit shift left 1",
(all_types.small_int << ?) AS "bit shift left 2",
(all_types.small_int >> (all_types.small_int DIV ?)) AS "bit shift right 1",
(all_types.small_int >> ?) AS "bit shift right 2",
ABS(all_types.big_int) AS "abs",
SQRT(ABS(all_types.big_int)) AS "sqrt",
POWER(ABS(all_types.big_int), (? / ?)) AS "cbrt"
FROM test_sample.all_types
LIMIT ?;
`)
var dest []struct {
common.AllTypesIntegerExpResult `alias:"."`
}
err := query.Query(db, &dest)
assert.NilError(t, err)
//testutils.JsonPrint(dest)
testutils.AssertJSONFile(t, "./testdata/common/int_operators.json", dest)
}
var allTypesJson = ` var allTypesJson = `
[ [
{ {
"Boolean": false, "Boolean": false,
"BooleanPtr": true, "BooleanPtr": true,
"TinyInt": -3, "TinyInt": -3,
"UtinyInt": 3, "UTinyInt": 3,
"SmallInt": -14, "SmallInt": 14,
"UsmallInt": 14, "USmallInt": 14,
"MediumInt": -150, "MediumInt": -150,
"UmediumInt": 150, "UMediumInt": 150,
"Integer": -1600, "Integer": -1600,
"Uinteger": 1600, "UInteger": 1600,
"BigInt": -17000, "BigInt": 5000,
"UbigInt": 17000, "UBigInt": 50000,
"TinyIntPtr": -3, "TinyIntPtr": -3,
"UtinyIntPtr": 3, "UTinyIntPtr": 3,
"SmallIntPtr": -14, "SmallIntPtr": 14,
"UsmallIntPtr": 14, "USmallIntPtr": 14,
"MediumIntPtr": -150, "MediumIntPtr": -150,
"UmediumIntPtr": 150, "UMediumIntPtr": 150,
"IntPtr": -1600, "IntegerPtr": -1600,
"UintPtr": 1600, "UIntegerPtr": 1600,
"BigIntPtr": -17000, "BigIntPtr": 50000,
"UbigIntPtr": 17000, "UBigIntPtr": 50000,
"Decimal": 1.11, "Decimal": 1.11,
"DecimalPtr": 1.11, "DecimalPtr": 1.11,
"Numeric": 2.22, "Numeric": 2.22,
@ -262,25 +401,25 @@ var allTypesJson = `
"Boolean": false, "Boolean": false,
"BooleanPtr": null, "BooleanPtr": null,
"TinyInt": -3, "TinyInt": -3,
"UtinyInt": 3, "UTinyInt": 3,
"SmallInt": -14, "SmallInt": 14,
"UsmallInt": 14, "USmallInt": 14,
"MediumInt": -150, "MediumInt": -150,
"UmediumInt": 150, "UMediumInt": 150,
"Integer": -1600, "Integer": -1600,
"Uinteger": 1600, "UInteger": 1600,
"BigInt": -17000, "BigInt": 5000,
"UbigInt": 17000, "UBigInt": 50000,
"TinyIntPtr": null, "TinyIntPtr": null,
"UtinyIntPtr": null, "UTinyIntPtr": null,
"SmallIntPtr": null, "SmallIntPtr": null,
"UsmallIntPtr": null, "USmallIntPtr": null,
"MediumIntPtr": null, "MediumIntPtr": null,
"UmediumIntPtr": null, "UMediumIntPtr": null,
"IntPtr": null, "IntegerPtr": null,
"UintPtr": null, "UIntegerPtr": null,
"BigIntPtr": null, "BigIntPtr": null,
"UbigIntPtr": null, "UBigIntPtr": null,
"Decimal": 1.11, "Decimal": 1.11,
"DecimalPtr": null, "DecimalPtr": null,
"Numeric": 2.22, "Numeric": 2.22,

View file

@ -1,6 +1,7 @@
package postgres package postgres
import ( import (
"fmt"
. "github.com/go-jet/jet" . "github.com/go-jet/jet"
"github.com/go-jet/jet/internal/testutils" "github.com/go-jet/jet/internal/testutils"
"github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model" "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model"
@ -58,10 +59,10 @@ func TestExpressionOperators(t *testing.T) {
query := AllTypes.SELECT( query := AllTypes.SELECT(
AllTypes.Integer.IS_NULL(), AllTypes.Integer.IS_NULL(),
AllTypes.Timestamp.IS_NOT_NULL(), AllTypes.Timestamp.IS_NOT_NULL(),
AllTypes.SmallintPtr.IN(Int(11), Int(22), NULL), AllTypes.SmallIntPtr.IN(Int(11), Int(22), NULL),
AllTypes.SmallintPtr.IN(AllTypes.SELECT(AllTypes.IntegerPtr)), AllTypes.SmallIntPtr.IN(AllTypes.SELECT(AllTypes.IntegerPtr)),
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)),
CAST(String("TRUE")).AS_BOOL(), CAST(String("TRUE")).AS_BOOL(),
CAST(String("111")).AS_SMALLINT(), CAST(String("111")).AS_SMALLINT(),
@ -87,7 +88,7 @@ func TestExpressionOperators(t *testing.T) {
TO_NUMBER(String("12,454"), String("99G999D9S")), TO_NUMBER(String("12,454"), String("99G999D9S")),
TO_TIMESTAMP(String("05 Dec 2000"), String("DD Mon YYYY")), TO_TIMESTAMP(String("05 Dec 2000"), String("DD Mon YYYY")),
COALESCE(AllTypes.IntegerPtr, AllTypes.SmallintPtr, NULL, Int(11)), COALESCE(AllTypes.IntegerPtr, AllTypes.SmallIntPtr, NULL, Int(11)),
NULLIF(AllTypes.Text, String("(none)")), NULLIF(AllTypes.Text, String("(none)")),
GREATEST(AllTypes.Numeric, AllTypes.NumericPtr), GREATEST(AllTypes.Numeric, AllTypes.NumericPtr),
LEAST(AllTypes.Numeric, AllTypes.NumericPtr), LEAST(AllTypes.Numeric, AllTypes.NumericPtr),
@ -313,8 +314,8 @@ SELECT (all_types.numeric = all_types.numeric) AS "eq1",
TRUNC((all_types.decimal / $21), $22) AS "div2", TRUNC((all_types.decimal / $21), $22) AS "div2",
TRUNC((all_types.decimal % all_types.decimal_ptr), $23) AS "mod1", TRUNC((all_types.decimal % all_types.decimal_ptr), $23) AS "mod1",
TRUNC((all_types.decimal % $24), $25) AS "mod2", TRUNC((all_types.decimal % $24), $25) AS "mod2",
TRUNC(POWER(all_types.decimal, all_types.decimal_ptr), $26) AS "pow1", TRUNC(POW(all_types.decimal, all_types.decimal_ptr), $26) AS "pow1",
TRUNC(POWER(all_types.decimal, $27), $28) AS "pow2", TRUNC(POW(all_types.decimal, $27), $28) AS "pow2",
TRUNC(ABS(all_types.decimal), $29) AS "abs", TRUNC(ABS(all_types.decimal), $29) AS "abs",
TRUNC(POWER(all_types.decimal, $30), $31) AS "power", TRUNC(POWER(all_types.decimal, $30), $31) AS "power",
TRUNC(SQRT(all_types.decimal), $32) AS "sqrt", TRUNC(SQRT(all_types.decimal), $32) AS "sqrt",
@ -344,53 +345,141 @@ LIMIT $35;
func TestIntegerOperators(t *testing.T) { func TestIntegerOperators(t *testing.T) {
query := AllTypes.SELECT( query := AllTypes.SELECT(
AllTypes.Integer.EQ(AllTypes.IntegerPtr), AllTypes.BigInt,
AllTypes.Bigint.EQ(Int(12)), AllTypes.BigIntPtr,
//AllTypes.Smallint.NOT_EQ(AllTypes.Real), AllTypes.SmallInt,
AllTypes.Integer.NOT_EQ(AllTypes.IntegerPtr), AllTypes.SmallIntPtr,
AllTypes.Bigint.NOT_EQ(Int(12)),
AllTypes.Integer.LT(AllTypes.IntegerPtr),
AllTypes.Bigint.LT(Int(65)),
//AllTypes.Smallint.LT_EQ(AllTypes.Numeric),
AllTypes.Integer.LT_EQ(AllTypes.IntegerPtr),
AllTypes.Bigint.LT_EQ(Int(65)),
//AllTypes.Smallint.GT_EQ(AllTypes.Numeric),
AllTypes.Integer.GT(AllTypes.IntegerPtr),
AllTypes.Bigint.GT(Int(65)),
AllTypes.Integer.GT_EQ(AllTypes.IntegerPtr),
AllTypes.Bigint.GT_EQ(Int(65)),
AllTypes.Integer.ADD(AllTypes.Integer), AllTypes.BigInt.EQ(AllTypes.BigInt).AS("eq1"),
AllTypes.Integer.ADD(Int(11)), AllTypes.BigInt.EQ(Int(12)).AS("eq2"),
AllTypes.Integer.SUB(AllTypes.Integer),
AllTypes.Integer.SUB(Int(11)),
AllTypes.Integer.MUL(AllTypes.Integer),
AllTypes.Integer.MUL(Int(11)),
AllTypes.Integer.DIV(AllTypes.Integer),
AllTypes.Integer.DIV(Int(11)),
AllTypes.Integer.MOD(AllTypes.Integer),
AllTypes.Integer.MOD(Int(11)),
AllTypes.Integer.POW(AllTypes.Smallint),
AllTypes.Integer.POW(Int(11)),
AllTypes.Integer.BIT_AND(AllTypes.Smallint),
AllTypes.Integer.BIT_OR(AllTypes.Smallint),
AllTypes.Integer.BIT_XOR(Int(11)),
BIT_NOT(AllTypes.Integer),
AllTypes.Integer.BIT_SHIFT_LEFT(AllTypes.Smallint),
AllTypes.Integer.BIT_SHIFT_LEFT(Int(11)),
AllTypes.Integer.BIT_SHIFT_RIGHT(AllTypes.Smallint),
AllTypes.Integer.BIT_SHIFT_RIGHT(Int(11)),
ABSi(AllTypes.Integer), AllTypes.BigInt.NOT_EQ(AllTypes.BigIntPtr).AS("neq1"),
SQRT(AllTypes.Integer), AllTypes.BigInt.NOT_EQ(Int(12)).AS("neq2"),
CBRT(AllTypes.Integer),
)
//fmt.Println(query.DebugSql()) AllTypes.BigInt.IS_DISTINCT_FROM(AllTypes.BigInt).AS("distinct1"),
AllTypes.BigInt.IS_DISTINCT_FROM(Int(12)).AS("distinct2"),
err := query.Query(db, &struct{}{}) AllTypes.BigInt.IS_NOT_DISTINCT_FROM(AllTypes.BigInt).AS("not distinct1"),
AllTypes.BigInt.IS_NOT_DISTINCT_FROM(Int(12)).AS("not distinct2"),
AllTypes.BigInt.LT(AllTypes.BigIntPtr).AS("lt1"),
AllTypes.BigInt.LT(Int(65)).AS("lt2"),
AllTypes.BigInt.LT_EQ(AllTypes.BigIntPtr).AS("lte1"),
AllTypes.BigInt.LT_EQ(Int(65)).AS("lte2"),
AllTypes.BigInt.GT(AllTypes.BigIntPtr).AS("gt1"),
AllTypes.BigInt.GT(Int(65)).AS("gt2"),
AllTypes.BigInt.GT_EQ(AllTypes.BigIntPtr).AS("gte1"),
AllTypes.BigInt.GT_EQ(Int(65)).AS("gte2"),
AllTypes.BigInt.ADD(AllTypes.BigInt).AS("add1"),
AllTypes.BigInt.ADD(Int(11)).AS("add2"),
AllTypes.BigInt.SUB(AllTypes.BigInt).AS("sub1"),
AllTypes.BigInt.SUB(Int(11)).AS("sub2"),
AllTypes.BigInt.MUL(AllTypes.BigInt).AS("mul1"),
AllTypes.BigInt.MUL(Int(11)).AS("mul2"),
AllTypes.BigInt.DIV(AllTypes.BigInt).AS("div1"),
AllTypes.BigInt.DIV(Int(11)).AS("div2"),
AllTypes.BigInt.MOD(AllTypes.BigInt).AS("mod1"),
AllTypes.BigInt.MOD(Int(11)).AS("mod2"),
AllTypes.SmallInt.POW(AllTypes.SmallInt.DIV(Int(3))).AS("pow1"),
AllTypes.SmallInt.POW(Int(6)).AS("pow2"),
AllTypes.SmallInt.BIT_AND(AllTypes.SmallInt).AS("bit_and1"),
AllTypes.SmallInt.BIT_AND(AllTypes.SmallInt).AS("bit_and2"),
AllTypes.SmallInt.BIT_OR(AllTypes.SmallInt).AS("bit or 1"),
AllTypes.SmallInt.BIT_OR(Int(22)).AS("bit or 2"),
AllTypes.SmallInt.BIT_XOR(AllTypes.SmallInt).AS("bit xor 1"),
AllTypes.SmallInt.BIT_XOR(Int(11)).AS("bit xor 2"),
BIT_NOT(MINUSi(AllTypes.SmallInt)).AS("bit_not_1"),
BIT_NOT(MINUSi(Int(11, true))).AS("bit_not_2"),
AllTypes.SmallInt.BIT_SHIFT_LEFT(AllTypes.SmallInt.DIV(Int(2))).AS("bit shift left 1"),
AllTypes.SmallInt.BIT_SHIFT_LEFT(Int(4)).AS("bit shift left 2"),
AllTypes.SmallInt.BIT_SHIFT_RIGHT(AllTypes.SmallInt.DIV(Int(5))).AS("bit shift right 1"),
AllTypes.SmallInt.BIT_SHIFT_RIGHT(Int(1)).AS("bit shift right 2"),
ABSi(AllTypes.BigInt).AS("abs"),
SQRT(ABSi(AllTypes.BigInt)).AS("sqrt"),
CBRT(ABSi(AllTypes.BigInt)).AS("cbrt"),
).LIMIT(2)
fmt.Println(query.Sql())
testutils.AssertStatementSql(t, query, `
SELECT all_types.big_int AS "all_types.big_int",
all_types.big_int_ptr AS "all_types.big_int_ptr",
all_types.small_int AS "all_types.small_int",
all_types.small_int_ptr AS "all_types.small_int_ptr",
(all_types.big_int = all_types.big_int) AS "eq1",
(all_types.big_int = $1) AS "eq2",
(all_types.big_int != all_types.big_int_ptr) AS "neq1",
(all_types.big_int != $2) AS "neq2",
(all_types.big_int IS DISTINCT FROM all_types.big_int) AS "distinct1",
(all_types.big_int IS DISTINCT FROM $3) AS "distinct2",
(all_types.big_int IS NOT DISTINCT FROM all_types.big_int) AS "not distinct1",
(all_types.big_int IS NOT DISTINCT FROM $4) AS "not distinct2",
(all_types.big_int < all_types.big_int_ptr) AS "lt1",
(all_types.big_int < $5) AS "lt2",
(all_types.big_int <= all_types.big_int_ptr) AS "lte1",
(all_types.big_int <= $6) AS "lte2",
(all_types.big_int > all_types.big_int_ptr) AS "gt1",
(all_types.big_int > $7) AS "gt2",
(all_types.big_int >= all_types.big_int_ptr) AS "gte1",
(all_types.big_int >= $8) AS "gte2",
(all_types.big_int + all_types.big_int) AS "add1",
(all_types.big_int + $9) AS "add2",
(all_types.big_int - all_types.big_int) AS "sub1",
(all_types.big_int - $10) AS "sub2",
(all_types.big_int * all_types.big_int) AS "mul1",
(all_types.big_int * $11) AS "mul2",
(all_types.big_int / all_types.big_int) AS "div1",
(all_types.big_int / $12) AS "div2",
(all_types.big_int % all_types.big_int) AS "mod1",
(all_types.big_int % $13) AS "mod2",
POW(all_types.small_int, (all_types.small_int / $14)) AS "pow1",
POW(all_types.small_int, $15) AS "pow2",
(all_types.small_int & all_types.small_int) AS "bit_and1",
(all_types.small_int & all_types.small_int) AS "bit_and2",
(all_types.small_int | all_types.small_int) AS "bit or 1",
(all_types.small_int | $16) AS "bit or 2",
(all_types.small_int # all_types.small_int) AS "bit xor 1",
(all_types.small_int # $17) AS "bit xor 2",
(~ (- all_types.small_int)) AS "bit_not_1",
(~ (- 11)) AS "bit_not_2",
(all_types.small_int << (all_types.small_int / $18)) AS "bit shift left 1",
(all_types.small_int << $19) AS "bit shift left 2",
(all_types.small_int >> (all_types.small_int / $20)) AS "bit shift right 1",
(all_types.small_int >> $21) AS "bit shift right 2",
ABS(all_types.big_int) AS "abs",
SQRT(ABS(all_types.big_int)) AS "sqrt",
CBRT(ABS(all_types.big_int)) AS "cbrt"
FROM test_sample.all_types
LIMIT $22;
`)
var dest []struct {
common.AllTypesIntegerExpResult `alias:"."`
}
err := query.Query(db, &dest)
assert.NilError(t, err) assert.NilError(t, err)
//testutils.JsonSave("./testdata/common/int_operators.json", dest)
//testutils.JsonPrint(dest)
testutils.AssertJSONFile(t, "./testdata/common/int_operators.json", dest)
} }
func TestTimeOperators(t *testing.T) { func TestTimeOperators(t *testing.T) {
@ -634,12 +723,12 @@ FROM`
} }
var allTypesRow0 = model.AllTypes{ var allTypesRow0 = model.AllTypes{
SmallintPtr: Int16Ptr(1), SmallIntPtr: Int16Ptr(14),
Smallint: 1, SmallInt: 14,
IntegerPtr: Int32Ptr(300), IntegerPtr: Int32Ptr(300),
Integer: 300, Integer: 300,
BigintPtr: Int64Ptr(50000), BigIntPtr: Int64Ptr(50000),
Bigint: 5000, BigInt: 5000,
DecimalPtr: Float64Ptr(1.11), DecimalPtr: Float64Ptr(1.11),
Decimal: 1.11, Decimal: 1.11,
NumericPtr: Float64Ptr(2.22), NumericPtr: Float64Ptr(2.22),
@ -700,12 +789,12 @@ var allTypesRow0 = model.AllTypes{
} }
var allTypesRow1 = model.AllTypes{ var allTypesRow1 = model.AllTypes{
SmallintPtr: nil, SmallIntPtr: nil,
Smallint: 1, SmallInt: 14,
IntegerPtr: nil, IntegerPtr: nil,
Integer: 300, Integer: 300,
BigintPtr: nil, BigIntPtr: nil,
Bigint: 5000, BigInt: 5000,
DecimalPtr: nil, DecimalPtr: nil,
Decimal: 1.11, Decimal: 1.11,
NumericPtr: nil, NumericPtr: nil,

84
tests/testdata/common/common.go vendored Normal file
View file

@ -0,0 +1,84 @@
package common
type EqualityExpResult struct {
Eq1 *bool
Eq2 *bool
Distinct1 *bool
Distinct2 *bool
NotDistinct1 *bool
NotDistinct2 *bool
Lt1 *bool
Lt2 *bool
Lte1 *bool
Lte2 *bool
Gt1 *bool
Gt2 *bool
Gte1 *bool
Gte2 *bool
}
type AllTypesIntegerExpResult struct {
EqualityExpResult `alias:"."`
Add1 *int64
Add2 *int64
Sub1 *int64
Sub2 *int64
Mul1 *int64
Mul2 *int64
Div1 *int64
Div2 *int64
Mod1 *int64
Mod2 *int64
Pow1 *int64
Pow2 *int64
BitAnd1 *int64
BitAnd2 *int64
BitOr1 *int64
BitOr2 *int64
BitXor1 *int64
BitXor2 *int64
BitShiftLeft1 *int64
BitShiftLeft2 *int64
BitShiftRight1 *int64
BitShiftRight2 *int64
}
type FloatExpressionTestResult struct {
Eq1 *bool
Eq2 *bool
Eq3 *bool
Distinct1 *bool
Distinct2 *bool
Distinct3 *bool
NotDistinct1 *bool
NotDistinct2 *bool
NotDistinct3 *bool
Lt1 *bool
Lt2 *bool
Gt1 *bool
Gt2 *bool
Add1 *float64
Add2 *float64
Sub1 *float64
Sub2 *float64
Mul1 *float64
Mul2 *float64
Div1 *float64
Div2 *float64
Mod1 *float64
Mod2 *float64
Pow1 *float64
Pow2 *float64
Abs *float64
Power *float64
Sqrt *float64
Cbrt *float64
Ceil *float64
Floor *float64
Round1 *float64
Round2 *float64
Sign *float64
Trunc *float64
}

View file

@ -1,40 +0,0 @@
package common
type FloatExpressionTestResult struct {
Eq1 *bool
Eq2 *bool
Eq3 *bool
Distinct1 *bool
Distinct2 *bool
Distinct3 *bool
NotDistinct1 *bool
NotDistinct2 *bool
NotDistinct3 *bool
Lt1 *bool
Lt2 *bool
Gt1 *bool
Gt2 *bool
Add1 *float64
Add2 *float64
Sub1 *float64
Sub2 *float64
Mul1 *float64
Mul2 *float64
Div1 *float64
Div2 *float64
Mod1 *float64
Mod2 *float64
Pow1 *float64
Pow2 *float64
Abs *float64
Power *float64
Sqrt *float64
Cbrt *float64
Ceil *float64
Floor *float64
Round1 *float64
Round2 *float64
Sign *float64
Trunc *float64
}

View file

@ -0,0 +1,78 @@
[
{
"Eq1": true,
"Eq2": false,
"Distinct1": false,
"Distinct2": true,
"NotDistinct1": true,
"NotDistinct2": false,
"Lt1": true,
"Lt2": false,
"Lte1": true,
"Lte2": false,
"Gt1": false,
"Gt2": true,
"Gte1": false,
"Gte2": true,
"Add1": 10000,
"Add2": 5011,
"Sub1": 0,
"Sub2": 4989,
"Mul1": 25000000,
"Mul2": 55000,
"Div1": 1,
"Div2": 454,
"Mod1": 0,
"Mod2": 6,
"Pow1": 38416,
"Pow2": 7529536,
"BitAnd1": 14,
"BitAnd2": 14,
"BitOr1": 14,
"BitOr2": 30,
"BitXor1": 0,
"BitXor2": 5,
"BitShiftLeft1": 1792,
"BitShiftLeft2": 224,
"BitShiftRight1": 3,
"BitShiftRight2": 7
},
{
"Eq1": true,
"Eq2": false,
"Distinct1": false,
"Distinct2": true,
"NotDistinct1": true,
"NotDistinct2": false,
"Lt1": null,
"Lt2": false,
"Lte1": null,
"Lte2": false,
"Gt1": null,
"Gt2": true,
"Gte1": null,
"Gte2": true,
"Add1": 10000,
"Add2": 5011,
"Sub1": 0,
"Sub2": 4989,
"Mul1": 25000000,
"Mul2": 55000,
"Div1": 1,
"Div2": 454,
"Mod1": 0,
"Mod2": 6,
"Pow1": 38416,
"Pow2": 7529536,
"BitAnd1": 14,
"BitAnd2": 14,
"BitOr1": 14,
"BitOr2": 30,
"BitXor1": 0,
"BitXor2": 5,
"BitShiftLeft1": 1792,
"BitShiftLeft2": 224,
"BitShiftRight1": 3,
"BitShiftRight2": 7
}
]