Skip to content

Commit

Permalink
x/sqlbuilder: Handle iter.Seq in the In clause
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisMontagne committed Sep 23, 2024
1 parent 60a3d13 commit 3a786d6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion x/migration/fs_source.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build go1.16
//go:build go1.16

package migration

Expand Down
2 changes: 1 addition & 1 deletion x/sqlbuilder/predicate_clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (bc *basicClause) WriteTo(w QueryWriter, vs map[string]interface{}) error {
return bc.fn(w, vv, bc.m.ToSQL())
}

func writeInClause(w QueryWriter, vv interface{}, k string) error {
func writeInClauseBasic(w QueryWriter, vv interface{}, k string) error {
v := reflect.ValueOf(vv)

if k := v.Kind(); k != reflect.Slice && k != reflect.Array {
Expand Down
7 changes: 7 additions & 0 deletions x/sqlbuilder/predicate_clause_go_1_22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !go1.23

package sqlbuilder

func writeInClause(w QueryWriter, vv interface{}, k string) error {
return writeInClauseBasic(w, vv, k)
}
22 changes: 22 additions & 0 deletions x/sqlbuilder/predicate_clause_go_1_23.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build go1.23

package sqlbuilder

import "reflect"

func writeInClause(w QueryWriter, vv interface{}, k string) error {
v := reflect.ValueOf(vv)
t := v.Type()

if t.CanSeq() && t.Kind() != reflect.Slice && t.Kind() != reflect.Array {
var vs []interface{}

for v := range v.Seq() {
vs = append(vs, v.Interface())
}

return writeInClauseBasic(w, vs, k)
}

return writeInClauseBasic(w, vv, k)
}
24 changes: 24 additions & 0 deletions x/sqlbuilder/predicate_clause_go_1_23_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build go1.23

package sqlbuilder

import (
"slices"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSeqIN(t *testing.T) {
var w queryWriter

err := StaticIn(
Column("foo"),
slices.Values([]int{4, 5, 6}),
).WriteTo(&w, nil)

require.NoError(t, err)
assert.Equal(t, "foo IN ($1, $2, $3)", w.String())
assert.Equal(t, []any{4, 5, 6}, w.vs)
}

0 comments on commit 3a786d6

Please sign in to comment.