/
igorm.go
400 lines (319 loc) · 10.3 KB
/
igorm.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package igorm
import "database/sql"
import "github.com/jinzhu/gorm"
// Gormw is an interface which DB implements
type Gormw interface {
Close() error
DB() *sql.DB
New() Gormw
NewScope(value interface{}) *gorm.Scope
CommonDB() gorm.SQLCommon
Callback() *gorm.Callback
SetLogger(l gorm.Logger)
LogMode(enable bool) Gormw
SingularTable(enable bool)
Where(query interface{}, args ...interface{}) Gormw
Or(query interface{}, args ...interface{}) Gormw
Not(query interface{}, args ...interface{}) Gormw
Limit(value int) Gormw
Offset(value int) Gormw
Order(value string, reorder ...bool) Gormw
Select(query interface{}, args ...interface{}) Gormw
Omit(columns ...string) Gormw
Group(query string) Gormw
Having(query string, values ...interface{}) Gormw
Joins(query string, args ...interface{}) Gormw
Scopes(funcs ...func(*gorm.DB) *gorm.DB) Gormw
Unscoped() Gormw
Attrs(attrs ...interface{}) Gormw
Assign(attrs ...interface{}) Gormw
First(out interface{}, where ...interface{}) Gormw
Last(out interface{}, where ...interface{}) Gormw
Find(out interface{}, where ...interface{}) Gormw
Scan(dest interface{}) Gormw
Row() *sql.Row
Rows() (*sql.Rows, error)
ScanRows(rows *sql.Rows, result interface{}) error
Pluck(column string, value interface{}) Gormw
Count(value interface{}) Gormw
Related(value interface{}, foreignKeys ...string) Gormw
FirstOrInit(out interface{}, where ...interface{}) Gormw
FirstOrCreate(out interface{}, where ...interface{}) Gormw
Update(attrs ...interface{}) Gormw
Updates(values interface{}, ignoreProtectedAttrs ...bool) Gormw
UpdateColumn(attrs ...interface{}) Gormw
UpdateColumns(values interface{}) Gormw
Save(value interface{}) Gormw
Create(value interface{}) Gormw
Delete(value interface{}, where ...interface{}) Gormw
Raw(sql string, values ...interface{}) Gormw
Exec(sql string, values ...interface{}) Gormw
Model(value interface{}) Gormw
Table(name string) Gormw
Debug() Gormw
Begin() Gormw
Commit() Gormw
Rollback() Gormw
RollbackUnlessCommitted() Gormw
NewRecord(value interface{}) bool
RecordNotFound() bool
CreateTable(values ...interface{}) Gormw
DropTable(values ...interface{}) Gormw
DropTableIfExists(values ...interface{}) Gormw
HasTable(value interface{}) bool
AutoMigrate(values ...interface{}) Gormw
ModifyColumn(column string, typ string) Gormw
DropColumn(column string) Gormw
AddIndex(indexName string, column ...string) Gormw
AddUniqueIndex(indexName string, column ...string) Gormw
RemoveIndex(indexName string) Gormw
AddForeignKey(field string, dest string, onDelete string, onUpdate string) Gormw
Association(column string) *gorm.Association
Preload(column string, conditions ...interface{}) Gormw
Set(name string, value interface{}) Gormw
InstantSet(name string, value interface{}) Gormw
Get(name string) (value interface{}, ok bool)
SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface)
AddError(err error) error
GetErrors() (errors []error)
// extra
Error() error
RowsAffected() int64
}
type gormw struct {
w *gorm.DB
}
// Openw is a drop-in replacement for Open()
func Openw(dialect string, args ...interface{}) (db Gormw, err error) {
gormdb, err := gorm.Open(dialect, args...)
return Wrap(gormdb), err
}
// Wrap wraps gorm.DB in an interface
func Wrap(db *gorm.DB) Gormw {
return &gormw{db}
}
func (it *gormw) Close() error {
return it.w.Close()
}
func (it *gormw) DB() *sql.DB {
return it.w.DB()
}
func (it *gormw) New() Gormw {
return Wrap(it.w.New())
}
func (it *gormw) NewScope(value interface{}) *gorm.Scope {
return it.w.NewScope(value)
}
func (it *gormw) CommonDB() gorm.SQLCommon {
return it.w.CommonDB()
}
func (it *gormw) Callback() *gorm.Callback {
return it.w.Callback()
}
func (it *gormw) SetLogger(log gorm.Logger) {
it.w.SetLogger(log)
}
func (it *gormw) LogMode(enable bool) Gormw {
return Wrap(it.w.LogMode(enable))
}
func (it *gormw) SingularTable(enable bool) {
it.w.SingularTable(enable)
}
func (it *gormw) Where(query interface{}, args ...interface{}) Gormw {
return Wrap(it.w.Where(query, args...))
}
func (it *gormw) Or(query interface{}, args ...interface{}) Gormw {
return Wrap(it.w.Or(query, args...))
}
func (it *gormw) Not(query interface{}, args ...interface{}) Gormw {
return Wrap(it.w.Not(query, args...))
}
func (it *gormw) Limit(value int) Gormw {
return Wrap(it.w.Limit(value))
}
func (it *gormw) Offset(value int) Gormw {
return Wrap(it.w.Offset(value))
}
func (it *gormw) Order(value string, reorder ...bool) Gormw {
return Wrap(it.w.Order(value, reorder...))
}
func (it *gormw) Select(query interface{}, args ...interface{}) Gormw {
return Wrap(it.w.Select(query, args...))
}
func (it *gormw) Omit(columns ...string) Gormw {
return Wrap(it.w.Omit(columns...))
}
func (it *gormw) Group(query string) Gormw {
return Wrap(it.w.Group(query))
}
func (it *gormw) Having(query string, values ...interface{}) Gormw {
return Wrap(it.w.Having(query, values...))
}
func (it *gormw) Joins(query string, args ...interface{}) Gormw {
return Wrap(it.w.Joins(query, args...))
}
func (it *gormw) Scopes(funcs ...func(*gorm.DB) *gorm.DB) Gormw {
return Wrap(it.w.Scopes(funcs...))
}
func (it *gormw) Unscoped() Gormw {
return Wrap(it.w.Unscoped())
}
func (it *gormw) Attrs(attrs ...interface{}) Gormw {
return Wrap(it.w.Attrs(attrs...))
}
func (it *gormw) Assign(attrs ...interface{}) Gormw {
return Wrap(it.w.Assign(attrs...))
}
func (it *gormw) First(out interface{}, where ...interface{}) Gormw {
return Wrap(it.w.First(out, where...))
}
func (it *gormw) Last(out interface{}, where ...interface{}) Gormw {
return Wrap(it.w.Last(out, where...))
}
func (it *gormw) Find(out interface{}, where ...interface{}) Gormw {
return Wrap(it.w.Find(out, where...))
}
func (it *gormw) Scan(dest interface{}) Gormw {
return Wrap(it.w.Scan(dest))
}
func (it *gormw) Row() *sql.Row {
return it.w.Row()
}
func (it *gormw) Rows() (*sql.Rows, error) {
return it.w.Rows()
}
func (it *gormw) ScanRows(rows *sql.Rows, result interface{}) error {
return it.w.ScanRows(rows, result)
}
func (it *gormw) Pluck(column string, value interface{}) Gormw {
return Wrap(it.w.Pluck(column, value))
}
func (it *gormw) Count(value interface{}) Gormw {
return Wrap(it.w.Count(value))
}
func (it *gormw) Related(value interface{}, foreignKeys ...string) Gormw {
return Wrap(it.w.Related(value, foreignKeys...))
}
func (it *gormw) FirstOrInit(out interface{}, where ...interface{}) Gormw {
return Wrap(it.w.FirstOrInit(out, where...))
}
func (it *gormw) FirstOrCreate(out interface{}, where ...interface{}) Gormw {
return Wrap(it.w.FirstOrCreate(out, where...))
}
func (it *gormw) Update(attrs ...interface{}) Gormw {
return Wrap(it.w.Update(attrs...))
}
func (it *gormw) Updates(values interface{}, ignoreProtectedAttrs ...bool) Gormw {
return Wrap(it.w.Updates(values, ignoreProtectedAttrs...))
}
func (it *gormw) UpdateColumn(attrs ...interface{}) Gormw {
return Wrap(it.w.UpdateColumn(attrs...))
}
func (it *gormw) UpdateColumns(values interface{}) Gormw {
return Wrap(it.w.UpdateColumns(values))
}
func (it *gormw) Save(value interface{}) Gormw {
return Wrap(it.w.Save(value))
}
func (it *gormw) Create(value interface{}) Gormw {
return Wrap(it.w.Create(value))
}
func (it *gormw) Delete(value interface{}, where ...interface{}) Gormw {
return Wrap(it.w.Delete(value, where...))
}
func (it *gormw) Raw(sql string, values ...interface{}) Gormw {
return Wrap(it.w.Raw(sql, values...))
}
func (it *gormw) Exec(sql string, values ...interface{}) Gormw {
return Wrap(it.w.Exec(sql, values...))
}
func (it *gormw) Model(value interface{}) Gormw {
return Wrap(it.w.Model(value))
}
func (it *gormw) Table(name string) Gormw {
return Wrap(it.w.Table(name))
}
func (it *gormw) Debug() Gormw {
return Wrap(it.w.Debug())
}
func (it *gormw) Begin() Gormw {
return Wrap(it.w.Begin())
}
func (it *gormw) Commit() Gormw {
return Wrap(it.w.Commit())
}
func (it *gormw) Rollback() Gormw {
return Wrap(it.w.Rollback())
}
func (it *gormw) RollbackUnlessCommitted() Gormw {
return Wrap(it.w.RollbackUnlessCommitted())
}
func (it *gormw) NewRecord(value interface{}) bool {
return it.w.NewRecord(value)
}
func (it *gormw) RecordNotFound() bool {
return it.w.RecordNotFound()
}
func (it *gormw) CreateTable(values ...interface{}) Gormw {
return Wrap(it.w.CreateTable(values...))
}
func (it *gormw) DropTable(values ...interface{}) Gormw {
return Wrap(it.w.DropTable(values...))
}
func (it *gormw) DropTableIfExists(values ...interface{}) Gormw {
return Wrap(it.w.DropTableIfExists(values...))
}
func (it *gormw) HasTable(value interface{}) bool {
return it.w.HasTable(value)
}
func (it *gormw) AutoMigrate(values ...interface{}) Gormw {
return Wrap(it.w.AutoMigrate(values...))
}
func (it *gormw) ModifyColumn(column string, typ string) Gormw {
return Wrap(it.w.ModifyColumn(column, typ))
}
func (it *gormw) DropColumn(column string) Gormw {
return Wrap(it.w.DropColumn(column))
}
func (it *gormw) AddIndex(indexName string, columns ...string) Gormw {
return Wrap(it.w.AddIndex(indexName, columns...))
}
func (it *gormw) AddUniqueIndex(indexName string, columns ...string) Gormw {
return Wrap(it.w.AddUniqueIndex(indexName, columns...))
}
func (it *gormw) RemoveIndex(indexName string) Gormw {
return Wrap(it.w.RemoveIndex(indexName))
}
func (it *gormw) Association(column string) *gorm.Association {
return it.w.Association(column)
}
func (it *gormw) Preload(column string, conditions ...interface{}) Gormw {
return Wrap(it.w.Preload(column, conditions...))
}
func (it *gormw) Set(name string, value interface{}) Gormw {
return Wrap(it.w.Set(name, value))
}
func (it *gormw) InstantSet(name string, value interface{}) Gormw {
return Wrap(it.w.InstantSet(name, value))
}
func (it *gormw) Get(name string) (interface{}, bool) {
return it.w.Get(name)
}
func (it *gormw) SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface) {
it.w.SetJoinTableHandler(source, column, handler)
}
func (it *gormw) AddForeignKey(field string, dest string, onDelete string, onUpdate string) Gormw {
return Wrap(it.w.AddForeignKey(field, dest, onDelete, onUpdate))
}
func (it *gormw) AddError(err error) error {
return it.w.AddError(err)
}
func (it *gormw) GetErrors() (errors []error) {
return it.w.GetErrors()
}
func (it *gormw) RowsAffected() int64 {
return it.w.RowsAffected
}
func (it *gormw) Error() error {
return it.w.Error
}