Skip to content

Commit 796d1f5

Browse files
committed
EB-4012 added whereIn function and unit test
1 parent 50d8885 commit 796d1f5

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

strmangle.go

+25
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,31 @@ func WhereClause(lq, rq string, start int, cols []string) string {
573573
return buf.String()
574574
}
575575

576+
// WhereInClause returns the where clause using start as the $ flag index
577+
// For example, if start was 2 output would be: "col IN ($2,$3)"
578+
func WhereInClause(lq, rq string, start int, cols []string, count int) string {
579+
buf := GetBuffer()
580+
defer PutBuffer(buf)
581+
582+
useIndexPlaceholders := true
583+
if start == 0 {
584+
start = 1
585+
useIndexPlaceholders = false
586+
}
587+
588+
for i, c := range cols {
589+
buf.WriteString(fmt.Sprintf(`%s%s%s IN (`, lq, c, rq))
590+
buf.WriteString(Placeholders(useIndexPlaceholders, count, start+i*count, 1))
591+
buf.WriteByte(')')
592+
593+
if i < len(cols)-1 {
594+
buf.WriteString(" AND ")
595+
}
596+
}
597+
598+
return buf.String()
599+
}
600+
576601
// WhereClauseRepeated returns the where clause repeated with OR clause using start as the $ flag index
577602
// For example, if start was 2 output would be: "(colthing=$2 AND colstuff=$3) OR (colthing=$4 AND colstuff=$5)"
578603
func WhereClauseRepeated(lq, rq string, start int, cols []string, count int) string {

strmangle_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,46 @@ func TestSetParamNames(t *testing.T) {
349349
}
350350
}
351351

352+
func TestWhereInClause(t *testing.T) {
353+
t.Parallel()
354+
355+
tests := []struct {
356+
name string
357+
lq string
358+
rq string
359+
start int
360+
cols []string
361+
count int
362+
expected string
363+
}{
364+
{
365+
name: "Test with indexed placeholders",
366+
lq: `"`,
367+
rq: `"`,
368+
start: 1,
369+
cols: []string{"col1", "col2"},
370+
count: 2,
371+
expected: `"col1" IN ($1,$2) AND "col2" IN ($3,$4)`,
372+
},
373+
{
374+
name: "Test with question mark placeholders",
375+
lq: `"`,
376+
rq: `"`,
377+
start: 0,
378+
cols: []string{"col1", "col2"},
379+
count: 2,
380+
expected: `"col1" IN (?,?) AND "col2" IN (?,?)`,
381+
},
382+
}
383+
384+
for _, tt := range tests {
385+
result := WhereInClause(tt.lq, tt.rq, tt.start, tt.cols, tt.count)
386+
if result != tt.expected {
387+
t.Errorf("WhereInClause() = %v, want %v", result, tt.expected)
388+
}
389+
}
390+
}
391+
352392
func TestWhereClause(t *testing.T) {
353393
t.Parallel()
354394

0 commit comments

Comments
 (0)