Add WhereMultiple
- Change the order of the args in WhereClause to support varargs
This commit is contained in:
parent
097de57e59
commit
005218d61f
2 changed files with 64 additions and 4 deletions
|
@ -245,7 +245,7 @@ func GenerateParamFlags(colCount int, startAt int, groupAt int) string {
|
|||
|
||||
// WhereClause returns the where clause using start as the $ flag index
|
||||
// For example, if start was 2 output would be: "colthing=$2 AND colstuff=$3"
|
||||
func WhereClause(cols []string, start int) string {
|
||||
func WhereClause(start int, cols ...string) string {
|
||||
if start == 0 {
|
||||
panic("0 is not a valid start number for whereClause")
|
||||
}
|
||||
|
@ -258,6 +258,32 @@ func WhereClause(cols []string, start int) string {
|
|||
return strings.Join(ret, " AND ")
|
||||
}
|
||||
|
||||
// WhereMultiple is a version of Where that binds multiple checks together
|
||||
// with an or statement.
|
||||
// WhereMultiple(1, 2, "a", "b") = "(a=$1 and b=$2) or (a=$3 and b=$4)"
|
||||
func WhereMultiple(start, count int, cols ...string) string {
|
||||
if start == 0 {
|
||||
panic("0 is not a valid start number for whereMultiple")
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
for i := 0; i < count; i++ {
|
||||
if i != 0 {
|
||||
buf.WriteString(" OR ")
|
||||
}
|
||||
buf.WriteByte('(')
|
||||
for j, key := range cols {
|
||||
if j != 0 {
|
||||
buf.WriteString(" AND ")
|
||||
}
|
||||
fmt.Fprintf(buf, `"%s"=$%d`, key, start+i*len(cols)+j)
|
||||
}
|
||||
buf.WriteByte(')')
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// InClause generates SQL that could go inside an "IN ()" statement
|
||||
// $1, $2, $3
|
||||
func InClause(start, count int) string {
|
||||
|
|
|
@ -315,14 +315,14 @@ func TestWhereClause(t *testing.T) {
|
|||
}
|
||||
|
||||
for i, test := range tests {
|
||||
r := WhereClause(test.Cols, test.Start)
|
||||
r := WhereClause(test.Start, test.Cols...)
|
||||
if r != test.Should {
|
||||
t.Errorf("(%d) want: %s, got: %s\nTest: %#v", i, test.Should, r, test)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWherePrimaryKeyPanic(t *testing.T) {
|
||||
func TestWhereClausePanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
|
@ -331,7 +331,41 @@ func TestWherePrimaryKeyPanic(t *testing.T) {
|
|||
}
|
||||
}()
|
||||
|
||||
WhereClause(nil, 0)
|
||||
WhereClause(0)
|
||||
}
|
||||
|
||||
func TestWhereMultiple(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
Cols []string
|
||||
Start int
|
||||
Count int
|
||||
Should string
|
||||
}{
|
||||
{Cols: []string{"col1", "col2"}, Start: 2, Count: 2, Should: `("col1"=$2 AND "col2"=$3) OR ("col1"=$4 AND "col2"=$5)`},
|
||||
{Cols: []string{"col1", "col2"}, Start: 4, Count: 2, Should: `("col1"=$4 AND "col2"=$5) OR ("col1"=$6 AND "col2"=$7)`},
|
||||
{Cols: []string{"col1", "col2", "col3"}, Start: 4, Count: 1, Should: `("col1"=$4 AND "col2"=$5 AND "col3"=$6)`},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
r := WhereMultiple(test.Start, test.Count, test.Cols...)
|
||||
if r != test.Should {
|
||||
t.Errorf("(%d) want: %s, got: %s", i, test.Should, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhereMultiplePanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Error("did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
WhereMultiple(0, 0)
|
||||
}
|
||||
|
||||
func TestInClause(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue