/
authorbookresult.xo.go
55 lines (46 loc) · 1.29 KB
/
authorbookresult.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
// Package models contains the types for schema 'booktest'.
package models
// Code generated by xo. DO NOT EDIT.
// AuthorBookResult is the result of a search.
type AuthorBookResult struct {
AuthorID int // author_id
AuthorName string // author_name
BookID int // book_id
BookIsbn string // book_isbn
BookTitle string // book_title
BookTags string // book_tags
}
// AuthorBookResultsByTag runs a custom query, returning results as AuthorBookResult.
func AuthorBookResultsByTag(db XODB, tag string) ([]*AuthorBookResult, error) {
var err error
// sql query
const sqlstr = `SELECT ` +
`a.author_id AS author_id, ` +
`a.name AS author_name, ` +
`b.book_id AS book_id, ` +
`b.isbn AS book_isbn, ` +
`b.title AS book_title, ` +
`b.tags AS book_tags ` +
`FROM books b ` +
`JOIN authors a ON a.author_id = b.author_id ` +
`WHERE b.tags LIKE CONCAT('%', ?, '%')`
// run query
XOLog(sqlstr, tag)
q, err := db.Query(sqlstr, tag)
if err != nil {
return nil, err
}
defer q.Close()
// load results
res := []*AuthorBookResult{}
for q.Next() {
abr := AuthorBookResult{}
// scan
err = q.Scan(&abr.AuthorID, &abr.AuthorName, &abr.BookID, &abr.BookIsbn, &abr.BookTitle, &abr.BookTags)
if err != nil {
return nil, err
}
res = append(res, &abr)
}
return res, nil
}