Skip to content

kotlin: remove runtime dependency #774

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 1 commit 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
12 changes: 4 additions & 8 deletions examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ 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?): RowQuery<Author>
fun createAuthor(name: String, bio: String?): Author

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

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

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

}

96 changes: 36 additions & 60 deletions examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ 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 @@ -36,91 +32,71 @@ ORDER BY name
class QueriesImpl(private val conn: Connection) : Queries {
Copy link

@tirsen tirsen Nov 10, 2020

Choose a reason for hiding this comment

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

Personally I try to avoid the Impl suffix because it doesn't say anything about the implementation which feels like a missed opportunity. Maybe GeneratedQueries?

(I assume this is generated right?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But it’s the Java way! I agree. But since naming didn’t change in this PR I’d like to leave it for a follow up if you feel strongly? Also all of this code is generated. Maybe RealQueries which isn’t anymore meaningful but seems like the Kotlin way.

I think you’ve been corrupted by Go

Copy link

Choose a reason for hiding this comment

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

The Impl suffix is afaict never used in the JDK so while it's common in some Java projects it's not really a "sanctioned" convention. :-) I don't care about it strongly but something to think about.


@Throws(SQLException::class)
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)
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(
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
}
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}

@Throws(SQLException::class)
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()
}
}
override fun deleteAuthor(id: Long) {
conn.prepareStatement(deleteAuthor).use { stmt ->
stmt.setLong(1, id)

stmt.execute()
}
}

@Throws(SQLException::class)
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(
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")
}
ret
}
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}

@Throws(SQLException::class)
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(
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
}
}
ret
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@ 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>): ListQuery<BooksByTagsRow>
fun booksByTags(dollar1: List<String>): List<BooksByTagsRow>

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

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

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

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

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

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

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

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

}

Loading