From 541a394d40d6101542bf532bd33733907bb97fd4 Mon Sep 17 00:00:00 2001 From: YamiOdymel Date: Wed, 27 Oct 2021 11:25:10 +0800 Subject: [PATCH] Updated ExecAffected and ExecID --- README.md | 18 ++++++++++++++---- goshia.go | 12 ++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 51b66af..189910e 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,22 @@ count, err := goshia.QueryCount(rushia.NewQuery("Users").Limit(10, 20).Select(), ### 執行(Exec) -執行 `DELETE` 或 `UPDATE`…等語法時,可以使用 `Exec` 執行並且取得影響的筆數為何。 +執行 `DELETE` 或 `UPDATE`…等語法時,可以使用 `Exec` 執行。 ```go var user User -affectedRows, err := goshia.Exec(rushia.NewQuery("Users").Where("user_id = ?", 30).Update(user)) -// 等效於:UPDATE Users SET ... WHERE `user_id` = ? +err := goshia.Exec(rushia.NewQuery("Users").Where("user_id = ?", 30).Update(user)) +// 等效於:UPDATE Users SET ... WHERE user_id = ? +``` + +### 執行與筆數(ExecAffected) + +執行時能夠取得影響的筆數為何。 + +```go +var user User +affectedRows, err := goshia.ExecAffected(rushia.NewQuery("Users").Where("user_id = ?", 10).Delete()) +// 等效於:DELETE FROM Users WHERE user_id = ? ``` ### 執行與編號(ExecID) @@ -65,7 +75,7 @@ affectedRows, err := goshia.Exec(rushia.NewQuery("Users").Where("user_id = ?", 3 ```go var user User -id, affectedRows, err := goshia.ExecID(rushia.NewQuery("Users").Insert(user)) +id, err := goshia.ExecID(rushia.NewQuery("Users").Insert(user)) // 等效於:INSERT INTO Users SET ... ``` diff --git a/goshia.go b/goshia.go index 593607b..ebc2dc1 100644 --- a/goshia.go +++ b/goshia.go @@ -46,7 +46,15 @@ func (g *Goshia) QueryCount(q *rushia.Query, dest interface{}) (count int, err e } // Exec -func (g *Goshia) Exec(q *rushia.Query) (affectedRows int, err error) { +func (g *Goshia) Exec(q *rushia.Query) (err error) { + query, params := rushia.Build(q) + result := g.Gorm.Exec(query, params...) + err = result.Error + return +} + +// ExecAffected +func (g *Goshia) ExecAffected(q *rushia.Query) (affectedRows int, err error) { query, params := rushia.Build(q) result := g.Gorm.Exec(query, params...) affectedRows = int(result.RowsAffected) @@ -55,7 +63,7 @@ func (g *Goshia) Exec(q *rushia.Query) (affectedRows int, err error) { } // ExecID -func (g *Goshia) ExecID(q *rushia.Query) (id int, affectedRows int, err error) { +func (g *Goshia) ExecID(q *rushia.Query) (id int, err error) { query, params := rushia.Build(q) err = g.Gorm.Transaction(func(tx *gorm.DB) error {