~ubuntu-branches/debian/sid/golang-github-go-xorm-builder/sid

« back to all changes in this revision

Viewing changes to README.md

  • Committer: Package Import Robot
  • Author(s): Michael Lustfield
  • Date: 2017-03-12 03:54:03 UTC
  • Revision ID: package-import@ubuntu.com-20170312035403-csl9031z2j2aswfd
Tags: upstream-0.0~git20170224.0.c6e604e
ImportĀ upstreamĀ versionĀ 0.0~git20170224.0.c6e604e

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# SQL builder
 
2
 
 
3
[![CircleCI](https://circleci.com/gh/go-xorm/builder/tree/master.svg?style=svg)](https://circleci.com/gh/go-xorm/builder/tree/master)
 
4
 
 
5
Package builder is a lightweight and fast SQL builder for Go and XORM.
 
6
 
 
7
Make sure you have installed Go 1.1+ and then:
 
8
 
 
9
    go get github.com/go-xorm/builder
 
10
 
 
11
# Insert
 
12
 
 
13
```Go
 
14
sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
 
15
```
 
16
 
 
17
# Select
 
18
 
 
19
```Go
 
20
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
 
21
 
 
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()
 
24
```
 
25
 
 
26
# Update
 
27
 
 
28
```Go
 
29
sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
 
30
```
 
31
 
 
32
# Delete
 
33
 
 
34
```Go
 
35
sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
 
36
```
 
37
 
 
38
# Conditions
 
39
 
 
40
* `Eq` is a redefine of a map, you can give one or more conditions to `Eq`
 
41
 
 
42
```Go
 
43
import . "github.com/go-xorm/builder"
 
44
 
 
45
sql, args, _ := ToSQL(Eq{"a":1})
 
46
// 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]
 
57
```
 
58
 
 
59
* `Neq` is the same to `Eq`
 
60
 
 
61
```Go
 
62
import . "github.com/go-xorm/builder"
 
63
 
 
64
sql, args, _ := ToSQL(Neq{"a":1})
 
65
// 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]
 
76
```
 
77
 
 
78
* `Gt`, `Gte`, `Lt`, `Lte`
 
79
 
 
80
```Go
 
81
import . "github.com/go-xorm/builder"
 
82
 
 
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}))
 
86
// a<? OR b<=? [1, 2]
 
87
```
 
88
 
 
89
* `Like`
 
90
 
 
91
```Go
 
92
import . "github.com/go-xorm/builder"
 
93
 
 
94
sql, args, _ := ToSQL(Like{"a", "c"})
 
95
// a LIKE ? [%c%]
 
96
```
 
97
 
 
98
* `Expr` you can customerize your sql with `Expr`
 
99
 
 
100
```Go
 
101
import . "github.com/go-xorm/builder"
 
102
 
 
103
sql, args, _ := ToSQL(Expr("a = ? ", 1))
 
104
// 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]
 
107
```
 
108
 
 
109
* `In` and `NotIn`
 
110
 
 
111
```Go
 
112
import . "github.com/go-xorm/builder"
 
113
 
 
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]
 
120
```
 
121
 
 
122
* `IsNull` and `NotNull`
 
123
 
 
124
```Go
 
125
import . "github.com/go-xorm/builder"
 
126
 
 
127
sql, args, _ := ToSQL(IsNull{"a"})
 
128
// a IS NULL []
 
129
sql, args, _ := ToSQL(NotNull{"b"})
 
130
        // b IS NOT NULL []
 
131
```
 
132
 
 
133
* `And(conds ...Cond)`, And can connect one or more condtions via And
 
134
 
 
135
```Go
 
136
import . "github.com/go-xorm/builder"
 
137
 
 
138
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
 
139
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
 
140
```
 
141
 
 
142
* `Or(conds ...Cond)`, Or can connect one or more conditions via Or
 
143
 
 
144
```Go
 
145
import . "github.com/go-xorm/builder"
 
146
 
 
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]
 
151
```
 
152
 
 
153
* `Between`
 
154
 
 
155
```Go
 
156
import . "github.com/go-xorm/builder"
 
157
 
 
158
sql, args, _ := ToSQL(Between{"a", 1, 2})
 
159
// a BETWEEN 1 AND 2
 
160
```
 
161
 
 
162
* Define yourself conditions
 
163
 
 
164
Since `Cond` is an interface.
 
165
 
 
166
```Go
 
167
type Cond interface {
 
168
        WriteTo(Writer) error
 
169
        And(...Cond) Cond
 
170
        Or(...Cond) Cond
 
171
        IsValid() bool
 
172
}
 
173
```
 
174
 
 
175
You can define yourself conditions and compose with other `Cond`.
 
 
b'\\ No newline at end of file'