3
[![CircleCI](https://circleci.com/gh/go-xorm/builder/tree/master.svg?style=svg)](https://circleci.com/gh/go-xorm/builder/tree/master)
5
Package builder is a lightweight and fast SQL builder for Go and XORM.
7
Make sure you have installed Go 1.1+ and then:
9
go get github.com/go-xorm/builder
14
sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
20
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
22
sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
23
RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
29
sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
35
sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
40
* `Eq` is a redefine of a map, you can give one or more conditions to `Eq`
43
import . "github.com/go-xorm/builder"
45
sql, args, _ := ToSQL(Eq{"a":1})
47
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
48
// b=? AND c=? ["c", 0]
49
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
50
// b=? AND c=? ["c", 0]
51
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
52
// b=? OR b=? ["c", "d"]
53
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
54
// b IN (?,?) ["c", "d"]
55
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
56
// b=? AND c IN (?,?) [1, 2, 3]
59
* `Neq` is the same to `Eq`
62
import . "github.com/go-xorm/builder"
64
sql, args, _ := ToSQL(Neq{"a":1})
66
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
67
// b<>? AND c<>? ["c", 0]
68
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
69
// b<>? AND c<>? ["c", 0]
70
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
71
// b<>? OR b<>? ["c", "d"]
72
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
73
// b NOT IN (?,?) ["c", "d"]
74
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
75
// b<>? AND c NOT IN (?,?) [1, 2, 3]
78
* `Gt`, `Gte`, `Lt`, `Lte`
81
import . "github.com/go-xorm/builder"
83
sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
84
// a>? AND b>=? [1, 2]
85
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
92
import . "github.com/go-xorm/builder"
94
sql, args, _ := ToSQL(Like{"a", "c"})
98
* `Expr` you can customerize your sql with `Expr`
101
import . "github.com/go-xorm/builder"
103
sql, args, _ := ToSQL(Expr("a = ? ", 1))
105
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
106
// a=(select id from table where c = ?) [1]
112
import . "github.com/go-xorm/builder"
114
sql, args, _ := ToSQL(In("a", 1, 2, 3))
115
// a IN (?,?,?) [1,2,3]
116
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
117
// a IN (?,?,?) [1,2,3]
118
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
119
// a IN (select id from b where c = ?) [1]
122
* `IsNull` and `NotNull`
125
import . "github.com/go-xorm/builder"
127
sql, args, _ := ToSQL(IsNull{"a"})
129
sql, args, _ := ToSQL(NotNull{"b"})
133
* `And(conds ...Cond)`, And can connect one or more condtions via And
136
import . "github.com/go-xorm/builder"
138
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
139
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
142
* `Or(conds ...Cond)`, Or can connect one or more conditions via Or
145
import . "github.com/go-xorm/builder"
147
sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
148
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
149
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
150
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
156
import . "github.com/go-xorm/builder"
158
sql, args, _ := ToSQL(Between{"a", 1, 2})
162
* Define yourself conditions
164
Since `Cond` is an interface.
167
type Cond interface {
168
WriteTo(Writer) error
175
You can define yourself conditions and compose with other `Cond`.
b'\\ No newline at end of file'