query.sql.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Code generated by sqlc. DO NOT EDIT.
  2. // versions:
  3. // sqlc v1.27.0
  4. // source: query.sql
  5. package booktest
  6. import (
  7. "context"
  8. "database/sql"
  9. "strings"
  10. "time"
  11. )
  12. const booksByTags = `-- name: BooksByTags :many
  13. SELECT
  14. book_id,
  15. title,
  16. name,
  17. isbn,
  18. tag
  19. FROM books
  20. LEFT JOIN authors ON books.author_id = authors.author_id
  21. WHERE tag IN (/*SLICE:tags*/?)
  22. `
  23. type BooksByTagsRow struct {
  24. BookID int64
  25. Title string
  26. Name sql.NullString
  27. Isbn string
  28. Tag string
  29. }
  30. func (q *Queries) BooksByTags(ctx context.Context, tags []string) ([]BooksByTagsRow, error) {
  31. query := booksByTags
  32. var queryParams []interface{}
  33. if len(tags) > 0 {
  34. for _, v := range tags {
  35. queryParams = append(queryParams, v)
  36. }
  37. query = strings.Replace(query, "/*SLICE:tags*/?", strings.Repeat(",?", len(tags))[1:], 1)
  38. } else {
  39. query = strings.Replace(query, "/*SLICE:tags*/?", "NULL", 1)
  40. }
  41. rows, err := q.db.QueryContext(ctx, query, queryParams...)
  42. if err != nil {
  43. return nil, err
  44. }
  45. defer rows.Close()
  46. var items []BooksByTagsRow
  47. for rows.Next() {
  48. var i BooksByTagsRow
  49. if err := rows.Scan(
  50. &i.BookID,
  51. &i.Title,
  52. &i.Name,
  53. &i.Isbn,
  54. &i.Tag,
  55. ); err != nil {
  56. return nil, err
  57. }
  58. items = append(items, i)
  59. }
  60. if err := rows.Close(); err != nil {
  61. return nil, err
  62. }
  63. if err := rows.Err(); err != nil {
  64. return nil, err
  65. }
  66. return items, nil
  67. }
  68. const booksByTitleYear = `-- name: BooksByTitleYear :many
  69. SELECT book_id, author_id, isbn, book_type, title, yr, available, tag FROM books
  70. WHERE title = ? AND yr = ?
  71. `
  72. type BooksByTitleYearParams struct {
  73. Title string
  74. Yr int64
  75. }
  76. func (q *Queries) BooksByTitleYear(ctx context.Context, arg BooksByTitleYearParams) ([]Book, error) {
  77. rows, err := q.db.QueryContext(ctx, booksByTitleYear, arg.Title, arg.Yr)
  78. if err != nil {
  79. return nil, err
  80. }
  81. defer rows.Close()
  82. var items []Book
  83. for rows.Next() {
  84. var i Book
  85. if err := rows.Scan(
  86. &i.BookID,
  87. &i.AuthorID,
  88. &i.Isbn,
  89. &i.BookType,
  90. &i.Title,
  91. &i.Yr,
  92. &i.Available,
  93. &i.Tag,
  94. ); err != nil {
  95. return nil, err
  96. }
  97. items = append(items, i)
  98. }
  99. if err := rows.Close(); err != nil {
  100. return nil, err
  101. }
  102. if err := rows.Err(); err != nil {
  103. return nil, err
  104. }
  105. return items, nil
  106. }
  107. const createAuthor = `-- name: CreateAuthor :one
  108. INSERT INTO authors (name) VALUES (?)
  109. RETURNING author_id, name
  110. `
  111. func (q *Queries) CreateAuthor(ctx context.Context, name string) (Author, error) {
  112. row := q.db.QueryRowContext(ctx, createAuthor, name)
  113. var i Author
  114. err := row.Scan(&i.AuthorID, &i.Name)
  115. return i, err
  116. }
  117. const createBook = `-- name: CreateBook :one
  118. INSERT INTO books (
  119. author_id,
  120. isbn,
  121. book_type,
  122. title,
  123. yr,
  124. available,
  125. tag
  126. ) VALUES (
  127. ?,
  128. ?,
  129. ?,
  130. ?,
  131. ?,
  132. ?,
  133. ?
  134. )
  135. RETURNING book_id, author_id, isbn, book_type, title, yr, available, tag
  136. `
  137. type CreateBookParams struct {
  138. AuthorID int64
  139. Isbn string
  140. BookType string
  141. Title string
  142. Yr int64
  143. Available time.Time
  144. Tag string
  145. }
  146. func (q *Queries) CreateBook(ctx context.Context, arg CreateBookParams) (Book, error) {
  147. row := q.db.QueryRowContext(ctx, createBook,
  148. arg.AuthorID,
  149. arg.Isbn,
  150. arg.BookType,
  151. arg.Title,
  152. arg.Yr,
  153. arg.Available,
  154. arg.Tag,
  155. )
  156. var i Book
  157. err := row.Scan(
  158. &i.BookID,
  159. &i.AuthorID,
  160. &i.Isbn,
  161. &i.BookType,
  162. &i.Title,
  163. &i.Yr,
  164. &i.Available,
  165. &i.Tag,
  166. )
  167. return i, err
  168. }
  169. const deleteAuthorBeforeYear = `-- name: DeleteAuthorBeforeYear :exec
  170. DELETE FROM books
  171. WHERE yr < ? AND author_id = ?
  172. `
  173. type DeleteAuthorBeforeYearParams struct {
  174. Yr int64
  175. AuthorID int64
  176. }
  177. func (q *Queries) DeleteAuthorBeforeYear(ctx context.Context, arg DeleteAuthorBeforeYearParams) error {
  178. _, err := q.db.ExecContext(ctx, deleteAuthorBeforeYear, arg.Yr, arg.AuthorID)
  179. return err
  180. }
  181. const deleteBook = `-- name: DeleteBook :exec
  182. DELETE FROM books
  183. WHERE book_id = ?
  184. `
  185. func (q *Queries) DeleteBook(ctx context.Context, bookID int64) error {
  186. _, err := q.db.ExecContext(ctx, deleteBook, bookID)
  187. return err
  188. }
  189. const getAuthor = `-- name: GetAuthor :one
  190. SELECT author_id, name FROM authors
  191. WHERE author_id = ?
  192. `
  193. func (q *Queries) GetAuthor(ctx context.Context, authorID int64) (Author, error) {
  194. row := q.db.QueryRowContext(ctx, getAuthor, authorID)
  195. var i Author
  196. err := row.Scan(&i.AuthorID, &i.Name)
  197. return i, err
  198. }
  199. const getBook = `-- name: GetBook :one
  200. SELECT book_id, author_id, isbn, book_type, title, yr, available, tag FROM books
  201. WHERE book_id = ?
  202. `
  203. func (q *Queries) GetBook(ctx context.Context, bookID int64) (Book, error) {
  204. row := q.db.QueryRowContext(ctx, getBook, bookID)
  205. var i Book
  206. err := row.Scan(
  207. &i.BookID,
  208. &i.AuthorID,
  209. &i.Isbn,
  210. &i.BookType,
  211. &i.Title,
  212. &i.Yr,
  213. &i.Available,
  214. &i.Tag,
  215. )
  216. return i, err
  217. }
  218. const updateBook = `-- name: UpdateBook :exec
  219. UPDATE books
  220. SET title = ?1, tag = ?2
  221. WHERE book_id = ?3
  222. `
  223. type UpdateBookParams struct {
  224. Title string
  225. Tag string
  226. BookID int64
  227. }
  228. func (q *Queries) UpdateBook(ctx context.Context, arg UpdateBookParams) error {
  229. _, err := q.db.ExecContext(ctx, updateBook, arg.Title, arg.Tag, arg.BookID)
  230. return err
  231. }
  232. const updateBookISBN = `-- name: UpdateBookISBN :exec
  233. UPDATE books
  234. SET title = ?1, tag = ?2, isbn = ?4
  235. WHERE book_id = ?3
  236. `
  237. type UpdateBookISBNParams struct {
  238. Title string
  239. Tag string
  240. BookID int64
  241. Isbn string
  242. }
  243. func (q *Queries) UpdateBookISBN(ctx context.Context, arg UpdateBookISBNParams) error {
  244. _, err := q.db.ExecContext(ctx, updateBookISBN,
  245. arg.Title,
  246. arg.Tag,
  247. arg.BookID,
  248. arg.Isbn,
  249. )
  250. return err
  251. }