forked from xo/xo
/
authpermission.xo.go
214 lines (200 loc) · 6.21 KB
/
authpermission.xo.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
package sqlite3
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// AuthPermission represents a row from 'auth_permission'.
type AuthPermission struct {
ID int `json:"id"` // id
ContentTypeID int `json:"content_type_id"` // content_type_id
Codename string `json:"codename"` // codename
Name string `json:"name"` // name
// xo fields
_exists, _deleted bool
}
// Exists returns true when the AuthPermission exists in the database.
func (ap *AuthPermission) Exists() bool {
return ap._exists
}
// Deleted returns true when the AuthPermission has been marked for deletion from
// the database.
func (ap *AuthPermission) Deleted() bool {
return ap._deleted
}
// Insert inserts the AuthPermission to the database.
func (ap *AuthPermission) Insert(ctx context.Context, db DB) error {
switch {
case ap._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case ap._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (primary key generated and returned by database)
const sqlstr = `INSERT INTO auth_permission (` +
`id, content_type_id, codename, name` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)`
// run
logf(sqlstr, ap.ContentTypeID, ap.Codename, ap.Name)
res, err := db.ExecContext(ctx, sqlstr, ap.ID, ap.ContentTypeID, ap.Codename, ap.Name)
if err != nil {
return logerror(err)
}
// retrieve id
id, err := res.LastInsertId()
if err != nil {
return logerror(err)
} // set primary key
ap.ID = int(id)
// set exists
ap._exists = true
return nil
}
// Update updates a AuthPermission in the database.
func (ap *AuthPermission) Update(ctx context.Context, db DB) error {
switch {
case !ap._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case ap._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE auth_permission SET ` +
`content_type_id = $1, codename = $2, name = $3 ` +
`WHERE id = $4`
// run
logf(sqlstr, ap.ContentTypeID, ap.Codename, ap.Name, ap.ID)
if _, err := db.ExecContext(ctx, sqlstr, ap.ContentTypeID, ap.Codename, ap.Name, ap.ID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the AuthPermission to the database.
func (ap *AuthPermission) Save(ctx context.Context, db DB) error {
if ap.Exists() {
return ap.Update(ctx, db)
}
return ap.Insert(ctx, db)
}
// Upsert performs an upsert for AuthPermission.
func (ap *AuthPermission) Upsert(ctx context.Context, db DB) error {
switch {
case ap._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO auth_permission (` +
`id, content_type_id, codename, name` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)` +
` ON CONFLICT (id) DO ` +
`UPDATE SET ` +
`content_type_id = EXCLUDED.content_type_id, codename = EXCLUDED.codename, name = EXCLUDED.name `
// run
logf(sqlstr, ap.ID, ap.ContentTypeID, ap.Codename, ap.Name)
if _, err := db.ExecContext(ctx, sqlstr, ap.ID, ap.ContentTypeID, ap.Codename, ap.Name); err != nil {
return logerror(err)
}
// set exists
ap._exists = true
return nil
}
// Delete deletes the AuthPermission from the database.
func (ap *AuthPermission) Delete(ctx context.Context, db DB) error {
switch {
case !ap._exists: // doesn't exist
return nil
case ap._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM auth_permission ` +
`WHERE id = $1`
// run
logf(sqlstr, ap.ID)
if _, err := db.ExecContext(ctx, sqlstr, ap.ID); err != nil {
return logerror(err)
}
// set deleted
ap._deleted = true
return nil
}
// AuthPermissionByContentTypeID retrieves a row from 'auth_permission' as a AuthPermission.
//
// Generated from index 'auth_permission_content_type_id_2f476e4b'.
func AuthPermissionByContentTypeID(ctx context.Context, db DB, contentTypeID int) ([]*AuthPermission, error) {
// query
const sqlstr = `SELECT ` +
`id, content_type_id, codename, name ` +
`FROM auth_permission ` +
`WHERE content_type_id = $1`
// run
logf(sqlstr, contentTypeID)
rows, err := db.QueryContext(ctx, sqlstr, contentTypeID)
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// process
var res []*AuthPermission
for rows.Next() {
ap := AuthPermission{
_exists: true,
}
// scan
if err := rows.Scan(&ap.ID, &ap.ContentTypeID, &ap.Codename, &ap.Name); err != nil {
return nil, logerror(err)
}
res = append(res, &ap)
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
}
// AuthPermissionByContentTypeIDCodename retrieves a row from 'auth_permission' as a AuthPermission.
//
// Generated from index 'auth_permission_content_type_id_codename_01ab375a_uniq'.
func AuthPermissionByContentTypeIDCodename(ctx context.Context, db DB, contentTypeID int, codename string) (*AuthPermission, error) {
// query
const sqlstr = `SELECT ` +
`id, content_type_id, codename, name ` +
`FROM auth_permission ` +
`WHERE content_type_id = $1 AND codename = $2`
// run
logf(sqlstr, contentTypeID, codename)
ap := AuthPermission{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, contentTypeID, codename).Scan(&ap.ID, &ap.ContentTypeID, &ap.Codename, &ap.Name); err != nil {
return nil, logerror(err)
}
return &ap, nil
}
// AuthPermissionByID retrieves a row from 'auth_permission' as a AuthPermission.
//
// Generated from index 'auth_permission_id_pkey'.
func AuthPermissionByID(ctx context.Context, db DB, id int) (*AuthPermission, error) {
// query
const sqlstr = `SELECT ` +
`id, content_type_id, codename, name ` +
`FROM auth_permission ` +
`WHERE id = $1`
// run
logf(sqlstr, id)
ap := AuthPermission{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, id).Scan(&ap.ID, &ap.ContentTypeID, &ap.Codename, &ap.Name); err != nil {
return nil, logerror(err)
}
return &ap, nil
}
// DjangoContentType returns the DjangoContentType associated with the AuthPermission's (ContentTypeID).
//
// Generated from foreign key 'auth_permission_content_type_id_fkey'.
func (ap *AuthPermission) DjangoContentType(ctx context.Context, db DB) (*DjangoContentType, error) {
return DjangoContentTypeByID(ctx, db, ap.ContentTypeID)
}