diff --git a/examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt b/examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt index 0f40b51622..4dde3eec1f 100644 --- a/examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt +++ b/examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt @@ -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 + 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 + fun getAuthor(id: Long): Author @Throws(SQLException::class) - fun listAuthors(): ListQuery + fun listAuthors(): List } diff --git a/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt b/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt index 97900fc754..e005d04204 100644 --- a/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt +++ b/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt @@ -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 @@ -36,91 +32,71 @@ ORDER BY name class QueriesImpl(private val conn: Connection) : Queries { @Throws(SQLException::class) - override fun createAuthor(name: String, bio: String?): RowQuery { - return object : RowQuery() { - 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 { - return object : RowQuery() { - 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 { - return object : ListQuery() { - override fun execute(): List { - return conn.prepareStatement(listAuthors).use { stmt -> - this.statement = stmt - - val results = stmt.executeQuery() - val ret = mutableListOf() - while (results.next()) { - ret.add(Author( + override fun listAuthors(): List { + return conn.prepareStatement(listAuthors).use { stmt -> + + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(Author( results.getLong(1), results.getString(2), results.getString(3) )) - } - ret - } } + ret } } diff --git a/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/Queries.kt b/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/Queries.kt index 19a9c71b73..addf28a721 100644 --- a/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/Queries.kt +++ b/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/Queries.kt @@ -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): ListQuery + fun booksByTags(dollar1: List): List @Throws(SQLException::class) - fun booksByTitleYear(title: String, year: Int): ListQuery + fun booksByTitleYear(title: String, year: Int): List @Throws(SQLException::class) - fun createAuthor(name: String): RowQuery + fun createAuthor(name: String): Author @Throws(SQLException::class) fun createBook( @@ -29,29 +25,29 @@ interface Queries { title: String, year: Int, available: OffsetDateTime, - tags: List): RowQuery + tags: List): Book @Throws(SQLException::class) - fun deleteBook(bookId: Int): ExecuteQuery + fun deleteBook(bookId: Int) @Throws(SQLException::class) - fun getAuthor(authorId: Int): RowQuery + fun getAuthor(authorId: Int): Author @Throws(SQLException::class) - fun getBook(bookId: Int): RowQuery + fun getBook(bookId: Int): Book @Throws(SQLException::class) fun updateBook( title: String, tags: List, - bookId: Int): ExecuteQuery + bookId: Int) @Throws(SQLException::class) fun updateBookISBN( title: String, tags: List, isbn: String, - bookId: Int): ExecuteQuery + bookId: Int) } diff --git a/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt b/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt index 5f9f7d70c0..2c8fd1ed91 100644 --- a/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt +++ b/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt @@ -7,10 +7,6 @@ import java.sql.SQLException import java.sql.Types import java.time.OffsetDateTime -import sqlc.runtime.ExecuteQuery -import sqlc.runtime.ListQuery -import sqlc.runtime.RowQuery - const val booksByTags = """-- name: booksByTags :many SELECT book_id, @@ -92,43 +88,35 @@ WHERE book_id = ? class QueriesImpl(private val conn: Connection) : Queries { @Throws(SQLException::class) - override fun booksByTags(dollar1: List): ListQuery { - return object : ListQuery() { - override fun execute(): List { - return conn.prepareStatement(booksByTags).use { stmt -> - this.statement = stmt - stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar1.toTypedArray())) - - val results = stmt.executeQuery() - val ret = mutableListOf() - while (results.next()) { - ret.add(BooksByTagsRow( + override fun booksByTags(dollar1: List): List { + return conn.prepareStatement(booksByTags).use { stmt -> + stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar1.toTypedArray())) + + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(BooksByTagsRow( results.getInt(1), results.getString(2), results.getString(3), results.getString(4), (results.getArray(5).array as Array).toList() )) - } - ret - } } + ret } } @Throws(SQLException::class) - override fun booksByTitleYear(title: String, year: Int): ListQuery { - return object : ListQuery() { - override fun execute(): List { - return conn.prepareStatement(booksByTitleYear).use { stmt -> - this.statement = stmt - stmt.setString(1, title) + override fun booksByTitleYear(title: String, year: Int): List { + return conn.prepareStatement(booksByTitleYear).use { stmt -> + stmt.setString(1, title) stmt.setInt(2, year) - val results = stmt.executeQuery() - val ret = mutableListOf() - while (results.next()) { - ret.add(Book( + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(Book( results.getInt(1), results.getInt(2), results.getString(3), @@ -138,35 +126,28 @@ class QueriesImpl(private val conn: Connection) : Queries { results.getObject(7, OffsetDateTime::class.java), (results.getArray(8).array as Array).toList() )) - } - ret - } } + ret } } @Throws(SQLException::class) - override fun createAuthor(name: String): RowQuery { - return object : RowQuery() { - override fun execute(): Author { - return conn.prepareStatement(createAuthor).use { stmt -> - this.statement = stmt - stmt.setString(1, name) - - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = Author( + override fun createAuthor(name: String): Author { + return conn.prepareStatement(createAuthor).use { stmt -> + stmt.setString(1, name) + + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = Author( results.getInt(1), results.getString(2) ) - 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 } } @@ -178,12 +159,9 @@ class QueriesImpl(private val conn: Connection) : Queries { title: String, year: Int, available: OffsetDateTime, - tags: List): RowQuery { - return object : RowQuery() { - override fun execute(): Book { - return conn.prepareStatement(createBook).use { stmt -> - this.statement = stmt - stmt.setInt(1, authorId) + tags: List): Book { + return conn.prepareStatement(createBook).use { stmt -> + stmt.setInt(1, authorId) stmt.setString(2, isbn) stmt.setObject(3, booktype.value, Types.OTHER) stmt.setString(4, title) @@ -191,11 +169,11 @@ class QueriesImpl(private val conn: Connection) : Queries { stmt.setObject(6, available) stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray())) - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = Book( + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = Book( results.getInt(1), results.getInt(2), results.getString(3), @@ -205,67 +183,52 @@ class QueriesImpl(private val conn: Connection) : Queries { results.getObject(7, OffsetDateTime::class.java), (results.getArray(8).array as Array).toList() ) - 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 deleteBook(bookId: Int): ExecuteQuery { - return object : ExecuteQuery() { - override fun execute() { - conn.prepareStatement(deleteBook).use { stmt -> - this.statement = stmt - stmt.setInt(1, bookId) - - stmt.execute() - } - } + override fun deleteBook(bookId: Int) { + conn.prepareStatement(deleteBook).use { stmt -> + stmt.setInt(1, bookId) + + stmt.execute() } } @Throws(SQLException::class) - override fun getAuthor(authorId: Int): RowQuery { - return object : RowQuery() { - override fun execute(): Author { - return conn.prepareStatement(getAuthor).use { stmt -> - this.statement = stmt - stmt.setInt(1, authorId) - - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = Author( + override fun getAuthor(authorId: Int): Author { + return conn.prepareStatement(getAuthor).use { stmt -> + stmt.setInt(1, authorId) + + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = Author( results.getInt(1), results.getString(2) ) - 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 getBook(bookId: Int): RowQuery { - return object : RowQuery() { - override fun execute(): Book { - return conn.prepareStatement(getBook).use { stmt -> - this.statement = stmt - stmt.setInt(1, bookId) - - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = Book( + override fun getBook(bookId: Int): Book { + return conn.prepareStatement(getBook).use { stmt -> + stmt.setInt(1, bookId) + + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = Book( results.getInt(1), results.getInt(2), results.getString(3), @@ -275,12 +238,10 @@ class QueriesImpl(private val conn: Connection) : Queries { results.getObject(7, OffsetDateTime::class.java), (results.getArray(8).array as Array).toList() ) - 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 } } @@ -288,18 +249,13 @@ class QueriesImpl(private val conn: Connection) : Queries { override fun updateBook( title: String, tags: List, - bookId: Int): ExecuteQuery { - return object : ExecuteQuery() { - override fun execute() { - conn.prepareStatement(updateBook).use { stmt -> - this.statement = stmt - stmt.setString(1, title) + bookId: Int) { + conn.prepareStatement(updateBook).use { stmt -> + stmt.setString(1, title) stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray())) stmt.setInt(3, bookId) - stmt.execute() - } - } + stmt.execute() } } @@ -308,19 +264,14 @@ class QueriesImpl(private val conn: Connection) : Queries { title: String, tags: List, isbn: String, - bookId: Int): ExecuteQuery { - return object : ExecuteQuery() { - override fun execute() { - conn.prepareStatement(updateBookISBN).use { stmt -> - this.statement = stmt - stmt.setString(1, title) + bookId: Int) { + conn.prepareStatement(updateBookISBN).use { stmt -> + stmt.setString(1, title) stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray())) stmt.setString(3, isbn) stmt.setInt(4, bookId) - stmt.execute() - } - } + stmt.execute() } } diff --git a/examples/kotlin/src/main/kotlin/com/example/jets/Queries.kt b/examples/kotlin/src/main/kotlin/com/example/jets/Queries.kt index 939d471db9..7437a6f2b0 100644 --- a/examples/kotlin/src/main/kotlin/com/example/jets/Queries.kt +++ b/examples/kotlin/src/main/kotlin/com/example/jets/Queries.kt @@ -5,19 +5,15 @@ package com.example.jets 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 countPilots(): RowQuery + fun countPilots(): Long @Throws(SQLException::class) - fun deletePilot(id: Int): ExecuteQuery + fun deletePilot(id: Int) @Throws(SQLException::class) - fun listPilots(): ListQuery + fun listPilots(): List } diff --git a/examples/kotlin/src/main/kotlin/com/example/jets/QueriesImpl.kt b/examples/kotlin/src/main/kotlin/com/example/jets/QueriesImpl.kt index 1bd9d55132..6155469a7d 100644 --- a/examples/kotlin/src/main/kotlin/com/example/jets/QueriesImpl.kt +++ b/examples/kotlin/src/main/kotlin/com/example/jets/QueriesImpl.kt @@ -5,10 +5,6 @@ package com.example.jets import java.sql.Connection import java.sql.SQLException -import sqlc.runtime.ExecuteQuery -import sqlc.runtime.ListQuery -import sqlc.runtime.RowQuery - const val countPilots = """-- name: countPilots :one SELECT COUNT(*) FROM pilots """ @@ -24,58 +20,43 @@ SELECT id, name FROM pilots LIMIT 5 class QueriesImpl(private val conn: Connection) : Queries { @Throws(SQLException::class) - override fun countPilots(): RowQuery { - return object : RowQuery() { - override fun execute(): Long { - return conn.prepareStatement(countPilots).use { stmt -> - this.statement = stmt - - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = results.getLong(1) - if (results.next()) { - throw SQLException("expected one row in result set, but got many") - } - ret - } + override fun countPilots(): Long { + return conn.prepareStatement(countPilots).use { stmt -> + + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = results.getLong(1) + if (results.next()) { + throw SQLException("expected one row in result set, but got many") } + ret } } @Throws(SQLException::class) - override fun deletePilot(id: Int): ExecuteQuery { - return object : ExecuteQuery() { - override fun execute() { - conn.prepareStatement(deletePilot).use { stmt -> - this.statement = stmt - stmt.setInt(1, id) + override fun deletePilot(id: Int) { + conn.prepareStatement(deletePilot).use { stmt -> + stmt.setInt(1, id) - stmt.execute() - } - } + stmt.execute() } } @Throws(SQLException::class) - override fun listPilots(): ListQuery { - return object : ListQuery() { - override fun execute(): List { - return conn.prepareStatement(listPilots).use { stmt -> - this.statement = stmt - - val results = stmt.executeQuery() - val ret = mutableListOf() - while (results.next()) { - ret.add(Pilot( + override fun listPilots(): List { + return conn.prepareStatement(listPilots).use { stmt -> + + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(Pilot( results.getInt(1), results.getString(2) )) - } - ret - } } + ret } } diff --git a/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt b/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt index 57cbca9697..69debb00a7 100644 --- a/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt +++ b/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt @@ -7,13 +7,9 @@ import java.sql.SQLException import java.sql.Types import java.time.LocalDateTime -import sqlc.runtime.ExecuteQuery -import sqlc.runtime.ListQuery -import sqlc.runtime.RowQuery - interface Queries { @Throws(SQLException::class) - fun createCity(name: String, slug: String): RowQuery + fun createCity(name: String, slug: String): City @Throws(SQLException::class) fun createVenue( @@ -23,31 +19,31 @@ interface Queries { spotifyPlaylist: String, status: Status, statuses: List, - tags: List): RowQuery + tags: List): Int @Throws(SQLException::class) - fun deleteVenue(slug: String): ExecuteQuery + fun deleteVenue(slug: String) @Throws(SQLException::class) - fun getCity(slug: String): RowQuery + fun getCity(slug: String): City @Throws(SQLException::class) - fun getVenue(slug: String, city: String): RowQuery + fun getVenue(slug: String, city: String): Venue @Throws(SQLException::class) - fun listCities(): ListQuery + fun listCities(): List @Throws(SQLException::class) - fun listVenues(city: String): ListQuery + fun listVenues(city: String): List @Throws(SQLException::class) - fun updateCityName(name: String, slug: String): ExecuteQuery + fun updateCityName(name: String, slug: String) @Throws(SQLException::class) - fun updateVenueName(name: String, slug: String): RowQuery + fun updateVenueName(name: String, slug: String): Int @Throws(SQLException::class) - fun venueCountByCity(): ListQuery + fun venueCountByCity(): List } diff --git a/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt b/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt index c3e049ce2d..71317222f8 100644 --- a/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt +++ b/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt @@ -7,10 +7,6 @@ import java.sql.SQLException import java.sql.Types import java.time.LocalDateTime -import sqlc.runtime.ExecuteQuery -import sqlc.runtime.ListQuery -import sqlc.runtime.RowQuery - const val createCity = """-- name: createCity :one INSERT INTO city ( name, @@ -107,28 +103,23 @@ class QueriesImpl(private val conn: Connection) : Queries { // This is the third line @Throws(SQLException::class) - override fun createCity(name: String, slug: String): RowQuery { - return object : RowQuery() { - override fun execute(): City { - return conn.prepareStatement(createCity).use { stmt -> - this.statement = stmt - stmt.setString(1, name) + override fun createCity(name: String, slug: String): City { + return conn.prepareStatement(createCity).use { stmt -> + stmt.setString(1, name) stmt.setString(2, slug) - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = City( + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = City( results.getString(1), results.getString(2) ) - 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 } } @@ -140,12 +131,9 @@ class QueriesImpl(private val conn: Connection) : Queries { spotifyPlaylist: String, status: Status, statuses: List, - tags: List): RowQuery { - return object : RowQuery() { - override fun execute(): Int { - return conn.prepareStatement(createVenue).use { stmt -> - this.statement = stmt - stmt.setString(1, slug) + tags: List): Int { + return conn.prepareStatement(createVenue).use { stmt -> + stmt.setString(1, slug) stmt.setString(2, name) stmt.setString(3, city) stmt.setString(4, spotifyPlaylist) @@ -153,74 +141,59 @@ class QueriesImpl(private val conn: Connection) : Queries { stmt.setArray(6, conn.createArrayOf("status", statuses.map { v -> v.value }.toTypedArray())) stmt.setArray(7, conn.createArrayOf("text", tags.toTypedArray())) - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = results.getInt(1) - if (results.next()) { - throw SQLException("expected one row in result set, but got many") - } - ret - } + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = results.getInt(1) + if (results.next()) { + throw SQLException("expected one row in result set, but got many") } + ret } } @Throws(SQLException::class) - override fun deleteVenue(slug: String): ExecuteQuery { - return object : ExecuteQuery() { - override fun execute() { - conn.prepareStatement(deleteVenue).use { stmt -> - this.statement = stmt - stmt.setString(1, slug) + override fun deleteVenue(slug: String) { + conn.prepareStatement(deleteVenue).use { stmt -> + stmt.setString(1, slug) stmt.setString(2, slug) - stmt.execute() - } - } + stmt.execute() } } @Throws(SQLException::class) - override fun getCity(slug: String): RowQuery { - return object : RowQuery() { - override fun execute(): City { - return conn.prepareStatement(getCity).use { stmt -> - this.statement = stmt - stmt.setString(1, slug) - - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = City( + override fun getCity(slug: String): City { + return conn.prepareStatement(getCity).use { stmt -> + stmt.setString(1, slug) + + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = City( results.getString(1), results.getString(2) ) - 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 getVenue(slug: String, city: String): RowQuery { - return object : RowQuery() { - override fun execute(): Venue { - return conn.prepareStatement(getVenue).use { stmt -> - this.statement = stmt - stmt.setString(1, slug) + override fun getVenue(slug: String, city: String): Venue { + return conn.prepareStatement(getVenue).use { stmt -> + stmt.setString(1, slug) stmt.setString(2, city) - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = Venue( + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = Venue( results.getInt(1), Status.lookup(results.getString(2))!!, (results.getArray(3).array as Array).map { v -> Status.lookup(v)!! }.toList(), @@ -232,48 +205,38 @@ class QueriesImpl(private val conn: Connection) : Queries { (results.getArray(9).array as Array).toList(), results.getObject(10, LocalDateTime::class.java) ) - 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 listCities(): ListQuery { - return object : ListQuery() { - override fun execute(): List { - return conn.prepareStatement(listCities).use { stmt -> - this.statement = stmt - - val results = stmt.executeQuery() - val ret = mutableListOf() - while (results.next()) { - ret.add(City( + override fun listCities(): List { + return conn.prepareStatement(listCities).use { stmt -> + + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(City( results.getString(1), results.getString(2) )) - } - ret - } } + ret } } @Throws(SQLException::class) - override fun listVenues(city: String): ListQuery { - return object : ListQuery() { - override fun execute(): List { - return conn.prepareStatement(listVenues).use { stmt -> - this.statement = stmt - stmt.setString(1, city) - - val results = stmt.executeQuery() - val ret = mutableListOf() - while (results.next()) { - ret.add(Venue( + override fun listVenues(city: String): List { + return conn.prepareStatement(listVenues).use { stmt -> + stmt.setString(1, city) + + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(Venue( results.getInt(1), Status.lookup(results.getString(2))!!, (results.getArray(3).array as Array).map { v -> Status.lookup(v)!! }.toList(), @@ -285,69 +248,52 @@ class QueriesImpl(private val conn: Connection) : Queries { (results.getArray(9).array as Array).toList(), results.getObject(10, LocalDateTime::class.java) )) - } - ret - } } + ret } } @Throws(SQLException::class) - override fun updateCityName(name: String, slug: String): ExecuteQuery { - return object : ExecuteQuery() { - override fun execute() { - conn.prepareStatement(updateCityName).use { stmt -> - this.statement = stmt - stmt.setString(1, name) + override fun updateCityName(name: String, slug: String) { + conn.prepareStatement(updateCityName).use { stmt -> + stmt.setString(1, name) stmt.setString(2, slug) - stmt.execute() - } - } + stmt.execute() } } @Throws(SQLException::class) - override fun updateVenueName(name: String, slug: String): RowQuery { - return object : RowQuery() { - override fun execute(): Int { - return conn.prepareStatement(updateVenueName).use { stmt -> - this.statement = stmt - stmt.setString(1, name) + override fun updateVenueName(name: String, slug: String): Int { + return conn.prepareStatement(updateVenueName).use { stmt -> + stmt.setString(1, name) stmt.setString(2, slug) - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = results.getInt(1) - if (results.next()) { - throw SQLException("expected one row in result set, but got many") - } - ret - } + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = results.getInt(1) + if (results.next()) { + throw SQLException("expected one row in result set, but got many") } + ret } } @Throws(SQLException::class) - override fun venueCountByCity(): ListQuery { - return object : ListQuery() { - override fun execute(): List { - return conn.prepareStatement(venueCountByCity).use { stmt -> - this.statement = stmt - - val results = stmt.executeQuery() - val ret = mutableListOf() - while (results.next()) { - ret.add(VenueCountByCityRow( + override fun venueCountByCity(): List { + return conn.prepareStatement(venueCountByCity).use { stmt -> + + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(VenueCountByCityRow( results.getString(1), results.getLong(2) )) - } - ret - } } + ret } } diff --git a/examples/kotlin/src/main/kotlin/sqlc/runtime/Query.kt b/examples/kotlin/src/main/kotlin/sqlc/runtime/Query.kt deleted file mode 100644 index 67a4a18834..0000000000 --- a/examples/kotlin/src/main/kotlin/sqlc/runtime/Query.kt +++ /dev/null @@ -1,42 +0,0 @@ -package sqlc.runtime - -import java.sql.Statement -import java.time.Duration - -abstract class Query { - @Volatile - var timeout: Duration? = null - - private var _statement: Statement? = null - protected var statement: Statement - @Synchronized get() { - return this._statement ?: throw Exception("Cannot get Statement before Query is executed") - } - @Synchronized set(value) { - val timeout = this.timeout - if (timeout != null) { - value.queryTimeout = timeout.seconds.toInt() - } - this._statement = value - } - - fun cancel() { - this.statement.cancel() - } -} - -abstract class RowQuery : Query() { - abstract fun execute(): T -} - -abstract class ListQuery : Query() { - abstract fun execute(): List -} - -abstract class ExecuteQuery : Query() { - abstract fun execute() -} - -abstract class ExecuteUpdateQuery : Query() { - abstract fun execute(): Int -} \ No newline at end of file diff --git a/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt b/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt index e2add5e72c..93608a1dfc 100644 --- a/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt +++ b/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt @@ -17,7 +17,7 @@ class QueriesImplTest() { fun testCreateAuthor() { val db = QueriesImpl(dbtest.getConnection()) - val initialAuthors = db.listAuthors().execute() + val initialAuthors = db.listAuthors() assert(initialAuthors.isEmpty()) val name = "Brian Kernighan" @@ -25,14 +25,14 @@ class QueriesImplTest() { val insertedAuthor = db.createAuthor( name = name, bio = bio - ).execute() + ) val expectedAuthor = Author(insertedAuthor.id, name, bio) assertEquals(expectedAuthor, insertedAuthor) - val fetchedAuthor = db.getAuthor(insertedAuthor.id).execute() + val fetchedAuthor = db.getAuthor(insertedAuthor.id) assertEquals(expectedAuthor, fetchedAuthor) - val listedAuthors = db.listAuthors().execute() + val listedAuthors = db.listAuthors() assertEquals(1, listedAuthors.size) assertEquals(expectedAuthor, listedAuthors[0]) } @@ -41,19 +41,19 @@ class QueriesImplTest() { fun testNull() { val db = QueriesImpl(dbtest.getConnection()) - val initialAuthors = db.listAuthors().execute() + val initialAuthors = db.listAuthors() assert(initialAuthors.isEmpty()) val name = "Brian Kernighan" val bio = null - val insertedAuthor = db.createAuthor(name, bio).execute() + val insertedAuthor = db.createAuthor(name, bio) val expectedAuthor = Author(insertedAuthor.id, name, bio) assertEquals(expectedAuthor, insertedAuthor) - val fetchedAuthor = db.getAuthor(insertedAuthor.id).execute() + val fetchedAuthor = db.getAuthor(insertedAuthor.id) assertEquals(expectedAuthor, fetchedAuthor) - val listedAuthors = db.listAuthors().execute() + val listedAuthors = db.listAuthors() assertEquals(1, listedAuthors.size) assertEquals(expectedAuthor, listedAuthors[0]) } diff --git a/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt b/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt index 4d67b1f70e..89e480b884 100644 --- a/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt +++ b/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt @@ -15,7 +15,7 @@ class QueriesImplTest { fun testQueries() { val conn = dbtest.getConnection() val db = QueriesImpl(conn) - val author = db.createAuthor("Unknown Master").execute() + val author = db.createAuthor("Unknown Master") // Start a transaction conn.autoCommit = false @@ -27,7 +27,7 @@ class QueriesImplTest { year = 2016, available = OffsetDateTime.now(), tags = listOf() - ).execute() + ) val b1 = db.createBook( authorId = author.authorId, @@ -37,13 +37,13 @@ class QueriesImplTest { year = 2016, available = OffsetDateTime.now(), tags = listOf("cool", "unique") - ).execute() + ) db.updateBook( bookId = b1.bookId, title = "changed second title", tags = listOf("cool", "disastor") - ).execute() + ) val b3 = db.createBook( authorId = author.authorId, @@ -53,7 +53,7 @@ class QueriesImplTest { year = 2001, available = OffsetDateTime.now(), tags = listOf("cool") - ).execute() + ) db.createBook( authorId = author.authorId, @@ -63,7 +63,7 @@ class QueriesImplTest { year = 2011, available = OffsetDateTime.now(), tags = listOf("other") - ).execute() + ) // Commit transaction conn.commit() @@ -76,20 +76,20 @@ class QueriesImplTest { isbn = "NEW ISBN", title = "never ever gonna finish, a quatrain", tags = listOf("someother") - ).execute() + ) - val books0 = db.booksByTitleYear("my book title", 2016).execute() + val books0 = db.booksByTitleYear("my book title", 2016) val formatter = DateTimeFormatter.ISO_DATE_TIME for (book in books0) { println("Book ${book.bookId} (${book.booktype}): ${book.title} available: ${book.available.format(formatter)}") - val author2 = db.getAuthor(book.authorId).execute() + val author2 = db.getAuthor(book.authorId) println("Book ${book.bookId} author: ${author2.name}") } // find a book with either "cool" or "other" tag println("---------\\nTag search results:\\n") - val res = db.booksByTags(listOf("cool", "other", "someother")).execute() + val res = db.booksByTags(listOf("cool", "other", "someother")) for (ab in res) { println("Book ${ab.bookId}: '${ab.title}', Author: '${ab.name}', ISBN: '${ab.isbn}' Tags: '${ab.tags.toList()}'") } diff --git a/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt b/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt index 07d5db4cea..dab8687f99 100644 --- a/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt +++ b/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt @@ -16,7 +16,7 @@ class QueriesImplTest { val city = q.createCity( slug = "san-francisco", name = "San Francisco" - ).execute() + ) val venueId = q.createVenue( slug = "the-fillmore", name = "The Fillmore", @@ -25,22 +25,22 @@ class QueriesImplTest { status = Status.OPEN, statuses = listOf(Status.OPEN, Status.CLOSED), tags = listOf("rock", "punk") - ).execute() + ) val venue = q.getVenue( slug = "the-fillmore", city = city.slug - ).execute() + ) assertEquals(venueId, venue.id) - assertEquals(city, q.getCity(city.slug).execute()) - assertEquals(listOf(VenueCountByCityRow(city.slug, 1)), q.venueCountByCity().execute()) - assertEquals(listOf(city), q.listCities().execute()) - assertEquals(listOf(venue), q.listVenues(city.slug).execute()) + assertEquals(city, q.getCity(city.slug)) + assertEquals(listOf(VenueCountByCityRow(city.slug, 1)), q.venueCountByCity()) + assertEquals(listOf(city), q.listCities()) + assertEquals(listOf(venue), q.listVenues(city.slug)) - q.updateCityName(slug = city.slug, name = "SF").execute() - val id = q.updateVenueName(slug = venue.slug, name = "Fillmore").execute() + q.updateCityName(slug = city.slug, name = "SF") + val id = q.updateVenueName(slug = venue.slug, name = "Fillmore") assertEquals(venue.id, id) - q.deleteVenue(venue.slug).execute() + q.deleteVenue(venue.slug) } } diff --git a/internal/codegen/kotlin/gen.go b/internal/codegen/kotlin/gen.go index eab3f9f416..a010ddd387 100644 --- a/internal/codegen/kotlin/gen.go +++ b/internal/codegen/kotlin/gen.go @@ -611,16 +611,16 @@ interface Queries { {{- range .Queries}} @Throws(SQLException::class) {{- if eq .Cmd ":one"}} - fun {{.MethodName}}({{.Arg.Args}}): RowQuery<{{.Ret.Type}}> + fun {{.MethodName}}({{.Arg.Args}}): {{.Ret.Type}} {{- end}} {{- if eq .Cmd ":many"}} - fun {{.MethodName}}({{.Arg.Args}}): ListQuery<{{.Ret.Type}}> + fun {{.MethodName}}({{.Arg.Args}}): List<{{.Ret.Type}}> {{- end}} {{- if eq .Cmd ":exec"}} - fun {{.MethodName}}({{.Arg.Args}}): ExecuteQuery + fun {{.MethodName}}({{.Arg.Args}}) {{- end}} {{- if eq .Cmd ":execrows"}} - fun {{.MethodName}}({{.Arg.Args}}): ExecuteUpdateQuery + fun {{.MethodName}}({{.Arg.Args}}): Int {{- end}} {{end}} } @@ -692,24 +692,19 @@ class QueriesImpl(private val conn: Connection) : Queries { {{range .Comments}}//{{.}} {{end}} @Throws(SQLException::class) - override fun {{.MethodName}}({{.Arg.Args}}): RowQuery<{{.Ret.Type}}> { - return object : RowQuery<{{.Ret.Type}}>() { - override fun execute(): {{.Ret.Type}} { - return conn.prepareStatement({{.ConstantName}}).use { stmt -> - this.statement = stmt - {{.Arg.Bindings}} - - val results = stmt.executeQuery() - if (!results.next()) { - throw SQLException("no rows in result set") - } - val ret = {{.Ret.ResultSet}} - if (results.next()) { - throw SQLException("expected one row in result set, but got many") - } - ret - } + override fun {{.MethodName}}({{.Arg.Args}}): {{.Ret.Type}} { + return conn.prepareStatement({{.ConstantName}}).use { stmt -> + {{.Arg.Bindings}} + + val results = stmt.executeQuery() + if (!results.next()) { + throw SQLException("no rows in result set") + } + val ret = {{.Ret.ResultSet}} + if (results.next()) { + throw SQLException("expected one row in result set, but got many") } + ret } } {{end}} @@ -718,21 +713,16 @@ class QueriesImpl(private val conn: Connection) : Queries { {{range .Comments}}//{{.}} {{end}} @Throws(SQLException::class) - override fun {{.MethodName}}({{.Arg.Args}}): ListQuery<{{.Ret.Type}}> { - return object : ListQuery<{{.Ret.Type}}>() { - override fun execute(): List<{{.Ret.Type}}> { - return conn.prepareStatement({{.ConstantName}}).use { stmt -> - this.statement = stmt - {{.Arg.Bindings}} - - val results = stmt.executeQuery() - val ret = mutableListOf<{{.Ret.Type}}>() - while (results.next()) { - ret.add({{.Ret.ResultSet}}) - } - ret - } + override fun {{.MethodName}}({{.Arg.Args}}): List<{{.Ret.Type}}> { + return conn.prepareStatement({{.ConstantName}}).use { stmt -> + {{.Arg.Bindings}} + + val results = stmt.executeQuery() + val ret = mutableListOf<{{.Ret.Type}}>() + while (results.next()) { + ret.add({{.Ret.ResultSet}}) } + ret } } {{end}} @@ -742,16 +732,11 @@ class QueriesImpl(private val conn: Connection) : Queries { {{end}} @Throws(SQLException::class) {{ if $.EmitInterface }}override {{ end -}} - override fun {{.MethodName}}({{.Arg.Args}}): ExecuteQuery { - return object : ExecuteQuery() { - override fun execute() { - conn.prepareStatement({{.ConstantName}}).use { stmt -> - this.statement = stmt - {{ .Arg.Bindings }} - - stmt.execute() - } - } + override fun {{.MethodName}}({{.Arg.Args}}) { + conn.prepareStatement({{.ConstantName}}).use { stmt -> + {{ .Arg.Bindings }} + + stmt.execute() } } {{end}} @@ -761,17 +746,12 @@ class QueriesImpl(private val conn: Connection) : Queries { {{end}} @Throws(SQLException::class) {{ if $.EmitInterface }}override {{ end -}} - override fun {{.MethodName}}({{.Arg.Args}}): ExecuteUpdateQuery { - return object : ExecUpdateQuery() { - override fun execute(): Int { - return conn.prepareStatement({{.ConstantName}}).use { stmt -> - this.statement = stmt - {{ .Arg.Bindings }} - - stmt.execute() - stmt.updateCount - } - } + override fun {{.MethodName}}({{.Arg.Args}}): Int { + return conn.prepareStatement({{.ConstantName}}).use { stmt -> + {{ .Arg.Bindings }} + + stmt.execute() + stmt.updateCount } } {{end}} diff --git a/internal/codegen/kotlin/imports.go b/internal/codegen/kotlin/imports.go index 7924f3d8ae..6480a1f5bd 100644 --- a/internal/codegen/kotlin/imports.go +++ b/internal/codegen/kotlin/imports.go @@ -1,7 +1,6 @@ package kotlin import ( - "fmt" "sort" "strings" @@ -63,7 +62,7 @@ func (i *importer) interfaceImports() [][]string { } sort.Strings(stds) - return [][]string{stds, runtimeImports(i.Queries)} + return [][]string{stds} } func (i *importer) modelImports() [][]string { @@ -109,30 +108,6 @@ func stdImports(uses func(name string) bool) map[string]struct{} { return std } -func runtimeImports(kq []Query) []string { - rt := map[string]struct{}{} - for _, q := range kq { - switch q.Cmd { - case ":one": - rt["sqlc.runtime.RowQuery"] = struct{}{} - case ":many": - rt["sqlc.runtime.ListQuery"] = struct{}{} - case ":exec": - rt["sqlc.runtime.ExecuteQuery"] = struct{}{} - case ":execUpdate": - rt["sqlc.runtime.ExecuteUpdateQuery"] = struct{}{} - default: - panic(fmt.Sprintf("invalid command %q", q.Cmd)) - } - } - rts := make([]string, 0, len(rt)) - for s, _ := range rt { - rts = append(rts, s) - } - sort.Strings(rts) - return rts -} - func (i *importer) queryImports(filename string) [][]string { uses := func(name string) bool { for _, q := range i.Queries { @@ -184,5 +159,5 @@ func (i *importer) queryImports(filename string) [][]string { } sort.Strings(stds) - return [][]string{stds, runtimeImports(i.Queries)} + return [][]string{stds} } diff --git a/internal/endtoend/endtoend_test.go b/internal/endtoend/endtoend_test.go index 85db9e58c0..cd82dfb69b 100644 --- a/internal/endtoend/endtoend_test.go +++ b/internal/endtoend/endtoend_test.go @@ -118,10 +118,6 @@ func cmpDirectory(t *testing.T, dir string, actual map[string]string) { if strings.HasSuffix(path, "_test.go") || strings.Contains(path, "src/test/") { return nil } - // TODO(mightyguava): Remove this after sqlc-kotlin-runtime is published to Maven. - if strings.HasSuffix(path, "Query.kt") { - return nil - } blob, err := ioutil.ReadFile(path) if err != nil { return err