Skip to content

Commit f44fe82

Browse files
committed
kotlin: port Go tests for examples
1 parent de9ede2 commit f44fe82

File tree

19 files changed

+422
-54
lines changed

19 files changed

+422
-54
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import java.time.OffsetDateTime
66

77
enum class BookType(val value: String) {
88
FICTION("FICTION"),
9-
NONFICTION("NONFICTION")
9+
NONFICTION("NONFICTION");
10+
11+
companion object {
12+
private val map = BookType.values().associateBy(BookType::value)
13+
fun lookup(value: String) = map[value]
14+
}
1015
}
1116

1217
data class Author (
@@ -22,6 +27,6 @@ data class Book (
2227
val title: String,
2328
val year: Int,
2429
val available: OffsetDateTime,
25-
val tags: Array<String>
30+
val tags: List<String>
2631
)
2732

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.example.booktest.postgresql
44

55
import java.sql.Connection
66
import java.sql.SQLException
7+
import java.sql.Types
78
import java.time.OffsetDateTime
89

910
const val booksByTags = """-- name: booksByTags :many
@@ -23,7 +24,7 @@ data class BooksByTagsRow (
2324
val title: String,
2425
val name: String,
2526
val isbn: String,
26-
val tags: Array<String>
27+
val tags: List<String>
2728
)
2829

2930
const val booksByTitleYear = """-- name: booksByTitleYear :many
@@ -69,7 +70,7 @@ data class CreateBookParams (
6970
val title: String,
7071
val year: Int,
7172
val available: OffsetDateTime,
72-
val tags: Array<String>
73+
val tags: List<String>
7374
)
7475

7576
const val deleteBook = """-- name: deleteBook :exec
@@ -95,7 +96,7 @@ WHERE book_id = ?
9596

9697
data class UpdateBookParams (
9798
val title: String,
98-
val tags: Array<String>,
99+
val tags: List<String>,
99100
val bookId: Int
100101
)
101102

@@ -107,17 +108,17 @@ WHERE book_id = ?
107108

108109
data class UpdateBookISBNParams (
109110
val title: String,
110-
val tags: Array<String>,
111+
val tags: List<String>,
111112
val bookId: Int,
112113
val isbn: String
113114
)
114115

115116
class QueriesImpl(private val conn: Connection) {
116117

117118
@Throws(SQLException::class)
118-
fun booksByTags(dollar_1: Array<String>): List<BooksByTagsRow> {
119+
fun booksByTags(dollar_1: List<String>): List<BooksByTagsRow> {
119120
val stmt = conn.prepareStatement(booksByTags)
120-
stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar_1))
121+
stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar_1.toTypedArray()))
121122

122123
return stmt.executeQuery().use { results ->
123124
val ret = mutableListOf<BooksByTagsRow>()
@@ -127,7 +128,7 @@ class QueriesImpl(private val conn: Connection) {
127128
results.getString(2),
128129
results.getString(3),
129130
results.getString(4),
130-
results.getArray(5).array as Array<String>
131+
(results.getArray(5).array as Array<String>).toList()
131132
))
132133
}
133134
ret
@@ -147,11 +148,11 @@ class QueriesImpl(private val conn: Connection) {
147148
results.getInt(1),
148149
results.getInt(2),
149150
results.getString(3),
150-
BookType.valueOf(results.getString(4)),
151+
BookType.lookup(results.getString(4))!!,
151152
results.getString(5),
152153
results.getInt(6),
153154
results.getObject(7, OffsetDateTime::class.java),
154-
results.getArray(8).array as Array<String>
155+
(results.getArray(8).array as Array<String>).toList()
155156
))
156157
}
157158
ret
@@ -183,11 +184,11 @@ class QueriesImpl(private val conn: Connection) {
183184
val stmt = conn.prepareStatement(createBook)
184185
stmt.setInt(1, arg.authorId)
185186
stmt.setString(2, arg.isbn)
186-
stmt.setString(3, arg.booktype.value)
187+
stmt.setObject(3, arg.booktype.value, Types.OTHER)
187188
stmt.setString(4, arg.title)
188189
stmt.setInt(5, arg.year)
189190
stmt.setObject(6, arg.available)
190-
stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", arg.tags))
191+
stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray()))
191192

192193
return stmt.executeQuery().use { results ->
193194
if (!results.next()) {
@@ -197,11 +198,11 @@ class QueriesImpl(private val conn: Connection) {
197198
results.getInt(1),
198199
results.getInt(2),
199200
results.getString(3),
200-
BookType.valueOf(results.getString(4)),
201+
BookType.lookup(results.getString(4))!!,
201202
results.getString(5),
202203
results.getInt(6),
203204
results.getObject(7, OffsetDateTime::class.java),
204-
results.getArray(8).array as Array<String>
205+
(results.getArray(8).array as Array<String>).toList()
205206
)
206207
if (results.next()) {
207208
throw SQLException("expected one row in result set, but got many")
@@ -252,11 +253,11 @@ class QueriesImpl(private val conn: Connection) {
252253
results.getInt(1),
253254
results.getInt(2),
254255
results.getString(3),
255-
BookType.valueOf(results.getString(4)),
256+
BookType.lookup(results.getString(4))!!,
256257
results.getString(5),
257258
results.getInt(6),
258259
results.getObject(7, OffsetDateTime::class.java),
259-
results.getArray(8).array as Array<String>
260+
(results.getArray(8).array as Array<String>).toList()
260261
)
261262
if (results.next()) {
262263
throw SQLException("expected one row in result set, but got many")
@@ -269,7 +270,7 @@ class QueriesImpl(private val conn: Connection) {
269270
fun updateBook(arg: UpdateBookParams) {
270271
val stmt = conn.prepareStatement(updateBook)
271272
stmt.setString(1, arg.title)
272-
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags))
273+
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray()))
273274
stmt.setInt(3, arg.bookId)
274275

275276
stmt.execute()
@@ -280,7 +281,7 @@ class QueriesImpl(private val conn: Connection) {
280281
fun updateBookISBN(arg: UpdateBookISBNParams) {
281282
val stmt = conn.prepareStatement(updateBookISBN)
282283
stmt.setString(1, arg.title)
283-
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags))
284+
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray()))
284285
stmt.setInt(3, arg.bookId)
285286
stmt.setString(4, arg.isbn)
286287

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import java.time.LocalDateTime
77
// Venues can be either open or closed
88
enum class Status(val value: String) {
99
OPEN("op!en"),
10-
CLOSED("clo@sed")
10+
CLOSED("clo@sed");
11+
12+
companion object {
13+
private val map = Status.values().associateBy(Status::value)
14+
fun lookup(value: String) = map[value]
15+
}
1116
}
1217

1318
data class City (
@@ -19,14 +24,14 @@ data class City (
1924
data class Venue (
2025
val id: Int,
2126
val status: Status,
22-
val statuses: Array<Status>,
27+
val statuses: List<Status>,
2328
// This value appears in public URLs
2429
val slug: String,
2530
val name: String,
2631
val city: String,
2732
val spotifyPlaylist: String,
2833
val songkickId: String?,
29-
val tags: Array<String>,
34+
val tags: List<String>,
3035
val createdAt: LocalDateTime
3136
)
3237

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.example.ondeck
44

55
import java.sql.Connection
66
import java.sql.SQLException
7+
import java.sql.Types
78
import java.time.LocalDateTime
89

910
interface Queries {

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.example.ondeck
44

55
import java.sql.Connection
66
import java.sql.SQLException
7+
import java.sql.Types
78
import java.time.LocalDateTime
89

910
const val createCity = """-- name: createCity :one
@@ -49,8 +50,8 @@ data class CreateVenueParams (
4950
val city: String,
5051
val spotifyPlaylist: String,
5152
val status: Status,
52-
val statuses: Array<Status>,
53-
val tags: Array<String>
53+
val statuses: List<Status>,
54+
val tags: List<String>
5455
)
5556

5657
const val deleteVenue = """-- name: deleteVenue :exec
@@ -159,9 +160,9 @@ class QueriesImpl(private val conn: Connection) : Queries {
159160
stmt.setString(2, arg.name)
160161
stmt.setString(3, arg.city)
161162
stmt.setString(4, arg.spotifyPlaylist)
162-
stmt.setString(5, arg.status.value)
163+
stmt.setObject(5, arg.status.value, Types.OTHER)
163164
stmt.setArray(6, conn.createArrayOf("status", arg.statuses.map { v -> v.value }.toTypedArray()))
164-
stmt.setArray(7, conn.createArrayOf("text", arg.tags))
165+
stmt.setArray(7, conn.createArrayOf("text", arg.tags.toTypedArray()))
165166

166167
return stmt.executeQuery().use { results ->
167168
if (!results.next()) {
@@ -216,14 +217,14 @@ class QueriesImpl(private val conn: Connection) : Queries {
216217
}
217218
val ret = Venue(
218219
results.getInt(1),
219-
Status.valueOf(results.getString(2)),
220-
(results.getArray(3).array as Array<String>).map { v -> Status.valueOf(v) }.toTypedArray(),
220+
Status.lookup(results.getString(2))!!,
221+
(results.getArray(3).array as Array<String>).map { v -> Status.lookup(v)!! }.toList(),
221222
results.getString(4),
222223
results.getString(5),
223224
results.getString(6),
224225
results.getString(7),
225226
results.getString(8),
226-
results.getArray(9).array as Array<String>,
227+
(results.getArray(9).array as Array<String>).toList(),
227228
results.getObject(10, LocalDateTime::class.java)
228229
)
229230
if (results.next()) {
@@ -259,14 +260,14 @@ class QueriesImpl(private val conn: Connection) : Queries {
259260
while (results.next()) {
260261
ret.add(Venue(
261262
results.getInt(1),
262-
Status.valueOf(results.getString(2)),
263-
(results.getArray(3).array as Array<String>).map { v -> Status.valueOf(v) }.toTypedArray(),
263+
Status.lookup(results.getString(2))!!,
264+
(results.getArray(3).array as Array<String>).map { v -> Status.lookup(v)!! }.toList(),
264265
results.getString(4),
265266
results.getString(5),
266267
results.getString(6),
267268
results.getString(7),
268269
results.getString(8),
269-
results.getArray(9).array as Array<String>,
270+
(results.getArray(9).array as Array<String>).toList(),
270271
results.getObject(10, LocalDateTime::class.java)
271272
))
272273
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE author_id = $1;
4+
5+
-- name: GetBook :one
6+
SELECT * FROM books
7+
WHERE book_id = $1;
8+
9+
-- name: DeleteBook :exec
10+
DELETE FROM books
11+
WHERE book_id = $1;
12+
13+
-- name: BooksByTitleYear :many
14+
SELECT * FROM books
15+
WHERE title = $1 AND year = $2;
16+
17+
-- name: BooksByTags :many
18+
SELECT
19+
book_id,
20+
title,
21+
name,
22+
isbn,
23+
tags
24+
FROM books
25+
LEFT JOIN authors ON books.author_id = authors.author_id
26+
WHERE tags && $1::varchar[];
27+
28+
-- name: CreateAuthor :one
29+
INSERT INTO authors (name) VALUES ($1)
30+
RETURNING *;
31+
32+
-- name: CreateBook :one
33+
INSERT INTO books (
34+
author_id,
35+
isbn,
36+
booktype,
37+
title,
38+
year,
39+
available,
40+
tags
41+
) VALUES (
42+
$1,
43+
$2,
44+
$3,
45+
$4,
46+
$5,
47+
$6,
48+
$7
49+
)
50+
RETURNING *;
51+
52+
-- name: UpdateBook :exec
53+
UPDATE books
54+
SET title = $1, tags = $2
55+
WHERE book_id = $3;
56+
57+
-- name: UpdateBookISBN :exec
58+
UPDATE books
59+
SET title = $1, tags = $2, isbn = $4
60+
WHERE book_id = $3;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
DROP TABLE IF EXISTS books CASCADE;
2+
DROP TYPE IF EXISTS book_type CASCADE;
3+
DROP TABLE IF EXISTS authors CASCADE;
4+
DROP FUNCTION IF EXISTS say_hello(text) CASCADE;
5+
6+
CREATE TABLE authors (
7+
author_id SERIAL PRIMARY KEY,
8+
name text NOT NULL DEFAULT ''
9+
);
10+
11+
CREATE INDEX authors_name_idx ON authors(name);
12+
13+
CREATE TYPE book_type AS ENUM (
14+
'FICTION',
15+
'NONFICTION'
16+
);
17+
18+
CREATE TABLE books (
19+
book_id SERIAL PRIMARY KEY,
20+
author_id integer NOT NULL REFERENCES authors(author_id),
21+
isbn text NOT NULL DEFAULT '' UNIQUE,
22+
booktype book_type NOT NULL DEFAULT 'FICTION',
23+
title text NOT NULL DEFAULT '',
24+
year integer NOT NULL DEFAULT 2000,
25+
available timestamp with time zone NOT NULL DEFAULT 'NOW()',
26+
tags varchar[] NOT NULL DEFAULT '{}'
27+
);
28+
29+
CREATE INDEX books_title_idx ON books(title, year);
30+
31+
CREATE FUNCTION say_hello(text) RETURNS text AS $$
32+
BEGIN
33+
RETURN CONCAT('hello ', $1);
34+
END;
35+
$$ LANGUAGE plpgsql;
36+
37+
CREATE INDEX books_title_lower_idx ON books(title);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- name: ListCities :many
2+
SELECT *
3+
FROM city
4+
ORDER BY name;
5+
6+
-- name: GetCity :one
7+
SELECT *
8+
FROM city
9+
WHERE slug = $1;
10+
11+
-- name: CreateCity :one
12+
-- Create a new city. The slug must be unique.
13+
-- This is the second line of the comment
14+
-- This is the third line
15+
INSERT INTO city (
16+
name,
17+
slug
18+
) VALUES (
19+
$1,
20+
$2
21+
) RETURNING *;
22+
23+
-- name: UpdateCityName :exec
24+
UPDATE city
25+
SET name = $2
26+
WHERE slug = $1;

0 commit comments

Comments
 (0)