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

fix: treat single subselect properly #357

Open
wants to merge 1 commit 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
23 changes: 15 additions & 8 deletions exp/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,27 @@ func lte(lhs Expression, rhs interface{}) BooleanExpression {
return NewBooleanExpression(LteOp, lhs, rhs)
}

func inNotIn(lhs Expression, op BooleanOperation, vals ...interface{}) BooleanExpression {
if len(vals) == 1 {
valType := reflect.Indirect(reflect.ValueOf(vals[0]))
if valType.Kind() == reflect.Slice {
return NewBooleanExpression(op, lhs, vals[0])
}
if _, ok := vals[0].(AppendableExpression); ok {
return NewBooleanExpression(op, lhs, vals[0])
}
}
return NewBooleanExpression(op, lhs, vals)
}

// used internally to create an IN BooleanExpression
func in(lhs Expression, vals ...interface{}) BooleanExpression {
if len(vals) == 1 && reflect.Indirect(reflect.ValueOf(vals[0])).Kind() == reflect.Slice {
return NewBooleanExpression(InOp, lhs, vals[0])
}
return NewBooleanExpression(InOp, lhs, vals)
return inNotIn(lhs, InOp, vals...)
}

// used internally to create a NOT IN BooleanExpression
func notIn(lhs Expression, vals ...interface{}) BooleanExpression {
if len(vals) == 1 && reflect.Indirect(reflect.ValueOf(vals[0])).Kind() == reflect.Slice {
return NewBooleanExpression(NotInOp, lhs, vals[0])
}
return NewBooleanExpression(NotInOp, lhs, vals)
return inNotIn(lhs, NotInOp, vals...)
}

// used internally to create an IS BooleanExpression
Expand Down
8 changes: 4 additions & 4 deletions sqlgen/expression_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,16 +565,16 @@ func (esgs *expressionSQLGeneratorSuite) TestGenerate_BooleanExpression() {
int64(1), int64(2), int64(3),
}},

expressionTestCase{val: ident.In(ae), sql: `("a" IN ((SELECT "id" FROM "test2")))`},
expressionTestCase{val: ident.In(ae), sql: `("a" IN ((SELECT "id" FROM "test2")))`, isPrepared: true},
expressionTestCase{val: ident.In(ae), sql: `("a" IN (SELECT "id" FROM "test2"))`},
Copy link
Contributor Author

@nic11 nic11 Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a expressionTestCase{val: ident.Eq(ae), sql: `("a" IN (SELECT "id" FROM "test2"))`}, above, so maybe this was intended for some reason(??)

But looks strange anyway, I don't think that this should be a desired behaviour

expressionTestCase{val: ident.In(ae), sql: `("a" IN (SELECT "id" FROM "test2"))`, isPrepared: true},

expressionTestCase{val: ident.NotIn([]int64{1, 2, 3}), sql: `("a" NOT IN (1, 2, 3))`},
expressionTestCase{val: ident.NotIn([]int64{1, 2, 3}), sql: `("a" NOT IN (?, ?, ?))`, isPrepared: true, args: []interface{}{
int64(1), int64(2), int64(3),
}},

expressionTestCase{val: ident.NotIn(ae), sql: `("a" NOT IN ((SELECT "id" FROM "test2")))`},
expressionTestCase{val: ident.NotIn(ae), sql: `("a" NOT IN ((SELECT "id" FROM "test2")))`, isPrepared: true},
expressionTestCase{val: ident.NotIn(ae), sql: `("a" NOT IN (SELECT "id" FROM "test2"))`},
expressionTestCase{val: ident.NotIn(ae), sql: `("a" NOT IN (SELECT "id" FROM "test2"))`, isPrepared: true},

expressionTestCase{val: ident.Like("a%"), sql: `("a" LIKE 'a%')`},
expressionTestCase{val: ident.Like("a%"), sql: `("a" LIKE ?)`, isPrepared: true, args: []interface{}{"a%"}},
Expand Down