forked from xo/xo
/
author.xo.go
186 lines (174 loc) · 4.31 KB
/
author.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
// Package sqlite3 contains generated code for schema 'booktest.db'.
package sqlite3
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// Author represents a row from 'authors'.
type Author struct {
AuthorID int `json:"author_id"` // author_id
Name string `json:"name"` // name
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Author exists in the database.
func (a *Author) Exists() bool {
return a._exists
}
// Deleted returns true when the Author has been marked for deletion from
// the database.
func (a *Author) Deleted() bool {
return a._deleted
}
// Insert inserts the Author to the database.
func (a *Author) Insert(ctx context.Context, db DB) error {
switch {
case a._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case a._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (primary key generated and returned by database)
const sqlstr = `INSERT INTO authors (` +
`name` +
`) VALUES (` +
`$1` +
`)`
// run
logf(sqlstr, a.Name)
res, err := db.ExecContext(ctx, sqlstr, a.Name)
if err != nil {
return logerror(err)
}
// retrieve id
id, err := res.LastInsertId()
if err != nil {
return logerror(err)
} // set primary key
a.AuthorID = int(id)
// set exists
a._exists = true
return nil
}
// Update updates a Author in the database.
func (a *Author) Update(ctx context.Context, db DB) error {
switch {
case !a._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case a._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE authors SET ` +
`name = $1 ` +
`WHERE author_id = $2`
// run
logf(sqlstr, a.Name, a.AuthorID)
if _, err := db.ExecContext(ctx, sqlstr, a.Name, a.AuthorID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Author to the database.
func (a *Author) Save(ctx context.Context, db DB) error {
if a.Exists() {
return a.Update(ctx, db)
}
return a.Insert(ctx, db)
}
// Upsert performs an upsert for Author.
func (a *Author) Upsert(ctx context.Context, db DB) error {
switch {
case a._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO authors (` +
`author_id, name` +
`) VALUES (` +
`$1, $2` +
`)` +
` ON CONFLICT (author_id) DO ` +
`UPDATE SET ` +
`name = EXCLUDED.name `
// run
logf(sqlstr, a.AuthorID, a.Name)
if _, err := db.ExecContext(ctx, sqlstr, a.AuthorID, a.Name); err != nil {
return logerror(err)
}
// set exists
a._exists = true
return nil
}
// Delete deletes the Author from the database.
func (a *Author) Delete(ctx context.Context, db DB) error {
switch {
case !a._exists: // doesn't exist
return nil
case a._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM authors ` +
`WHERE author_id = $1`
// run
logf(sqlstr, a.AuthorID)
if _, err := db.ExecContext(ctx, sqlstr, a.AuthorID); err != nil {
return logerror(err)
}
// set deleted
a._deleted = true
return nil
}
// AuthorByAuthorID retrieves a row from 'authors' as a Author.
//
// Generated from index 'authors_author_id_pkey'.
func AuthorByAuthorID(ctx context.Context, db DB, authorID int) (*Author, error) {
// query
const sqlstr = `SELECT ` +
`author_id, name ` +
`FROM authors ` +
`WHERE author_id = $1`
// run
logf(sqlstr, authorID)
a := Author{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, authorID).Scan(&a.AuthorID, &a.Name); err != nil {
return nil, logerror(err)
}
return &a, nil
}
// AuthorsByName retrieves a row from 'authors' as a Author.
//
// Generated from index 'authors_name_idx'.
func AuthorsByName(ctx context.Context, db DB, name string) ([]*Author, error) {
// query
const sqlstr = `SELECT ` +
`author_id, name ` +
`FROM authors ` +
`WHERE name = $1`
// run
logf(sqlstr, name)
rows, err := db.QueryContext(ctx, sqlstr, name)
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// process
var res []*Author
for rows.Next() {
a := Author{
_exists: true,
}
// scan
if err := rows.Scan(&a.AuthorID, &a.Name); err != nil {
return nil, logerror(err)
}
res = append(res, &a)
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
}