Skip to content

Commit 0f04fd3

Browse files
committed
kotlin: unbox query params
1 parent bfa6704 commit 0f04fd3

File tree

8 files changed

+154
-229
lines changed

8 files changed

+154
-229
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ INSERT INTO authors (
1414
RETURNING id, name, bio
1515
"""
1616

17-
data class CreateAuthorParams (
18-
val name: String,
19-
val bio: String?
20-
)
21-
2217
const val deleteAuthor = """-- name: deleteAuthor :exec
2318
DELETE FROM authors
2419
WHERE id = ?
@@ -37,10 +32,10 @@ ORDER BY name
3732
class QueriesImpl(private val conn: Connection) {
3833

3934
@Throws(SQLException::class)
40-
fun createAuthor(arg: CreateAuthorParams): Author {
35+
fun createAuthor(name: String, bio: String?): Author {
4136
return conn.prepareStatement(createAuthor).use { stmt ->
42-
stmt.setString(1, arg.name)
43-
stmt.setString(2, arg.bio)
37+
stmt.setString(1, name)
38+
stmt.setString(2, bio)
4439

4540
val results = stmt.executeQuery()
4641
if (!results.next()) {

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

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ SELECT book_id, author_id, isbn, booktype, title, year, available, tags FROM boo
3232
WHERE title = ? AND year = ?
3333
"""
3434

35-
data class BooksByTitleYearParams (
36-
val title: String,
37-
val year: Int
38-
)
39-
4035
const val createAuthor = """-- name: createAuthor :one
4136
INSERT INTO authors (name) VALUES (?)
4237
RETURNING author_id, name
@@ -63,16 +58,6 @@ INSERT INTO books (
6358
RETURNING book_id, author_id, isbn, booktype, title, year, available, tags
6459
"""
6560

66-
data class CreateBookParams (
67-
val authorId: Int,
68-
val isbn: String,
69-
val booktype: BookType,
70-
val title: String,
71-
val year: Int,
72-
val available: OffsetDateTime,
73-
val tags: List<String>
74-
)
75-
7661
const val deleteBook = """-- name: deleteBook :exec
7762
DELETE FROM books
7863
WHERE book_id = ?
@@ -94,31 +79,18 @@ SET title = ?, tags = ?
9479
WHERE book_id = ?
9580
"""
9681

97-
data class UpdateBookParams (
98-
val title: String,
99-
val tags: List<String>,
100-
val bookId: Int
101-
)
102-
10382
const val updateBookISBN = """-- name: updateBookISBN :exec
10483
UPDATE books
10584
SET title = ?, tags = ?, isbn = ?
10685
WHERE book_id = ?
10786
"""
10887

109-
data class UpdateBookISBNParams (
110-
val title: String,
111-
val tags: List<String>,
112-
val isbn: String,
113-
val bookId: Int
114-
)
115-
11688
class QueriesImpl(private val conn: Connection) {
11789

11890
@Throws(SQLException::class)
119-
fun booksByTags(dollar_1: List<String>): List<BooksByTagsRow> {
91+
fun booksByTags(dollar1: List<String>): List<BooksByTagsRow> {
12092
return conn.prepareStatement(booksByTags).use { stmt ->
121-
stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar_1.toTypedArray()))
93+
stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar1.toTypedArray()))
12294

12395
val results = stmt.executeQuery()
12496
val ret = mutableListOf<BooksByTagsRow>()
@@ -136,10 +108,10 @@ class QueriesImpl(private val conn: Connection) {
136108
}
137109

138110
@Throws(SQLException::class)
139-
fun booksByTitleYear(arg: BooksByTitleYearParams): List<Book> {
111+
fun booksByTitleYear(title: String, year: Int): List<Book> {
140112
return conn.prepareStatement(booksByTitleYear).use { stmt ->
141-
stmt.setString(1, arg.title)
142-
stmt.setInt(2, arg.year)
113+
stmt.setString(1, title)
114+
stmt.setInt(2, year)
143115

144116
val results = stmt.executeQuery()
145117
val ret = mutableListOf<Book>()
@@ -180,15 +152,22 @@ class QueriesImpl(private val conn: Connection) {
180152
}
181153

182154
@Throws(SQLException::class)
183-
fun createBook(arg: CreateBookParams): Book {
155+
fun createBook(
156+
authorId: Int,
157+
isbn: String,
158+
booktype: BookType,
159+
title: String,
160+
year: Int,
161+
available: OffsetDateTime,
162+
tags: List<String>): Book {
184163
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()))
164+
stmt.setInt(1, authorId)
165+
stmt.setString(2, isbn)
166+
stmt.setObject(3, booktype.value, Types.OTHER)
167+
stmt.setString(4, title)
168+
stmt.setInt(5, year)
169+
stmt.setObject(6, available)
170+
stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray()))
192171

193172
val results = stmt.executeQuery()
194173
if (!results.next()) {
@@ -267,23 +246,30 @@ class QueriesImpl(private val conn: Connection) {
267246
}
268247

269248
@Throws(SQLException::class)
270-
fun updateBook(arg: UpdateBookParams) {
249+
fun updateBook(
250+
title: String,
251+
tags: List<String>,
252+
bookId: Int) {
271253
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)
254+
stmt.setString(1, title)
255+
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray()))
256+
stmt.setInt(3, bookId)
275257

276258
stmt.execute()
277259
}
278260
}
279261

280262
@Throws(SQLException::class)
281-
fun updateBookISBN(arg: UpdateBookISBNParams) {
263+
fun updateBookISBN(
264+
title: String,
265+
tags: List<String>,
266+
isbn: String,
267+
bookId: Int) {
282268
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)
269+
stmt.setString(1, title)
270+
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray()))
271+
stmt.setString(3, isbn)
272+
stmt.setInt(4, bookId)
287273

288274
stmt.execute()
289275
}

examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ import java.time.LocalDateTime
99

1010
interface Queries {
1111
@Throws(SQLException::class)
12-
fun createCity(arg: CreateCityParams): City
12+
fun createCity(name: String, slug: String): City
1313

1414
@Throws(SQLException::class)
15-
fun createVenue(arg: CreateVenueParams): Int
15+
fun createVenue(
16+
slug: String,
17+
name: String,
18+
city: String,
19+
spotifyPlaylist: String,
20+
status: Status,
21+
statuses: List<Status>,
22+
tags: List<String>): Int
1623

1724
@Throws(SQLException::class)
1825
fun deleteVenue(slug: String)
@@ -21,7 +28,7 @@ interface Queries {
2128
fun getCity(slug: String): City
2229

2330
@Throws(SQLException::class)
24-
fun getVenue(arg: GetVenueParams): Venue
31+
fun getVenue(slug: String, city: String): Venue
2532

2633
@Throws(SQLException::class)
2734
fun listCities(): List<City>
@@ -30,10 +37,10 @@ interface Queries {
3037
fun listVenues(city: String): List<Venue>
3138

3239
@Throws(SQLException::class)
33-
fun updateCityName(arg: UpdateCityNameParams)
40+
fun updateCityName(name: String, slug: String)
3441

3542
@Throws(SQLException::class)
36-
fun updateVenueName(arg: UpdateVenueNameParams): Int
43+
fun updateVenueName(name: String, slug: String): Int
3744

3845
@Throws(SQLException::class)
3946
fun venueCountByCity(): List<VenueCountByCityRow>

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

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ INSERT INTO city (
1717
) RETURNING slug, name
1818
"""
1919

20-
data class CreateCityParams (
21-
val name: String,
22-
val slug: String
23-
)
24-
2520
const val createVenue = """-- name: createVenue :one
2621
INSERT INTO venue (
2722
slug,
@@ -44,16 +39,6 @@ INSERT INTO venue (
4439
) RETURNING id
4540
"""
4641

47-
data class CreateVenueParams (
48-
val slug: String,
49-
val name: String,
50-
val city: String,
51-
val spotifyPlaylist: String,
52-
val status: Status,
53-
val statuses: List<Status>,
54-
val tags: List<String>
55-
)
56-
5742
const val deleteVenue = """-- name: deleteVenue :exec
5843
DELETE FROM venue
5944
WHERE slug = ? AND slug = ?
@@ -71,11 +56,6 @@ FROM venue
7156
WHERE slug = ? AND city = ?
7257
"""
7358

74-
data class GetVenueParams (
75-
val slug: String,
76-
val city: String
77-
)
78-
7959
const val listCities = """-- name: listCities :many
8060
SELECT slug, name
8161
FROM city
@@ -95,23 +75,13 @@ SET name = ?
9575
WHERE slug = ?
9676
"""
9777

98-
data class UpdateCityNameParams (
99-
val name: String,
100-
val slug: String
101-
)
102-
10378
const val updateVenueName = """-- name: updateVenueName :one
10479
UPDATE venue
10580
SET name = ?
10681
WHERE slug = ?
10782
RETURNING id
10883
"""
10984

110-
data class UpdateVenueNameParams (
111-
val name: String,
112-
val slug: String
113-
)
114-
11585
const val venueCountByCity = """-- name: venueCountByCity :many
11686
SELECT
11787
city,
@@ -133,10 +103,10 @@ class QueriesImpl(private val conn: Connection) : Queries {
133103
// This is the third line
134104

135105
@Throws(SQLException::class)
136-
override fun createCity(arg: CreateCityParams): City {
106+
override fun createCity(name: String, slug: String): City {
137107
return conn.prepareStatement(createCity).use { stmt ->
138-
stmt.setString(1, arg.name)
139-
stmt.setString(2, arg.slug)
108+
stmt.setString(1, name)
109+
stmt.setString(2, slug)
140110

141111
val results = stmt.executeQuery()
142112
if (!results.next()) {
@@ -154,15 +124,22 @@ class QueriesImpl(private val conn: Connection) : Queries {
154124
}
155125

156126
@Throws(SQLException::class)
157-
override fun createVenue(arg: CreateVenueParams): Int {
127+
override fun createVenue(
128+
slug: String,
129+
name: String,
130+
city: String,
131+
spotifyPlaylist: String,
132+
status: Status,
133+
statuses: List<Status>,
134+
tags: List<String>): Int {
158135
return conn.prepareStatement(createVenue).use { stmt ->
159-
stmt.setString(1, arg.slug)
160-
stmt.setString(2, arg.name)
161-
stmt.setString(3, arg.city)
162-
stmt.setString(4, arg.spotifyPlaylist)
163-
stmt.setObject(5, arg.status.value, Types.OTHER)
164-
stmt.setArray(6, conn.createArrayOf("status", arg.statuses.map { v -> v.value }.toTypedArray()))
165-
stmt.setArray(7, conn.createArrayOf("text", arg.tags.toTypedArray()))
136+
stmt.setString(1, slug)
137+
stmt.setString(2, name)
138+
stmt.setString(3, city)
139+
stmt.setString(4, spotifyPlaylist)
140+
stmt.setObject(5, status.value, Types.OTHER)
141+
stmt.setArray(6, conn.createArrayOf("status", statuses.map { v -> v.value }.toTypedArray()))
142+
stmt.setArray(7, conn.createArrayOf("text", tags.toTypedArray()))
166143

167144
val results = stmt.executeQuery()
168145
if (!results.next()) {
@@ -207,10 +184,10 @@ class QueriesImpl(private val conn: Connection) : Queries {
207184
}
208185

209186
@Throws(SQLException::class)
210-
override fun getVenue(arg: GetVenueParams): Venue {
187+
override fun getVenue(slug: String, city: String): Venue {
211188
return conn.prepareStatement(getVenue).use { stmt ->
212-
stmt.setString(1, arg.slug)
213-
stmt.setString(2, arg.city)
189+
stmt.setString(1, slug)
190+
stmt.setString(2, city)
214191

215192
val results = stmt.executeQuery()
216193
if (!results.next()) {
@@ -277,20 +254,20 @@ class QueriesImpl(private val conn: Connection) : Queries {
277254
}
278255

279256
@Throws(SQLException::class)
280-
override fun updateCityName(arg: UpdateCityNameParams) {
257+
override fun updateCityName(name: String, slug: String) {
281258
conn.prepareStatement(updateCityName).use { stmt ->
282-
stmt.setString(1, arg.name)
283-
stmt.setString(2, arg.slug)
259+
stmt.setString(1, name)
260+
stmt.setString(2, slug)
284261

285262
stmt.execute()
286263
}
287264
}
288265

289266
@Throws(SQLException::class)
290-
override fun updateVenueName(arg: UpdateVenueNameParams): Int {
267+
override fun updateVenueName(name: String, slug: String): Int {
291268
return conn.prepareStatement(updateVenueName).use { stmt ->
292-
stmt.setString(1, arg.name)
293-
stmt.setString(2, arg.slug)
269+
stmt.setString(1, name)
270+
stmt.setString(2, slug)
294271

295272
val results = stmt.executeQuery()
296273
if (!results.next()) {

0 commit comments

Comments
 (0)