Skip to content

Commit f9fd0b6

Browse files
Kotlin mysql (#775)
* Kotlin MySQL support Not much changed really. * Added support for `:execresult` * Add Kotlin MySQL examples and get tests working. * kotlin: support mysql types and adds MySQL tests for all existing examples * kotlin: change :one to rely on null assertions Co-authored-by: Kyle Conroy <kyle@conroy.org>
1 parent ec46941 commit f9fd0b6

Some content is hidden

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

57 files changed

+1531
-465
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: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
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": {
10-
"out": "src/main/kotlin/com/example/authors",
11-
"package": "com.example.authors"
10+
"out": "src/main/kotlin/com/example/authors/postgresql",
11+
"package": "com.example.authors.postgresql"
1212
}
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,39 @@
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+
}
58+
},
59+
{
60+
"schema": "src/main/resources/booktest/mysql/schema.sql",
61+
"queries": "src/main/resources/booktest/mysql/query.sql",
62+
"engine": "mysql:beta",
63+
"gen": {
64+
"kotlin": {
65+
"out": "src/main/kotlin/com/example/booktest/mysql",
66+
"package": "com.example.booktest.mysql"
67+
}
68+
}
69+
},
70+
{
71+
"schema": "src/main/resources/ondeck/mysql/schema",
72+
"queries": "src/main/resources/ondeck/mysql/query",
73+
"engine": "mysql:beta",
74+
"gen": {
75+
"kotlin": {
76+
"out": "src/main/kotlin/com/example/ondeck/mysql",
77+
"package": "com.example.ondeck.mysql"
78+
}
79+
}
4780
}
4881
]
4982
}

examples/kotlin/src/main/kotlin/com/example/authors/Models.kt renamed to examples/kotlin/src/main/kotlin/com/example/authors/mysql/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.authors
3+
package com.example.authors.mysql
44

55
data class Author (
66
val id: Long,

examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt renamed to examples/kotlin/src/main/kotlin/com/example/authors/mysql/Queries.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
// Code generated by sqlc. DO NOT EDIT.
22

3-
package com.example.authors
3+
package com.example.authors.mysql
44

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

89
interface Queries {
910
@Throws(SQLException::class)
10-
fun createAuthor(name: String, bio: String?): Author
11+
fun createAuthor(name: String, bio: String?): Long
1112

1213
@Throws(SQLException::class)
1314
fun deleteAuthor(id: Long)
1415

1516
@Throws(SQLException::class)
16-
fun getAuthor(id: Long): Author
17+
fun getAuthor(id: Long): Author?
1718

1819
@Throws(SQLException::class)
1920
fun listAuthors(): List<Author>
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+
return null
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+
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.postgresql
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.postgresql
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?): Author?
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+

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

Lines changed: 6 additions & 5 deletions
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.authors
3+
package com.example.authors.postgresql
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 (
@@ -32,14 +33,14 @@ ORDER BY name
3233
class QueriesImpl(private val conn: Connection) : Queries {
3334

3435
@Throws(SQLException::class)
35-
override fun createAuthor(name: String, bio: String?): Author {
36+
override fun createAuthor(name: String, bio: String?): Author? {
3637
return conn.prepareStatement(createAuthor).use { stmt ->
3738
stmt.setString(1, name)
3839
stmt.setString(2, bio)
3940

4041
val results = stmt.executeQuery()
4142
if (!results.next()) {
42-
throw SQLException("no rows in result set")
43+
return null
4344
}
4445
val ret = Author(
4546
results.getLong(1),
@@ -63,13 +64,13 @@ class QueriesImpl(private val conn: Connection) : Queries {
6364
}
6465

6566
@Throws(SQLException::class)
66-
override fun getAuthor(id: Long): Author {
67+
override fun getAuthor(id: Long): Author? {
6768
return conn.prepareStatement(getAuthor).use { stmt ->
6869
stmt.setLong(1, id)
6970

7071
val results = stmt.executeQuery()
7172
if (!results.next()) {
72-
throw SQLException("no rows in result set")
73+
return null
7374
}
7475
val ret = Author(
7576
results.getLong(1),
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Code generated by sqlc. DO NOT EDIT.
2+
3+
package com.example.booktest.mysql
4+
5+
import java.time.LocalDateTime
6+
7+
enum class BooksBookType(val value: String) {
8+
FICTION("FICTION"),
9+
NONFICTION("NONFICTION");
10+
11+
companion object {
12+
private val map = BooksBookType.values().associateBy(BooksBookType::value)
13+
fun lookup(value: String) = map[value]
14+
}
15+
}
16+
17+
data class Author (
18+
val authorId: Int,
19+
val name: String
20+
)
21+
22+
data class Book (
23+
val bookId: Int,
24+
val authorId: Int,
25+
val isbn: String,
26+
val bookType: BooksBookType,
27+
val title: String,
28+
val yr: Int,
29+
val available: LocalDateTime,
30+
val tags: String
31+
)
32+

0 commit comments

Comments
 (0)