Skip to content

Commit

Permalink
uint64 cast to int64 caused lost of data doug-martin#317
Browse files Browse the repository at this point in the history
  • Loading branch information
XIELongDragon committed Dec 19, 2021
1 parent e40b45c commit 981d251
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 15 additions & 0 deletions sqlgen/expression_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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 @@ -326,6 +332,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
7 changes: 4 additions & 3 deletions sqlgen/expression_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,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 +449,7 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_LiteralExpression() {
int64(2),
int64(3),
int64(4),
uint64(11169823557460058355),
},
},
)
Expand Down

0 comments on commit 981d251

Please sign in to comment.