/
QueriesImpl.kt
278 lines (240 loc) · 6.55 KB
/
QueriesImpl.kt
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
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.12.0
package com.example.booktest.mysql
import java.sql.Connection
import java.sql.SQLException
import java.sql.Statement
import java.time.LocalDateTime
const val booksByTags = """-- name: booksByTags :many
SELECT
book_id,
title,
name,
isbn,
tags
FROM books
LEFT JOIN authors ON books.author_id = authors.author_id
WHERE tags = ?
"""
data class BooksByTagsRow (
val bookId: Int,
val title: String,
val name: String?,
val isbn: String,
val tags: String
)
const val booksByTitleYear = """-- name: booksByTitleYear :many
SELECT book_id, author_id, isbn, book_type, title, yr, available, tags FROM books
WHERE title = ? AND yr = ?
"""
const val createAuthor = """-- name: createAuthor :execresult
INSERT INTO authors (name) VALUES (?)
"""
const val createBook = """-- name: createBook :execresult
INSERT INTO books (
author_id,
isbn,
book_type,
title,
yr,
available,
tags
) VALUES (
?,
?,
?,
?,
?,
?,
?
)
"""
const val deleteAuthorBeforeYear = """-- name: deleteAuthorBeforeYear :exec
DELETE FROM books
WHERE yr < ? AND author_id = ?
"""
const val deleteBook = """-- name: deleteBook :exec
DELETE FROM books
WHERE book_id = ?
"""
const val getAuthor = """-- name: getAuthor :one
SELECT author_id, name FROM authors
WHERE author_id = ?
"""
const val getBook = """-- name: getBook :one
SELECT book_id, author_id, isbn, book_type, title, yr, available, tags FROM books
WHERE book_id = ?
"""
const val updateBook = """-- name: updateBook :exec
UPDATE books
SET title = ?, tags = ?
WHERE book_id = ?
"""
const val updateBookISBN = """-- name: updateBookISBN :exec
UPDATE books
SET title = ?, tags = ?, isbn = ?
WHERE book_id = ?
"""
class QueriesImpl(private val conn: Connection) : Queries {
@Throws(SQLException::class)
override fun booksByTags(tags: String): List<BooksByTagsRow> {
return conn.prepareStatement(booksByTags).use { stmt ->
stmt.setString(1, tags)
val results = stmt.executeQuery()
val ret = mutableListOf<BooksByTagsRow>()
while (results.next()) {
ret.add(BooksByTagsRow(
results.getInt(1),
results.getString(2),
results.getString(3),
results.getString(4),
results.getString(5)
))
}
ret
}
}
@Throws(SQLException::class)
override fun booksByTitleYear(title: String, yr: Int): List<Book> {
return conn.prepareStatement(booksByTitleYear).use { stmt ->
stmt.setString(1, title)
stmt.setInt(2, yr)
val results = stmt.executeQuery()
val ret = mutableListOf<Book>()
while (results.next()) {
ret.add(Book(
results.getInt(1),
results.getInt(2),
results.getString(3),
BooksBookType.lookup(results.getString(4))!!,
results.getString(5),
results.getInt(6),
results.getObject(7, LocalDateTime::class.java),
results.getString(8)
))
}
ret
}
}
@Throws(SQLException::class)
override fun createAuthor(name: String): Long {
return conn.prepareStatement(createAuthor, Statement.RETURN_GENERATED_KEYS).use { stmt ->
stmt.setString(1, name)
stmt.execute()
val results = stmt.generatedKeys
if (!results.next()) {
throw SQLException("no generated key returned")
}
results.getLong(1)
}
}
@Throws(SQLException::class)
override fun createBook(
authorId: Int,
isbn: String,
bookType: BooksBookType,
title: String,
yr: Int,
available: LocalDateTime,
tags: String): Long {
return conn.prepareStatement(createBook, Statement.RETURN_GENERATED_KEYS).use { stmt ->
stmt.setInt(1, authorId)
stmt.setString(2, isbn)
stmt.setString(3, bookType.value)
stmt.setString(4, title)
stmt.setInt(5, yr)
stmt.setObject(6, available)
stmt.setString(7, tags)
stmt.execute()
val results = stmt.generatedKeys
if (!results.next()) {
throw SQLException("no generated key returned")
}
results.getLong(1)
}
}
@Throws(SQLException::class)
override fun deleteAuthorBeforeYear(yr: Int, authorId: Int) {
conn.prepareStatement(deleteAuthorBeforeYear).use { stmt ->
stmt.setInt(1, yr)
stmt.setInt(2, authorId)
stmt.execute()
}
}
@Throws(SQLException::class)
override fun deleteBook(bookId: Int) {
conn.prepareStatement(deleteBook).use { stmt ->
stmt.setInt(1, bookId)
stmt.execute()
}
}
@Throws(SQLException::class)
override fun getAuthor(authorId: Int): Author? {
return conn.prepareStatement(getAuthor).use { stmt ->
stmt.setInt(1, authorId)
val results = stmt.executeQuery()
if (!results.next()) {
return null
}
val ret = Author(
results.getInt(1),
results.getString(2)
)
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}
@Throws(SQLException::class)
override fun getBook(bookId: Int): Book? {
return conn.prepareStatement(getBook).use { stmt ->
stmt.setInt(1, bookId)
val results = stmt.executeQuery()
if (!results.next()) {
return null
}
val ret = Book(
results.getInt(1),
results.getInt(2),
results.getString(3),
BooksBookType.lookup(results.getString(4))!!,
results.getString(5),
results.getInt(6),
results.getObject(7, LocalDateTime::class.java),
results.getString(8)
)
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}
@Throws(SQLException::class)
override fun updateBook(
title: String,
tags: String,
bookId: Int) {
conn.prepareStatement(updateBook).use { stmt ->
stmt.setString(1, title)
stmt.setString(2, tags)
stmt.setInt(3, bookId)
stmt.execute()
}
}
@Throws(SQLException::class)
override fun updateBookISBN(
title: String,
tags: String,
isbn: String,
bookId: Int) {
conn.prepareStatement(updateBookISBN).use { stmt ->
stmt.setString(1, title)
stmt.setString(2, tags)
stmt.setString(3, isbn)
stmt.setInt(4, bookId)
stmt.execute()
}
}
}