-
Notifications
You must be signed in to change notification settings - Fork 13
/
stmt_result.go
98 lines (78 loc) · 1.59 KB
/
stmt_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mysql
/*
#include "cgo.h"
*/
import "C"
type stmtResult struct {
s *C.MY_STMT
c C.MY_STMT_RES
}
func (res *stmtResult) RowsAffected() int64 {
return int64(res.c.affected_rows)
}
func (res *stmtResult) InsertId() int64 {
return int64(res.c.insert_id)
}
func (res *stmtResult) close() {
C.my_stmt_close_result(res.s, &res.c)
}
type stmtQueryResult struct {
stmtResult
stmt *Stmt
fields []Field
}
func (res *stmtQueryResult) fillFields() {
res.fields = fetchFields(res.stmt.s.meta)
}
func (res *stmtQueryResult) fetchNext() (row []Value, err error) {
crow := C.my_stmt_fetch_next(res.s, &res.c)
if crow.has_error != 0 {
return nil, res.stmt.lastError()
}
return fetchNext(res.stmt.s.meta, crow, true)
}
func (res *stmtQueryResult) Fields() []Field {
return res.fields
}
func (res *stmtQueryResult) IndexOf(name string) int {
for i, field := range res.fields {
if field.Name == name {
return i
}
}
return -1
}
type stmtDataTable struct {
stmtQueryResult
rows [][]Value
}
func (res *stmtDataTable) fillRows(stmt *Stmt) (err error) {
rowCount := int(res.c.affected_rows)
if rowCount == 0 {
return nil
}
if rowCount < 0 {
return stmt.lastError()
}
rows := make([][]Value, rowCount)
for i := 0; i < rowCount; i++ {
rows[i], err = res.fetchNext()
if err != nil {
return err
}
}
res.rows = rows
return nil
}
func (res *stmtDataTable) Rows() [][]Value {
return res.rows
}
type stmtDataReader struct {
stmtQueryResult
}
func (res *stmtDataReader) FetchNext() ([]Value, error) {
return res.fetchNext()
}
func (res *stmtDataReader) Close() {
res.close()
}