Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,91 @@ func TestBuilderCond(t *testing.T) {
"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},
},
{
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{"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{"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{"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{"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 ?",
Expand Down Expand Up @@ -100,6 +160,61 @@ func TestBuilderCond(t *testing.T) {
"0=1",
[]interface{}{},
},
{
In("a", []int8{}),
"0=1",
[]interface{}{},
},
{
In("a", []int16{}),
"0=1",
[]interface{}{},
},
{
In("a", []int32{}),
"0=1",
[]interface{}{},
},
{
In("a", []int64{}),
"0=1",
[]interface{}{},
},
{
In("a", []uint{}),
"0=1",
[]interface{}{},
},
{
In("a", []uint8{}),
"0=1",
[]interface{}{},
},
{
In("a", []uint16{}),
"0=1",
[]interface{}{},
},
{
In("a", []uint32{}),
"0=1",
[]interface{}{},
},
{
In("a", []uint64{}),
"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 > ?)",
Expand All @@ -110,11 +225,71 @@ func TestBuilderCond(t *testing.T) {
"0=0",
[]interface{}{},
},
{
NotIn("a", []int8{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []int16{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []int32{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []int64{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []uint{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []uint8{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []uint16{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []uint32{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []uint64{}),
"0=0",
[]interface{}{},
},
{
NotIn("a", []interface{}{1, 2, 3}).And(Eq{"b": "c"}),
"a NOT IN (?,?,?) AND 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},
},
}

for _, k := range cases {
Expand Down
2 changes: 1 addition & 1 deletion cond_like.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (like Like) WriteTo(w Writer) error {
if _, err := fmt.Fprintf(w, "%s LIKE ?", like[0]); err != nil {
return err
}
// FIXME: if use other regular express, this will be failed. but for compitable, keep this
// FIXME: if use other regular express, this will be failed. but for compatible, keep this
if like[1][0] == '%' || like[1][len(like[1])-1] == '%' {
w.Append(like[1])
} else {
Expand Down
24 changes: 24 additions & 0 deletions cond_not.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func (not Not) WriteTo(w Writer) error {
if _, err := fmt.Fprint(w, "("); err != nil {
return err
}
case Eq:
if len(not[0].(Eq)) > 1 {
if _, err := fmt.Fprint(w, "("); err != nil {
return err
}
}
case Neq:
if len(not[0].(Neq)) > 1 {
if _, err := fmt.Fprint(w, "("); err != nil {
return err
}
}
}

if err := not[0].WriteTo(w); err != nil {
Expand All @@ -32,6 +44,18 @@ func (not Not) WriteTo(w Writer) error {
if _, err := fmt.Fprint(w, ")"); err != nil {
return err
}
case Eq:
if len(not[0].(Eq)) > 1 {
if _, err := fmt.Fprint(w, ")"); err != nil {
return err
}
}
case Neq:
if len(not[0].(Neq)) > 1 {
if _, err := fmt.Fprint(w, ")"); err != nil {
return err
}
}
}

return nil
Expand Down