-
Notifications
You must be signed in to change notification settings - Fork 48
/
result.go
190 lines (170 loc) · 4.37 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package freetds
import (
"errors"
"fmt"
"reflect"
"time"
)
type Result struct {
Columns []*ResultColumn
Rows [][]interface{}
ReturnValue int
RowsAffected int
Message string
currentRow int
scanCount int
}
func NewResult() *Result {
return &Result{
Columns: make([]*ResultColumn, 0),
Rows: nil,
currentRow: -1,
}
}
func (r *Result) addColumn(name string, dbSize, dbType int) {
c := new(ResultColumn)
c.Name = name
c.DbSize = dbSize
c.DbType = dbType
r.Columns = append(r.Columns, c)
}
func (r *Result) addValue(row, col int, value interface{}) {
if r.Rows == nil {
r.Rows = make([][]interface{}, 1)
r.Rows[0] = make([]interface{}, len(r.Columns))
}
for rc := len(r.Rows) - 1; rc < row; rc++ {
r.Rows = append(r.Rows, make([]interface{}, len(r.Columns)))
}
r.Rows[row][col] = value
}
// CurrentRow() returns current row (set by Next()).
// Returns -1 as an error if Next() wasn't called.
func (r *Result) CurrentRow() int {
return r.currentRow
}
// HasNext returns true if we have more rows to process.
func (r *Result) HasNext() bool {
if len(r.Rows) == 0 {
return false
}
return r.currentRow < len(r.Rows)-1
}
// Advances to the next row. Returns false if there is no more rows (i.e. we are on the last row).
func (r *Result) Next() bool {
if !r.HasNext() {
return false
}
r.currentRow++
return true
}
//Scan copies the columns in the current row into the values pointed at by dest.
func (r *Result) Scan(dest ...interface{}) error {
r.scanCount = 0
if r.currentRow == -1 {
return errors.New("Scan called without calling Next.")
}
for _, d := range dest {
if !isPointer(d) {
return errors.New("Destination not a pointer.")
}
}
if len(dest) == 1 {
if s := asStructPointer(dest[0]); s != nil {
return r.scanStruct(s)
}
}
err := assignValues(r.Rows[r.currentRow], dest)
if err == nil {
r.scanCount = len(dest)
}
return err
}
//Must Scan exactly cnt number of values from result.
//Useful when scanning into structure, to know whether are all expected fields filled with values.
//cnt - number of values assigned to fields
func (r *Result) MustScan(cnt int, dest ...interface{}) error {
if err := r.Scan(dest...); err != nil {
return err
}
if cnt != r.scanCount {
return errors.New(fmt.Sprintf("Worng scan count, expected %d, actual %d.", cnt, r.scanCount))
}
return nil
}
// FindColumn returns an index of a column, found by name.
// Returns error if the column isn't found.
func (r *Result) FindColumn(name string) (int, error) {
for i, col := range r.Columns {
if name == col.Name {
return i, nil
}
}
return -1, fmt.Errorf("FindColumn('%s'): column not found in result", name)
}
// Find column with given name and scan it's value to the result.
// Returns error if the column isn't found, otherwise returns error if the scan fails.
func (r *Result) ScanColumn(name string, dest interface{}) error {
if r.currentRow == -1 {
return errors.New("ScanColumn called without calling Next.")
}
if !isPointer(dest) {
return errors.New("Destination not a pointer.")
}
i, err := r.FindColumn(name)
if err != nil {
return err
}
err = convertAssign(dest, r.Rows[r.currentRow][i])
if err != nil {
return err
}
return nil
}
//Copies values for the current row to the structure.
//Struct filed name must match database column name.
func (r *Result) scanStruct(s *reflect.Value) error {
for i, col := range r.Columns {
f := s.FieldByName(camelize(col.Name))
if f.IsValid() {
if f.CanSet() {
if err := convertAssign(f.Addr().Interface(), r.Rows[r.currentRow][i]); err != nil {
return err
}
r.scanCount++
}
}
}
return nil
}
func asStructPointer(p interface{}) *reflect.Value {
sp := reflect.ValueOf(p)
if _, ok := p.(*time.Time); ok {
return nil
} else if sp.Kind() == reflect.Ptr {
s := sp.Elem()
if s.Kind() == reflect.Struct {
return &s
}
}
return nil
}
func isPointer(p interface{}) bool {
sp := reflect.ValueOf(p)
return sp.Kind() == reflect.Ptr
}
//assignValues copies to dest values in src
//dest should be a pointer type
//error is returned if types don't match and conversion failed
func assignValues(src, dest []interface{}) error {
if len(dest) > len(src) {
return errors.New(fmt.Sprintf("More dest values %d than src values %d.", len(dest), len(src)))
}
for i, d := range dest {
err := convertAssign(d, src[i])
if err != nil {
return err
}
}
return nil
}