forked from xo/xo
/
book.xo.go
221 lines (207 loc) · 6.67 KB
/
book.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
220
221
package oracle
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
"time"
)
// Book represents a row from 'booktest.books'.
type Book struct {
BookID int `json:"book_id"` // book_id
AuthorID int `json:"author_id"` // author_id
ISBN string `json:"isbn"` // isbn
Title string `json:"title"` // title
Year int `json:"year"` // year
Available time.Time `json:"available"` // available
Description sql.NullString `json:"description"` // description
Tags string `json:"tags"` // tags
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Book exists in the database.
func (b *Book) Exists() bool {
return b._exists
}
// Deleted returns true when the Book has been marked for deletion from
// the database.
func (b *Book) Deleted() bool {
return b._deleted
}
// Insert inserts the Book to the database.
func (b *Book) Insert(ctx context.Context, db DB) error {
switch {
case b._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case b._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (primary key generated and returned by database)
const sqlstr = `INSERT INTO booktest.books (` +
`author_id, isbn, title, year, available, description, tags` +
`) VALUES (` +
`:1, :2, :3, :4, :5, :6, :7` +
`) RETURNING book_id INTO :8`
// run
logf(sqlstr, b.AuthorID, b.ISBN, b.Title, b.Year, b.Available, b.Description, b.Tags)
var id int64
if _, err := db.ExecContext(ctx, sqlstr, b.AuthorID, b.ISBN, b.Title, b.Year, b.Available, b.Description, b.Tags, sql.Out{Dest: &id}); err != nil {
return logerror(err)
} // set primary key
b.BookID = int(id)
// set exists
b._exists = true
return nil
}
// Update updates a Book in the database.
func (b *Book) Update(ctx context.Context, db DB) error {
switch {
case !b._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case b._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE booktest.books SET ` +
`author_id = :1, isbn = :2, title = :3, year = :4, available = :5, description = :6, tags = :7 ` +
`WHERE book_id = :8`
// run
logf(sqlstr, b.AuthorID, b.ISBN, b.Title, b.Year, b.Available, b.Description, b.Tags, b.BookID)
if _, err := db.ExecContext(ctx, sqlstr, b.AuthorID, b.ISBN, b.Title, b.Year, b.Available, b.Description, b.Tags, b.BookID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Book to the database.
func (b *Book) Save(ctx context.Context, db DB) error {
if b.Exists() {
return b.Update(ctx, db)
}
return b.Insert(ctx, db)
}
// Upsert performs an upsert for Book.
func (b *Book) Upsert(ctx context.Context, db DB) error {
switch {
case b._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `MERGE booktest.bookst ` +
`USING (` +
`SELECT :1 book_id, :2 author_id, :3 isbn, :4 title, :5 year, :6 available, :7 description, :8 tags ` +
`FROM DUAL ) s ` +
`ON s.book_id = t.book_id ` +
`WHEN MATCHED THEN ` +
`UPDATE SET ` +
`t.author_id = s.author_id, t.isbn = s.isbn, t.title = s.title, t.year = s.year, t.available = s.available, t.description = s.description, t.tags = s.tags ` +
`WHEN NOT MATCHED THEN ` +
`INSERT (` +
`author_id, isbn, title, year, available, description, tags` +
`) VALUES (` +
`s.author_id, s.isbn, s.title, s.year, s.available, s.description, s.tags` +
`);`
// run
logf(sqlstr, b.BookID, b.AuthorID, b.ISBN, b.Title, b.Year, b.Available, b.Description, b.Tags)
if _, err := db.ExecContext(ctx, sqlstr, b.BookID, b.AuthorID, b.ISBN, b.Title, b.Year, b.Available, b.Description, b.Tags); err != nil {
return logerror(err)
}
// set exists
b._exists = true
return nil
}
// Delete deletes the Book from the database.
func (b *Book) Delete(ctx context.Context, db DB) error {
switch {
case !b._exists: // doesn't exist
return nil
case b._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM booktest.books ` +
`WHERE book_id = :1`
// run
logf(sqlstr, b.BookID)
if _, err := db.ExecContext(ctx, sqlstr, b.BookID); err != nil {
return logerror(err)
}
// set deleted
b._deleted = true
return nil
}
// BookByISBN retrieves a row from 'booktest.books' as a Book.
//
// Generated from index 'books_isbn_key'.
func BookByISBN(ctx context.Context, db DB, isbn string) (*Book, error) {
// query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, title, year, available, description, tags ` +
`FROM booktest.books ` +
`WHERE isbn = :1`
// run
logf(sqlstr, isbn)
b := Book{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, isbn).Scan(&b.BookID, &b.AuthorID, &b.ISBN, &b.Title, &b.Year, &b.Available, &b.Description, &b.Tags); err != nil {
return nil, logerror(err)
}
return &b, nil
}
// BookByBookID retrieves a row from 'booktest.books' as a Book.
//
// Generated from index 'books_pkey'.
func BookByBookID(ctx context.Context, db DB, bookID int) (*Book, error) {
// query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, title, year, available, description, tags ` +
`FROM booktest.books ` +
`WHERE book_id = :1`
// run
logf(sqlstr, bookID)
b := Book{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, bookID).Scan(&b.BookID, &b.AuthorID, &b.ISBN, &b.Title, &b.Year, &b.Available, &b.Description, &b.Tags); err != nil {
return nil, logerror(err)
}
return &b, nil
}
// BooksByTitleYear retrieves a row from 'booktest.books' as a Book.
//
// Generated from index 'books_title_idx'.
func BooksByTitleYear(ctx context.Context, db DB, title string, year int) ([]*Book, error) {
// query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, title, year, available, description, tags ` +
`FROM booktest.books ` +
`WHERE title = :1 AND year = :2`
// run
logf(sqlstr, title, year)
rows, err := db.QueryContext(ctx, sqlstr, title, year)
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// process
var res []*Book
for rows.Next() {
b := Book{
_exists: true,
}
// scan
if err := rows.Scan(&b.BookID, &b.AuthorID, &b.ISBN, &b.Title, &b.Year, &b.Available, &b.Description, &b.Tags); err != nil {
return nil, logerror(err)
}
res = append(res, &b)
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
}
// Author returns the Author associated with the Book's (AuthorID).
//
// Generated from foreign key 'books_author_id_fkey'.
func (b *Book) Author(ctx context.Context, db DB) (*Author, error) {
return AuthorByAuthorID(ctx, db, b.AuthorID)
}