This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
db.go
285 lines (219 loc) · 7.48 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Package gomodel is a library help for interact with database efficiently
package gomodel
import (
"database/sql"
"fmt"
"github.com/cosiner/gomodel/utils"
)
type (
// DB holds database connections, store all tables
DB struct {
*sql.DB
driver Driver
tables map[string]*Table
cache cache
// initial models count for select 'All', default 20
InitialModels int
}
)
// Open create a database manager and connect to database server
func Open(driver Driver, dsn string, maxIdle, maxOpen int) (*DB, error) {
db := NewDB()
err := db.Connect(driver, dsn, maxIdle, maxOpen)
return db, err
}
// NewDB create a new DB instance
func NewDB() *DB {
return &DB{
tables: make(map[string]*Table),
InitialModels: 20,
}
}
// Connect to database server
func (db *DB) Connect(driver Driver, dsn string, maxIdle, maxOpen int) error {
db_, err := sql.Open(driver.String(), dsn)
if err != nil {
return err
}
db_.SetMaxIdleConns(maxIdle)
db_.SetMaxOpenConns(maxOpen)
return db.Use(driver, db_)
}
func (db *DB) Use(driver Driver, db_ *sql.DB) error {
db.driver = driver
db.DB = db_
db.cache = newCache()
return nil
}
func (db *DB) Driver() Driver {
return db.driver
}
// Table return infomation of given model
// if table not exist, do parse and save it
func (db *DB) Table(model Model) *Table {
table := model.Table()
t, has := db.tables[table]
if !has {
t = parseModel(model, db)
db.tables[table] = t
}
return t
}
func (db *DB) Insert(model Model, fields uint64, resType ResultType) (int64, error) {
return db.ArgsInsert(model, fields, resType, FieldVals(model, fields)...)
}
func (db *DB) ArgsInsert(model Model, fields uint64, resType ResultType, args ...interface{}) (int64, error) {
stmt, err := db.Table(model).StmtInsert(db, fields)
return Exec(stmt, err, resType, args...)
}
func (db *DB) Update(model Model, fields, whereFields uint64) (int64, error) {
c1, c2 := NumFields(fields), NumFields(whereFields)
args := make([]interface{}, c1+c2)
model.Vals(fields, args)
model.Vals(whereFields, args[c1:])
return db.ArgsUpdate(model, fields, whereFields, args...)
}
func (db *DB) ArgsUpdate(model Model, fields, whereFields uint64, args ...interface{}) (int64, error) {
stmt, err := db.Table(model).StmtUpdate(db, fields, whereFields)
return Update(stmt, err, args...)
}
func (db *DB) Delete(model Model, whereFields uint64) (int64, error) {
return db.ArgsDelete(model, whereFields, FieldVals(model, whereFields)...)
}
func (db *DB) ArgsDelete(model Model, whereFields uint64, args ...interface{}) (int64, error) {
stmt, err := db.Table(model).StmtDelete(db, whereFields)
return Update(stmt, err, args...)
}
// One select one row from database
func (db *DB) One(model Model, fields, whereFields uint64) error {
return db.ArgsOne(model, fields, whereFields, FieldVals(model, whereFields))
}
func (db *DB) ArgsOne(model Model, fields, whereFields uint64, args []interface{}, ptrs ...interface{}) error {
stmt, err := db.Table(model).StmtOne(db, fields, whereFields)
scanner := Query(stmt, err, args...)
defer scanner.Close()
if len(ptrs) == 0 {
ptrs = FieldPtrs(model, fields)
}
return scanner.One(ptrs...)
}
func (db *DB) Limit(store Store, model Model, fields, whereFields uint64, start, count int64) error {
args := FieldVals(model, whereFields, start, count)
return db.ArgsLimit(store, model, fields, whereFields, args...)
}
// The last two arguments must be "start" and "count" of limition with type "int"
func (db *DB) ArgsLimit(store Store, model Model, fields, whereFields uint64, args ...interface{}) error {
argc := len(args)
if argc < 2 {
return fmt.Errorf("ArgsLimit need at least two parameters, but only got %d", argc)
}
offset, err := utils.ConvToInt64(args[argc-2])
if err != nil {
return err
}
count, err := utils.ConvToInt64(args[argc-1])
if err != nil {
return err
}
arg1, arg2 := db.driver.ParamLimit(int(offset), int(count))
args[argc-2], args[argc-1] = arg1, arg2
stmt, err := db.Table(model).StmtLimit(db, fields, whereFields)
scanner := Query(stmt, err, args...)
defer scanner.Close()
return scanner.Limit(store, int(count))
}
func (db *DB) All(store Store, model Model, fields, whereFields uint64) error {
return db.ArgsAll(store, model, fields, whereFields, FieldVals(model, whereFields)...)
}
func (db *DB) ArgsAll(store Store, model Model, fields, whereFields uint64, args ...interface{}) error {
stmt, err := db.Table(model).StmtAll(db, fields, whereFields)
scanner := Query(stmt, err, args...)
defer scanner.Close()
return scanner.All(store, db.InitialModels)
}
// Count return count of rows for model, arguments was extracted from Model
func (db *DB) Count(model Model, whereFields uint64) (count int64, err error) {
return db.ArgsCount(model, whereFields, FieldVals(model, whereFields)...)
}
// ArgsCount return count of rows for model use custome arguments
func (db *DB) ArgsCount(model Model, whereFields uint64, args ...interface{}) (count int64, err error) {
t := db.Table(model)
stmt, err := t.StmtCount(db, whereFields)
scanner := Query(stmt, err, args...)
defer scanner.Close()
err = scanner.One(&count)
return
}
func (db *DB) IncrBy(model Model, fields, whereFields uint64, counts ...int) (int64, error) {
cntLen := len(counts)
args := make([]interface{}, NumFields(whereFields)+cntLen)
for i, count := range counts {
args[i] = count
}
model.Vals(whereFields, args[cntLen:])
return db.ArgsIncrBy(model, fields, whereFields, args...)
}
func (db *DB) ArgsIncrBy(model Model, fields, whereFields uint64, args ...interface{}) (int64, error) {
stmt, err := db.Table(model).StmtIncrBy(db, fields, whereFields)
return Update(stmt, err, args...)
}
func (db *DB) Exists(model Model, field, whereFields uint64) (bool, error) {
return db.ArgsExists(model, field, whereFields, FieldVals(model, whereFields)...)
}
func (db *DB) ArgsExists(model Model, field, whereFields uint64, args ...interface{}) (exist bool, err error) {
stmt, err := db.Table(model).StmtExists(db, field, whereFields)
scanner := Query(stmt, err, args...)
defer scanner.Close()
err = scanner.One(&exist)
return
}
func (db *DB) ExecById(sqlid uint64, resTyp ResultType, args ...interface{}) (int64, error) {
stmt, err := db.StmtById(sqlid)
return Exec(stmt, err, resTyp, args...)
}
func (db *DB) UpdateById(sqlid uint64, args ...interface{}) (int64, error) {
return db.ExecById(sqlid, RES_ROWS, args...)
}
func (db *DB) QueryById(sqlid uint64, args ...interface{}) Scanner {
stmt, err := db.StmtById(sqlid)
return Query(stmt, err, args...)
}
func (db *DB) prepare(sql string) (Stmt, error) {
sql = db.driver.Prepare(sql)
sqlPrinter(sql)
stmt, err := db.DB.Prepare(sql)
return WrapStmt(true, stmt, err)
}
func (db *DB) Query(sql string, args ...interface{}) Scanner {
stmt, err := db.prepare(sql)
return Query(stmt, err, args...)
}
func (db *DB) Exec(sql string, resTyp ResultType, args ...interface{}) (int64, error) {
stmt, err := db.prepare(sql)
return CloseExec(stmt, err, resTyp, args...)
}
func (db *DB) ExecUpdate(sql string, args ...interface{}) (int64, error) {
return db.Exec(sql, RES_ROWS, args...)
}
var emptyTX = &Tx{}
func (db *DB) Begin() (*Tx, error) {
tx, err := db.DB.Begin()
if err != nil {
return emptyTX, err
}
return newTx(tx, db), nil
}
func (db *DB) TxDo(do func(*Tx) error) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Close()
err = do(tx)
tx.Success(err == nil)
return err
}
func (db *DB) StmtById(sqlid uint64) (Stmt, error) {
stmt, err := db.cache.StmtById(db, sqlid)
return WrapStmt(STMT_NOPCLOSE, stmt, err)
}