From 2eb25936ee4643dd5d8cdbf6e15350daf96cc2b2 Mon Sep 17 00:00:00 2001 From: "sen.xie" Date: Tue, 11 Mar 2025 18:12:58 +0800 Subject: [PATCH] fix: build delete --- builder/dao.go | 14 ++++++++------ builder/dao_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/builder/dao.go b/builder/dao.go index 7c0fb9a..0990969 100644 --- a/builder/dao.go +++ b/builder/dao.go @@ -452,12 +452,14 @@ func buildUpdate(table string, update map[string]interface{}, limit uint, condit func buildDelete(table string, limit uint, conditions ...Comparable) (string, []interface{}, error) { whereString, vals := whereConnector("AND", conditions...) - if "" == whereString { - return fmt.Sprintf("DELETE FROM %s", table), nil, nil - } - format := "DELETE FROM %s WHERE %s" - - cond := fmt.Sprintf(format, quoteField(table), whereString) + format := "DELETE FROM %s" + args := make([]interface{}, 0, 2) + args = append(args, quoteField(table)) + if len(whereString) > 0 { + format += " WHERE %s" + args = append(args, whereString) + } + cond := fmt.Sprintf(format, args...) if limit > 0 { cond += " LIMIT ?" vals = append(vals, int(limit)) diff --git a/builder/dao_test.go b/builder/dao_test.go index 4b9aef1..02d157f 100644 --- a/builder/dao_test.go +++ b/builder/dao_test.go @@ -384,6 +384,20 @@ func TestBuildDelete(t *testing.T) { outVals: []interface{}{2, "tt", 1, 5}, outErr: nil, }, + { + table: "tb", + limit: 5, + outStr: "DELETE FROM tb LIMIT ?", + outVals: []interface{}{5}, + outErr: nil, + }, + { + table: "tb", + limit: 0, + outStr: "DELETE FROM tb", + outVals: nil, + outErr: nil, + }, } ass := assert.New(t) for _, tc := range data {