-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.xo.go
207 lines (193 loc) · 5.78 KB
/
service.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
package models
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// Service represents a row from 'services'.
type Service struct {
UserID string `json:"user_id"` // user_id
RuntimeName string `json:"runtime_name"` // runtime_name
Service string `json:"service"` // service
Addr string `json:"addr"` // addr
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Service exists in the database.
func (s *Service) Exists() bool {
return s._exists
}
// Deleted returns true when the Service has been marked for deletion from
// the database.
func (s *Service) Deleted() bool {
return s._deleted
}
// Insert inserts the Service to the database.
func (s *Service) Insert(ctx context.Context, db DB) error {
switch {
case s._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case s._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO services (` +
`user_id, runtime_name, service, addr` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)`
// run
logf(sqlstr, s.UserID, s.RuntimeName, s.Service, s.Addr)
if _, err := db.ExecContext(ctx, sqlstr, s.UserID, s.RuntimeName, s.Service, s.Addr); err != nil {
return logerror(err)
}
// set exists
s._exists = true
return nil
}
// Update updates a Service in the database.
func (s *Service) Update(ctx context.Context, db DB) error {
switch {
case !s._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case s._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE services SET ` +
`user_id = $1, addr = $2 ` +
`WHERE runtime_name = $3 AND service = $4`
// run
logf(sqlstr, s.UserID, s.Addr, s.RuntimeName, s.Service)
if _, err := db.ExecContext(ctx, sqlstr, s.UserID, s.Addr, s.RuntimeName, s.Service); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Service to the database.
func (s *Service) Save(ctx context.Context, db DB) error {
if s.Exists() {
return s.Update(ctx, db)
}
return s.Insert(ctx, db)
}
// Upsert performs an upsert for Service.
func (s *Service) Upsert(ctx context.Context, db DB) error {
switch {
case s._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO services (` +
`user_id, runtime_name, service, addr` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)` +
` ON CONFLICT (runtime_name, service) DO ` +
`UPDATE SET ` +
`user_id = EXCLUDED.user_id, addr = EXCLUDED.addr `
// run
logf(sqlstr, s.UserID, s.RuntimeName, s.Service, s.Addr)
if _, err := db.ExecContext(ctx, sqlstr, s.UserID, s.RuntimeName, s.Service, s.Addr); err != nil {
return logerror(err)
}
// set exists
s._exists = true
return nil
}
// Delete deletes the Service from the database.
func (s *Service) Delete(ctx context.Context, db DB) error {
switch {
case !s._exists: // doesn't exist
return nil
case s._deleted: // deleted
return nil
}
// delete with composite primary key
const sqlstr = `DELETE FROM services ` +
`WHERE runtime_name = $1 AND service = $2`
// run
logf(sqlstr, s.RuntimeName, s.Service)
if _, err := db.ExecContext(ctx, sqlstr, s.RuntimeName, s.Service); err != nil {
return logerror(err)
}
// set deleted
s._deleted = true
return nil
}
// ServicesByUserIDRuntimeName retrieves a row from 'services' as a Service.
//
// Generated from index 'idx_services_runtime_name'.
func ServicesByUserIDRuntimeName(ctx context.Context, db DB, userID, runtimeName string) ([]*Service, error) {
// query
const sqlstr = `SELECT ` +
`user_id, runtime_name, service, addr ` +
`FROM services ` +
`WHERE user_id = $1 AND runtime_name = $2`
// run
logf(sqlstr, userID, runtimeName)
rows, err := db.QueryContext(ctx, sqlstr, userID, runtimeName)
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// process
var res []*Service
for rows.Next() {
s := Service{
_exists: true,
}
// scan
if err := rows.Scan(&s.UserID, &s.RuntimeName, &s.Service, &s.Addr); err != nil {
return nil, logerror(err)
}
res = append(res, &s)
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
}
// ServiceByUserIDRuntimeNameService retrieves a row from 'services' as a Service.
//
// Generated from index 'service_user_runtime_service_idx'.
func ServiceByUserIDRuntimeNameService(ctx context.Context, db DB, userID, runtimeName, service string) (*Service, error) {
// query
const sqlstr = `SELECT ` +
`user_id, runtime_name, service, addr ` +
`FROM services ` +
`WHERE user_id = $1 AND runtime_name = $2 AND service = $3`
// run
logf(sqlstr, userID, runtimeName, service)
s := Service{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, userID, runtimeName, service).Scan(&s.UserID, &s.RuntimeName, &s.Service, &s.Addr); err != nil {
return nil, logerror(err)
}
return &s, nil
}
// ServiceByRuntimeNameService retrieves a row from 'services' as a Service.
//
// Generated from index 'sqlite_autoindex_services_1'.
func ServiceByRuntimeNameService(ctx context.Context, db DB, runtimeName, service string) (*Service, error) {
// query
const sqlstr = `SELECT ` +
`user_id, runtime_name, service, addr ` +
`FROM services ` +
`WHERE runtime_name = $1 AND service = $2`
// run
logf(sqlstr, runtimeName, service)
s := Service{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, runtimeName, service).Scan(&s.UserID, &s.RuntimeName, &s.Service, &s.Addr); err != nil {
return nil, logerror(err)
}
return &s, nil
}
// User returns the User associated with the Service's (UserID).
//
// Generated from foreign key 'services_user_id_fkey'.
func (s *Service) User(ctx context.Context, db DB) (*User, error) {
return UserByID(ctx, db, s.UserID)
}