/
gen.go
153 lines (129 loc) · 3.29 KB
/
gen.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
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package dal
import (
"context"
"database/sql"
)
import (
"gorm.io/gen"
"gorm.io/gorm"
"gorm.io/plugin/dbresolver"
)
var (
Q = new(Query)
Commit *commit
CommitFile *commitFile
FileBlob *fileBlob
Repository *repository
Tag *tag
Token *token
User *user
)
func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
*Q = *Use(db, opts...)
Commit = &Q.Commit
CommitFile = &Q.CommitFile
FileBlob = &Q.FileBlob
Repository = &Q.Repository
Tag = &Q.Tag
Token = &Q.Token
User = &Q.User
}
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
return &Query{
db: db,
Commit: newCommit(db, opts...),
CommitFile: newCommitFile(db, opts...),
FileBlob: newFileBlob(db, opts...),
Repository: newRepository(db, opts...),
Tag: newTag(db, opts...),
Token: newToken(db, opts...),
User: newUser(db, opts...),
}
}
type Query struct {
db *gorm.DB
Commit commit
CommitFile commitFile
FileBlob fileBlob
Repository repository
Tag tag
Token token
User user
}
func (q *Query) Available() bool { return q.db != nil }
func (q *Query) clone(db *gorm.DB) *Query {
return &Query{
db: db,
Commit: q.Commit.clone(db),
CommitFile: q.CommitFile.clone(db),
FileBlob: q.FileBlob.clone(db),
Repository: q.Repository.clone(db),
Tag: q.Tag.clone(db),
Token: q.Token.clone(db),
User: q.User.clone(db),
}
}
func (q *Query) ReadDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}
func (q *Query) WriteDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
}
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
return &Query{
db: db,
Commit: q.Commit.replaceDB(db),
CommitFile: q.CommitFile.replaceDB(db),
FileBlob: q.FileBlob.replaceDB(db),
Repository: q.Repository.replaceDB(db),
Tag: q.Tag.replaceDB(db),
Token: q.Token.replaceDB(db),
User: q.User.replaceDB(db),
}
}
type queryCtx struct {
Commit ICommitDo
CommitFile ICommitFileDo
FileBlob IFileBlobDo
Repository IRepositoryDo
Tag ITagDo
Token ITokenDo
User IUserDo
}
func (q *Query) WithContext(ctx context.Context) *queryCtx {
return &queryCtx{
Commit: q.Commit.WithContext(ctx),
CommitFile: q.CommitFile.WithContext(ctx),
FileBlob: q.FileBlob.WithContext(ctx),
Repository: q.Repository.WithContext(ctx),
Tag: q.Tag.WithContext(ctx),
Token: q.Token.WithContext(ctx),
User: q.User.WithContext(ctx),
}
}
func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
}
func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
tx := q.db.Begin(opts...)
return &QueryTx{Query: q.clone(tx), Error: tx.Error}
}
type QueryTx struct {
*Query
Error error
}
func (q *QueryTx) Commit() error {
return q.db.Commit().Error
}
func (q *QueryTx) Rollback() error {
return q.db.Rollback().Error
}
func (q *QueryTx) SavePoint(name string) error {
return q.db.SavePoint(name).Error
}
func (q *QueryTx) RollbackTo(name string) error {
return q.db.RollbackTo(name).Error
}