-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb.go
275 lines (230 loc) · 7.07 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
package yago
import (
"context"
"database/sql"
"fmt"
"github.com/orus-io/qb"
)
// IDB is the common interface of DB and Tx
type IDB interface {
Insert(MappedStruct) error
Update(MappedStruct, ...string) error
Delete(MappedStruct) error
Query(MapperProvider) Query
InsertContext(context.Context, MappedStruct) error
UpdateContext(context.Context, MappedStruct, ...string) error
DeleteContext(context.Context, MappedStruct) error
GetEngine() Engine
}
// Engine is the common interface of qb.Engine and qb.Tx
type Engine interface {
Exec(builder qb.Builder) (sql.Result, error)
Query(builder qb.Builder) (*sql.Rows, error)
QueryRow(builder qb.Builder) qb.Row
ExecContext(ctx context.Context, builder qb.Builder) (sql.Result, error)
QueryContext(ctx context.Context, builder qb.Builder) (*sql.Rows, error)
QueryRowContext(ctx context.Context, builder qb.Builder) qb.Row
}
// New initialise a new DB
func New(metadata *Metadata, engine *qb.Engine) *DB {
return &DB{
metadata,
engine,
DefaultCallbacks,
}
}
// DB is a database handle with callbacks that links a Metadata and a qb.Engine
type DB struct {
Metadata *Metadata
Engine *qb.Engine
Callbacks Callbacks
}
// GetEngine returns the underlying engine
func (db DB) GetEngine() Engine {
return db.Engine
}
// Insert a struct in the database
func (db *DB) Insert(s MappedStruct) error {
return db.doInsert(context.Background(), db.Engine, s)
}
// InsertContext a struct in the database
func (db *DB) InsertContext(ctx context.Context, s MappedStruct) error {
return db.doInsert(ctx, db.Engine, s)
}
// Update the struct attributes in DB
func (db *DB) Update(s MappedStruct, fields ...string) error {
return db.doUpdate(context.Background(), db.Engine, s, fields...)
}
// UpdateContext the struct attributes in DB
func (db *DB) UpdateContext(ctx context.Context, s MappedStruct, fields ...string) error {
return db.doUpdate(ctx, db.Engine, s, fields...)
}
// Delete a struct in the database
func (db *DB) Delete(s MappedStruct) error {
return db.doDelete(context.Background(), db.Engine, s)
}
// Delete a struct in the database
func (db *DB) DeleteContext(ctx context.Context, s MappedStruct) error {
return db.doDelete(ctx, db.Engine, s)
}
func (db *DB) doInsertWithReturning(ctx context.Context, engine Engine, s MappedStruct) error {
mapper := db.Metadata.GetMapper(s)
insert := mapper.Table().Insert().
Values(mapper.SQLValues(s)).
Returning(mapper.Table().PrimaryCols()...)
rows, err := engine.QueryContext(ctx, insert)
if err != nil {
return err
}
defer rows.Close()
if !rows.Next() {
return fmt.Errorf("yago Insert: No row returned by insert")
}
if err := mapper.ScanPKey(rows, s); err != nil {
return fmt.Errorf("yago Insert: Error scanning the returned pkey: %s", err)
}
if rows.Next() {
return ErrMultipleRecords
}
return nil
}
// Close closes the underlying db connection
func (db *DB) Close() error {
return db.Engine.Close()
}
func (db *DB) doInsert(ctx context.Context, engine Engine, s MappedStruct) error {
db.Callbacks.BeforeInsert.Call(db, s)
mapper := db.Metadata.GetMapper(s)
if mapper.AutoIncrementPKey() && db.Engine.Dialect().Driver() == "postgres" {
return db.doInsertWithReturning(ctx, engine, s)
}
insert := mapper.Table().Insert().Values(mapper.SQLValues(s))
res, err := engine.ExecContext(ctx, insert)
if err != nil {
return err
}
ra, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("yago Insert: RowsAffected() failed with '%s'", err)
}
if ra != 1 {
return fmt.Errorf("Update Insert. More than 1 row where affected")
}
if mapper.AutoIncrementPKey() {
if pkey, err := res.LastInsertId(); err != nil {
panic(err)
} else {
mapper.LoadAutoIncrementPKeyValue(s, pkey)
}
}
// TODO get the generated pkey, if any, and set it on the MappedStruct
db.Callbacks.AfterInsert.Call(db, s)
return nil
}
func (db *DB) doUpdate(ctx context.Context, engine Engine, s MappedStruct, fields ...string) error {
db.Callbacks.BeforeUpdate.Call(db, s)
mapper := db.Metadata.GetMapper(s)
update := mapper.Table().Update().
Values(mapper.SQLValues(s, fields...)).
Where(mapper.PKeyClause(mapper.PKey(s)))
res, err := engine.ExecContext(ctx, update)
if err != nil {
return err
}
ra, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("yago Update: RowsAffected() failed with '%s'", err)
}
if ra == 0 {
return ErrRecordNotFound
} else if ra > 1 {
return ErrMultipleRecords
}
db.Callbacks.AfterUpdate.Call(db, s)
return nil
}
// Query returns a new Query for the struct
func (db *DB) Query(mp MapperProvider) Query {
mapper := mp.GetMapper()
return NewQuery(db, mapper)
}
// Delete a struct from the database
func (db *DB) doDelete(ctx context.Context, engine Engine, s MappedStruct) error {
db.Callbacks.BeforeDelete.Call(db, s)
mapper := db.Metadata.GetMapper(s)
del := mapper.Table().Delete().Where(mapper.PKeyClause(mapper.PKey(s)))
res, err := engine.ExecContext(ctx, del)
if err != nil {
return err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return ErrRecordNotFound
} else if rowsAffected != 1 {
return fmt.Errorf("Wrong number of rows affected. Expected 1, got %v", rowsAffected)
}
db.Callbacks.AfterDelete.Call(db, s)
return nil
}
// Begin start a new transaction
func (db *DB) Begin() (*Tx, error) {
return db.BeginContext(context.Background())
}
// BeginContext start a new transaction
func (db *DB) BeginContext(ctx context.Context) (*Tx, error) {
tx, err := db.Engine.BeginContext(ctx)
if err != nil {
return nil, err
}
return &Tx{db: db, tx: tx}, nil
}
// Tx is an on-going database transaction
type Tx struct {
db *DB
tx *qb.Tx
}
// GetEngine returns the underlying qb.Tx
func (tx Tx) GetEngine() Engine {
return tx.tx
}
// Insert a new struct to the database
func (tx Tx) Insert(s MappedStruct) error {
return tx.db.doInsert(context.Background(), tx.tx, s)
}
// InsertContext a new struct to the database
func (tx Tx) InsertContext(ctx context.Context, s MappedStruct) error {
return tx.db.doInsert(ctx, tx.tx, s)
}
// Update write struct values to the database
// If fields is provided, only theses fields are written
func (tx Tx) Update(s MappedStruct, fields ...string) error {
return tx.db.doUpdate(context.Background(), tx.tx, s, fields...)
}
// UpdateContext write struct values to the database
// If fields is provided, only theses fields are written
func (tx Tx) UpdateContext(ctx context.Context, s MappedStruct, fields ...string) error {
return tx.db.doUpdate(ctx, tx.tx, s, fields...)
}
// Delete drop a struct from the database
func (tx Tx) Delete(s MappedStruct) error {
return tx.db.doDelete(context.Background(), tx.tx, s)
}
// DeleteContext drop a struct from the database
func (tx Tx) DeleteContext(ctx context.Context, s MappedStruct) error {
return tx.db.doDelete(ctx, tx.tx, s)
}
// Query returns a new Query
func (tx Tx) Query(mp MapperProvider) Query {
return NewQuery(tx, mp.GetMapper())
}
// Commit commits the transaction
func (tx Tx) Commit() error {
return tx.tx.Commit()
}
// Rollback aborts the transaction
func (tx Tx) Rollback() error {
return tx.tx.Rollback()
}