-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatementwrapper.go
More file actions
70 lines (61 loc) · 1.64 KB
/
statementwrapper.go
File metadata and controls
70 lines (61 loc) · 1.64 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
package main
import (
"crawshaw.io/sqlite"
"github.com/jyopp/absorb"
)
// CastAbsorbable returns a simple typecast of an *sqlite.Stmt
// to the alias type StmtAbsorbable, which is an Absorbable facade.
func CastAbsorbable(s *sqlite.Stmt) *StmtAbsorbable {
return (*StmtAbsorbable)(s)
}
// Casting a *sqlite.Stmt to a *StatementWrapper makes it absorb.Absorbable
type StmtAbsorbable sqlite.Stmt
func (a *StmtAbsorbable) Stmt() *sqlite.Stmt {
return (*sqlite.Stmt)(a)
}
// StmtAbsorbable implements absorb.Absorbable
func (a *StmtAbsorbable) Emit(into absorb.Absorber) error {
s := (*sqlite.Stmt)(a)
colCount := s.ColumnCount()
keys := make([]string, colCount)
for idx := range keys {
keys[idx] = s.ColumnName(idx)
}
into.Open("sqlite", -1, keys...)
defer into.Close()
// Create one slice that we'll overwrite with each row.
rowData := make([]interface{}, colCount)
hasRow, err := s.Step()
for ; hasRow && (err == nil); hasRow, err = s.Step() {
// Copy stmt into rowData
for idx := range rowData {
rowData[idx] = a.colInterface(idx)
}
into.Absorb(rowData...)
}
return err
}
func (a *StmtAbsorbable) colInterface(colNum int) interface{} {
s := (*sqlite.Stmt)(a)
switch s.ColumnType(colNum) {
case sqlite.SQLITE_INTEGER:
return s.ColumnInt64(colNum)
case sqlite.SQLITE_FLOAT:
return s.ColumnFloat(colNum)
case sqlite.SQLITE_TEXT:
return s.ColumnText(colNum)
case sqlite.SQLITE_BLOB:
l := s.ColumnLen(colNum)
if l > 0 {
blob := make([]byte, l)
if l == s.ColumnBytes(colNum, blob) {
return blob
}
}
return []byte{}
case sqlite.SQLITE_NULL:
return nil
default:
panic("Encountered undocumented sqlite type")
}
}