Skip to content
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
16 changes: 8 additions & 8 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sqlx

import (
"bytes"
"database/sql/driver"
//"database/sql/driver"
"errors"
"reflect"
"strconv"
Expand Down Expand Up @@ -116,13 +116,13 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
meta := make([]argMeta, len(args))

for i, arg := range args {
if a, ok := arg.(driver.Valuer); ok {
var err error
arg, err = a.Value()
if err != nil {
return "", nil, err
}
}
//if a, ok := arg.(driver.Valuer); ok {
// var err error
// arg, err = a.Value()
// if err != nil {
// return "", nil, err
// }
//}
v := reflect.ValueOf(arg)
t := reflectx.Deref(v.Type())

Expand Down
63 changes: 63 additions & 0 deletions sqlx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/lib/pq"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -1494,6 +1495,68 @@ func TestIssue197(t *testing.T) {
})
}

func TestInStringSlices(t *testing.T) {
// some quite normal situations
type tr struct {
q string
args []interface{}
c int
}
tests := []tr{
// pq.StringArrays get passed into sqlx.In in the failed premerge tests:
// https://sourcegraph.iap.tmachine.io/git.gaia.tmachine.io/diffusion/CORE/-/blob/vault/payment_hub/payments/credit_transfer_api/payments/db/list_payments.go?subtree=true#L356:1

{"SELECT * FROM foo WHERE x = ? AND y in (?)",
[]interface{}{1, pq.StringArray([]string{"MyAccountID"})},
2},
{"SELECT * FROM foo WHERE x = ? AND y in (?)",
[]interface{}{0, pq.StringArray([]string{"MyAccountID", "MySchemeID"})},
3},
}
for _, test := range tests {
q, a, err := In(test.q, test.args...)
if err != nil {
t.Error(err)
}
for _, arg := range a {
log.Printf("arg = %v, type = %v", arg, reflect.TypeOf(arg))
}
if len(a) != test.c {
t.Errorf("Expected %d args, but got %d (%+v)", test.c, len(a), a)
}
if strings.Count(q, "?") != test.c {
t.Errorf("Expected %d bindVars, got %d", test.c, strings.Count(q, "?"))
}
}
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
loadDefaultFixture(db, t)
//tx.MustExec(tx.Rebind("INSERT INTO place (country, city, telcode) VALUES (?, ?, ?)"), "United States", "New York", "1")
//tx.MustExec(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Hong Kong", "852")
//tx.MustExec(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Singapore", "65")
telcodes := []int{852, 65}
q := "SELECT * FROM place WHERE telcode IN(?) ORDER BY telcode"
query, args, err := In(q, telcodes)
if err != nil {
t.Error(err)
}
query = db.Rebind(query)
places := []Place{}
err = db.Select(&places, query, args...)
if err != nil {
t.Error(err)
}
if len(places) != 2 {
t.Fatalf("Expecting 2 results, got %d", len(places))
}
if places[0].TelCode != 65 {
t.Errorf("Expecting singapore first, but got %#v", places[0])
}
if places[1].TelCode != 852 {
t.Errorf("Expecting hong kong second, but got %#v", places[1])
}
})
}

func TestIn(t *testing.T) {
// some quite normal situations
type tr struct {
Expand Down