-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.go
158 lines (140 loc) · 3.09 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package xsql
import (
"database/sql"
"reflect"
"strings"
)
type TimeFunc func(placeholder string) string
type DB struct {
Options *sqlOptions
raw *sql.DB
executor executor
query query
}
func New(db *sql.DB, opts ...SqlOption) *DB {
return &DB{
Options: mergeOptions(opts),
raw: db,
executor: executor{
Executor: db,
},
query: query{
Query: db,
},
}
}
func (t *DB) mergeOptions(opts []SqlOption) *sqlOptions {
cp := *t.Options // copy
for _, o := range opts {
o.apply(&cp)
}
return &cp
}
func (t *DB) Insert(data interface{}, opts ...SqlOption) (sql.Result, error) {
return t.executor.Insert(data, t.mergeOptions(opts))
}
func (t *DB) BatchInsert(data interface{}, opts ...SqlOption) (sql.Result, error) {
return t.executor.BatchInsert(data, t.mergeOptions(opts))
}
func (t *DB) Update(data interface{}, expr string, args ...interface{}) (sql.Result, error) {
return t.executor.Update(data, expr, args, t.Options)
}
func (t *DB) Model(s interface{}) *ModelExecutor {
return t.executor.model(s, t.Options)
}
func (t *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
return t.executor.Exec(query, args, t.Options)
}
func (t *DB) Begin() (*Tx, error) {
tx, err := t.raw.Begin()
if err != nil {
return nil, err
}
return &Tx{
raw: tx,
DB: &DB{
Options: t.Options,
executor: executor{
Executor: tx,
},
query: query{
Query: tx,
},
},
}, nil
}
func (t *DB) Query(query string, args ...interface{}) ([]*Row, error) {
f, err := t.query.Fetch(query, args, t.Options)
if err != nil {
return nil, err
}
r, err := f.Rows()
if err != nil {
return nil, err
}
return r, nil
}
func (t *DB) QueryFirst(query string, args ...interface{}) (*Row, error) {
rows, err := t.Query(query, args...)
if err != nil {
return nil, err
}
if len(rows) == 0 {
return nil, sql.ErrNoRows
}
return rows[0], nil
}
func (t *DB) Find(i interface{}, query string, args ...interface{}) error {
query = t.tableComplete(i, query)
f, err := t.query.Fetch(query, args, t.Options)
if err != nil {
return err
}
if err := f.Find(i); err != nil {
return err
}
return nil
}
func (t *DB) First(i interface{}, query string, args ...interface{}) error {
query = t.tableComplete(i, query)
f, err := t.query.Fetch(query, args, t.Options)
if err != nil {
return err
}
if err := f.First(i); err != nil {
return err
}
return nil
}
func (t *DB) tableComplete(i interface{}, query string) string {
var table string
value := reflect.ValueOf(i)
switch value.Kind() {
case reflect.Ptr:
return t.tableComplete(value.Elem().Interface(), query)
case reflect.Struct:
if tab, ok := i.(Table); ok {
table = tab.TableName()
} else {
table = value.Type().Name()
}
break
case reflect.Array, reflect.Slice:
typ := value.Type().Elem()
switch typ.Kind() {
case reflect.Struct:
if tab, ok := reflect.New(typ).Interface().(Table); ok {
table = tab.TableName()
} else {
table = typ.Name()
}
break
default:
return query // err
}
break
default:
return query // err
}
return strings.Replace(query, "${TABLE}", table, 1)
}