forked from admpub/go-sqlite3-win64
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite3_rows.go
executable file
·136 lines (126 loc) · 3.07 KB
/
sqlite3_rows.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
// +build windows
// Copyright (C) 2016 Samuel Melrose <[email protected]>.
//
// Based on work by Yasuhiro Matsumoto <[email protected]>
// https://github.com/mattn/go-sqlite3
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package sqlite3
import (
"database/sql/driver"
"io"
"strings"
"time"
)
// Close the rows.
func (rc *SQLiteRows) Close() error {
if rc.s.closed {
return nil
}
if rc.done != nil {
close(rc.done)
}
if rc.cls {
return rc.s.Close()
}
rv := sqlite3_reset(rc.s.s)
if rv != SQLITE_OK {
return rc.s.c.lastError()
}
return nil
}
// Columns return column names.
func (rc *SQLiteRows) Columns() []string {
if rc.nc != len(rc.cols) {
rc.cols = make([]string, rc.nc)
for i := 0; i < rc.nc; i++ {
rc.cols[i] = sqlite3_column_name(rc.s.s, i)
}
}
return rc.cols
}
// DeclTypes return column types.
func (rc *SQLiteRows) DeclTypes() []string {
if rc.decltype == nil {
rc.decltype = make([]string, rc.nc)
for i := 0; i < rc.nc; i++ {
rc.decltype[i] = strings.ToLower(sqlite3_column_decltype(rc.s.s, i))
}
}
return rc.decltype
}
// Next move cursor to next.
func (rc *SQLiteRows) Next(dest []driver.Value) error {
var rowid, changes int64
rv := sqlite3_step(rc.s.s, &rowid, &changes)
if rv == SQLITE_DONE {
return io.EOF
}
if rv != SQLITE_ROW {
rv = sqlite3_reset(rc.s.s)
if rv != SQLITE_OK {
return rc.s.c.lastError()
}
return nil
}
rc.DeclTypes()
for i := range dest {
switch sqlite3_column_type(rc.s.s, i) {
case SQLITE_INTEGER:
val := sqlite3_column_int64(rc.s.s, i)
switch rc.decltype[i] {
case "timestamp", "datetime", "date":
var t time.Time
// Assume a millisecond unix timestamp if it's 13 digits -- too
// large to be a reasonable timestamp in seconds.
if val > 1e12 || val < -1e12 {
val *= int64(time.Millisecond) // convert ms to nsec
} else {
val *= int64(time.Second) // convert sec to nsec
}
t = time.Unix(0, val).UTC()
if rc.s.c.loc != nil {
t = t.In(rc.s.c.loc)
}
dest[i] = t
case "boolean":
dest[i] = val > 0
default:
dest[i] = val
}
case SQLITE_FLOAT:
dest[i] = sqlite3_column_double(rc.s.s, i)
case SQLITE_BLOB:
dest[i] = sqlite3_column_blob(rc.s.s, i)
case SQLITE_NULL:
dest[i] = nil
case SQLITE_TEXT:
var err error
var timeVal time.Time
s := sqlite3_column_text(rc.s.s, i)
switch rc.decltype[i] {
case "timestamp", "datetime", "date":
var t time.Time
s = strings.TrimSuffix(s, "Z")
for _, format := range SQLiteTimestampFormats {
if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
t = timeVal
break
}
}
if err != nil {
// The column is a time value, so return the zero time on parse failure.
t = time.Time{}
}
if rc.s.c.loc != nil {
t = t.In(rc.s.c.loc)
}
dest[i] = t
default:
dest[i] = []byte(s)
}
}
}
return nil
}