diff --git a/builder/README.md b/builder/README.md index 101018a..a0fe67d 100644 --- a/builder/README.md +++ b/builder/README.md @@ -184,6 +184,46 @@ cond, vals, err := qb.BuildInsert(table, data) db.Exec(cond, vals...) ``` +#### `BuildInsertIgnore` + +sign: `BuildInsertIgnore(table string, data []map[string]interface{}) (string, []interface{}, error)` + +data is a slice and every element(map) in it must have the same keys: + +``` go +var data []map[string]interface{} +data = append(data, map[string]interface{}{ + "name": "deen", + "age": 23, +}) +data = append(data, map[string]interface{}{ + "name": "Tony", + "age": 30, +}) +cond, vals, err := qb.BuildInsertIgnore(table, data) +db.Exec(cond, vals...) +``` + +#### `BuildReplaceInsert` + +sign: `BuildReplaceInsert(table string, data []map[string]interface{}) (string, []interface{}, error)` + +data is a slice and every element(map) in it must have the same keys: + +``` go +var data []map[string]interface{} +data = append(data, map[string]interface{}{ + "name": "deen", + "age": 23, +}) +data = append(data, map[string]interface{}{ + "name": "Tony", + "age": 30, +}) +cond, vals, err := qb.BuildReplaceInsert(table, data) +db.Exec(cond, vals...) +``` + #### `NamedQuery` sign: `func NamedQuery(sql string, data map[string]interface{}) (string, []interface{}, error)` diff --git a/builder/builder.go b/builder/builder.go index 4b21dcb..b3dd504 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -177,7 +177,17 @@ func BuildDelete(table string, where map[string]interface{}) (string, []interfac // BuildInsert work as its name says func BuildInsert(table string, data []map[string]interface{}) (string, []interface{}, error) { - return buildInsert(table, data) + return buildInsert(table, data, commonInsert) +} + +// BuildInsertIgnore work as its name says +func BuildInsertIgnore(table string, data []map[string]interface{}) (string, []interface{}, error) { + return buildInsert(table, data, ignoreInsert) +} + +// BuildReplaceInsert work as its name says +func BuildReplaceInsert(table string, data []map[string]interface{}) (string, []interface{}, error) { + return buildInsert(table, data, replaceInsert) } var ( diff --git a/builder/dao.go b/builder/dao.go index 6806c82..5cb12b1 100644 --- a/builder/dao.go +++ b/builder/dao.go @@ -283,8 +283,16 @@ func quoteField(field string) string { return field } -func buildInsert(table string, setMap []map[string]interface{}) (string, []interface{}, error) { - format := "INSERT INTO %s (%s) VALUES %s" +type insertType string + +const ( + commonInsert insertType = "INSERT INTO" + ignoreInsert insertType = "INSERT IGNORE INTO" + replaceInsert insertType = "REPLACE INTO" +) + +func buildInsert(table string, setMap []map[string]interface{}, insertType insertType) (string, []interface{}, error) { + format := "%s %s (%s) VALUES %s" var fields []string var vals []interface{} if len(setMap) < 1 { @@ -303,7 +311,7 @@ func buildInsert(table string, setMap []map[string]interface{}) (string, []inter vals = append(vals, val) } } - return fmt.Sprintf(format, quoteField(table), strings.Join(fields, ","), strings.Join(sets, ",")), vals, nil + return fmt.Sprintf(format, insertType, quoteField(table), strings.Join(fields, ","), strings.Join(sets, ",")), vals, nil } func buildUpdate(table string, update map[string]interface{}, conditions ...Comparable) (string, []interface{}, error) { diff --git a/builder/dao_test.go b/builder/dao_test.go index 45bcd41..420ff80 100644 --- a/builder/dao_test.go +++ b/builder/dao_test.go @@ -169,14 +169,16 @@ func TestWhereConnector(t *testing.T) { func TestBuildInsert(t *testing.T) { var data = []struct { - table string - data []map[string]interface{} - outStr string - outVals []interface{} - outErr error + table string + insertType insertType + data []map[string]interface{} + outStr string + outVals []interface{} + outErr error }{ { - table: "tb1", + table: "tb1", + insertType: commonInsert, data: []map[string]interface{}{ { "foo": 1, @@ -195,10 +197,52 @@ func TestBuildInsert(t *testing.T) { outVals: []interface{}{2, 1, 4, 3, 6, 5}, outErr: nil, }, + { + table: "tb1", + insertType: replaceInsert, + data: []map[string]interface{}{ + { + "foo": 1, + "bar": 2, + }, + { + "foo": 3, + "bar": 4, + }, + { + "foo": 5, + "bar": 6, + }, + }, + outStr: "REPLACE INTO tb1 (bar,foo) VALUES (?,?),(?,?),(?,?)", + outVals: []interface{}{2, 1, 4, 3, 6, 5}, + outErr: nil, + }, + { + table: "tb1", + insertType: ignoreInsert, + data: []map[string]interface{}{ + { + "foo": 1, + "bar": 2, + }, + { + "foo": 3, + "bar": 4, + }, + { + "foo": 5, + "bar": 6, + }, + }, + outStr: "INSERT IGNORE INTO tb1 (bar,foo) VALUES (?,?),(?,?),(?,?)", + outVals: []interface{}{2, 1, 4, 3, 6, 5}, + outErr: nil, + }, } ass := assert.New(t) for _, tc := range data { - actualStr, actualVals, err := buildInsert(tc.table, tc.data) + actualStr, actualVals, err := buildInsert(tc.table, tc.data, tc.insertType) ass.Equal(tc.outErr, err) ass.Equal(tc.outStr, actualStr) ass.Equal(tc.outVals, actualVals)