Skip to content

Commit

Permalink
uint64 cast to int64 caused lost of data #317
Browse files Browse the repository at this point in the history
  • Loading branch information
XIELongDragon committed Dec 20, 2021
1 parent e40b45c commit 28cea5a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
18 changes: 17 additions & 1 deletion sqlgen/expression_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (esg *expressionSQLGenerator) Dialect() string {

var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()

// nolint:gocyclo // processing all possible types
func (esg *expressionSQLGenerator) Generate(b sb.SQLBuilder, val interface{}) {
if b.Error() != nil {
return
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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() {
Expand Down
17 changes: 14 additions & 3 deletions sqlgen/expression_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,28 @@ 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()),
expressionTestCase{val: i, sql: "10"},
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"},
Expand Down Expand Up @@ -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",
Expand All @@ -449,6 +459,7 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_LiteralExpression() {
int64(2),
int64(3),
int64(4),
uint64(11169823557460058355),
},
},
)
Expand Down

0 comments on commit 28cea5a

Please sign in to comment.