From 0391e1a0064557b26b8164c4908af6b6f7f3c684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Thu, 24 Aug 2023 11:22:15 +0800 Subject: [PATCH] =?UTF-8?q?Add=20Upsert=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbpool.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/dbpool.go b/dbpool.go index b18c4fc..fc2ab87 100644 --- a/dbpool.go +++ b/dbpool.go @@ -143,10 +143,22 @@ func (this *dbPool) Replace( func (this *dbPool) Upsert( dataSource string, tblName string, - whereKv map[string]string, - updateKv map[string]string, - insertKv map[string]string, + whereKv [][]string, + updateKv [][]string, + insertKv [][]string, cb ExecResultCb) { + this.OrmSelectOne(dataSource, tblName, whereKv, + func(err error, ds *DataSet) { + if err != nil { + cb(err, 0, 0) + return + } + if ds.Next() { + this.Update(dataSource, tblName, updateKv, whereKv, cb) + } else { + this.Insert(dataSource, tblName, insertKv, cb) + } + }) } func (this *dbPool) PageQuery(