-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.xo.go
301 lines (244 loc) · 6.79 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Package models contains the types for schema 'public'.
package models
// Code generated by xo. DO NOT EDIT.
import (
"errors"
"time"
)
// Book represents a row from 'public.books'.
type Book struct {
BookID int `json:"book_id"` // book_id
AuthorID int `json:"author_id"` // author_id
Isbn string `json:"isbn"` // isbn
Booktype BookType `json:"booktype"` // booktype
Title string `json:"title"` // title
Year int `json:"year"` // year
Available time.Time `json:"available"` // available
Tags StringSlice `json:"tags"` // tags
// xo fields
_exists, _deleted bool
}
// Exists determines if the Book exists in the database.
func (b *Book) Exists() bool {
return b._exists
}
// Deleted provides information if the Book has been deleted from the database.
func (b *Book) Deleted() bool {
return b._deleted
}
// Insert inserts the Book to the database.
func (b *Book) Insert(db XODB) error {
var err error
// if already exist, bail
if b._exists {
return errors.New("insert failed: already exists")
}
// sql insert query, primary key provided by sequence
const sqlstr = `INSERT INTO public.books (` +
`author_id, isbn, booktype, title, year, available, tags` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7` +
`) RETURNING book_id`
// run query
XOLog(sqlstr, b.AuthorID, b.Isbn, b.Booktype, b.Title, b.Year, b.Available, b.Tags)
err = db.QueryRow(sqlstr, b.AuthorID, b.Isbn, b.Booktype, b.Title, b.Year, b.Available, b.Tags).Scan(&b.BookID)
if err != nil {
return err
}
// set existence
b._exists = true
return nil
}
// Update updates the Book in the database.
func (b *Book) Update(db XODB) error {
var err error
// if doesn't exist, bail
if !b._exists {
return errors.New("update failed: does not exist")
}
// if deleted, bail
if b._deleted {
return errors.New("update failed: marked for deletion")
}
// sql query
const sqlstr = `UPDATE public.books SET (` +
`author_id, isbn, booktype, title, year, available, tags` +
`) = ( ` +
`$1, $2, $3, $4, $5, $6, $7` +
`) WHERE book_id = $8`
// run query
XOLog(sqlstr, b.AuthorID, b.Isbn, b.Booktype, b.Title, b.Year, b.Available, b.Tags, b.BookID)
_, err = db.Exec(sqlstr, b.AuthorID, b.Isbn, b.Booktype, b.Title, b.Year, b.Available, b.Tags, b.BookID)
return err
}
// Save saves the Book to the database.
func (b *Book) Save(db XODB) error {
if b.Exists() {
return b.Update(db)
}
return b.Insert(db)
}
// Upsert performs an upsert for Book.
//
// NOTE: PostgreSQL 9.5+ only
func (b *Book) Upsert(db XODB) error {
var err error
// if already exist, bail
if b._exists {
return errors.New("insert failed: already exists")
}
// sql query
const sqlstr = `INSERT INTO public.books (` +
`book_id, author_id, isbn, booktype, title, year, available, tags` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7, $8` +
`) ON CONFLICT (book_id) DO UPDATE SET (` +
`book_id, author_id, isbn, booktype, title, year, available, tags` +
`) = (` +
`EXCLUDED.book_id, EXCLUDED.author_id, EXCLUDED.isbn, EXCLUDED.booktype, EXCLUDED.title, EXCLUDED.year, EXCLUDED.available, EXCLUDED.tags` +
`)`
// run query
XOLog(sqlstr, b.BookID, b.AuthorID, b.Isbn, b.Booktype, b.Title, b.Year, b.Available, b.Tags)
_, err = db.Exec(sqlstr, b.BookID, b.AuthorID, b.Isbn, b.Booktype, b.Title, b.Year, b.Available, b.Tags)
if err != nil {
return err
}
// set existence
b._exists = true
return nil
}
// Delete deletes the Book from the database.
func (b *Book) Delete(db XODB) error {
var err error
// if doesn't exist, bail
if !b._exists {
return nil
}
// if deleted, bail
if b._deleted {
return nil
}
// sql query
const sqlstr = `DELETE FROM public.books WHERE book_id = $1`
// run query
XOLog(sqlstr, b.BookID)
_, err = db.Exec(sqlstr, b.BookID)
if err != nil {
return err
}
// set deleted
b._deleted = true
return nil
}
// Author returns the Author associated with the Book's AuthorID (author_id).
//
// Generated from foreign key 'books_author_id_fkey'.
func (b *Book) Author(db XODB) (*Author, error) {
return AuthorByAuthorID(db, b.AuthorID)
}
// BookByIsbn retrieves a row from 'public.books' as a Book.
//
// Generated from index 'books_isbn_key'.
func BookByIsbn(db XODB, isbn string) (*Book, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, booktype, title, year, available, tags ` +
`FROM public.books ` +
`WHERE isbn = $1`
// run query
XOLog(sqlstr, isbn)
b := Book{
_exists: true,
}
err = db.QueryRow(sqlstr, isbn).Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.Booktype, &b.Title, &b.Year, &b.Available, &b.Tags)
if err != nil {
return nil, err
}
return &b, nil
}
// BookByBookID retrieves a row from 'public.books' as a Book.
//
// Generated from index 'books_pkey'.
func BookByBookID(db XODB, bookID int) (*Book, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, booktype, title, year, available, tags ` +
`FROM public.books ` +
`WHERE book_id = $1`
// run query
XOLog(sqlstr, bookID)
b := Book{
_exists: true,
}
err = db.QueryRow(sqlstr, bookID).Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.Booktype, &b.Title, &b.Year, &b.Available, &b.Tags)
if err != nil {
return nil, err
}
return &b, nil
}
// BooksByTitleYear retrieves a row from 'public.books' as a Book.
//
// Generated from index 'books_title_idx'.
func BooksByTitleYear(db XODB, title string, year int) ([]*Book, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, booktype, title, year, available, tags ` +
`FROM public.books ` +
`WHERE title = $1 AND year = $2`
// run query
XOLog(sqlstr, title, year)
q, err := db.Query(sqlstr, title, year)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Book{}
for q.Next() {
b := Book{
_exists: true,
}
// scan
err = q.Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.Booktype, &b.Title, &b.Year, &b.Available, &b.Tags)
if err != nil {
return nil, err
}
res = append(res, &b)
}
return res, nil
}
// BooksByTitle retrieves a row from 'public.books' as a Book.
//
// Generated from index 'books_title_lower_idx'.
func BooksByTitle(db XODB, title string) ([]*Book, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, booktype, title, year, available, tags ` +
`FROM public.books ` +
`WHERE title = $1`
// run query
XOLog(sqlstr, title)
q, err := db.Query(sqlstr, title)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*Book{}
for q.Next() {
b := Book{
_exists: true,
}
// scan
err = q.Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.Booktype, &b.Title, &b.Year, &b.Available, &b.Tags)
if err != nil {
return nil, err
}
res = append(res, &b)
}
return res, nil
}