forked from jinzhu/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
297 lines (240 loc) · 6.74 KB
/
main.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
package gorm
import (
"database/sql"
"github.com/jinzhu/gorm/dialect"
)
type DB struct {
Value interface{}
callback *callback
Error error
db sqlCommon
parent *DB
search *search
logMode int
logger logger
dialect dialect.Dialect
tagIdentifier string
singularTable bool
}
func Open(driver, source string) (DB, error) {
var err error
db := DB{dialect: dialect.New(driver), tagIdentifier: "sql", logger: defaultLogger, callback: DefaultCallback}
db.db, err = sql.Open(driver, source)
db.parent = &db
return db, err
}
func (s *DB) DB() *sql.DB {
return s.db.(*sql.DB)
}
func (s *DB) SetTagIdentifier(str string) {
s.parent.tagIdentifier = str
}
func (s *DB) SetLogger(l logger) {
s.parent.logger = l
}
func (s *DB) LogMode(b bool) *DB {
if b {
s.logMode = 2
} else {
s.logMode = 1
}
return s
}
func (s *DB) SingularTable(b bool) {
s.parent.singularTable = b
}
func (s *DB) Where(query interface{}, args ...interface{}) *DB {
return s.clone().search.where(query, args...).db
}
func (s *DB) Or(query interface{}, args ...interface{}) *DB {
return s.clone().search.or(query, args...).db
}
func (s *DB) Not(query interface{}, args ...interface{}) *DB {
return s.clone().search.not(query, args...).db
}
func (s *DB) Limit(value interface{}) *DB {
return s.clone().search.limit(value).db
}
func (s *DB) Offset(value interface{}) *DB {
return s.clone().search.offset(value).db
}
func (s *DB) Order(value string, reorder ...bool) *DB {
return s.clone().search.order(value, reorder...).db
}
func (s *DB) Select(value interface{}) *DB {
return s.clone().search.selects(value).db
}
func (s *DB) Group(query string) *DB {
return s.clone().search.group(query).db
}
func (s *DB) Having(query string, values ...interface{}) *DB {
return s.clone().search.having(query, values...).db
}
func (s *DB) Joins(query string) *DB {
return s.clone().search.joins(query).db
}
func (s *DB) Includes(value interface{}) *DB {
return s.clone().search.includes(value).db
}
func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
c := s
for _, f := range funcs {
c = f(c)
}
return c
}
func (s *DB) Unscoped() *DB {
return s.clone().search.unscoped().db
}
func (s *DB) First(out interface{}, where ...interface{}) *DB {
return s.clone().do(out).where(where...).first().db
}
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
return s.clone().do(out).where(where...).last().db
}
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
return s.clone().do(out).where(where...).query().db
}
func (s *DB) Row() *sql.Row {
return s.do(s.Value).row()
}
func (s *DB) Rows() (*sql.Rows, error) {
return s.do(s.Value).rows()
}
func (s *DB) Scan(dest interface{}) *DB {
return s.do(s.Value).query(dest).db
}
func (s *DB) Attrs(attrs ...interface{}) *DB {
return s.clone().search.attrs(attrs...).db
}
func (s *DB) Assign(attrs ...interface{}) *DB {
return s.clone().search.assign(attrs...).db
}
func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {
c := s.clone()
if c.First(out, where...).Error == RecordNotFound {
return c.do(out).where(where).initialize().db
} else if len(s.search.assignAttrs) > 0 {
return c.do(out).updateAttrs(s.search.assignAttrs).db
}
return c
}
func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB {
c := s.clone()
if c.First(out, where...).Error == RecordNotFound {
return c.do(out).where(where...).initialize().db.Save(out)
} else if len(s.search.assignAttrs) > 0 {
return c.do(out).updateAttrs(s.search.assignAttrs).update().db
}
return c
}
func (s *DB) Update(attrs ...interface{}) *DB {
return s.Updates(toSearchableMap(attrs...), true)
}
func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
return s.clone().do(s.Value).begin().updateAttrs(values, ignoreProtectedAttrs...).update().commit_or_rollback().db
// return s.clone().NewScope(s.Value).callCallbacks(s.parent.callback.updates).db
}
func (s *DB) UpdateColumn(attrs ...interface{}) *DB {
return s.UpdateColumns(toSearchableMap(attrs...), true)
}
func (s *DB) UpdateColumns(values interface{}, ignore_protected_attrs ...bool) *DB {
return s.clone().do(s.Value).begin().updateColumns(values).commit_or_rollback().db
}
func (s *DB) Save(value interface{}) *DB {
scope := s.clone().NewScope(value)
if scope.PrimaryKeyZero() {
return scope.callCallbacks(s.parent.callback.creates).db
} else {
return scope.callCallbacks(s.parent.callback.updates).db
}
}
func (s *DB) Delete(value interface{}) *DB {
return s.clone().NewScope(value).callCallbacks(s.parent.callback.deletes).db
}
func (s *DB) Raw(sql string, values ...interface{}) *DB {
return s.clone().search.setraw(true).where(sql, values...).db
}
func (s *DB) Exec(sql string, values ...interface{}) *DB {
return s.clone().do(nil).raw(sql, values...).exec().db
}
func (s *DB) Model(value interface{}) *DB {
c := s.clone()
c.Value = value
return c
}
func (s *DB) Related(value interface{}, foreign_keys ...string) *DB {
old_data := s.Value
return s.do(value).related(old_data, foreign_keys...).db
}
func (s *DB) Pluck(column string, value interface{}) *DB {
return s.do(s.Value).pluck(column, value).db
}
func (s *DB) Count(value interface{}) *DB {
return s.do(s.Value).count(value).db
}
func (s *DB) Table(name string) *DB {
return s.clone().search.table(name).db
}
func (s *DB) Debug() *DB {
return s.clone().LogMode(true)
}
func (s *DB) Begin() *DB {
c := s.clone()
if db, ok := c.db.(sqlDb); ok {
tx, err := db.Begin()
c.db = interface{}(tx).(sqlCommon)
c.err(err)
} else {
c.err(CantStartTransaction)
}
return c
}
func (s *DB) Commit() *DB {
if db, ok := s.db.(sqlTx); ok {
s.err(db.Commit())
} else {
s.err(NoValidTransaction)
}
return s
}
func (s *DB) Rollback() *DB {
if db, ok := s.db.(sqlTx); ok {
s.err(db.Rollback())
} else {
s.err(NoValidTransaction)
}
return s
}
func (s *DB) NewRecord(value interface{}) bool {
return s.clone().do(value).model.primaryKeyZero()
}
func (s *DB) RecordNotFound() bool {
return s.Error == RecordNotFound
}
// Migrations
func (s *DB) CreateTable(value interface{}) *DB {
return s.clone().do(value).createTable().db
}
func (s *DB) DropTable(value interface{}) *DB {
return s.clone().do(value).dropTable().db
}
func (s *DB) AutoMigrate(value interface{}) *DB {
return s.clone().do(value).autoMigrate().db
}
func (s *DB) ModifyColumn(column string, typ string) *DB {
s.clone().do(s.Value).modifyColumn(column, typ)
return s
}
func (s *DB) DropColumn(column string) *DB {
s.do(s.Value).dropColumn(column)
return s
}
func (s *DB) AddIndex(column string, index_name ...string) *DB {
s.clone().do(s.Value).addIndex(column, index_name...)
return s
}
func (s *DB) RemoveIndex(column string) *DB {
s.clone().do(s.Value).removeIndex(column)
return s
}