-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.go
110 lines (99 loc) · 1.9 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
99
100
101
102
103
104
105
106
107
108
109
110
// Code generated by queryx, DO NOT EDIT.
package queryx
import (
"database/sql"
"regexp"
)
type DB interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
}
type Adapter struct {
db DB
}
func NewAdapter(db DB) *Adapter {
return &Adapter{db: db}
}
func (a *Adapter) Query(query string, args ...interface{}) *Rows {
return &Rows{
adapter: a,
query: query,
args: args,
}
}
type Rows struct {
adapter *Adapter
query string
args []interface{}
err error
}
func (r *Rows) Scan(v interface{}) error {
if r.err != nil {
return r.err
}
var err error
query, args := r.query, r.args
matched1, err := regexp.MatchString(`.* IN (.*?)`, query)
if err != nil {
return err
}
matched2, err := regexp.MatchString(`.* in (.*?)`, query)
if err != nil {
return err
}
if matched1 || matched2 {
query, args, err = In(query, args...)
if err != nil {
return err
}
}
query, args = rebind(query, args)
rows, err := r.adapter.db.Query(query, args...)
if err != nil {
return err
}
err = ScanSlice(rows, v)
if err != nil {
return err
}
return err
}
type Row struct {
adapter *Adapter
query string
args []interface{}
}
func (r *Row) Scan(v interface{}) error {
query, args := r.query, r.args
matched1, err := regexp.MatchString(`.* IN (.*?)`, query)
if err != nil {
return err
}
matched2, err := regexp.MatchString(`.* in (.*?)`, query)
if err != nil {
return err
}
if matched1 || matched2 {
query, args, err = In(query, args...)
if err != nil {
return err
}
}
query, args = rebind(query, args)
rows, err := r.adapter.db.Query(query, args...)
if err != nil {
return err
}
err = ScanOne(rows, v)
if err != nil {
return err
}
return err
}
func (a *Adapter) QueryOne(query string, args ...interface{}) *Row {
return &Row{
adapter: a,
query: query,
args: args,
}
}