5
5
package models
6
6
7
7
import (
8
+ "context"
8
9
"fmt"
9
10
"path"
10
11
@@ -38,7 +39,7 @@ func init() {
38
39
// IncreaseDownloadCount is update download count + 1
39
40
func (a * Attachment ) IncreaseDownloadCount () error {
40
41
// Update download count.
41
- if _ , err := db .DefaultContext (). Engine ( ).Exec ("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?" , a .ID ); err != nil {
42
+ if _ , err := db .GetEngine ( db . DefaultContext ).Exec ("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?" , a .ID ); err != nil {
42
43
return fmt .Errorf ("increase attachment count: %v" , err )
43
44
}
44
45
@@ -86,7 +87,7 @@ func (a *Attachment) LinkedRepository() (*Repository, UnitType, error) {
86
87
87
88
// GetAttachmentByID returns attachment by given id
88
89
func GetAttachmentByID (id int64 ) (* Attachment , error ) {
89
- return getAttachmentByID (db .DefaultContext (). Engine ( ), id )
90
+ return getAttachmentByID (db .GetEngine ( db . DefaultContext ), id )
90
91
}
91
92
92
93
func getAttachmentByID (e db.Engine , id int64 ) (* Attachment , error ) {
@@ -111,8 +112,8 @@ func getAttachmentByUUID(e db.Engine, uuid string) (*Attachment, error) {
111
112
}
112
113
113
114
// GetAttachmentsByUUIDs returns attachment by given UUID list.
114
- func GetAttachmentsByUUIDs (ctx * db .Context , uuids []string ) ([]* Attachment , error ) {
115
- return getAttachmentsByUUIDs (ctx . Engine ( ), uuids )
115
+ func GetAttachmentsByUUIDs (ctx context .Context , uuids []string ) ([]* Attachment , error ) {
116
+ return getAttachmentsByUUIDs (db . GetEngine ( ctx ), uuids )
116
117
}
117
118
118
119
func getAttachmentsByUUIDs (e db.Engine , uuids []string ) ([]* Attachment , error ) {
@@ -127,17 +128,17 @@ func getAttachmentsByUUIDs(e db.Engine, uuids []string) ([]*Attachment, error) {
127
128
128
129
// GetAttachmentByUUID returns attachment by given UUID.
129
130
func GetAttachmentByUUID (uuid string ) (* Attachment , error ) {
130
- return getAttachmentByUUID (db .DefaultContext (). Engine ( ), uuid )
131
+ return getAttachmentByUUID (db .GetEngine ( db . DefaultContext ), uuid )
131
132
}
132
133
133
134
// ExistAttachmentsByUUID returns true if attachment is exist by given UUID
134
135
func ExistAttachmentsByUUID (uuid string ) (bool , error ) {
135
- return db .DefaultContext (). Engine ( ).Where ("`uuid`=?" , uuid ).Exist (new (Attachment ))
136
+ return db .GetEngine ( db . DefaultContext ).Where ("`uuid`=?" , uuid ).Exist (new (Attachment ))
136
137
}
137
138
138
139
// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName.
139
140
func GetAttachmentByReleaseIDFileName (releaseID int64 , fileName string ) (* Attachment , error ) {
140
- return getAttachmentByReleaseIDFileName (db .DefaultContext (). Engine ( ), releaseID , fileName )
141
+ return getAttachmentByReleaseIDFileName (db .GetEngine ( db . DefaultContext ), releaseID , fileName )
141
142
}
142
143
143
144
func getAttachmentsByIssueID (e db.Engine , issueID int64 ) ([]* Attachment , error ) {
@@ -147,12 +148,12 @@ func getAttachmentsByIssueID(e db.Engine, issueID int64) ([]*Attachment, error)
147
148
148
149
// GetAttachmentsByIssueID returns all attachments of an issue.
149
150
func GetAttachmentsByIssueID (issueID int64 ) ([]* Attachment , error ) {
150
- return getAttachmentsByIssueID (db .DefaultContext (). Engine ( ), issueID )
151
+ return getAttachmentsByIssueID (db .GetEngine ( db . DefaultContext ), issueID )
151
152
}
152
153
153
154
// GetAttachmentsByCommentID returns all attachments if comment by given ID.
154
155
func GetAttachmentsByCommentID (commentID int64 ) ([]* Attachment , error ) {
155
- return getAttachmentsByCommentID (db .DefaultContext (). Engine ( ), commentID )
156
+ return getAttachmentsByCommentID (db .GetEngine ( db . DefaultContext ), commentID )
156
157
}
157
158
158
159
func getAttachmentsByCommentID (e db.Engine , commentID int64 ) ([]* Attachment , error ) {
@@ -174,12 +175,12 @@ func getAttachmentByReleaseIDFileName(e db.Engine, releaseID int64, fileName str
174
175
175
176
// DeleteAttachment deletes the given attachment and optionally the associated file.
176
177
func DeleteAttachment (a * Attachment , remove bool ) error {
177
- _ , err := DeleteAttachments (db .DefaultContext () , []* Attachment {a }, remove )
178
+ _ , err := DeleteAttachments (db .DefaultContext , []* Attachment {a }, remove )
178
179
return err
179
180
}
180
181
181
182
// DeleteAttachments deletes the given attachments and optionally the associated files.
182
- func DeleteAttachments (ctx * db .Context , attachments []* Attachment , remove bool ) (int , error ) {
183
+ func DeleteAttachments (ctx context .Context , attachments []* Attachment , remove bool ) (int , error ) {
183
184
if len (attachments ) == 0 {
184
185
return 0 , nil
185
186
}
@@ -189,7 +190,7 @@ func DeleteAttachments(ctx *db.Context, attachments []*Attachment, remove bool)
189
190
ids = append (ids , a .ID )
190
191
}
191
192
192
- cnt , err := ctx . Engine ( ).In ("id" , ids ).NoAutoCondition ().Delete (attachments [0 ])
193
+ cnt , err := db . GetEngine ( ctx ).In ("id" , ids ).NoAutoCondition ().Delete (attachments [0 ])
193
194
if err != nil {
194
195
return 0 , err
195
196
}
@@ -211,7 +212,7 @@ func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
211
212
return 0 , err
212
213
}
213
214
214
- return DeleteAttachments (db .DefaultContext () , attachments , remove )
215
+ return DeleteAttachments (db .DefaultContext , attachments , remove )
215
216
}
216
217
217
218
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
@@ -221,20 +222,20 @@ func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
221
222
return 0 , err
222
223
}
223
224
224
- return DeleteAttachments (db .DefaultContext () , attachments , remove )
225
+ return DeleteAttachments (db .DefaultContext , attachments , remove )
225
226
}
226
227
227
228
// UpdateAttachment updates the given attachment in database
228
229
func UpdateAttachment (atta * Attachment ) error {
229
- return updateAttachment (db .DefaultContext (). Engine ( ), atta )
230
+ return updateAttachment (db .GetEngine ( db . DefaultContext ), atta )
230
231
}
231
232
232
233
// UpdateAttachmentByUUID Updates attachment via uuid
233
- func UpdateAttachmentByUUID (ctx * db .Context , attach * Attachment , cols ... string ) error {
234
+ func UpdateAttachmentByUUID (ctx context .Context , attach * Attachment , cols ... string ) error {
234
235
if attach .UUID == "" {
235
- return fmt .Errorf ("Attachement uuid should not blank" )
236
+ return fmt .Errorf ("attachment uuid should be not blank" )
236
237
}
237
- _ , err := ctx . Engine ( ).Where ("uuid=?" , attach .UUID ).Cols (cols ... ).Update (attach )
238
+ _ , err := db . GetEngine ( ctx ).Where ("uuid=?" , attach .UUID ).Cols (cols ... ).Update (attach )
238
239
return err
239
240
}
240
241
@@ -252,7 +253,7 @@ func updateAttachment(e db.Engine, atta *Attachment) error {
252
253
253
254
// DeleteAttachmentsByRelease deletes all attachments associated with the given release.
254
255
func DeleteAttachmentsByRelease (releaseID int64 ) error {
255
- _ , err := db .DefaultContext (). Engine ( ).Where ("release_id = ?" , releaseID ).Delete (& Attachment {})
256
+ _ , err := db .GetEngine ( db . DefaultContext ).Where ("release_id = ?" , releaseID ).Delete (& Attachment {})
256
257
return err
257
258
}
258
259
@@ -262,7 +263,7 @@ func IterateAttachment(f func(attach *Attachment) error) error {
262
263
const batchSize = 100
263
264
for {
264
265
attachments := make ([]* Attachment , 0 , batchSize )
265
- if err := db .DefaultContext (). Engine ( ).Limit (batchSize , start ).Find (& attachments ); err != nil {
266
+ if err := db .GetEngine ( db . DefaultContext ).Limit (batchSize , start ).Find (& attachments ); err != nil {
266
267
return err
267
268
}
268
269
if len (attachments ) == 0 {
@@ -280,13 +281,13 @@ func IterateAttachment(f func(attach *Attachment) error) error {
280
281
281
282
// CountOrphanedAttachments returns the number of bad attachments
282
283
func CountOrphanedAttachments () (int64 , error ) {
283
- return db .DefaultContext (). Engine ( ).Where ("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ).
284
+ return db .GetEngine ( db . DefaultContext ).Where ("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ).
284
285
Count (new (Attachment ))
285
286
}
286
287
287
288
// DeleteOrphanedAttachments delete all bad attachments
288
289
func DeleteOrphanedAttachments () error {
289
- _ , err := db .DefaultContext (). Engine ( ).Where ("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ).
290
+ _ , err := db .GetEngine ( db . DefaultContext ).Where ("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ).
290
291
Delete (new (Attachment ))
291
292
return err
292
293
}
0 commit comments