forked from dolthub/driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
65 lines (52 loc) · 1.22 KB
/
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
package embedded
import (
"io"
"database/sql/driver"
gms "github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)
var _ driver.Result = (*doltResult)(nil)
type doltResult struct {
affected int64
last int64
err error
}
func newResult(gmsCtx *gms.Context, sch gms.Schema, rowItr gms.RowIter) *doltResult {
var resultErr error
var affected int64
var last int64
for {
r, err := rowItr.Next(gmsCtx)
if err != nil {
if err != io.EOF {
resultErr = translateError(err)
}
break
}
for i := range r {
if res, ok := r[i].(types.OkResult); ok {
affected += int64(res.RowsAffected)
last = int64(res.InsertID)
}
}
}
return &doltResult{
affected: affected,
last: last,
err: resultErr,
}
}
// LastInsertId returns the database's auto-generated ID after, for example, an INSERT into a table with primary key.
func (result *doltResult) LastInsertId() (int64, error) {
if result.err != nil {
return 0, result.err
}
return result.last, nil
}
// RowsAffected returns the number of rows affected by the query.
func (result *doltResult) RowsAffected() (int64, error) {
if result.err != nil {
return 0, result.err
}
return result.affected, nil
}