github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/builder/README.md (about) 1 # SQL builder 2 3 Package builder is a simple and powerful sql builder for Go. 4 5 Make sure you have installed Go 1.1+ and then: 6 7 go get github.com/go-xorm/builder 8 9 # Insert 10 11 ```Go 12 sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL() 13 ``` 14 15 # Select 16 17 ```Go 18 sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL() 19 20 sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})). 21 RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL() 22 ``` 23 24 # Update 25 26 ```Go 27 sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL() 28 ``` 29 30 # Delete 31 32 ```Go 33 sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL() 34 ``` 35 36 # Conditions 37 38 * `Eq` is a redefine of a map, you can give one or more conditions to `Eq` 39 40 ```Go 41 import . "github.com/go-xorm/builder" 42 43 sql, args, _ := ToSQL(Eq{"a":1}) 44 // a=? [1] 45 sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0})) 46 // b=? AND c=? ["c", 0] 47 sql, args, _ := ToSQL(Eq{"b":"c", "c":0}) 48 // b=? AND c=? ["c", 0] 49 sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"})) 50 // b=? OR b=? ["c", "d"] 51 sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}}) 52 // b IN (?,?) ["c", "d"] 53 sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}}) 54 // b=? AND c IN (?,?) [1, 2, 3] 55 ``` 56 57 * `Neq` is the same to `Eq` 58 59 ```Go 60 import . "github.com/go-xorm/builder" 61 62 sql, args, _ := ToSQL(Neq{"a":1}) 63 // a<>? [1] 64 sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0})) 65 // b<>? AND c<>? ["c", 0] 66 sql, args, _ := ToSQL(Neq{"b":"c", "c":0}) 67 // b<>? AND c<>? ["c", 0] 68 sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"})) 69 // b<>? OR b<>? ["c", "d"] 70 sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}}) 71 // b NOT IN (?,?) ["c", "d"] 72 sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}}) 73 // b<>? AND c NOT IN (?,?) [1, 2, 3] 74 ``` 75 76 * `Gt`, `Gte`, `Lt`, `Lte` 77 78 ```Go 79 import . "github.com/go-xorm/builder" 80 81 sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2})) 82 // a>? AND b>=? [1, 2] 83 sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2})) 84 // a<? OR b<=? [1, 2] 85 ``` 86 87 * `Like` 88 89 ```Go 90 import . "github.com/go-xorm/builder" 91 92 sql, args, _ := ToSQL(Like{"a", "c"}) 93 // a LIKE ? [%c%] 94 ``` 95 96 * `Expr` you can customerize your sql with `Expr` 97 98 ```Go 99 import . "github.com/go-xorm/builder" 100 101 sql, args, _ := ToSQL(Expr("a = ? ", 1)) 102 // a = ? [1] 103 sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)}) 104 // a=(select id from table where c = ?) [1] 105 ``` 106 107 * `In` and `NotIn` 108 109 ```Go 110 import . "github.com/go-xorm/builder" 111 112 sql, args, _ := ToSQL(In("a", 1, 2, 3)) 113 // a IN (?,?,?) [1,2,3] 114 sql, args, _ := ToSQL(In("a", []int{1, 2, 3})) 115 // a IN (?,?,?) [1,2,3] 116 sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1)))) 117 // a IN (select id from b where c = ?) [1] 118 ``` 119 120 * `IsNull` and `NotNull` 121 122 ```Go 123 import . "github.com/go-xorm/builder" 124 125 sql, args, _ := ToSQL(IsNull{"a"}) 126 // a IS NULL [] 127 sql, args, _ := ToSQL(NotNull{"b"}) 128 // b IS NOT NULL [] 129 ``` 130 131 * `And(conds ...Cond)`, And can connect one or more condtions via And 132 133 ```Go 134 import . "github.com/go-xorm/builder" 135 136 sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2})) 137 // a=? AND b LIKE ? AND d<>? [1, %c%, 2] 138 ``` 139 140 * `Or(conds ...Cond)`, Or can connect one or more conditions via Or 141 142 ```Go 143 import . "github.com/go-xorm/builder" 144 145 sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2})) 146 // a=? OR b LIKE ? OR d<>? [1, %c%, 2] 147 sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2}))) 148 // a=? OR (b LIKE ? AND d<>?) [1, %c%, 2] 149 ``` 150 151 * `Between` 152 153 ```Go 154 import . "github.com/go-xorm/builder" 155 156 sql, args, _ := ToSQL(Between{"a", 1, 2}) 157 // a BETWEEN 1 AND 2 158 ``` 159 160 * Define yourself conditions 161 162 Since `Cond` is an interface. 163 164 ```Go 165 type Cond interface { 166 WriteTo(Writer) error 167 And(...Cond) Cond 168 Or(...Cond) Cond 169 IsValid() bool 170 } 171 ``` 172 173 You can define yourself conditions and compose with other `Cond`.