Skip to content

Commit b02a053

Browse files
committed
Kotlin MySQL support
Not much changed really. * Added support for `:execresult` * Add Kotlin MySQL examples and get tests working.
1 parent cd3919a commit b02a053

File tree

42 files changed

+353
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+353
-307
lines changed

.github/workflows/ci-kotlin.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ jobs:
1717
- 5432:5432
1818
# needed because the postgres container does not provide a healthcheck
1919
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
20+
mysql:
21+
image: mysql:8
22+
env:
23+
MYSQL_ROOT_PASSWORD: mysecretpassword
24+
MYSQL_DATABASE: mysql
25+
ports:
26+
- 3306:3306
2027

2128
steps:
2229
- uses: actions/checkout@v2
@@ -30,6 +37,10 @@ jobs:
3037
PG_DATABASE: postgres
3138
PG_PASSWORD: postgres
3239
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
40+
MYSQL_DATABASE: mysql
41+
MYSQL_HOST: localhost
42+
MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
43+
MYSQL_ROOT_PASSWORD: mysecretpassword
3344
with:
3445
build-root-directory: examples/kotlin
3546
wrapper-directory: examples/kotlin

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ jobs:
4646
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
4747
MYSQL_DATABASE: mysql
4848
MYSQL_HOST: localhost
49-
MYSQL_PORT: ${{ job.services.mysql.ports['5432'] }}
49+
MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
5050
MYSQL_ROOT_PASSWORD: mysecretpassword
5151

examples/kotlin/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repositories {
1010
}
1111

1212
dependencies {
13+
implementation 'mysql:mysql-connector-java:8.0.22'
1314
implementation 'org.postgresql:postgresql:42.2.9'
1415
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
1516
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'

examples/kotlin/sqlc.json

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"version": "2",
33
"sql": [
44
{
5-
"schema": "src/main/resources/authors/schema.sql",
6-
"queries": "src/main/resources/authors/query.sql",
5+
"schema": "src/main/resources/authors/postgresql/schema.sql",
6+
"queries": "src/main/resources/authors/postgresql/query.sql",
77
"engine": "postgresql",
88
"gen": {
99
"kotlin": {
@@ -13,13 +13,13 @@
1313
}
1414
},
1515
{
16-
"schema": "src/main/resources/ondeck/schema",
17-
"queries": "src/main/resources/ondeck/query",
16+
"schema": "src/main/resources/ondeck/postgresql/schema",
17+
"queries": "src/main/resources/ondeck/postgresql/query",
1818
"engine": "postgresql",
1919
"gen": {
2020
"kotlin": {
21-
"out": "src/main/kotlin/com/example/ondeck",
22-
"package": "com.example.ondeck"
21+
"out": "src/main/kotlin/com/example/ondeck/postgresql",
22+
"package": "com.example.ondeck.postgresql"
2323
}
2424
}
2525
},
@@ -44,6 +44,17 @@
4444
"package": "com.example.booktest.postgresql"
4545
}
4646
}
47+
},
48+
{
49+
"schema": "src/main/resources/authors/mysql/schema.sql",
50+
"queries": "src/main/resources/authors/mysql/query.sql",
51+
"engine": "mysql:beta",
52+
"gen": {
53+
"kotlin": {
54+
"out": "src/main/kotlin/com/example/authors/mysql",
55+
"package": "com.example.authors.mysql"
56+
}
57+
}
4758
}
4859
]
4960
}

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

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

55
import java.sql.Connection
66
import java.sql.SQLException
7+
import java.sql.Statement
78

89
interface Queries {
910
@Throws(SQLException::class)

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

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

55
import java.sql.Connection
66
import java.sql.SQLException
7+
import java.sql.Statement
78

89
const val createAuthor = """-- name: createAuthor :one
910
INSERT INTO authors (
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Code generated by sqlc. DO NOT EDIT.
2+
3+
package com.example.authors.mysql
4+
5+
data class Author (
6+
val id: Long,
7+
val name: String,
8+
val bio: String?
9+
)
10+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Code generated by sqlc. DO NOT EDIT.
2+
3+
package com.example.authors.mysql
4+
5+
import java.sql.Connection
6+
import java.sql.SQLException
7+
import java.sql.Statement
8+
9+
interface Queries {
10+
@Throws(SQLException::class)
11+
fun createAuthor(name: String, bio: String?): Long
12+
13+
@Throws(SQLException::class)
14+
fun deleteAuthor(id: Long)
15+
16+
@Throws(SQLException::class)
17+
fun getAuthor(id: Long): Author
18+
19+
@Throws(SQLException::class)
20+
fun listAuthors(): List<Author>
21+
22+
}
23+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Code generated by sqlc. DO NOT EDIT.
2+
3+
package com.example.authors.mysql
4+
5+
import java.sql.Connection
6+
import java.sql.SQLException
7+
import java.sql.Statement
8+
9+
const val createAuthor = """-- name: createAuthor :execresult
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
?, ?
14+
)
15+
"""
16+
17+
const val deleteAuthor = """-- name: deleteAuthor :exec
18+
DELETE FROM authors
19+
WHERE id = ?
20+
"""
21+
22+
const val getAuthor = """-- name: getAuthor :one
23+
SELECT id, name, bio FROM authors
24+
WHERE id = ? LIMIT 1
25+
"""
26+
27+
const val listAuthors = """-- name: listAuthors :many
28+
SELECT id, name, bio FROM authors
29+
ORDER BY name
30+
"""
31+
32+
class QueriesImpl(private val conn: Connection) : Queries {
33+
34+
@Throws(SQLException::class)
35+
override fun createAuthor(name: String, bio: String?): Long {
36+
return conn.prepareStatement(createAuthor, Statement.RETURN_GENERATED_KEYS).use { stmt ->
37+
stmt.setString(1, name)
38+
stmt.setString(2, bio)
39+
40+
stmt.execute()
41+
42+
val results = stmt.generatedKeys
43+
if (!results.next()) {
44+
throw SQLException("no generated key returned")
45+
}
46+
results.getLong(1)
47+
}
48+
}
49+
50+
@Throws(SQLException::class)
51+
override fun deleteAuthor(id: Long) {
52+
conn.prepareStatement(deleteAuthor).use { stmt ->
53+
stmt.setLong(1, id)
54+
55+
stmt.execute()
56+
}
57+
}
58+
59+
@Throws(SQLException::class)
60+
override fun getAuthor(id: Long): Author {
61+
return conn.prepareStatement(getAuthor).use { stmt ->
62+
stmt.setLong(1, id)
63+
64+
val results = stmt.executeQuery()
65+
if (!results.next()) {
66+
throw SQLException("no rows in result set")
67+
}
68+
val ret = Author(
69+
results.getLong(1),
70+
results.getString(2),
71+
results.getString(3)
72+
)
73+
if (results.next()) {
74+
throw SQLException("expected one row in result set, but got many")
75+
}
76+
ret
77+
}
78+
}
79+
80+
@Throws(SQLException::class)
81+
override fun listAuthors(): List<Author> {
82+
return conn.prepareStatement(listAuthors).use { stmt ->
83+
84+
val results = stmt.executeQuery()
85+
val ret = mutableListOf<Author>()
86+
while (results.next()) {
87+
ret.add(Author(
88+
results.getLong(1),
89+
results.getString(2),
90+
results.getString(3)
91+
))
92+
}
93+
ret
94+
}
95+
}
96+
97+
}
98+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ data class Book (
2323
val bookId: Int,
2424
val authorId: Int,
2525
val isbn: String,
26-
val booktype: BookType,
26+
val bookType: BookType,
2727
val title: String,
2828
val year: Int,
2929
val available: OffsetDateTime,

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

Lines changed: 2 additions & 1 deletion
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.Statement
78
import java.sql.Types
89
import java.time.OffsetDateTime
910

@@ -21,7 +22,7 @@ interface Queries {
2122
fun createBook(
2223
authorId: Int,
2324
isbn: String,
24-
booktype: BookType,
25+
bookType: BookType,
2526
title: String,
2627
year: Int,
2728
available: OffsetDateTime,

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

Lines changed: 7 additions & 6 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.Statement
78
import java.sql.Types
89
import java.time.OffsetDateTime
910

@@ -28,7 +29,7 @@ data class BooksByTagsRow (
2829
)
2930

3031
const val booksByTitleYear = """-- name: booksByTitleYear :many
31-
SELECT book_id, author_id, isbn, booktype, title, year, available, tags FROM books
32+
SELECT book_id, author_id, isbn, book_type, title, year, available, tags FROM books
3233
WHERE title = ? AND year = ?
3334
"""
3435

@@ -41,7 +42,7 @@ const val createBook = """-- name: createBook :one
4142
INSERT INTO books (
4243
author_id,
4344
isbn,
44-
booktype,
45+
book_type,
4546
title,
4647
year,
4748
available,
@@ -55,7 +56,7 @@ INSERT INTO books (
5556
?,
5657
?
5758
)
58-
RETURNING book_id, author_id, isbn, booktype, title, year, available, tags
59+
RETURNING book_id, author_id, isbn, book_type, title, year, available, tags
5960
"""
6061

6162
const val deleteBook = """-- name: deleteBook :exec
@@ -69,7 +70,7 @@ WHERE author_id = ?
6970
"""
7071

7172
const val getBook = """-- name: getBook :one
72-
SELECT book_id, author_id, isbn, booktype, title, year, available, tags FROM books
73+
SELECT book_id, author_id, isbn, book_type, title, year, available, tags FROM books
7374
WHERE book_id = ?
7475
"""
7576

@@ -155,15 +156,15 @@ class QueriesImpl(private val conn: Connection) : Queries {
155156
override fun createBook(
156157
authorId: Int,
157158
isbn: String,
158-
booktype: BookType,
159+
bookType: BookType,
159160
title: String,
160161
year: Int,
161162
available: OffsetDateTime,
162163
tags: List<String>): Book {
163164
return conn.prepareStatement(createBook).use { stmt ->
164165
stmt.setInt(1, authorId)
165166
stmt.setString(2, isbn)
166-
stmt.setObject(3, booktype.value, Types.OTHER)
167+
stmt.setObject(3, bookType.value, Types.OTHER)
167168
stmt.setString(4, title)
168169
stmt.setInt(5, year)
169170
stmt.setObject(6, available)

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

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

55
import java.sql.Connection
66
import java.sql.SQLException
7+
import java.sql.Statement
78

89
interface Queries {
910
@Throws(SQLException::class)

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

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

55
import java.sql.Connection
66
import java.sql.SQLException
7+
import java.sql.Statement
78

89
const val countPilots = """-- name: countPilots :one
910
SELECT COUNT(*) FROM pilots

examples/kotlin/src/main/kotlin/com/example/ondeck/Models.kt renamed to examples/kotlin/src/main/kotlin/com/example/ondeck/postgresql/Models.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Code generated by sqlc. DO NOT EDIT.
22

3-
package com.example.ondeck
3+
package com.example.ondeck.postgresql
44

55
import java.time.LocalDateTime
66

examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt renamed to examples/kotlin/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Code generated by sqlc. DO NOT EDIT.
22

3-
package com.example.ondeck
3+
package com.example.ondeck.postgresql
44

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

examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt renamed to examples/kotlin/src/main/kotlin/com/example/ondeck/postgresql/QueriesImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Code generated by sqlc. DO NOT EDIT.
22

3-
package com.example.ondeck
3+
package com.example.ondeck.postgresql
44

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../authors

examples/kotlin/src/main/resources/authors/query.sql

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)