Skip to content

Commit

Permalink
支持常用insert类型ignore replace (#40)
Browse files Browse the repository at this point in the history
* 支持常用insert类型

* 支持常用insert类型

* 兼容1.8以下

* add document

* 去掉go.mod
  • Loading branch information
guangxuewu authored and caibirdme committed Apr 26, 2019
1 parent 0a22272 commit 0dca476
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
40 changes: 40 additions & 0 deletions builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
12 changes: 11 additions & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
14 changes: 11 additions & 3 deletions builder/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
58 changes: 51 additions & 7 deletions builder/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 0dca476

Please sign in to comment.