-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlitecx.go
More file actions
142 lines (120 loc) · 3.05 KB
/
sqlitecx.go
File metadata and controls
142 lines (120 loc) · 3.05 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
package sqlitecx
import (
"context"
"math"
"time"
"github.com/georgysavva/scany/v2/dbscan"
sqlite "github.com/go-llsqlite/crawshaw"
"github.com/go-llsqlite/crawshaw/sqlitex"
)
type Executor struct {
Transient bool
}
func (s Executor) PooledExecute(ctx context.Context, pool *sqlitex.Pool, query string, prep QueryPrep, result QueryResult) error {
conn := pool.Get(ctx)
if conn == nil {
return ctx.Err()
}
defer pool.Put(conn)
return s.JustExec(conn, query, prep, result)
}
func PooledExecute(ctx context.Context, pool *sqlitex.Pool, query string, prep QueryPrep, result QueryResult) error {
return Executor{}.PooledExecute(ctx, pool, query, prep, result)
}
func processQueryPrep(s *sqlite.Stmt, p QueryPrep) {
if p.prepFn != nil {
p.prepFn(s)
} else if p.prepBind.IsValid() {
bindFields(s, p.prepBind)
}
}
func (s Executor) query(conn *sqlite.Conn, query string, prep QueryPrep) (*Rows, error) {
var stmt *sqlite.Stmt
var err error
if s.Transient {
stmt, _, err = conn.PrepareTransient(query)
} else {
stmt, err = conn.Prepare(query)
}
if err != nil {
return nil, err
}
processQueryPrep(stmt, prep)
return &Rows{
s: stmt,
transient: s.Transient,
}, nil
}
type QueryResult struct {
fn func(*sqlite.Stmt) error
dbScanAll interface{}
dbScanOne interface{}
}
func ResultFunc(f func(s *sqlite.Stmt) error) QueryResult {
return QueryResult{fn: f}
}
func NoResult() QueryResult { return QueryResult{} }
func ScanAll(dst interface{}) QueryResult {
return QueryResult{dbScanAll: dst}
}
func ScanOne(dst interface{}) QueryResult {
return QueryResult{dbScanOne: dst}
}
func (s Executor) JustExec(conn *sqlite.Conn, query string, prep QueryPrep, result QueryResult) error {
rows, err := s.query(conn, query, prep)
if err != nil {
return err
}
if result.dbScanAll != nil {
return dbscan.ScanAll(result.dbScanAll, rows)
} else if result.dbScanOne != nil {
return dbscan.ScanOne(result.dbScanOne, rows)
}
defer rows.Close()
return execLoop(rows.s, result.fn)
}
func JustExec(conn *sqlite.Conn, query string, prep QueryPrep, result QueryResult) error {
return Executor{}.JustExec(conn, query, prep, result)
}
func execLoop(stmt *sqlite.Stmt, resultFn func(stmt *sqlite.Stmt) error) error {
for {
hasRow, err := stmt.Step()
if err != nil {
return err
}
if !hasRow {
return nil
}
if resultFn == nil {
continue
}
if err := resultFn(stmt); err != nil {
return err
}
}
}
func ToSQLiteTime(t time.Time) float64 {
return ((float64(t.UnixNano()) / float64(time.Second)) / 86400.0) + 2440587.5
}
func FromSQLiteTime(f float64) time.Time {
nt := int64(math.Round((f - 2440587.5) * 86400.0 * 1000))
return time.Unix(0, nt*1000*1000)
}
func StmtGetBytesName(stmt *sqlite.Stmt, index string) []byte {
n := stmt.GetLen(index)
if n == 0 {
return nil
}
result := make([]byte, n)
stmt.GetBytes(index, result)
return result
}
func StmtGetBytes(stmt *sqlite.Stmt, index int) []byte {
n := stmt.ColumnLen(index)
if n == 0 {
return nil
}
result := make([]byte, n)
stmt.ColumnBytes(index, result)
return result
}