Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/317 #318

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion sqlgen/select_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (ssgs *selectSQLGeneratorSuite) TestGenerate_withOffset() {
ssgs.assertCases(
sqlgen.NewSelectSQLGenerator("test", sqlgen.DefaultDialectOptions()),
selectTestCase{clause: sc, sql: `SELECT * FROM "test" OFFSET 10`},
selectTestCase{clause: sc, sql: `SELECT * FROM "test" OFFSET ?`, isPrepared: true, args: []interface{}{int64(10)}},
selectTestCase{clause: sc, sql: `SELECT * FROM "test" OFFSET ?`, isPrepared: true, args: []interface{}{uint64(10)}},
)
}

Expand Down