Skip to content

Kotlin mysql #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci-kotlin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
mysql:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: mysecretpassword
MYSQL_DATABASE: mysql
ports:
- 3306:3306

steps:
- uses: actions/checkout@v2
Expand All @@ -30,6 +37,10 @@ jobs:
PG_DATABASE: postgres
PG_PASSWORD: postgres
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
MYSQL_DATABASE: mysql
MYSQL_HOST: localhost
MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
MYSQL_ROOT_PASSWORD: mysecretpassword
with:
build-root-directory: examples/kotlin
wrapper-directory: examples/kotlin
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ jobs:
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
MYSQL_DATABASE: mysql
MYSQL_HOST: localhost
MYSQL_PORT: ${{ job.services.mysql.ports['5432'] }}
MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably never worked? It probably interpolated into the empty string, and the tests used the default port which is 3306

MYSQL_ROOT_PASSWORD: mysecretpassword

1 change: 1 addition & 0 deletions examples/kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {
}

dependencies {
implementation 'mysql:mysql-connector-java:8.0.22'
implementation 'org.postgresql:postgresql:42.2.9'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
Expand Down
49 changes: 41 additions & 8 deletions examples/kotlin/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
"version": "2",
"sql": [
{
"schema": "src/main/resources/authors/schema.sql",
"queries": "src/main/resources/authors/query.sql",
"schema": "src/main/resources/authors/postgresql/schema.sql",
"queries": "src/main/resources/authors/postgresql/query.sql",
"engine": "postgresql",
"gen": {
"kotlin": {
"out": "src/main/kotlin/com/example/authors",
"package": "com.example.authors"
"out": "src/main/kotlin/com/example/authors/postgresql",
"package": "com.example.authors.postgresql"
}
}
},
{
"schema": "src/main/resources/ondeck/schema",
"queries": "src/main/resources/ondeck/query",
"schema": "src/main/resources/ondeck/postgresql/schema",
"queries": "src/main/resources/ondeck/postgresql/query",
"engine": "postgresql",
"gen": {
"kotlin": {
"out": "src/main/kotlin/com/example/ondeck",
"package": "com.example.ondeck"
"out": "src/main/kotlin/com/example/ondeck/postgresql",
"package": "com.example.ondeck.postgresql"
}
}
},
Expand All @@ -44,6 +44,39 @@
"package": "com.example.booktest.postgresql"
}
}
},
{
"schema": "src/main/resources/authors/mysql/schema.sql",
"queries": "src/main/resources/authors/mysql/query.sql",
"engine": "mysql:beta",
"gen": {
"kotlin": {
"out": "src/main/kotlin/com/example/authors/mysql",
"package": "com.example.authors.mysql"
}
}
},
{
"schema": "src/main/resources/booktest/mysql/schema.sql",
"queries": "src/main/resources/booktest/mysql/query.sql",
"engine": "mysql:beta",
"gen": {
"kotlin": {
"out": "src/main/kotlin/com/example/booktest/mysql",
"package": "com.example.booktest.mysql"
}
}
},
{
"schema": "src/main/resources/ondeck/mysql/schema",
"queries": "src/main/resources/ondeck/mysql/query",
"engine": "mysql:beta",
"gen": {
"kotlin": {
"out": "src/main/kotlin/com/example/ondeck/mysql",
"package": "com.example.ondeck.mysql"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.

package com.example.authors
package com.example.authors.mysql

data class Author (
val id: Long,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// Code generated by sqlc. DO NOT EDIT.

package com.example.authors
package com.example.authors.mysql

import java.sql.Connection
import java.sql.SQLException
import java.sql.Statement

interface Queries {
@Throws(SQLException::class)
fun createAuthor(name: String, bio: String?): Author
fun createAuthor(name: String, bio: String?): Long

@Throws(SQLException::class)
fun deleteAuthor(id: Long)

@Throws(SQLException::class)
fun getAuthor(id: Long): Author
fun getAuthor(id: Long): Author?

@Throws(SQLException::class)
fun listAuthors(): List<Author>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Code generated by sqlc. DO NOT EDIT.

package com.example.authors.mysql

import java.sql.Connection
import java.sql.SQLException
import java.sql.Statement

const val createAuthor = """-- name: createAuthor :execresult
INSERT INTO authors (
name, bio
) VALUES (
?, ?
)
"""

const val deleteAuthor = """-- name: deleteAuthor :exec
DELETE FROM authors
WHERE id = ?
"""

const val getAuthor = """-- name: getAuthor :one
SELECT id, name, bio FROM authors
WHERE id = ? LIMIT 1
"""

const val listAuthors = """-- name: listAuthors :many
SELECT id, name, bio FROM authors
ORDER BY name
"""

class QueriesImpl(private val conn: Connection) : Queries {

@Throws(SQLException::class)
override fun createAuthor(name: String, bio: String?): Long {
return conn.prepareStatement(createAuthor, Statement.RETURN_GENERATED_KEYS).use { stmt ->
stmt.setString(1, name)
stmt.setString(2, bio)

stmt.execute()

val results = stmt.generatedKeys
if (!results.next()) {
throw SQLException("no generated key returned")
}
results.getLong(1)
}
}

@Throws(SQLException::class)
override fun deleteAuthor(id: Long) {
conn.prepareStatement(deleteAuthor).use { stmt ->
stmt.setLong(1, id)

stmt.execute()
}
}

@Throws(SQLException::class)
override fun getAuthor(id: Long): Author? {
return conn.prepareStatement(getAuthor).use { stmt ->
stmt.setLong(1, id)

val results = stmt.executeQuery()
if (!results.next()) {
return null
}
val ret = Author(
results.getLong(1),
results.getString(2),
results.getString(3)
)
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}

@Throws(SQLException::class)
override fun listAuthors(): List<Author> {
return conn.prepareStatement(listAuthors).use { stmt ->

val results = stmt.executeQuery()
val ret = mutableListOf<Author>()
while (results.next()) {
ret.add(Author(
results.getLong(1),
results.getString(2),
results.getString(3)
))
}
ret
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Code generated by sqlc. DO NOT EDIT.

package com.example.authors.postgresql

data class Author (
val id: Long,
val name: String,
val bio: String?
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Code generated by sqlc. DO NOT EDIT.

package com.example.authors.postgresql

import java.sql.Connection
import java.sql.SQLException
import java.sql.Statement

interface Queries {
@Throws(SQLException::class)
fun createAuthor(name: String, bio: String?): Author?

@Throws(SQLException::class)
fun deleteAuthor(id: Long)

@Throws(SQLException::class)
fun getAuthor(id: Long): Author?

@Throws(SQLException::class)
fun listAuthors(): List<Author>

}

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Code generated by sqlc. DO NOT EDIT.

package com.example.authors
package com.example.authors.postgresql

import java.sql.Connection
import java.sql.SQLException
import java.sql.Statement

const val createAuthor = """-- name: createAuthor :one
INSERT INTO authors (
Expand Down Expand Up @@ -32,14 +33,14 @@ ORDER BY name
class QueriesImpl(private val conn: Connection) : Queries {

@Throws(SQLException::class)
override fun createAuthor(name: String, bio: String?): Author {
override fun createAuthor(name: String, bio: String?): Author? {
return conn.prepareStatement(createAuthor).use { stmt ->
stmt.setString(1, name)
stmt.setString(2, bio)

val results = stmt.executeQuery()
if (!results.next()) {
throw SQLException("no rows in result set")
return null
}
val ret = Author(
results.getLong(1),
Expand All @@ -63,13 +64,13 @@ class QueriesImpl(private val conn: Connection) : Queries {
}

@Throws(SQLException::class)
override fun getAuthor(id: Long): Author {
override fun getAuthor(id: Long): Author? {
return conn.prepareStatement(getAuthor).use { stmt ->
stmt.setLong(1, id)

val results = stmt.executeQuery()
if (!results.next()) {
throw SQLException("no rows in result set")
return null
}
val ret = Author(
results.getLong(1),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.

package com.example.booktest.mysql

import java.time.LocalDateTime

enum class BooksBookType(val value: String) {
FICTION("FICTION"),
NONFICTION("NONFICTION");

companion object {
private val map = BooksBookType.values().associateBy(BooksBookType::value)
fun lookup(value: String) = map[value]
}
}

data class Author (
val authorId: Int,
val name: String
)

data class Book (
val bookId: Int,
val authorId: Int,
val isbn: String,
val bookType: BooksBookType,
val title: String,
val yr: Int,
val available: LocalDateTime,
val tags: String
)

Loading