forked from xo/xo
/
authusergroup.xo.go
219 lines (204 loc) · 6.06 KB
/
authusergroup.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
215
216
217
218
219
package mysql
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// AuthUserGroup represents a row from 'django.auth_user_groups'.
type AuthUserGroup struct {
ID int64 `json:"id"` // id
UserID int `json:"user_id"` // user_id
GroupID int `json:"group_id"` // group_id
// xo fields
_exists, _deleted bool
}
// Exists returns true when the AuthUserGroup exists in the database.
func (aug *AuthUserGroup) Exists() bool {
return aug._exists
}
// Deleted returns true when the AuthUserGroup has been marked for deletion from
// the database.
func (aug *AuthUserGroup) Deleted() bool {
return aug._deleted
}
// Insert inserts the AuthUserGroup to the database.
func (aug *AuthUserGroup) Insert(ctx context.Context, db DB) error {
switch {
case aug._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case aug._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (primary key generated and returned by database)
const sqlstr = `INSERT INTO django.auth_user_groups (` +
`user_id, group_id` +
`) VALUES (` +
`?, ?` +
`)`
// run
logf(sqlstr, aug.UserID, aug.GroupID)
res, err := db.ExecContext(ctx, sqlstr, aug.UserID, aug.GroupID)
if err != nil {
return logerror(err)
}
// retrieve id
id, err := res.LastInsertId()
if err != nil {
return logerror(err)
} // set primary key
aug.ID = int64(id)
// set exists
aug._exists = true
return nil
}
// Update updates a AuthUserGroup in the database.
func (aug *AuthUserGroup) Update(ctx context.Context, db DB) error {
switch {
case !aug._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case aug._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE django.auth_user_groups SET ` +
`user_id = ?, group_id = ? ` +
`WHERE id = ?`
// run
logf(sqlstr, aug.UserID, aug.GroupID, aug.ID)
if _, err := db.ExecContext(ctx, sqlstr, aug.UserID, aug.GroupID, aug.ID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the AuthUserGroup to the database.
func (aug *AuthUserGroup) Save(ctx context.Context, db DB) error {
if aug.Exists() {
return aug.Update(ctx, db)
}
return aug.Insert(ctx, db)
}
// Upsert performs an upsert for AuthUserGroup.
func (aug *AuthUserGroup) Upsert(ctx context.Context, db DB) error {
switch {
case aug._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO django.auth_user_groups (` +
`id, user_id, group_id` +
`) VALUES (` +
`?, ?, ?` +
`)` +
` ON DUPLICATE KEY UPDATE ` +
`user_id = VALUES(user_id), group_id = VALUES(group_id)`
// run
logf(sqlstr, aug.ID, aug.UserID, aug.GroupID)
if _, err := db.ExecContext(ctx, sqlstr, aug.ID, aug.UserID, aug.GroupID); err != nil {
return logerror(err)
}
// set exists
aug._exists = true
return nil
}
// Delete deletes the AuthUserGroup from the database.
func (aug *AuthUserGroup) Delete(ctx context.Context, db DB) error {
switch {
case !aug._exists: // doesn't exist
return nil
case aug._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM django.auth_user_groups ` +
`WHERE id = ?`
// run
logf(sqlstr, aug.ID)
if _, err := db.ExecContext(ctx, sqlstr, aug.ID); err != nil {
return logerror(err)
}
// set deleted
aug._deleted = true
return nil
}
// AuthUserGroupsByGroupID retrieves a row from 'django.auth_user_groups' as a AuthUserGroup.
//
// Generated from index 'auth_user_groups_group_id_97559544_fk_auth_group_id'.
func AuthUserGroupsByGroupID(ctx context.Context, db DB, groupID int) ([]*AuthUserGroup, error) {
// query
const sqlstr = `SELECT ` +
`id, user_id, group_id ` +
`FROM django.auth_user_groups ` +
`WHERE group_id = ?`
// run
logf(sqlstr, groupID)
rows, err := db.QueryContext(ctx, sqlstr, groupID)
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// process
var res []*AuthUserGroup
for rows.Next() {
aug := AuthUserGroup{
_exists: true,
}
// scan
if err := rows.Scan(&aug.ID, &aug.UserID, &aug.GroupID); err != nil {
return nil, logerror(err)
}
res = append(res, &aug)
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
}
// AuthUserGroupByID retrieves a row from 'django.auth_user_groups' as a AuthUserGroup.
//
// Generated from index 'auth_user_groups_id_pkey'.
func AuthUserGroupByID(ctx context.Context, db DB, id int64) (*AuthUserGroup, error) {
// query
const sqlstr = `SELECT ` +
`id, user_id, group_id ` +
`FROM django.auth_user_groups ` +
`WHERE id = ?`
// run
logf(sqlstr, id)
aug := AuthUserGroup{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, id).Scan(&aug.ID, &aug.UserID, &aug.GroupID); err != nil {
return nil, logerror(err)
}
return &aug, nil
}
// AuthUserGroupByUserIDGroupID retrieves a row from 'django.auth_user_groups' as a AuthUserGroup.
//
// Generated from index 'auth_user_groups_user_id_group_id_94350c0c_uniq'.
func AuthUserGroupByUserIDGroupID(ctx context.Context, db DB, userID, groupID int) (*AuthUserGroup, error) {
// query
const sqlstr = `SELECT ` +
`id, user_id, group_id ` +
`FROM django.auth_user_groups ` +
`WHERE user_id = ? AND group_id = ?`
// run
logf(sqlstr, userID, groupID)
aug := AuthUserGroup{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, userID, groupID).Scan(&aug.ID, &aug.UserID, &aug.GroupID); err != nil {
return nil, logerror(err)
}
return &aug, nil
}
// AuthGroup returns the AuthGroup associated with the AuthUserGroup's (GroupID).
//
// Generated from foreign key 'auth_user_groups_group_id_97559544_fk_auth_group_id'.
func (aug *AuthUserGroup) AuthGroup(ctx context.Context, db DB) (*AuthGroup, error) {
return AuthGroupByID(ctx, db, aug.GroupID)
}
// AuthUser returns the AuthUser associated with the AuthUserGroup's (UserID).
//
// Generated from foreign key 'auth_user_groups_user_id_6a12ed8b_fk_auth_user_id'.
func (aug *AuthUserGroup) AuthUser(ctx context.Context, db DB) (*AuthUser, error) {
return AuthUserByID(ctx, db, aug.UserID)
}