-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
gdb_result.go
67 lines (59 loc) · 1.77 KB
/
gdb_result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gdb
import (
"database/sql"
"github.com/gogf/gf/v2/errors/gerror"
)
// SqlResult is execution result for sql operations.
// It also supports batch operation result for rowsAffected.
type SqlResult struct {
Result sql.Result
Affected int64
}
// MustGetAffected returns the affected rows count, if any error occurs, it panics.
func (r *SqlResult) MustGetAffected() int64 {
rows, err := r.RowsAffected()
if err != nil {
err = gerror.Wrap(err, `sql.Result.RowsAffected failed`)
panic(err)
}
return rows
}
// MustGetInsertId returns the last insert id, if any error occurs, it panics.
func (r *SqlResult) MustGetInsertId() int64 {
id, err := r.LastInsertId()
if err != nil {
err = gerror.Wrap(err, `sql.Result.LastInsertId failed`)
panic(err)
}
return id
}
// RowsAffected returns the number of rows affected by an
// update, insert, or delete. Not every database or database
// driver may support this.
// Also, See sql.Result.
func (r *SqlResult) RowsAffected() (int64, error) {
if r.Affected > 0 {
return r.Affected, nil
}
if r.Result == nil {
return 0, nil
}
return r.Result.RowsAffected()
}
// LastInsertId returns the integer generated by the database
// in response to a command. Typically, this will be from an
// "auto increment" column when inserting a new row. Not all
// databases support this feature, and the syntax of such
// statements varies.
// Also, See sql.Result.
func (r *SqlResult) LastInsertId() (int64, error) {
if r.Result == nil {
return 0, nil
}
return r.Result.LastInsertId()
}