query.sql.go 4.7 KB

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