This repository was archived by the owner on Jan 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrows.go
More file actions
257 lines (242 loc) · 5.06 KB
/
rows.go
File metadata and controls
257 lines (242 loc) · 5.06 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package turso_go
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io"
"reflect"
"strings"
"sync"
)
type tursoRows struct {
mu sync.Mutex
ctx uintptr
columns []string
err error
closed bool
}
func newRows(ctx uintptr) *tursoRows {
return &tursoRows{
mu: sync.Mutex{},
ctx: ctx,
columns: nil,
err: nil,
closed: false,
}
}
// database/sql driver.go 462
/**
* RowsColumnTypeScanType may be implemented by [Rows]. It should return
* the value type that can be used to scan types into. For example, the database
* column type "bigint" this should return "[reflect.TypeOf](int64(0))".
**/
func (r *tursoRows) ColumnTypeScanType(idx int) reflect.Type {
r.mu.Lock()
defer r.mu.Unlock()
if r.isClosed() {
return reflect.TypeOf((*interface{})(nil)).Elem()
}
ptr := rowsGetColumnType(r.ctx, int32(idx))
if ptr == 0 {
return reflect.TypeOf((*interface{})(nil)).Elem()
}
colType := GoString(ptr)
freeCString(ptr)
switch colType {
case "INTEGER", "NUMERIC":
return reflect.TypeOf(sql.NullInt64{})
case "REAL", "FLOAT":
return reflect.TypeOf(sql.NullFloat64{})
case "TEXT":
return reflect.TypeOf(sql.NullString{})
case "BLOB":
return reflect.TypeOf([]byte{})
case "NULL":
return reflect.TypeOf(nil)
default:
return reflect.TypeOf((*interface{})(nil)).Elem()
}
}
func (r *tursoRows) isClosed() bool {
return r.ctx == 0 || r.closed
}
func dequoteIdent(s string) string {
s = strings.TrimSpace(s)
if len(s) >= 2 {
switch s[0] {
case '`':
if s[len(s)-1] == '`' {
s = s[1 : len(s)-1]
s = strings.ReplaceAll(s, "``", "`")
}
case '"':
if s[len(s)-1] == '"' {
s = s[1 : len(s)-1]
s = strings.ReplaceAll(s, `""`, `"`)
}
case '[':
if s[len(s)-1] == ']' {
s = s[1 : len(s)-1]
}
}
}
return s
}
func baseName(s string) string {
parts := strings.Split(s, ".")
return parts[len(parts)-1]
}
// database/sql driver.go 425
/**
* Columns returns the names of the columns. The number of
* columns of the result is inferred from the length of the
* slice. If a particular column name isn't known, an empty
* string should be returned for that entry.
**/
func (r *tursoRows) Columns() []string {
if r.isClosed() {
return nil
}
r.mu.Lock()
defer r.mu.Unlock()
if r.columns != nil {
return r.columns
}
count := rowsGetColumns(r.ctx)
if count <= 0 {
return nil
}
cols := make([]string, 0, count)
for i := 0; i < int(count); i++ {
cstr := rowsGetColumnName(r.ctx, int32(i))
raw := GoString(cstr)
defer freeCString(cstr)
cols = append(cols, dequoteIdent(baseName(raw)))
}
r.columns = cols
return r.columns
}
// database/sql driver.go 431
/**
* Close closes the rows iterator.
**/
func (r *tursoRows) Close() error {
if r.isClosed() {
return nil
}
r.mu.Lock()
r.closed = true
if r.err == nil {
for {
rc := ResultCode(rowsNext(r.ctx))
if rc == Row {
continue
} else if rc == Done {
break
} else if rc == ConstraintViolation {
r.err = errors.New("constraint violation")
} else {
if e := r.getError(); e != nil {
r.err = e
} else {
r.err = fmt.Errorf("query failed: %s", rc.String())
}
}
break
}
}
closeRows(r.ctx)
r.ctx = 0
r.mu.Unlock()
return nil
}
func (r *tursoRows) Err() error {
if r.err == nil {
r.mu.Lock()
defer r.mu.Unlock()
r.getError()
}
return r.err
}
// database/sql driver.go 235
/**
* Next is called to populate the next row of data into
* the provided slice. The provided slice will be the same
* size as the Columns() are wide.
*
* Next should return io.EOF when there are no more rows.
*
* The dest should not be written to outside of Next. Care
* should be taken when closing Rows not to modify
* a buffer held in dest.
**/
func (r *tursoRows) Next(dest []driver.Value) error {
r.mu.Lock()
defer r.mu.Unlock()
if r.isClosed() {
if r.err != nil {
return r.err
}
return io.EOF
}
for {
rc := ResultCode(rowsNext(r.ctx))
switch rc {
case Row:
ncol := int(rowsGetColumns(r.ctx))
if ncol < 0 {
if e := r.getError(); e != nil {
r.err = e
} else {
r.err = errors.New("rows: negative column count")
}
return r.err
}
if len(dest) > ncol {
dest = dest[:ncol]
}
for i := 0; i < len(dest); i++ {
vp := rowsGetValue(r.ctx, uint64(i))
if vp == 0 {
if e := r.getError(); e != nil {
r.err = e
} else {
r.err = fmt.Errorf("rows: missing value at column %d", i)
}
return r.err
}
dest[i] = toGoValue(vp)
}
return nil
case ConstraintViolation:
r.err = errors.New("constraint violation")
return r.err
case Io:
continue
case Done:
return io.EOF
default:
if e := r.getError(); e != nil {
r.err = e
} else {
r.err = fmt.Errorf("query failed: %s", rc.String())
}
return r.err
}
}
}
// mutex will already be locked. this is always called after FFI
func (r *tursoRows) getError() error {
if r.isClosed() {
return r.err
}
err := rowsGetError(r.ctx)
if err == 0 {
return nil
}
defer freeCString(err)
cpy := fmt.Sprintf("%s", GoString(err))
r.err = errors.New(cpy)
return r.err
}