diff --git a/sqlgen/expression_sql_generator.go b/sqlgen/expression_sql_generator.go index 82ce15c5..1801db0e 100644 --- a/sqlgen/expression_sql_generator.go +++ b/sqlgen/expression_sql_generator.go @@ -83,6 +83,7 @@ func (esg *expressionSQLGenerator) Generate(b sb.SQLBuilder, val interface{}) { esg.literalNil(b) return } + // nolint:gocyclo // processing all possible types switch v := val.(type) { case exp.Expression: esg.expressionSQL(b, v) @@ -92,6 +93,12 @@ func (esg *expressionSQLGenerator) Generate(b sb.SQLBuilder, val interface{}) { esg.literalInt(b, int64(v)) case int64: esg.literalInt(b, v) + case uint: + esg.literalUint(b, uint64(v)) + case uint32: + esg.literalUint(b, uint64(v)) + case uint64: + esg.literalUint(b, v) case float32: esg.literalFloat(b, float64(v)) case float64: @@ -145,7 +152,7 @@ func (esg *expressionSQLGenerator) reflectSQL(b sb.SQLBuilder, val interface{}) case util.IsInt(valKind): esg.Generate(b, v.Int()) case util.IsUint(valKind): - esg.Generate(b, int64(v.Uint())) + esg.Generate(b, v.Uint()) case util.IsFloat(valKind): esg.Generate(b, v.Float()) case util.IsString(valKind): @@ -326,6 +333,15 @@ func (esg *expressionSQLGenerator) literalInt(b sb.SQLBuilder, i int64) { b.WriteStrings(strconv.FormatInt(i, 10)) } +// Generates SQL for an uint value +func (esg *expressionSQLGenerator) literalUint(b sb.SQLBuilder, i uint64) { + if b.IsPrepared() { + esg.placeHolderSQL(b, i) + return + } + b.WriteStrings(strconv.FormatUint(i, 10)) +} + // Generates SQL for a string func (esg *expressionSQLGenerator) literalString(b sb.SQLBuilder, s string) { if b.IsPrepared() { diff --git a/sqlgen/expression_sql_generator_test.go b/sqlgen/expression_sql_generator_test.go index ed75e12f..362ea1ce 100644 --- a/sqlgen/expression_sql_generator_test.go +++ b/sqlgen/expression_sql_generator_test.go @@ -179,11 +179,14 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_IntTypes() { int16(10), int32(10), int64(10), + } + uints := []interface{}{ uint(10), uint16(10), uint32(10), uint64(10), } + for _, i := range ints { esgs.assertCases( sqlgen.NewExpressionSQLGenerator("test", sqlgen.DefaultDialectOptions()), @@ -191,6 +194,13 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_IntTypes() { expressionTestCase{val: i, sql: "?", isPrepared: true, args: []interface{}{int64(10)}}, ) } + for _, i := range uints { + esgs.assertCases( + sqlgen.NewExpressionSQLGenerator("test", sqlgen.DefaultDialectOptions()), + expressionTestCase{val: i, sql: "10"}, + expressionTestCase{val: i, sql: "?", isPrepared: true, args: []interface{}{uint64(10)}}, + ) + } esgs.assertCases( sqlgen.NewExpressionSQLGenerator("test", sqlgen.DefaultDialectOptions()), expressionTestCase{val: &i, sql: "0"}, @@ -430,17 +440,17 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_ExpressionList() { func (esgs *expressionSQLGeneratorSuite) TestGenerate_LiteralExpression() { noArgsL := exp.NewLiteralExpression(`"b"::DATE = '2010-09-02'`) - argsL := exp.NewLiteralExpression(`"b" = ? or "c" = ? or d IN ?`, "a", 1, []int{1, 2, 3, 4}) + argsL := exp.NewLiteralExpression(`"b" = ? or "c" = ? or d IN ? or "e" = ?`, "a", 1, []int64{1, 2, 3, 4}, uint64(11169823557460058355)) esgs.assertCases( sqlgen.NewExpressionSQLGenerator("test", sqlgen.DefaultDialectOptions()), expressionTestCase{val: noArgsL, sql: `"b"::DATE = '2010-09-02'`}, expressionTestCase{val: noArgsL, sql: `"b"::DATE = '2010-09-02'`, isPrepared: true}, - expressionTestCase{val: argsL, sql: `"b" = 'a' or "c" = 1 or d IN (1, 2, 3, 4)`}, + expressionTestCase{val: argsL, sql: `"b" = 'a' or "c" = 1 or d IN (1, 2, 3, 4) or "e" = 11169823557460058355`}, expressionTestCase{ val: argsL, - sql: `"b" = ? or "c" = ? or d IN (?, ?, ?, ?)`, + sql: `"b" = ? or "c" = ? or d IN (?, ?, ?, ?) or "e" = ?`, isPrepared: true, args: []interface{}{ "a", @@ -449,6 +459,7 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_LiteralExpression() { int64(2), int64(3), int64(4), + uint64(11169823557460058355), }, }, )