@@ -117,61 +117,61 @@ class QueriesImpl(private val conn: Connection) {
117117
118118 @Throws(SQLException ::class )
119119 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()))
122122
123- return stmt.executeQuery().use { results ->
123+ val results = stmt.executeQuery()
124124 val ret = mutableListOf<BooksByTagsRow >()
125125 while (results.next()) {
126126 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+ ))
133133 }
134134 ret
135135 }
136136 }
137137
138138 @Throws(SQLException ::class )
139139 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)
143143
144- return stmt.executeQuery().use { results ->
144+ val results = stmt.executeQuery()
145145 val ret = mutableListOf<Book >()
146146 while (results.next()) {
147147 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+ ))
157157 }
158158 ret
159159 }
160160 }
161161
162162 @Throws(SQLException ::class )
163163 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)
166166
167- return stmt.executeQuery().use { results ->
167+ val results = stmt.executeQuery()
168168 if (! results.next()) {
169169 throw SQLException (" no rows in result set" )
170170 }
171171 val ret = Author (
172- results.getInt(1 ),
173- results.getString(2 )
174- )
172+ results.getInt(1 ),
173+ results.getString(2 )
174+ )
175175 if (results.next()) {
176176 throw SQLException (" expected one row in result set, but got many" )
177177 }
@@ -181,29 +181,29 @@ class QueriesImpl(private val conn: Connection) {
181181
182182 @Throws(SQLException ::class )
183183 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()
194194 if (! results.next()) {
195195 throw SQLException (" no rows in result set" )
196196 }
197197 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+ )
207207 if (results.next()) {
208208 throw SQLException (" expected one row in result set, but got many" )
209209 }
@@ -213,26 +213,26 @@ class QueriesImpl(private val conn: Connection) {
213213
214214 @Throws(SQLException ::class )
215215 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)
218218
219- stmt.execute()
220- stmt.close()
219+ stmt.execute()
220+ }
221221 }
222222
223223 @Throws(SQLException ::class )
224224 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)
227227
228- return stmt.executeQuery().use { results ->
228+ val results = stmt.executeQuery()
229229 if (! results.next()) {
230230 throw SQLException (" no rows in result set" )
231231 }
232232 val ret = Author (
233- results.getInt(1 ),
234- results.getString(2 )
235- )
233+ results.getInt(1 ),
234+ results.getString(2 )
235+ )
236236 if (results.next()) {
237237 throw SQLException (" expected one row in result set, but got many" )
238238 }
@@ -242,23 +242,23 @@ class QueriesImpl(private val conn: Connection) {
242242
243243 @Throws(SQLException ::class )
244244 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)
247247
248- return stmt.executeQuery().use { results ->
248+ val results = stmt.executeQuery()
249249 if (! results.next()) {
250250 throw SQLException (" no rows in result set" )
251251 }
252252 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+ )
262262 if (results.next()) {
263263 throw SQLException (" expected one row in result set, but got many" )
264264 }
@@ -268,25 +268,25 @@ class QueriesImpl(private val conn: Connection) {
268268
269269 @Throws(SQLException ::class )
270270 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)
275275
276- stmt.execute()
277- stmt.close()
276+ stmt.execute()
277+ }
278278 }
279279
280280 @Throws(SQLException ::class )
281281 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+ }
290290 }
291291
292292}
0 commit comments