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

Query in support with slice args. #267

Merged
merged 2 commits into from Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions query_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/gobuffalo/uuid"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -48,6 +49,26 @@ func Test_Where_In(t *testing.T) {
})
}

func Test_Where_In_Slice(t *testing.T) {
r := require.New(t)
transaction(func(tx *Connection) {
u1 := &Song{Title: "A"}
u2 := &Song{Title: "A"}
u3 := &Song{Title: "A"}
err := tx.Create(u1)
r.NoError(err)
err = tx.Create(u2)
r.NoError(err)
err = tx.Create(u3)
r.NoError(err)

songs := []Song{}
err = tx.Where("id in (?)", []uuid.UUID{u1.ID, u3.ID}).Where("title = ?", "A").All(&songs)
r.NoError(err)
r.Len(songs, 2)
})
}

func Test_Where_In_Complex(t *testing.T) {
r := require.New(t)
transaction(func(tx *Connection) {
Expand Down
3 changes: 2 additions & 1 deletion sql_builder.go
Expand Up @@ -88,9 +88,10 @@ func (sq *sqlBuilder) compile() {
}

if inRegex.MatchString(sq.sql) {
s, _, err := sqlx.In(sq.sql, sq.Args())
s, args, err := sqlx.In(sq.sql, sq.Args()...)
if err == nil {
sq.sql = s
sq.args = args
}
}
sq.sql = sq.Query.Connection.Dialect.TranslateSQL(sq.sql)
Expand Down