SQL builder
Package builder is a lightweight and fast SQL builder for Go and XORM.
Make sure you have installed Go 1.8+ and then:
go get github.com/go-xorm/builder
Insert
sql, args, err := builder.Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
sql, err := builder.Insert().Into("table1").Select().From("table2").ToBoundSQL()
sql, err = builder.Insert("a, b").Into("table1").Select("b, c").From("table2").ToBoundSQL()
Select
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
sql, args, err := Select("sub.id").From(Select("c").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
sql, args, err = Select("sub.id").From(
Select("id").From("table1").Where(Eq{"a": 1}).Union("all", Select("id").From("table1").Where(Eq{"a": 2})),"sub").
Where(Eq{"b": 1}).ToSQL()
sql, args, err = Select("a", "b", "c").From("table1").Where(Eq{"f1": "v1", "f2": "v2"}).
OrderBy("a ASC").ToSQL()
sql, args, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").OrderBy("a ASC").
Limit(5, 10).ToSQL()
Update
sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
Delete
sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
Union
sql, args, err := Select("*").From("a").Where(Eq{"status": "1"}).
Union("all", Select("*").From("a").Where(Eq{"status": "2"})).
Union("distinct", Select("*").From("a").Where(Eq{"status": "3"})).
Union("", Select("*").From("a").Where(Eq{"status": "4"})).
ToSQL()
Conditions
Eq
is a redefine of a map, you can give one or more conditions to Eq
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(Eq{"a":1})
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(Neq{"a":1})
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(Like{"a", "c"})
Expr
you can customerize your sql with Expr
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(Expr("a = ? ", 1))
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(In("a", 1, 2, 3))
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(IsNull{"a"})
sql, args, _ := ToSQL(NotNull{"b"})
And(conds ...Cond)
, And can connect one or more condtions via And
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
Or(conds ...Cond)
, Or can connect one or more conditions via Or
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
import . "github.com/go-xorm/builder"
sql, args, _ := ToSQL(Between{"a", 1, 2})
- Define yourself conditions
Since Cond
is an interface.
type Cond interface {
WriteTo(Writer) error
And(...Cond) Cond
Or(...Cond) Cond
IsValid() bool
}
You can define yourself conditions and compose with other Cond
.