Skip to content

Commit bfa6704

Browse files
committed
kotlin: always use use, fix indents
1 parent 2cd3bb8 commit bfa6704

File tree

5 files changed

+240
-225
lines changed

5 files changed

+240
-225
lines changed

examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ class QueriesImpl(private val conn: Connection) {
3838

3939
@Throws(SQLException::class)
4040
fun createAuthor(arg: CreateAuthorParams): Author {
41-
val stmt = conn.prepareStatement(createAuthor)
42-
stmt.setString(1, arg.name)
43-
stmt.setString(2, arg.bio)
41+
return conn.prepareStatement(createAuthor).use { stmt ->
42+
stmt.setString(1, arg.name)
43+
stmt.setString(2, arg.bio)
4444

45-
return stmt.executeQuery().use { results ->
45+
val results = stmt.executeQuery()
4646
if (!results.next()) {
4747
throw SQLException("no rows in result set")
4848
}
4949
val ret = Author(
50-
results.getLong(1),
51-
results.getString(2),
52-
results.getString(3)
53-
)
50+
results.getLong(1),
51+
results.getString(2),
52+
results.getString(3)
53+
)
5454
if (results.next()) {
5555
throw SQLException("expected one row in result set, but got many")
5656
}
@@ -60,27 +60,27 @@ class QueriesImpl(private val conn: Connection) {
6060

6161
@Throws(SQLException::class)
6262
fun deleteAuthor(id: Long) {
63-
val stmt = conn.prepareStatement(deleteAuthor)
64-
stmt.setLong(1, id)
63+
conn.prepareStatement(deleteAuthor).use { stmt ->
64+
stmt.setLong(1, id)
6565

66-
stmt.execute()
67-
stmt.close()
66+
stmt.execute()
67+
}
6868
}
6969

7070
@Throws(SQLException::class)
7171
fun getAuthor(id: Long): Author {
72-
val stmt = conn.prepareStatement(getAuthor)
73-
stmt.setLong(1, id)
72+
return conn.prepareStatement(getAuthor).use { stmt ->
73+
stmt.setLong(1, id)
7474

75-
return stmt.executeQuery().use { results ->
75+
val results = stmt.executeQuery()
7676
if (!results.next()) {
7777
throw SQLException("no rows in result set")
7878
}
7979
val ret = Author(
80-
results.getLong(1),
81-
results.getString(2),
82-
results.getString(3)
83-
)
80+
results.getLong(1),
81+
results.getString(2),
82+
results.getString(3)
83+
)
8484
if (results.next()) {
8585
throw SQLException("expected one row in result set, but got many")
8686
}
@@ -90,16 +90,16 @@ class QueriesImpl(private val conn: Connection) {
9090

9191
@Throws(SQLException::class)
9292
fun listAuthors(): List<Author> {
93-
val stmt = conn.prepareStatement(listAuthors)
94-
95-
return stmt.executeQuery().use { results ->
93+
return conn.prepareStatement(listAuthors).use { stmt ->
94+
95+
val results = stmt.executeQuery()
9696
val ret = mutableListOf<Author>()
9797
while (results.next()) {
9898
ret.add(Author(
99-
results.getLong(1),
100-
results.getString(2),
101-
results.getString(3)
102-
))
99+
results.getLong(1),
100+
results.getString(2),
101+
results.getString(3)
102+
))
103103
}
104104
ret
105105
}

examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

examples/kotlin/src/main/kotlin/com/example/jets/QueriesImpl.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class QueriesImpl(private val conn: Connection) {
2121

2222
@Throws(SQLException::class)
2323
fun countPilots(): Long {
24-
val stmt = conn.prepareStatement(countPilots)
25-
26-
return stmt.executeQuery().use { results ->
24+
return conn.prepareStatement(countPilots).use { stmt ->
25+
26+
val results = stmt.executeQuery()
2727
if (!results.next()) {
2828
throw SQLException("no rows in result set")
2929
}
@@ -37,24 +37,24 @@ class QueriesImpl(private val conn: Connection) {
3737

3838
@Throws(SQLException::class)
3939
fun deletePilot(id: Int) {
40-
val stmt = conn.prepareStatement(deletePilot)
41-
stmt.setInt(1, id)
40+
conn.prepareStatement(deletePilot).use { stmt ->
41+
stmt.setInt(1, id)
4242

43-
stmt.execute()
44-
stmt.close()
43+
stmt.execute()
44+
}
4545
}
4646

4747
@Throws(SQLException::class)
4848
fun listPilots(): List<Pilot> {
49-
val stmt = conn.prepareStatement(listPilots)
50-
51-
return stmt.executeQuery().use { results ->
49+
return conn.prepareStatement(listPilots).use { stmt ->
50+
51+
val results = stmt.executeQuery()
5252
val ret = mutableListOf<Pilot>()
5353
while (results.next()) {
5454
ret.add(Pilot(
55-
results.getInt(1),
56-
results.getString(2)
57-
))
55+
results.getInt(1),
56+
results.getString(2)
57+
))
5858
}
5959
ret
6060
}

0 commit comments

Comments
 (0)