-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
db.go
98 lines (84 loc) · 1.46 KB
/
db.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
//go:build wasm && js
package main
import (
"database/sql/driver"
"fmt"
"hash/fnv"
"io"
"sync"
"syscall/js"
)
var rowsPool = sync.Pool{
New: func() interface{} {
return new(Rows)
},
}
type Rows struct {
cols []string
rows [][]interface{}
index int
colsA [1]string
rowsA [1][1]interface{}
useArray bool
}
var zeroRows = Rows{}
func (r *Rows) Columns() []string {
if r.useArray {
return r.colsA[:]
} else {
return r.cols
}
}
func (r *Rows) Close() error {
rowsPool.Put(r)
return nil
}
func (r *Rows) Next(dest []driver.Value) error {
if r.useArray {
if r.index > 0 {
return io.EOF
}
dest[0] = r.rowsA[0][0]
r.index++
return nil
}
if len(r.rows) == 0 || r.index >= len(r.rows) {
return io.EOF
}
if len(r.rows[0]) != len(dest) {
return fmt.Errorf("expected %d destination fields got %d",
len(dest), len(r.rows[0]))
}
for i := range dest {
dest[i] = r.rows[r.index][i]
}
r.index++
return nil
}
type Result struct {
lastInsertId int64
rowsAffected int64
}
func (r *Result) LastInsertId() (int64, error) {
return r.lastInsertId, nil
}
func (r *Result) RowsAffected() (int64, error) {
return r.rowsAffected, nil
}
func colVal(col js.Value) interface{} {
switch col.Type() {
case js.TypeBoolean:
return col.Bool()
case js.TypeNumber:
return col.Int()
case js.TypeString:
return []byte(col.String())
default:
return nil
}
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}