Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Updated ExecAffected and ExecID
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Oct 27, 2021
1 parent 30bfbd3 commit 541a394
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ...
```

Expand Down
12 changes: 10 additions & 2 deletions goshia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 541a394

Please sign in to comment.