2016-09-15 05:45:09 +02:00
|
|
|
package queries
|
2016-08-06 23:42:22 +02:00
|
|
|
|
2016-08-07 22:37:51 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
|
|
|
var writeGoldenFiles = flag.Bool(
|
|
|
|
"test.golden",
|
|
|
|
false,
|
|
|
|
"Write golden files.",
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildQuery(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
q *Query
|
|
|
|
args []interface{}
|
|
|
|
}{
|
|
|
|
{&Query{from: []string{"t"}}, nil},
|
|
|
|
{&Query{from: []string{"q"}, limit: 5, offset: 6}, nil},
|
|
|
|
{&Query{from: []string{"q"}, orderBy: []string{"a ASC", "b DESC"}}, nil},
|
|
|
|
{&Query{from: []string{"t"}, selectCols: []string{"count(*) as ab, thing as bd", `"stuff"`}}, nil},
|
|
|
|
{&Query{from: []string{"a", "b"}, selectCols: []string{"count(*) as ab, thing as bd", `"stuff"`}}, nil},
|
|
|
|
{&Query{
|
|
|
|
selectCols: []string{"a.happy", "r.fun", "q"},
|
|
|
|
from: []string{"happiness as a"},
|
|
|
|
joins: []join{{clause: "rainbows r on a.id = r.happy_id"}},
|
|
|
|
}, nil},
|
2016-08-07 23:09:56 +02:00
|
|
|
{&Query{
|
|
|
|
from: []string{"happiness as a"},
|
|
|
|
joins: []join{{clause: "rainbows r on a.id = r.happy_id"}},
|
|
|
|
}, nil},
|
2016-08-12 19:28:46 +02:00
|
|
|
{&Query{
|
|
|
|
from: []string{"videos"},
|
|
|
|
joins: []join{{
|
|
|
|
clause: "(select id from users where deleted = ?) u on u.id = videos.user_id",
|
|
|
|
args: []interface{}{true},
|
|
|
|
}},
|
|
|
|
where: []where{{clause: "videos.deleted = ?", args: []interface{}{false}}},
|
|
|
|
}, []interface{}{true, false}},
|
2016-08-08 09:28:01 +02:00
|
|
|
{&Query{
|
|
|
|
from: []string{"a"},
|
|
|
|
groupBy: []string{"id", "name"},
|
2016-08-12 19:08:09 +02:00
|
|
|
where: []where{
|
|
|
|
{clause: "a=? or b=?", args: []interface{}{1, 2}},
|
|
|
|
{clause: "c=?", args: []interface{}{3}},
|
|
|
|
},
|
|
|
|
having: []having{
|
|
|
|
{clause: "id <> ?", args: []interface{}{1}},
|
|
|
|
{clause: "length(name, ?) > ?", args: []interface{}{"utf8", 5}},
|
|
|
|
},
|
|
|
|
}, []interface{}{1, 2, 3, 1, "utf8", 5}},
|
2016-08-09 12:19:42 +02:00
|
|
|
{&Query{
|
|
|
|
delete: true,
|
|
|
|
from: []string{"thing happy", `upset as "sad"`, "fun", "thing as stuff", `"angry" as mad`},
|
|
|
|
where: []where{
|
2016-09-20 08:55:36 +02:00
|
|
|
{clause: "a=?", args: []interface{}{1}},
|
|
|
|
{clause: "b=?", args: []interface{}{2}},
|
|
|
|
{clause: "c=?", args: []interface{}{3}},
|
2016-08-09 12:19:42 +02:00
|
|
|
},
|
2016-09-20 08:55:36 +02:00
|
|
|
}, []interface{}{1, 2, 3}},
|
2016-08-09 12:19:42 +02:00
|
|
|
{&Query{
|
|
|
|
delete: true,
|
2016-08-09 14:29:59 +02:00
|
|
|
from: []string{"thing happy", `upset as "sad"`, "fun", "thing as stuff", `"angry" as mad`},
|
2016-08-09 12:19:42 +02:00
|
|
|
where: []where{
|
2016-09-20 08:55:36 +02:00
|
|
|
{clause: "(id=? and thing=?) or stuff=?", args: []interface{}{1, 2, 3}},
|
2016-08-09 12:19:42 +02:00
|
|
|
},
|
2016-08-11 10:23:47 +02:00
|
|
|
limit: 5,
|
2016-09-20 08:55:36 +02:00
|
|
|
}, []interface{}{1, 2, 3}},
|
2016-08-11 10:23:47 +02:00
|
|
|
{&Query{
|
|
|
|
from: []string{"thing happy", `"fun"`, `stuff`},
|
|
|
|
update: map[string]interface{}{
|
|
|
|
"col1": 1,
|
|
|
|
`"col2"`: 2,
|
|
|
|
`"fun".col3`: 3,
|
|
|
|
},
|
|
|
|
where: []where{
|
2016-08-12 19:08:09 +02:00
|
|
|
{clause: "aa=? or bb=? or cc=?", orSeparator: true, args: []interface{}{4, 5, 6}},
|
|
|
|
{clause: "dd=? or ee=? or ff=? and gg=?", args: []interface{}{7, 8, 9, 10}},
|
2016-08-11 10:23:47 +02:00
|
|
|
},
|
|
|
|
limit: 5,
|
2016-08-12 19:08:09 +02:00
|
|
|
}, []interface{}{2, 3, 1, 4, 5, 6, 7, 8, 9, 10}},
|
2016-09-01 05:14:35 +02:00
|
|
|
{&Query{from: []string{"cats"}, joins: []join{{JoinInner, "dogs d on d.cat_id = cats.id", nil}}}, nil},
|
|
|
|
{&Query{from: []string{"cats c"}, joins: []join{{JoinInner, "dogs d on d.cat_id = cats.id", nil}}}, nil},
|
|
|
|
{&Query{from: []string{"cats as c"}, joins: []join{{JoinInner, "dogs d on d.cat_id = cats.id", nil}}}, nil},
|
|
|
|
{&Query{from: []string{"cats as c", "dogs as d"}, joins: []join{{JoinInner, "dogs d on d.cat_id = cats.id", nil}}}, nil},
|
2016-08-07 22:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
filename := filepath.Join("_fixtures", fmt.Sprintf("%02d.sql", i))
|
2016-09-09 21:15:50 +02:00
|
|
|
test.q.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
|
2016-08-07 22:37:51 +02:00
|
|
|
out, args := buildQuery(test.q)
|
|
|
|
|
|
|
|
if *writeGoldenFiles {
|
|
|
|
err := ioutil.WriteFile(filename, []byte(out), 0664)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to write golden file %s: %s\n", filename, err)
|
|
|
|
}
|
|
|
|
t.Logf("wrote golden file: %s\n", filename)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
byt, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read golden file %q: %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(bytes.TrimSpace(byt)) != out {
|
|
|
|
t.Errorf("[%02d] Test failed:\nWant:\n%s\nGot:\n%s", i, byt, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(args, test.args) {
|
|
|
|
t.Errorf("[%02d] Test failed:\nWant:\n%s\nGot:\n%s", i, spew.Sdump(test.args), spew.Sdump(args))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-06 23:42:22 +02:00
|
|
|
|
2016-08-07 23:09:56 +02:00
|
|
|
func TestWriteStars(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
In Query
|
|
|
|
Out []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
In: Query{from: []string{`a`}},
|
|
|
|
Out: []string{`"a".*`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: Query{from: []string{`a as b`}},
|
|
|
|
Out: []string{`"b".*`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: Query{from: []string{`a as b`, `c`}},
|
|
|
|
Out: []string{`"b".*`, `"c".*`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: Query{from: []string{`a as b`, `c as d`}},
|
|
|
|
Out: []string{`"b".*`, `"d".*`},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-09-09 21:15:50 +02:00
|
|
|
test.In.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
|
2016-08-07 23:09:56 +02:00
|
|
|
selects := writeStars(&test.In)
|
|
|
|
if !reflect.DeepEqual(selects, test.Out) {
|
|
|
|
t.Errorf("writeStar test fail %d\nwant: %v\ngot: %v", i, test.Out, selects)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 14:29:59 +02:00
|
|
|
func TestWhereClause(t *testing.T) {
|
2016-08-13 16:54:46 +02:00
|
|
|
t.Parallel()
|
2016-08-09 14:29:59 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
q Query
|
|
|
|
expect string
|
|
|
|
}{
|
2016-08-11 08:44:15 +02:00
|
|
|
// Or("a=?")
|
2016-08-09 14:29:59 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
2016-09-05 13:47:17 +02:00
|
|
|
where: []where{{clause: "a=?", orSeparator: true}},
|
2016-08-11 08:44:15 +02:00
|
|
|
},
|
|
|
|
expect: " WHERE (a=$1)",
|
|
|
|
},
|
|
|
|
// Where("a=?")
|
|
|
|
{
|
|
|
|
q: Query{
|
2016-09-05 13:47:17 +02:00
|
|
|
where: []where{{clause: "a=?"}},
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
expect: " WHERE (a=$1)",
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
// Where("(a=?)")
|
|
|
|
{
|
|
|
|
q: Query{
|
2016-09-05 13:47:17 +02:00
|
|
|
where: []where{{clause: "(a=?)"}},
|
2016-08-11 08:44:15 +02:00
|
|
|
},
|
|
|
|
expect: " WHERE ((a=$1))",
|
|
|
|
},
|
|
|
|
// Where("((a=? OR b=?))")
|
|
|
|
{
|
|
|
|
q: Query{
|
2016-09-05 13:47:17 +02:00
|
|
|
where: []where{{clause: "((a=? OR b=?))"}},
|
2016-08-11 08:44:15 +02:00
|
|
|
},
|
|
|
|
expect: " WHERE (((a=$1 OR b=$2)))",
|
|
|
|
},
|
|
|
|
// Where("(a=?)", Or("(b=?)")
|
2016-08-09 14:29:59 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
where: []where{
|
2016-09-05 13:47:17 +02:00
|
|
|
{clause: "(a=?)"},
|
|
|
|
{clause: "(b=?)", orSeparator: true},
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
expect: " WHERE ((a=$1)) OR ((b=$2))",
|
|
|
|
},
|
|
|
|
// Where("a=? OR b=?")
|
|
|
|
{
|
|
|
|
q: Query{
|
2016-09-05 13:47:17 +02:00
|
|
|
where: []where{{clause: "a=? OR b=?"}},
|
2016-08-11 08:44:15 +02:00
|
|
|
},
|
|
|
|
expect: " WHERE (a=$1 OR b=$2)",
|
|
|
|
},
|
|
|
|
// Where("a=?"), Where("b=?")
|
|
|
|
{
|
|
|
|
q: Query{
|
2016-09-05 13:47:17 +02:00
|
|
|
where: []where{{clause: "a=?"}, {clause: "b=?"}},
|
2016-08-11 08:44:15 +02:00
|
|
|
},
|
|
|
|
expect: " WHERE (a=$1) AND (b=$2)",
|
|
|
|
},
|
|
|
|
// Where("(a=? AND b=?) OR c=?")
|
|
|
|
{
|
|
|
|
q: Query{
|
2016-09-05 13:47:17 +02:00
|
|
|
where: []where{{clause: "(a=? AND b=?) OR c=?"}},
|
2016-08-11 08:44:15 +02:00
|
|
|
},
|
|
|
|
expect: " WHERE ((a=$1 AND b=$2) OR c=$3)",
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
// Where("a=? OR b=?"), Where("c=? OR d=? OR e=?")
|
2016-08-09 14:29:59 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
where: []where{
|
2016-09-05 13:47:17 +02:00
|
|
|
{clause: "(a=? OR b=?)"},
|
|
|
|
{clause: "(c=? OR d=? OR e=?)"},
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
expect: " WHERE ((a=$1 OR b=$2)) AND ((c=$3 OR d=$4 OR e=$5))",
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
// Where("(a=? AND b=?) OR (c=? AND d=? AND e=?) OR f=? OR f=?")
|
2016-08-09 14:29:59 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
where: []where{
|
2016-09-05 13:47:17 +02:00
|
|
|
{clause: "(a=? AND b=?) OR (c=? AND d=? AND e=?) OR f=? OR g=?"},
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
expect: " WHERE ((a=$1 AND b=$2) OR (c=$3 AND d=$4 AND e=$5) OR f=$6 OR g=$7)",
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
// Where("(a=? AND b=?) OR (c=? AND d=? OR e=?) OR f=? OR g=?")
|
2016-08-09 14:29:59 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
where: []where{
|
2016-09-05 13:47:17 +02:00
|
|
|
{clause: "(a=? AND b=?) OR (c=? AND d=? OR e=?) OR f=? OR g=?"},
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
expect: " WHERE ((a=$1 AND b=$2) OR (c=$3 AND d=$4 OR e=$5) OR f=$6 OR g=$7)",
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
// Where("a=? or b=?"), Or("c=? and d=?"), Or("e=? or f=?")
|
2016-08-09 14:29:59 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
where: []where{
|
2016-09-05 13:47:17 +02:00
|
|
|
{clause: "a=? or b=?", orSeparator: true},
|
|
|
|
{clause: "c=? and d=?", orSeparator: true},
|
|
|
|
{clause: "e=? or f=?", orSeparator: true},
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
expect: " WHERE (a=$1 or b=$2) OR (c=$3 and d=$4) OR (e=$5 or f=$6)",
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
2016-08-11 08:44:15 +02:00
|
|
|
// Where("a=? or b=?"), Or("c=? and d=?"), Or("e=? or f=?")
|
2016-08-09 14:29:59 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
where: []where{
|
2016-09-05 13:47:17 +02:00
|
|
|
{clause: "a=? or b=?"},
|
|
|
|
{clause: "c=? and d=?", orSeparator: true},
|
|
|
|
{clause: "e=? or f=?"},
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-18 19:21:53 +02:00
|
|
|
expect: " WHERE (a=$1 or b=$2) OR (c=$3 and d=$4) AND (e=$5 or f=$6)",
|
2016-08-09 14:29:59 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-09-09 21:15:50 +02:00
|
|
|
test.q.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
|
2016-08-11 10:23:47 +02:00
|
|
|
result, _ := whereClause(&test.q, 1)
|
2016-08-09 14:29:59 +02:00
|
|
|
if result != test.expect {
|
|
|
|
t.Errorf("%d) Mismatch between expect and result:\n%s\n%s\n", i, test.expect, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
func TestInClause(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
q Query
|
|
|
|
expect string
|
2016-08-18 19:21:53 +02:00
|
|
|
args []interface{}
|
2016-08-17 07:19:23 +02:00
|
|
|
}{
|
2016-08-18 19:21:53 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{{clause: "a in ?", args: []interface{}{}, orSeparator: true}},
|
|
|
|
},
|
|
|
|
expect: ` WHERE "a" IN ()`,
|
|
|
|
},
|
2016-08-17 07:19:23 +02:00
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{{clause: "a in ?", args: []interface{}{1}, orSeparator: true}},
|
|
|
|
},
|
2016-08-18 19:21:53 +02:00
|
|
|
expect: ` WHERE "a" IN ($1)`,
|
|
|
|
args: []interface{}{1},
|
2016-08-17 07:19:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{{clause: "a in ?", args: []interface{}{1, 2, 3}}},
|
|
|
|
},
|
2016-08-18 19:21:53 +02:00
|
|
|
expect: ` WHERE "a" IN ($1,$2,$3)`,
|
|
|
|
args: []interface{}{1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{{clause: "? in ?", args: []interface{}{1, 2, 3}}},
|
|
|
|
},
|
|
|
|
expect: " WHERE $1 IN ($2,$3)",
|
|
|
|
args: []interface{}{1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{{clause: "( ? , ? ) in ( ? )", orSeparator: true, args: []interface{}{"a", "b", 1, 2, 3, 4}}},
|
|
|
|
},
|
|
|
|
expect: " WHERE ( $1 , $2 ) IN ( (($3,$4),($5,$6)) )",
|
|
|
|
args: []interface{}{"a", "b", 1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{{clause: `("a")in(?)`, orSeparator: true, args: []interface{}{1, 2, 3}}},
|
|
|
|
},
|
|
|
|
expect: ` WHERE ("a") IN (($1,$2,$3))`,
|
|
|
|
args: []interface{}{1, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{{clause: `("a")in?`, args: []interface{}{1}}},
|
|
|
|
},
|
|
|
|
expect: ` WHERE ("a") IN ($1)`,
|
|
|
|
args: []interface{}{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
where: []where{
|
|
|
|
{clause: "a=?", args: []interface{}{1}},
|
|
|
|
},
|
|
|
|
in: []in{
|
|
|
|
{clause: `?,?,"name" in ?`, orSeparator: true, args: []interface{}{"c", "d", 3, 4, 5, 6, 7, 8}},
|
|
|
|
{clause: `?,?,"name" in ?`, orSeparator: true, args: []interface{}{"e", "f", 9, 10, 11, 12, 13, 14}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: ` OR $1,$2,"name" IN (($3,$4,$5),($6,$7,$8)) OR $9,$10,"name" IN (($11,$12,$13),($14,$15,$16))`,
|
|
|
|
args: []interface{}{"c", "d", 3, 4, 5, 6, 7, 8, "e", "f", 9, 10, 11, 12, 13, 14},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{
|
|
|
|
{clause: `("a")in`, args: []interface{}{1}},
|
|
|
|
{clause: `("a")in?`, orSeparator: true, args: []interface{}{1}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: ` WHERE ("a")in OR ("a") IN ($1)`,
|
|
|
|
args: []interface{}{1, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{
|
|
|
|
{clause: `\?,\? in \?`, args: []interface{}{1}},
|
|
|
|
{clause: `\?,\?in \?`, orSeparator: true, args: []interface{}{1}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: ` WHERE ?,? IN ? OR ?,? IN ?`,
|
|
|
|
args: []interface{}{1, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{
|
|
|
|
{clause: `("a")in`, args: []interface{}{1}},
|
|
|
|
{clause: `("a") in thing`, args: []interface{}{1, 2, 3}},
|
|
|
|
{clause: `("a")in?`, orSeparator: true, args: []interface{}{4, 5, 6}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: ` WHERE ("a")in AND ("a") IN thing OR ("a") IN ($1,$2,$3)`,
|
|
|
|
args: []interface{}{1, 1, 2, 3, 4, 5, 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{
|
|
|
|
{clause: `("a")in?`, orSeparator: true, args: []interface{}{4, 5, 6}},
|
|
|
|
{clause: `("a") in thing`, args: []interface{}{1, 2, 3}},
|
|
|
|
{clause: `("a")in`, args: []interface{}{1}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: ` WHERE ("a") IN ($1,$2,$3) AND ("a") IN thing AND ("a")in`,
|
|
|
|
args: []interface{}{4, 5, 6, 1, 2, 3, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
q: Query{
|
|
|
|
in: []in{
|
|
|
|
{clause: `("a")in?`, orSeparator: true, args: []interface{}{4, 5, 6}},
|
|
|
|
{clause: `("a")in`, args: []interface{}{1}},
|
|
|
|
{clause: `("a") in thing`, args: []interface{}{1, 2, 3}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: ` WHERE ("a") IN ($1,$2,$3) AND ("a")in AND ("a") IN thing`,
|
|
|
|
args: []interface{}{4, 5, 6, 1, 1, 2, 3},
|
|
|
|
},
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-09-09 21:15:50 +02:00
|
|
|
test.q.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
|
2016-08-18 19:21:53 +02:00
|
|
|
result, args := inClause(&test.q, 1)
|
2016-08-17 07:19:23 +02:00
|
|
|
if result != test.expect {
|
|
|
|
t.Errorf("%d) Mismatch between expect and result:\n%s\n%s\n", i, test.expect, result)
|
|
|
|
}
|
2016-08-18 19:21:53 +02:00
|
|
|
if !reflect.DeepEqual(args, test.args) {
|
|
|
|
t.Errorf("%d) Mismatch between expected args:\n%#v\n%#v\n", i, test.args, args)
|
|
|
|
}
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:54:46 +02:00
|
|
|
func TestConvertQuestionMarks(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
clause string
|
|
|
|
start int
|
|
|
|
expect string
|
2016-08-18 19:21:53 +02:00
|
|
|
count int
|
2016-08-13 16:54:46 +02:00
|
|
|
}{
|
2016-08-18 19:21:53 +02:00
|
|
|
{clause: "hello friend", start: 1, expect: "hello friend", count: 0},
|
|
|
|
{clause: "thing=?", start: 2, expect: "thing=$2", count: 1},
|
|
|
|
{clause: "thing=? and stuff=? and happy=?", start: 2, expect: "thing=$2 and stuff=$3 and happy=$4", count: 3},
|
|
|
|
{clause: `thing \? stuff`, start: 2, expect: `thing ? stuff`, count: 0},
|
|
|
|
{clause: `thing \? stuff and happy \? fun`, start: 2, expect: `thing ? stuff and happy ? fun`, count: 0},
|
2016-08-13 16:54:46 +02:00
|
|
|
{
|
|
|
|
clause: `thing \? stuff ? happy \? and mad ? fun \? \? \?`,
|
|
|
|
start: 2,
|
|
|
|
expect: `thing ? stuff $2 happy ? and mad $3 fun ? ? ?`,
|
2016-08-18 19:21:53 +02:00
|
|
|
count: 2,
|
2016-08-13 16:54:46 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
clause: `thing ? stuff ? happy \? fun \? ? ?`,
|
|
|
|
start: 1,
|
|
|
|
expect: `thing $1 stuff $2 happy ? fun ? $3 $4`,
|
2016-08-18 19:21:53 +02:00
|
|
|
count: 4,
|
2016-08-13 16:54:46 +02:00
|
|
|
},
|
2016-08-18 19:21:53 +02:00
|
|
|
{clause: `?`, start: 1, expect: `$1`, count: 1},
|
|
|
|
{clause: `???`, start: 1, expect: `$1$2$3`, count: 3},
|
2016-08-17 07:19:23 +02:00
|
|
|
{clause: `\?`, start: 1, expect: `?`},
|
|
|
|
{clause: `\?\?\?`, start: 1, expect: `???`},
|
2016-08-18 19:21:53 +02:00
|
|
|
{clause: `\??\??\??`, start: 1, expect: `?$1?$2?$3`, count: 3},
|
|
|
|
{clause: `?\??\??\?`, start: 1, expect: `$1?$2?$3?`, count: 3},
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-08-18 19:21:53 +02:00
|
|
|
res, count := convertQuestionMarks(test.clause, test.start)
|
2016-08-17 07:19:23 +02:00
|
|
|
if res != test.expect {
|
|
|
|
t.Errorf("%d) Mismatch between expect and result:\n%s\n%s\n", i, test.expect, res)
|
|
|
|
}
|
2016-08-18 19:21:53 +02:00
|
|
|
if count != test.count {
|
|
|
|
t.Errorf("%d) Expected count %d, got %d", i, test.count, count)
|
|
|
|
}
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertInQuestionMarks(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
clause string
|
|
|
|
start int
|
2016-08-18 19:21:53 +02:00
|
|
|
group int
|
|
|
|
total int
|
2016-08-17 07:19:23 +02:00
|
|
|
expect string
|
|
|
|
}{
|
2016-08-18 19:21:53 +02:00
|
|
|
{clause: "?", expect: "(($1,$2,$3),($4,$5,$6),($7,$8,$9))", start: 1, total: 9, group: 3},
|
|
|
|
{clause: "?", expect: "(($2,$3),($4))", start: 2, total: 3, group: 2},
|
|
|
|
{clause: "hello friend", start: 1, expect: "hello friend", total: 0, group: 1},
|
|
|
|
{clause: "thing ? thing", start: 2, expect: "thing ($2,$3) thing", total: 2, group: 1},
|
|
|
|
{clause: "thing?thing", start: 2, expect: "thing($2)thing", total: 1, group: 1},
|
|
|
|
{clause: `thing \? stuff`, start: 2, expect: `thing ? stuff`, total: 0, group: 1},
|
|
|
|
{clause: `thing \? stuff and happy \? fun`, start: 2, expect: `thing ? stuff and happy ? fun`, total: 0, group: 1},
|
|
|
|
{clause: "thing ? thing ? thing", start: 1, expect: "thing ($1,$2,$3) thing ? thing", total: 3, group: 1},
|
|
|
|
{clause: `?`, start: 1, expect: `($1)`, total: 1, group: 1},
|
|
|
|
{clause: `???`, start: 1, expect: `($1,$2,$3)??`, total: 3, group: 1},
|
|
|
|
{clause: `\?`, start: 1, expect: `?`, total: 0, group: 1},
|
|
|
|
{clause: `\?\?\?`, start: 1, expect: `???`, total: 0, group: 1},
|
|
|
|
{clause: `\??\??\??`, start: 1, expect: `?($1,$2,$3)????`, total: 3, group: 1},
|
|
|
|
{clause: `?\??\??\?`, start: 1, expect: `($1,$2,$3)?????`, total: 3, group: 1},
|
2016-08-13 16:54:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-09-09 21:15:50 +02:00
|
|
|
res, count := convertInQuestionMarks(true, test.clause, test.start, test.group, test.total)
|
2016-08-13 16:54:46 +02:00
|
|
|
if res != test.expect {
|
|
|
|
t.Errorf("%d) Mismatch between expect and result:\n%s\n%s\n", i, test.expect, res)
|
|
|
|
}
|
2016-08-18 19:21:53 +02:00
|
|
|
if count != test.total {
|
|
|
|
t.Errorf("%d) Expected %d, got %d", i, test.total, count)
|
|
|
|
}
|
2016-08-13 16:54:46 +02:00
|
|
|
}
|
2016-09-09 21:15:50 +02:00
|
|
|
|
|
|
|
res, count := convertInQuestionMarks(false, "?", 1, 3, 9)
|
|
|
|
if res != "((?,?,?),(?,?,?),(?,?,?))" {
|
|
|
|
t.Errorf("Mismatch between expected and result: %s", res)
|
|
|
|
}
|
|
|
|
if count != 9 {
|
|
|
|
t.Errorf("Expected 9 results, got %d", count)
|
|
|
|
}
|
2016-08-13 16:54:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 23:09:56 +02:00
|
|
|
func TestWriteAsStatements(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
query := Query{
|
|
|
|
selectCols: []string{
|
|
|
|
`a`,
|
|
|
|
`a.fun`,
|
|
|
|
`"b"."fun"`,
|
|
|
|
`"b".fun`,
|
|
|
|
`b."fun"`,
|
|
|
|
`a.clown.run`,
|
|
|
|
`COUNT(a)`,
|
|
|
|
},
|
2016-09-09 21:15:50 +02:00
|
|
|
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
|
2016-08-07 23:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expect := []string{
|
|
|
|
`"a"`,
|
|
|
|
`"a"."fun" as "a.fun"`,
|
|
|
|
`"b"."fun" as "b.fun"`,
|
|
|
|
`"b"."fun" as "b.fun"`,
|
|
|
|
`"b"."fun" as "b.fun"`,
|
|
|
|
`"a"."clown"."run" as "a.clown.run"`,
|
|
|
|
`COUNT(a)`,
|
|
|
|
}
|
|
|
|
|
|
|
|
gots := writeAsStatements(&query)
|
|
|
|
|
|
|
|
for i, got := range gots {
|
|
|
|
if expect[i] != got {
|
|
|
|
t.Errorf(`%d) want: %s, got: %s`, i, expect[i], got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|