Remove Or query mod, add builder whereClause test
* Add fixture tests/golden files
This commit is contained in:
parent
99b292b1ee
commit
af1b647fb4
7 changed files with 92 additions and 68 deletions
|
@ -1 +1 @@
|
||||||
DELETE FROM thing happy, upset as "sad", "fun", thing as stuff, "angry" as mad WHERE id=$1 and $thing=$2 OR stuff=$3;
|
DELETE FROM thing happy, upset as "sad", "fun", thing as stuff, "angry" as mad WHERE a=$1 AND b=$2 AND c=$3;
|
|
@ -1 +1 @@
|
||||||
DELETE FROM "thing" "happy" WHERE id=$1 and $thing=$2 AND stuff=$3;
|
DELETE FROM thing happy, upset as "sad", "fun", thing as stuff, "angry" as mad WHERE (id=$1 and $thing=$2) or stuff=$3;
|
|
@ -19,20 +19,6 @@ func SQL(sql string, args ...interface{}) QueryMod {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Or surrounds where clauses to join them with OR as opposed to AND
|
|
||||||
func Or(whereMods ...QueryMod) QueryMod {
|
|
||||||
return func(q *boil.Query) {
|
|
||||||
if len(whereMods) < 2 {
|
|
||||||
panic("Or requires at least two arguments")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, w := range whereMods {
|
|
||||||
w(q)
|
|
||||||
boil.SetLastWhereAsOr(q)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit the number of returned rows
|
// Limit the number of returned rows
|
||||||
func Limit(limit int) QueryMod {
|
func Limit(limit int) QueryMod {
|
||||||
return func(q *boil.Query) {
|
return func(q *boil.Query) {
|
||||||
|
|
|
@ -35,9 +35,8 @@ type Query struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type where struct {
|
type where struct {
|
||||||
clause string
|
clause string
|
||||||
orSeparator bool
|
args []interface{}
|
||||||
args []interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type plainSQL struct {
|
type plainSQL struct {
|
||||||
|
@ -180,11 +179,6 @@ func SetWhere(q *Query, clause string, args ...interface{}) {
|
||||||
q.where = append([]where(nil), where{clause: clause, args: args})
|
q.where = append([]where(nil), where{clause: clause, args: args})
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLastWhereAsOr sets the or separator for the last element in the where slice
|
|
||||||
func SetLastWhereAsOr(q *Query) {
|
|
||||||
q.where[len(q.where)-1].orSeparator = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyGroupBy on the query.
|
// ApplyGroupBy on the query.
|
||||||
func ApplyGroupBy(q *Query, clause string) {
|
func ApplyGroupBy(q *Query, clause string) {
|
||||||
q.groupBy = append(q.groupBy, clause)
|
q.groupBy = append(q.groupBy, clause)
|
||||||
|
|
|
@ -177,12 +177,7 @@ func whereClause(q *Query) (string, []interface{}) {
|
||||||
for i := 0; i < len(q.where); i++ {
|
for i := 0; i < len(q.where); i++ {
|
||||||
buf.WriteString(fmt.Sprintf("%s", q.where[i].clause))
|
buf.WriteString(fmt.Sprintf("%s", q.where[i].clause))
|
||||||
args = append(args, q.where[i].args...)
|
args = append(args, q.where[i].args...)
|
||||||
if i >= len(q.where)-1 {
|
if i < len(q.where)-1 {
|
||||||
continue
|
|
||||||
}
|
|
||||||
if q.where[i].orSeparator {
|
|
||||||
buf.WriteString(" OR ")
|
|
||||||
} else {
|
|
||||||
buf.WriteString(" AND ")
|
buf.WriteString(" AND ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,16 +48,16 @@ func TestBuildQuery(t *testing.T) {
|
||||||
delete: true,
|
delete: true,
|
||||||
from: []string{"thing happy", `upset as "sad"`, "fun", "thing as stuff", `"angry" as mad`},
|
from: []string{"thing happy", `upset as "sad"`, "fun", "thing as stuff", `"angry" as mad`},
|
||||||
where: []where{
|
where: []where{
|
||||||
where{clause: "id=$1 and $thing=$2", orSeparator: true, args: []interface{}{}},
|
where{clause: "a=$1", args: []interface{}{}},
|
||||||
where{clause: "stuff=$3", args: []interface{}{}},
|
where{clause: "b=$2", args: []interface{}{}},
|
||||||
|
where{clause: "c=$3", args: []interface{}{}},
|
||||||
},
|
},
|
||||||
}, nil},
|
}, nil},
|
||||||
{&Query{
|
{&Query{
|
||||||
delete: true,
|
delete: true,
|
||||||
from: []string{`"thing" "happy"`},
|
from: []string{"thing happy", `upset as "sad"`, "fun", "thing as stuff", `"angry" as mad`},
|
||||||
where: []where{
|
where: []where{
|
||||||
where{clause: "id=$1 and $thing=$2", orSeparator: false, args: []interface{}{}},
|
where{clause: "(id=$1 and $thing=$2) or stuff=$3", args: []interface{}{}},
|
||||||
where{clause: "stuff=$3", args: []interface{}{}},
|
|
||||||
},
|
},
|
||||||
}, nil},
|
}, nil},
|
||||||
}
|
}
|
||||||
|
@ -179,6 +179,88 @@ func TestWriteStars(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWhereClause(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
q Query
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
// Where("a=$1")
|
||||||
|
{
|
||||||
|
q: Query{
|
||||||
|
where: []where{
|
||||||
|
where{clause: "a=$1"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: " WHERE a=$1",
|
||||||
|
},
|
||||||
|
// Where("a=$1 OR b=$2")
|
||||||
|
{
|
||||||
|
q: Query{
|
||||||
|
where: []where{
|
||||||
|
where{clause: "a=$1 OR b=$2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: " WHERE a=$1 OR b=$2",
|
||||||
|
},
|
||||||
|
// Where("a=$1", "b=$2")
|
||||||
|
{
|
||||||
|
q: Query{
|
||||||
|
where: []where{
|
||||||
|
where{clause: "a=$1"},
|
||||||
|
where{clause: "b=$2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: " WHERE a=$1 AND b=$2",
|
||||||
|
},
|
||||||
|
// Where("(a=$1 AND b=$2) OR c=$3")
|
||||||
|
{
|
||||||
|
q: Query{
|
||||||
|
where: []where{
|
||||||
|
where{clause: "(a=$1 AND b=$2) OR c=$3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: " WHERE (a=$1 AND b=$2) OR c=$3",
|
||||||
|
},
|
||||||
|
// Where("a=$1 OR b=$2", "c=$3 OR d=$4 OR e=$5")
|
||||||
|
{
|
||||||
|
q: Query{
|
||||||
|
where: []where{
|
||||||
|
where{clause: "(a=$1 OR b=$2)"},
|
||||||
|
where{clause: "(c=$3 OR d=$4 OR e=$5)"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: " WHERE (a=$1 OR b=$2) AND (c=$3 OR d=$4 OR e=$5)",
|
||||||
|
},
|
||||||
|
// Where("(a=$1 AND b=$2) OR (c=$3 AND d=$4 AND e=$5) OR f=$6 OR f=$7")
|
||||||
|
{
|
||||||
|
q: Query{
|
||||||
|
where: []where{
|
||||||
|
where{clause: "(a=$1 AND b=$2) OR (c=$3 AND d=$4 AND e=$5) OR f=$6 OR g=$7"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: " WHERE (a=$1 AND b=$2) OR (c=$3 AND d=$4 AND e=$5) OR f=$6 OR g=$7",
|
||||||
|
},
|
||||||
|
// Where("(a=$1 AND b=$2) OR (c=$3 AND d=$4 OR e=$5) OR f=$6 OR g=$7")
|
||||||
|
{
|
||||||
|
q: Query{
|
||||||
|
where: []where{
|
||||||
|
where{clause: "(a=$1 AND b=$2) OR (c=$3 AND d=$4 OR e=$5) OR f=$6 OR g=$7"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: " WHERE (a=$1 AND b=$2) OR (c=$3 AND d=$4 OR e=$5) OR f=$6 OR g=$7",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
result, _ := whereClause(&test.q)
|
||||||
|
if result != test.expect {
|
||||||
|
t.Errorf("%d) Mismatch between expect and result:\n%s\n%s\n", i, test.expect, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteAsStatements(t *testing.T) {
|
func TestWriteAsStatements(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
|
@ -6,39 +6,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSetLastWhereAsOr(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
q := &Query{}
|
|
||||||
|
|
||||||
AppendWhere(q, "")
|
|
||||||
|
|
||||||
if q.where[0].orSeparator {
|
|
||||||
t.Errorf("Do not want or separator")
|
|
||||||
}
|
|
||||||
|
|
||||||
SetLastWhereAsOr(q)
|
|
||||||
|
|
||||||
if len(q.where) != 1 {
|
|
||||||
t.Errorf("Want len 1")
|
|
||||||
}
|
|
||||||
if !q.where[0].orSeparator {
|
|
||||||
t.Errorf("Want or separator")
|
|
||||||
}
|
|
||||||
|
|
||||||
AppendWhere(q, "")
|
|
||||||
SetLastWhereAsOr(q)
|
|
||||||
|
|
||||||
if len(q.where) != 2 {
|
|
||||||
t.Errorf("Want len 2")
|
|
||||||
}
|
|
||||||
if q.where[0].orSeparator != true {
|
|
||||||
t.Errorf("Expected true")
|
|
||||||
}
|
|
||||||
if q.where[1].orSeparator != true {
|
|
||||||
t.Errorf("Expected true")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetLimit(t *testing.T) {
|
func TestSetLimit(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue