Skip to content

Commit f10f9d2

Browse files
authored
fix panic on in list expansion with a subselect (#18)
1 parent 5ba24ac commit f10f9d2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

bind.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ func In(query string, args ...any) (string, []any, error) {
211211

212212
argMeta := meta[arg]
213213
arg++
214+
isSlice := argMeta.v.IsValid()
214215

215-
// not an in-list
216-
if !inIn {
216+
if !inIn || !isSlice {
217217
newArgs = append(newArgs, argMeta.i)
218218
buf.WriteString(token.Text)
219219
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)