Skip to content

kotlin: add Query class to support timeout and cancellation #368

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 2 commits into from
Mar 1, 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
1 change: 1 addition & 0 deletions examples/kotlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This is a Kotlin gradle project configured to compile and test all examples. Cur
To run tests:

```shell script
docker run --name dinosql-postgres -d -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_DB=postgres -p 5432:5432 postgres:11
./gradlew clean test
```

Expand Down
4 changes: 2 additions & 2 deletions examples/kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ repositories {
dependencies {
implementation 'org.postgresql:postgresql:42.2.9'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
}

test {
Expand Down
2 changes: 1 addition & 1 deletion examples/kotlin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Sat Jan 25 10:45:34 EST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
12 changes: 8 additions & 4 deletions examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ package com.example.authors
import java.sql.Connection
import java.sql.SQLException

import sqlc.runtime.ExecuteQuery
import sqlc.runtime.ListQuery
import sqlc.runtime.RowQuery

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

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

@Throws(SQLException::class)
fun getAuthor(id: Long): Author
fun getAuthor(id: Long): RowQuery<Author>

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

}

124 changes: 74 additions & 50 deletions examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package com.example.authors
import java.sql.Connection
import java.sql.SQLException

import sqlc.runtime.ExecuteQuery
import sqlc.runtime.ListQuery
import sqlc.runtime.RowQuery

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

@Throws(SQLException::class)
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")
}
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")
override fun createAuthor(name: String, bio: String?): RowQuery<Author> {
return object : RowQuery<Author>() {
override fun execute(): Author {
return conn.prepareStatement(createAuthor).use { stmt ->
this.statement = stmt
stmt.setString(1, name)
stmt.setString(2, bio)

val results = stmt.executeQuery()
if (!results.next()) {
throw SQLException("no rows in result set")
}
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
}
}
ret
}
}

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

stmt.execute()
override fun deleteAuthor(id: Long): ExecuteQuery {
return object : ExecuteQuery() {
override fun execute() {
conn.prepareStatement(deleteAuthor).use { stmt ->
this.statement = 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()) {
throw SQLException("no rows in result set")
}
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")
override fun getAuthor(id: Long): RowQuery<Author> {
return object : RowQuery<Author>() {
override fun execute(): Author {
return conn.prepareStatement(getAuthor).use { stmt ->
this.statement = stmt
stmt.setLong(1, id)

val results = stmt.executeQuery()
if (!results.next()) {
throw SQLException("no rows in result set")
}
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
}
}
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)
))
override fun listAuthors(): ListQuery<Author> {
return object : ListQuery<Author>() {
override fun execute(): List<Author> {
return conn.prepareStatement(listAuthors).use { stmt ->
this.statement = 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
}
}
ret
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import java.sql.SQLException
import java.sql.Types
import java.time.OffsetDateTime

import sqlc.runtime.ExecuteQuery
import sqlc.runtime.ListQuery
import sqlc.runtime.RowQuery

interface Queries {
@Throws(SQLException::class)
fun booksByTags(dollar1: List<String>): List<BooksByTagsRow>
fun booksByTags(dollar1: List<String>): ListQuery<BooksByTagsRow>

@Throws(SQLException::class)
fun booksByTitleYear(title: String, year: Int): List<Book>
fun booksByTitleYear(title: String, year: Int): ListQuery<Book>

@Throws(SQLException::class)
fun createAuthor(name: String): Author
fun createAuthor(name: String): RowQuery<Author>

@Throws(SQLException::class)
fun createBook(
Expand All @@ -25,29 +29,29 @@ interface Queries {
title: String,
year: Int,
available: OffsetDateTime,
tags: List<String>): Book
tags: List<String>): RowQuery<Book>

@Throws(SQLException::class)
fun deleteBook(bookId: Int)
fun deleteBook(bookId: Int): ExecuteQuery

@Throws(SQLException::class)
fun getAuthor(authorId: Int): Author
fun getAuthor(authorId: Int): RowQuery<Author>

@Throws(SQLException::class)
fun getBook(bookId: Int): Book
fun getBook(bookId: Int): RowQuery<Book>

@Throws(SQLException::class)
fun updateBook(
title: String,
tags: List<String>,
bookId: Int)
bookId: Int): ExecuteQuery

@Throws(SQLException::class)
fun updateBookISBN(
title: String,
tags: List<String>,
isbn: String,
bookId: Int)
bookId: Int): ExecuteQuery

}

Loading