Skip to content

Commit e6d8f5c

Browse files
xxxsensen.xie
andauthored
fix: build delete (#157)
Co-authored-by: sen.xie <sen.xie@shopee.com>
1 parent 0e169a4 commit e6d8f5c

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

builder/dao.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,14 @@ func buildUpdate(table string, update map[string]interface{}, limit uint, condit
452452

453453
func buildDelete(table string, limit uint, conditions ...Comparable) (string, []interface{}, error) {
454454
whereString, vals := whereConnector("AND", conditions...)
455-
if "" == whereString {
456-
return fmt.Sprintf("DELETE FROM %s", table), nil, nil
457-
}
458-
format := "DELETE FROM %s WHERE %s"
459-
460-
cond := fmt.Sprintf(format, quoteField(table), whereString)
455+
format := "DELETE FROM %s"
456+
args := make([]interface{}, 0, 2)
457+
args = append(args, quoteField(table))
458+
if len(whereString) > 0 {
459+
format += " WHERE %s"
460+
args = append(args, whereString)
461+
}
462+
cond := fmt.Sprintf(format, args...)
461463
if limit > 0 {
462464
cond += " LIMIT ?"
463465
vals = append(vals, int(limit))

builder/dao_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,20 @@ func TestBuildDelete(t *testing.T) {
384384
outVals: []interface{}{2, "tt", 1, 5},
385385
outErr: nil,
386386
},
387+
{
388+
table: "tb",
389+
limit: 5,
390+
outStr: "DELETE FROM tb LIMIT ?",
391+
outVals: []interface{}{5},
392+
outErr: nil,
393+
},
394+
{
395+
table: "tb",
396+
limit: 0,
397+
outStr: "DELETE FROM tb",
398+
outVals: nil,
399+
outErr: nil,
400+
},
387401
}
388402
ass := assert.New(t)
389403
for _, tc := range data {

0 commit comments

Comments
 (0)