Skip to content

Commit 0dca476

Browse files
guangxuewucaibirdme
authored andcommitted
支持常用insert类型ignore replace (#40)
* 支持常用insert类型 * 支持常用insert类型 * 兼容1.8以下 * add document * 去掉go.mod
1 parent 0a22272 commit 0dca476

File tree

4 files changed

+113
-11
lines changed

4 files changed

+113
-11
lines changed

builder/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,46 @@ cond, vals, err := qb.BuildInsert(table, data)
184184
db.Exec(cond, vals...)
185185
```
186186

187+
#### `BuildInsertIgnore`
188+
189+
sign: `BuildInsertIgnore(table string, data []map[string]interface{}) (string, []interface{}, error)`
190+
191+
data is a slice and every element(map) in it must have the same keys:
192+
193+
``` go
194+
var data []map[string]interface{}
195+
data = append(data, map[string]interface{}{
196+
"name": "deen",
197+
"age": 23,
198+
})
199+
data = append(data, map[string]interface{}{
200+
"name": "Tony",
201+
"age": 30,
202+
})
203+
cond, vals, err := qb.BuildInsertIgnore(table, data)
204+
db.Exec(cond, vals...)
205+
```
206+
207+
#### `BuildReplaceInsert`
208+
209+
sign: `BuildReplaceInsert(table string, data []map[string]interface{}) (string, []interface{}, error)`
210+
211+
data is a slice and every element(map) in it must have the same keys:
212+
213+
``` go
214+
var data []map[string]interface{}
215+
data = append(data, map[string]interface{}{
216+
"name": "deen",
217+
"age": 23,
218+
})
219+
data = append(data, map[string]interface{}{
220+
"name": "Tony",
221+
"age": 30,
222+
})
223+
cond, vals, err := qb.BuildReplaceInsert(table, data)
224+
db.Exec(cond, vals...)
225+
```
226+
187227
#### `NamedQuery`
188228

189229
sign: `func NamedQuery(sql string, data map[string]interface{}) (string, []interface{}, error)`

builder/builder.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,17 @@ func BuildDelete(table string, where map[string]interface{}) (string, []interfac
177177

178178
// BuildInsert work as its name says
179179
func BuildInsert(table string, data []map[string]interface{}) (string, []interface{}, error) {
180-
return buildInsert(table, data)
180+
return buildInsert(table, data, commonInsert)
181+
}
182+
183+
// BuildInsertIgnore work as its name says
184+
func BuildInsertIgnore(table string, data []map[string]interface{}) (string, []interface{}, error) {
185+
return buildInsert(table, data, ignoreInsert)
186+
}
187+
188+
// BuildReplaceInsert work as its name says
189+
func BuildReplaceInsert(table string, data []map[string]interface{}) (string, []interface{}, error) {
190+
return buildInsert(table, data, replaceInsert)
181191
}
182192

183193
var (

builder/dao.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,16 @@ func quoteField(field string) string {
283283
return field
284284
}
285285

286-
func buildInsert(table string, setMap []map[string]interface{}) (string, []interface{}, error) {
287-
format := "INSERT INTO %s (%s) VALUES %s"
286+
type insertType string
287+
288+
const (
289+
commonInsert insertType = "INSERT INTO"
290+
ignoreInsert insertType = "INSERT IGNORE INTO"
291+
replaceInsert insertType = "REPLACE INTO"
292+
)
293+
294+
func buildInsert(table string, setMap []map[string]interface{}, insertType insertType) (string, []interface{}, error) {
295+
format := "%s %s (%s) VALUES %s"
288296
var fields []string
289297
var vals []interface{}
290298
if len(setMap) < 1 {
@@ -303,7 +311,7 @@ func buildInsert(table string, setMap []map[string]interface{}) (string, []inter
303311
vals = append(vals, val)
304312
}
305313
}
306-
return fmt.Sprintf(format, quoteField(table), strings.Join(fields, ","), strings.Join(sets, ",")), vals, nil
314+
return fmt.Sprintf(format, insertType, quoteField(table), strings.Join(fields, ","), strings.Join(sets, ",")), vals, nil
307315
}
308316

309317
func buildUpdate(table string, update map[string]interface{}, conditions ...Comparable) (string, []interface{}, error) {

builder/dao_test.go

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,16 @@ func TestWhereConnector(t *testing.T) {
169169

170170
func TestBuildInsert(t *testing.T) {
171171
var data = []struct {
172-
table string
173-
data []map[string]interface{}
174-
outStr string
175-
outVals []interface{}
176-
outErr error
172+
table string
173+
insertType insertType
174+
data []map[string]interface{}
175+
outStr string
176+
outVals []interface{}
177+
outErr error
177178
}{
178179
{
179-
table: "tb1",
180+
table: "tb1",
181+
insertType: commonInsert,
180182
data: []map[string]interface{}{
181183
{
182184
"foo": 1,
@@ -195,10 +197,52 @@ func TestBuildInsert(t *testing.T) {
195197
outVals: []interface{}{2, 1, 4, 3, 6, 5},
196198
outErr: nil,
197199
},
200+
{
201+
table: "tb1",
202+
insertType: replaceInsert,
203+
data: []map[string]interface{}{
204+
{
205+
"foo": 1,
206+
"bar": 2,
207+
},
208+
{
209+
"foo": 3,
210+
"bar": 4,
211+
},
212+
{
213+
"foo": 5,
214+
"bar": 6,
215+
},
216+
},
217+
outStr: "REPLACE INTO tb1 (bar,foo) VALUES (?,?),(?,?),(?,?)",
218+
outVals: []interface{}{2, 1, 4, 3, 6, 5},
219+
outErr: nil,
220+
},
221+
{
222+
table: "tb1",
223+
insertType: ignoreInsert,
224+
data: []map[string]interface{}{
225+
{
226+
"foo": 1,
227+
"bar": 2,
228+
},
229+
{
230+
"foo": 3,
231+
"bar": 4,
232+
},
233+
{
234+
"foo": 5,
235+
"bar": 6,
236+
},
237+
},
238+
outStr: "INSERT IGNORE INTO tb1 (bar,foo) VALUES (?,?),(?,?),(?,?)",
239+
outVals: []interface{}{2, 1, 4, 3, 6, 5},
240+
outErr: nil,
241+
},
198242
}
199243
ass := assert.New(t)
200244
for _, tc := range data {
201-
actualStr, actualVals, err := buildInsert(tc.table, tc.data)
245+
actualStr, actualVals, err := buildInsert(tc.table, tc.data, tc.insertType)
202246
ass.Equal(tc.outErr, err)
203247
ass.Equal(tc.outStr, actualStr)
204248
ass.Equal(tc.outVals, actualVals)

0 commit comments

Comments
 (0)