@@ -117,61 +117,61 @@ class QueriesImpl(private val conn: Connection) {
117
117
118
118
@Throws(SQLException ::class )
119
119
fun booksByTags (dollar_1 : List <String >): List <BooksByTagsRow > {
120
- val stmt = conn.prepareStatement(booksByTags)
121
- stmt.setArray(1 , conn.createArrayOf(" pg_catalog.varchar" , dollar_1.toTypedArray()))
120
+ return conn.prepareStatement(booksByTags).use { stmt ->
121
+ stmt.setArray(1 , conn.createArrayOf(" pg_catalog.varchar" , dollar_1.toTypedArray()))
122
122
123
- return stmt.executeQuery().use { results ->
123
+ val results = stmt.executeQuery()
124
124
val ret = mutableListOf<BooksByTagsRow >()
125
125
while (results.next()) {
126
126
ret.add(BooksByTagsRow (
127
- results.getInt(1 ),
128
- results.getString(2 ),
129
- results.getString(3 ),
130
- results.getString(4 ),
131
- (results.getArray(5 ).array as Array <String >).toList()
132
- ))
127
+ results.getInt(1 ),
128
+ results.getString(2 ),
129
+ results.getString(3 ),
130
+ results.getString(4 ),
131
+ (results.getArray(5 ).array as Array <String >).toList()
132
+ ))
133
133
}
134
134
ret
135
135
}
136
136
}
137
137
138
138
@Throws(SQLException ::class )
139
139
fun booksByTitleYear (arg : BooksByTitleYearParams ): List <Book > {
140
- val stmt = conn.prepareStatement(booksByTitleYear)
141
- stmt.setString(1 , arg.title)
142
- stmt.setInt(2 , arg.year)
140
+ return conn.prepareStatement(booksByTitleYear).use { stmt ->
141
+ stmt.setString(1 , arg.title)
142
+ stmt.setInt(2 , arg.year)
143
143
144
- return stmt.executeQuery().use { results ->
144
+ val results = stmt.executeQuery()
145
145
val ret = mutableListOf<Book >()
146
146
while (results.next()) {
147
147
ret.add(Book (
148
- results.getInt(1 ),
149
- results.getInt(2 ),
150
- results.getString(3 ),
151
- BookType .lookup(results.getString(4 ))!! ,
152
- results.getString(5 ),
153
- results.getInt(6 ),
154
- results.getObject(7 , OffsetDateTime ::class .java),
155
- (results.getArray(8 ).array as Array <String >).toList()
156
- ))
148
+ results.getInt(1 ),
149
+ results.getInt(2 ),
150
+ results.getString(3 ),
151
+ BookType .lookup(results.getString(4 ))!! ,
152
+ results.getString(5 ),
153
+ results.getInt(6 ),
154
+ results.getObject(7 , OffsetDateTime ::class .java),
155
+ (results.getArray(8 ).array as Array <String >).toList()
156
+ ))
157
157
}
158
158
ret
159
159
}
160
160
}
161
161
162
162
@Throws(SQLException ::class )
163
163
fun createAuthor (name : String ): Author {
164
- val stmt = conn.prepareStatement(createAuthor)
165
- stmt.setString(1 , name)
164
+ return conn.prepareStatement(createAuthor).use { stmt ->
165
+ stmt.setString(1 , name)
166
166
167
- return stmt.executeQuery().use { results ->
167
+ val results = stmt.executeQuery()
168
168
if (! results.next()) {
169
169
throw SQLException (" no rows in result set" )
170
170
}
171
171
val ret = Author (
172
- results.getInt(1 ),
173
- results.getString(2 )
174
- )
172
+ results.getInt(1 ),
173
+ results.getString(2 )
174
+ )
175
175
if (results.next()) {
176
176
throw SQLException (" expected one row in result set, but got many" )
177
177
}
@@ -181,29 +181,29 @@ class QueriesImpl(private val conn: Connection) {
181
181
182
182
@Throws(SQLException ::class )
183
183
fun createBook (arg : CreateBookParams ): Book {
184
- val stmt = conn.prepareStatement(createBook)
185
- stmt.setInt(1 , arg.authorId)
186
- stmt.setString(2 , arg.isbn)
187
- stmt.setObject(3 , arg.booktype.value, Types .OTHER )
188
- stmt.setString(4 , arg.title)
189
- stmt.setInt(5 , arg.year)
190
- stmt.setObject(6 , arg.available)
191
- stmt.setArray(7 , conn.createArrayOf(" pg_catalog.varchar" , arg.tags.toTypedArray()))
192
-
193
- return stmt.executeQuery().use { results ->
184
+ return conn.prepareStatement(createBook).use { stmt ->
185
+ stmt.setInt(1 , arg.authorId)
186
+ stmt.setString(2 , arg.isbn)
187
+ stmt.setObject(3 , arg.booktype.value, Types .OTHER )
188
+ stmt.setString(4 , arg.title)
189
+ stmt.setInt(5 , arg.year)
190
+ stmt.setObject(6 , arg.available)
191
+ stmt.setArray(7 , conn.createArrayOf(" pg_catalog.varchar" , arg.tags.toTypedArray()))
192
+
193
+ val results = stmt.executeQuery()
194
194
if (! results.next()) {
195
195
throw SQLException (" no rows in result set" )
196
196
}
197
197
val ret = Book (
198
- results.getInt(1 ),
199
- results.getInt(2 ),
200
- results.getString(3 ),
201
- BookType .lookup(results.getString(4 ))!! ,
202
- results.getString(5 ),
203
- results.getInt(6 ),
204
- results.getObject(7 , OffsetDateTime ::class .java),
205
- (results.getArray(8 ).array as Array <String >).toList()
206
- )
198
+ results.getInt(1 ),
199
+ results.getInt(2 ),
200
+ results.getString(3 ),
201
+ BookType .lookup(results.getString(4 ))!! ,
202
+ results.getString(5 ),
203
+ results.getInt(6 ),
204
+ results.getObject(7 , OffsetDateTime ::class .java),
205
+ (results.getArray(8 ).array as Array <String >).toList()
206
+ )
207
207
if (results.next()) {
208
208
throw SQLException (" expected one row in result set, but got many" )
209
209
}
@@ -213,26 +213,26 @@ class QueriesImpl(private val conn: Connection) {
213
213
214
214
@Throws(SQLException ::class )
215
215
fun deleteBook (bookId : Int ) {
216
- val stmt = conn.prepareStatement(deleteBook)
217
- stmt.setInt(1 , bookId)
216
+ conn.prepareStatement(deleteBook).use { stmt ->
217
+ stmt.setInt(1 , bookId)
218
218
219
- stmt.execute()
220
- stmt.close()
219
+ stmt.execute()
220
+ }
221
221
}
222
222
223
223
@Throws(SQLException ::class )
224
224
fun getAuthor (authorId : Int ): Author {
225
- val stmt = conn.prepareStatement(getAuthor)
226
- stmt.setInt(1 , authorId)
225
+ return conn.prepareStatement(getAuthor).use { stmt ->
226
+ stmt.setInt(1 , authorId)
227
227
228
- return stmt.executeQuery().use { results ->
228
+ val results = stmt.executeQuery()
229
229
if (! results.next()) {
230
230
throw SQLException (" no rows in result set" )
231
231
}
232
232
val ret = Author (
233
- results.getInt(1 ),
234
- results.getString(2 )
235
- )
233
+ results.getInt(1 ),
234
+ results.getString(2 )
235
+ )
236
236
if (results.next()) {
237
237
throw SQLException (" expected one row in result set, but got many" )
238
238
}
@@ -242,23 +242,23 @@ class QueriesImpl(private val conn: Connection) {
242
242
243
243
@Throws(SQLException ::class )
244
244
fun getBook (bookId : Int ): Book {
245
- val stmt = conn.prepareStatement(getBook)
246
- stmt.setInt(1 , bookId)
245
+ return conn.prepareStatement(getBook).use { stmt ->
246
+ stmt.setInt(1 , bookId)
247
247
248
- return stmt.executeQuery().use { results ->
248
+ val results = stmt.executeQuery()
249
249
if (! results.next()) {
250
250
throw SQLException (" no rows in result set" )
251
251
}
252
252
val ret = Book (
253
- results.getInt(1 ),
254
- results.getInt(2 ),
255
- results.getString(3 ),
256
- BookType .lookup(results.getString(4 ))!! ,
257
- results.getString(5 ),
258
- results.getInt(6 ),
259
- results.getObject(7 , OffsetDateTime ::class .java),
260
- (results.getArray(8 ).array as Array <String >).toList()
261
- )
253
+ results.getInt(1 ),
254
+ results.getInt(2 ),
255
+ results.getString(3 ),
256
+ BookType .lookup(results.getString(4 ))!! ,
257
+ results.getString(5 ),
258
+ results.getInt(6 ),
259
+ results.getObject(7 , OffsetDateTime ::class .java),
260
+ (results.getArray(8 ).array as Array <String >).toList()
261
+ )
262
262
if (results.next()) {
263
263
throw SQLException (" expected one row in result set, but got many" )
264
264
}
@@ -268,25 +268,25 @@ class QueriesImpl(private val conn: Connection) {
268
268
269
269
@Throws(SQLException ::class )
270
270
fun updateBook (arg : UpdateBookParams ) {
271
- val stmt = conn.prepareStatement(updateBook)
272
- stmt.setString(1 , arg.title)
273
- stmt.setArray(2 , conn.createArrayOf(" pg_catalog.varchar" , arg.tags.toTypedArray()))
274
- stmt.setInt(3 , arg.bookId)
271
+ conn.prepareStatement(updateBook).use { stmt ->
272
+ stmt.setString(1 , arg.title)
273
+ stmt.setArray(2 , conn.createArrayOf(" pg_catalog.varchar" , arg.tags.toTypedArray()))
274
+ stmt.setInt(3 , arg.bookId)
275
275
276
- stmt.execute()
277
- stmt.close()
276
+ stmt.execute()
277
+ }
278
278
}
279
279
280
280
@Throws(SQLException ::class )
281
281
fun updateBookISBN (arg : UpdateBookISBNParams ) {
282
- val stmt = conn.prepareStatement(updateBookISBN)
283
- stmt.setString(1 , arg.title)
284
- stmt.setArray(2 , conn.createArrayOf(" pg_catalog.varchar" , arg.tags.toTypedArray()))
285
- stmt.setString(3 , arg.isbn)
286
- stmt.setInt(4 , arg.bookId)
287
-
288
- stmt.execute()
289
- stmt.close()
282
+ conn.prepareStatement(updateBookISBN).use { stmt ->
283
+ stmt.setString(1 , arg.title)
284
+ stmt.setArray(2 , conn.createArrayOf(" pg_catalog.varchar" , arg.tags.toTypedArray()))
285
+ stmt.setString(3 , arg.isbn)
286
+ stmt.setInt(4 , arg.bookId)
287
+
288
+ stmt.execute()
289
+ }
290
290
}
291
291
292
292
}
0 commit comments