123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- // Copyright 2016 The Xorm Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package builder
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- )
- type MyInt int
- func TestBuilderCond(t *testing.T) {
- var cases = []struct {
- cond Cond
- sql string
- args []interface{}
- }{
- {
- Eq{"a": 1}.And(Like{"b", "c"}).Or(Eq{"a": 2}.And(Like{"b", "g"})),
- "(a=? AND b LIKE ?) OR (a=? AND b LIKE ?)",
- []interface{}{1, "%c%", 2, "%g%"},
- },
- {
- Eq{"a": 1}.Or(Like{"b", "c"}).And(Eq{"a": 2}.Or(Like{"b", "g"})),
- "(a=? OR b LIKE ?) AND (a=? OR b LIKE ?)",
- []interface{}{1, "%c%", 2, "%g%"},
- },
- {
- Eq{"d": []string{"e", "f"}},
- "d IN (?,?)",
- []interface{}{"e", "f"},
- },
- {
- Eq{"e": Select("id").From("f").Where(Eq{"g": 1})},
- "e=(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Eq{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
- "e=(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Like{"a", "%1"}.And(Like{"b", "%2"}),
- "a LIKE ? AND b LIKE ?",
- []interface{}{"%1", "%2"},
- },
- {
- Like{"a", "%1"}.Or(Like{"b", "%2"}),
- "a LIKE ? OR b LIKE ?",
- []interface{}{"%1", "%2"},
- },
- {
- Neq{"d": "e"}.Or(Neq{"f": "g"}),
- "d<>? OR f<>?",
- []interface{}{"e", "g"},
- },
- {
- Neq{"d": []string{"e", "f"}},
- "d NOT IN (?,?)",
- []interface{}{"e", "f"},
- },
- {
- Neq{"e": Select("id").From("f").Where(Eq{"g": 1})},
- "e<>(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Neq{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
- "e<>(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Lt{"d": 3},
- "d<?",
- []interface{}{3},
- },
- {
- Lt{"d": 3}.And(Lt{"e": 4}),
- "d<? AND e<?",
- []interface{}{3, 4},
- },
- {
- Lt{"d": 3}.Or(Lt{"e": 4}),
- "d<? OR e<?",
- []interface{}{3, 4},
- },
- {
- Lt{"e": Select("id").From("f").Where(Eq{"g": 1})},
- "e<(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Lt{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
- "e<(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Lte{"d": 3},
- "d<=?",
- []interface{}{3},
- },
- {
- Lte{"d": 3}.And(Lte{"e": 4}),
- "d<=? AND e<=?",
- []interface{}{3, 4},
- },
- {
- Lte{"d": 3}.Or(Lte{"e": 4}),
- "d<=? OR e<=?",
- []interface{}{3, 4},
- },
- {
- Lte{"e": Select("id").From("f").Where(Eq{"g": 1})},
- "e<=(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Lte{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
- "e<=(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Gt{"d": 3},
- "d>?",
- []interface{}{3},
- },
- {
- Gt{"d": 3}.And(Gt{"e": 4}),
- "d>? AND e>?",
- []interface{}{3, 4},
- },
- {
- Gt{"d": 3}.Or(Gt{"e": 4}),
- "d>? OR e>?",
- []interface{}{3, 4},
- },
- {
- Gt{"e": Select("id").From("f").Where(Eq{"g": 1})},
- "e>(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Gt{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
- "e>(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Gte{"d": 3},
- "d>=?",
- []interface{}{3},
- },
- {
- Gte{"d": 3}.And(Gte{"e": 4}),
- "d>=? AND e>=?",
- []interface{}{3, 4},
- },
- {
- Gte{"d": 3}.Or(Gte{"e": 4}),
- "d>=? OR e>=?",
- []interface{}{3, 4},
- },
- {
- Gte{"e": Select("id").From("f").Where(Eq{"g": 1})},
- "e>=(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Gte{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
- "e>=(SELECT id FROM f WHERE g=?)",
- []interface{}{1},
- },
- {
- Between{"d", 0, 2},
- "d BETWEEN ? AND ?",
- []interface{}{0, 2},
- },
- {
- Between{"d", 0, Expr("CAST('2003-01-01' AS DATE)")},
- "d BETWEEN ? AND CAST('2003-01-01' AS DATE)",
- []interface{}{0},
- },
- {
- Between{"d", Expr("CAST('2003-01-01' AS DATE)"), 2},
- "d BETWEEN CAST('2003-01-01' AS DATE) AND ?",
- []interface{}{2},
- },
- {
- Between{"d", Expr("CAST('2003-01-01' AS DATE)"), Expr("CAST('2003-01-01' AS DATE)")},
- "d BETWEEN CAST('2003-01-01' AS DATE) AND CAST('2003-01-01' AS DATE)",
- []interface{}{},
- },
- {
- Between{"d", 0, 2}.And(Between{"e", 3, 4}),
- "d BETWEEN ? AND ? AND e BETWEEN ? AND ?",
- []interface{}{0, 2, 3, 4},
- },
- {
- Between{"d", 0, 2}.Or(Between{"e", 3, 4}),
- "d BETWEEN ? AND ? OR e BETWEEN ? AND ?",
- []interface{}{0, 2, 3, 4},
- },
- {
- Expr("a < ?", 1),
- "a < ?",
- []interface{}{1},
- },
- {
- Expr("a < ?", 1).And(Eq{"b": 2}),
- "(a < ?) AND b=?",
- []interface{}{1, 2},
- },
- {
- Expr("a < ?", 1).Or(Neq{"b": 2}),
- "(a < ?) OR b<>?",
- []interface{}{1, 2},
- },
- {
- IsNull{"d"},
- "d IS NULL",
- []interface{}{},
- },
- {
- IsNull{"d"}.And(IsNull{"e"}),
- "d IS NULL AND e IS NULL",
- []interface{}{},
- },
- {
- IsNull{"d"}.Or(IsNull{"e"}),
- "d IS NULL OR e IS NULL",
- []interface{}{},
- },
- {
- NotNull{"d"},
- "d IS NOT NULL",
- []interface{}{},
- },
- {
- NotNull{"d"}.And(NotNull{"e"}),
- "d IS NOT NULL AND e IS NOT NULL",
- []interface{}{},
- },
- {
- NotNull{"d"}.Or(NotNull{"e"}),
- "d IS NOT NULL OR e IS NOT NULL",
- []interface{}{},
- },
- {
- NotIn("a", 1, 2).And(NotIn("b", "c", "d")),
- "a NOT IN (?,?) AND b NOT IN (?,?)",
- []interface{}{1, 2, "c", "d"},
- },
- {
- In("a", 1, 2).Or(In("b", "c", "d")),
- "a IN (?,?) OR b IN (?,?)",
- []interface{}{1, 2, "c", "d"},
- },
- {
- In("a", []int{1, 2}).Or(In("b", []string{"c", "d"})),
- "a IN (?,?) OR b IN (?,?)",
- []interface{}{1, 2, "c", "d"},
- },
- {
- In("a", Expr("select id from x where name > ?", "b")),
- "a IN (select id from x where name > ?)",
- []interface{}{"b"},
- },
- {
- In("a", []MyInt{1, 2}).Or(In("b", []string{"c", "d"})),
- "a IN (?,?) OR b IN (?,?)",
- []interface{}{MyInt(1), MyInt(2), "c", "d"},
- },
- {
- In("a", []int{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []int{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []int8{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []int8{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []int16{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []int16{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []int32{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []int32{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []int64{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []int64{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []uint{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []uint{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []uint8{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []uint8{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []uint16{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []uint16{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []uint32{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []uint32{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []uint64{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []uint64{1}),
- "a IN (?)",
- []interface{}{1},
- },
- {
- In("a", []string{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []interface{}{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []MyInt{}),
- "0=1",
- []interface{}{},
- },
- {
- In("a", []interface{}{1, 2, 3}).And(Eq{"b": "c"}),
- "a IN (?,?,?) AND b=?",
- []interface{}{1, 2, 3, "c"},
- },
- {
- In("a", Select("id").From("b").Where(Eq{"c": 1})),
- "a IN (SELECT id FROM b WHERE c=?)",
- []interface{}{1},
- },
- {
- NotIn("a", Expr("select id from x where name > ?", "b")),
- "a NOT IN (select id from x where name > ?)",
- []interface{}{"b"},
- },
- {
- NotIn("a", []int{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []int{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []int8{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []int8{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []int16{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []int16{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []int32{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []int32{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []int64{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []int64{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []uint{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []uint{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []uint8{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []uint8{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []uint16{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []uint16{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []uint32{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []uint32{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []uint64{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []uint64{1}),
- "a NOT IN (?)",
- []interface{}{1},
- },
- {
- NotIn("a", []interface{}{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []string{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []MyInt{}),
- "0=0",
- []interface{}{},
- },
- {
- NotIn("a", []MyInt{1, 2}),
- "a NOT IN (?,?)",
- []interface{}{1, 2},
- },
- {
- NotIn("a", []interface{}{1, 2, 3}).And(Eq{"b": "c"}),
- "a NOT IN (?,?,?) AND b=?",
- []interface{}{1, 2, 3, "c"},
- },
- {
- NotIn("a", []interface{}{1, 2, 3}).Or(Eq{"b": "c"}),
- "a NOT IN (?,?,?) OR b=?",
- []interface{}{1, 2, 3, "c"},
- },
- {
- NotIn("a", Select("id").From("b").Where(Eq{"c": 1})),
- "a NOT IN (SELECT id FROM b WHERE c=?)",
- []interface{}{1},
- },
- {
- Or(Eq{"a": 1, "b": 2}, Eq{"c": 3, "d": 4}),
- "(a=? AND b=?) OR (c=? AND d=?)",
- []interface{}{1, 2, 3, 4},
- },
- {
- Not{Eq{"a": 1, "b": 2}},
- "NOT (a=? AND b=?)",
- []interface{}{1, 2},
- },
- {
- Not{Neq{"a": 1, "b": 2}},
- "NOT (a<>? AND b<>?)",
- []interface{}{1, 2},
- },
- {
- Not{Eq{"a": 1}.And(Eq{"b": 2})},
- "NOT (a=? AND b=?)",
- []interface{}{1, 2},
- },
- {
- Not{Neq{"a": 1}.And(Neq{"b": 2})},
- "NOT (a<>? AND b<>?)",
- []interface{}{1, 2},
- },
- {
- Not{Eq{"a": 1}}.And(Neq{"b": 2}),
- "NOT a=? AND b<>?",
- []interface{}{1, 2},
- },
- {
- Not{Eq{"a": 1}}.Or(Neq{"b": 2}),
- "NOT a=? OR b<>?",
- []interface{}{1, 2},
- },
- }
- for _, k := range cases {
- sql, args, err := ToSQL(k.cond)
- assert.NoError(t, err)
- assert.EqualValues(t, k.sql, sql)
- for i := 0; i < 10; i++ {
- sql2, _, err := ToSQL(k.cond)
- assert.NoError(t, err)
- assert.EqualValues(t, sql, sql2)
- }
- assert.EqualValues(t, len(args), len(k.args))
- if len(args) > 0 {
- for i := 0; i < len(args); i++ {
- assert.EqualValues(t, k.args[i], args[i])
- }
- }
- }
- }
- func TestSubquery(t *testing.T) {
- subb := Select("id").From("table_b").Where(Eq{"b": "a"})
- b := Select("a, b").From("table_a").Where(
- Eq{
- "b_id": subb,
- "id": 23,
- },
- )
- sql, args, err := b.ToSQL()
- assert.NoError(t, err)
- assert.EqualValues(t, "SELECT a, b FROM table_a WHERE b_id=(SELECT id FROM table_b WHERE b=?) AND id=?", sql)
- assert.EqualValues(t, []interface{}{"a", 23}, args)
- }
- // https://github.com/go-xorm/xorm/issues/820
- func TestExprCond(t *testing.T) {
- b := Select("id").From("table1").Where(expr{sql: "a=? OR b=?", args: []interface{}{1, 2}}).Where(Or(Eq{"c": 3}, Eq{"d": 4}))
- sql, args, err := b.ToSQL()
- assert.NoError(t, err)
- assert.EqualValues(t, "table1", b.TableName())
- assert.EqualValues(t, "SELECT id FROM table1 WHERE (a=? OR b=?) AND (c=? OR d=?)", sql)
- assert.EqualValues(t, []interface{}{1, 2, 3, 4}, args)
- }
- func TestBuilder_ToBoundSQL(t *testing.T) {
- newSQL, err := Select("id").From("table").Where(In("a", 1, 2)).ToBoundSQL()
- assert.NoError(t, err)
- assert.EqualValues(t, "SELECT id FROM table WHERE a IN (1,2)", newSQL)
- }
- func TestBuilder_From2(t *testing.T) {
- b := Select("id").From("table_b", "tb").Where(Eq{"b": "a"})
- sql, args, err := b.ToSQL()
- assert.NoError(t, err)
- assert.EqualValues(t, "SELECT id FROM table_b tb WHERE b=?", sql)
- assert.EqualValues(t, []interface{}{"a"}, args)
- b = Select().From("table_b", "tb").Where(Eq{"b": "a"})
- sql, args, err = b.ToSQL()
- assert.NoError(t, err)
- assert.EqualValues(t, "SELECT * FROM table_b tb WHERE b=?", sql)
- assert.EqualValues(t, []interface{}{"a"}, args)
- }
- func TestBuilder_And(t *testing.T) {
- b := Select("id").From("table_b", "tb").Where(Eq{"b": "a"}).And(Neq{"c": "d"})
- sql, args, err := b.ToSQL()
- assert.NoError(t, err)
- assert.EqualValues(t, "SELECT id FROM table_b tb WHERE b=? AND c<>?", sql)
- assert.EqualValues(t, []interface{}{"a", "d"}, args)
- }
- func TestBuilder_Or(t *testing.T) {
- b := Select("id").From("table_b", "tb").Where(Eq{"b": "a"}).Or(Neq{"c": "d"})
- sql, args, err := b.ToSQL()
- assert.NoError(t, err)
- assert.EqualValues(t, "SELECT id FROM table_b tb WHERE b=? OR c<>?", sql)
- assert.EqualValues(t, []interface{}{"a", "d"}, args)
- }
|