Skip to content

Commit 4c69699

Browse files
committed
fix panic on in list expansion with a subselect
1 parent bfe5d09 commit 4c69699

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

bind.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
212212
argMeta := meta[arg]
213213
arg++
214214

215-
// not an in-list
216-
if !inIn {
215+
if !inIn || !argMeta.v.IsValid() {
216+
// this QuestionMark is not in an in-list that needs expansion.
217+
// if v is invalid, then the arg isn't a slice
217218
newArgs = append(newArgs, argMeta.i)
218219
buf.WriteString(token.Text)
219220
continue

bind_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package sqlx
33
import (
44
"math/rand"
55
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
610
)
711

812
func oldBindType(driverName string) int {
@@ -77,3 +81,14 @@ func BenchmarkBindSpeed(b *testing.B) {
7781

7882
})
7983
}
84+
85+
func TestInNotSlice(t *testing.T) {
86+
now := time.Now()
87+
args := []any{[]string{"a", "b"}, now}
88+
query := ` SELECT * FROM person WHERE first_name IN (?) AND id IN ( SELECT id FROM something WHERE created_at = ? )`
89+
insql, newArgs, err := In(query, args...)
90+
require.NoError(t, err)
91+
assert.Contains(t, insql, "IN (?, ?)")
92+
assert.Contains(t, insql, "WHERE created_at = ? )")
93+
assert.Equal(t, []any{"a", "b", now}, newArgs)
94+
}

0 commit comments

Comments
 (0)