Skip to content

Commit cd3919a

Browse files
committed
kotlin: remove runtime dependency
The runtime in #368 turned out to be a bad idea. It's pretty much unnecessary and adds quite a bit to the complexity of the generated code. It's _possible_ a runtime would be useful in the future. At that point we can add that back in
1 parent fd3b940 commit cd3919a

File tree

15 files changed

+317
-570
lines changed

15 files changed

+317
-570
lines changed

examples/kotlin/src/main/kotlin/com/example/authors/Queries.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@ package com.example.authors
55
import java.sql.Connection
66
import java.sql.SQLException
77

8-
import sqlc.runtime.ExecuteQuery
9-
import sqlc.runtime.ListQuery
10-
import sqlc.runtime.RowQuery
11-
128
interface Queries {
139
@Throws(SQLException::class)
14-
fun createAuthor(name: String, bio: String?): RowQuery<Author>
10+
fun createAuthor(name: String, bio: String?): Author
1511

1612
@Throws(SQLException::class)
17-
fun deleteAuthor(id: Long): ExecuteQuery
13+
fun deleteAuthor(id: Long)
1814

1915
@Throws(SQLException::class)
20-
fun getAuthor(id: Long): RowQuery<Author>
16+
fun getAuthor(id: Long): Author
2117

2218
@Throws(SQLException::class)
23-
fun listAuthors(): ListQuery<Author>
19+
fun listAuthors(): List<Author>
2420

2521
}
2622

examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ package com.example.authors
55
import java.sql.Connection
66
import java.sql.SQLException
77

8-
import sqlc.runtime.ExecuteQuery
9-
import sqlc.runtime.ListQuery
10-
import sqlc.runtime.RowQuery
11-
128
const val createAuthor = """-- name: createAuthor :one
139
INSERT INTO authors (
1410
name, bio
@@ -36,91 +32,71 @@ ORDER BY name
3632
class QueriesImpl(private val conn: Connection) : Queries {
3733

3834
@Throws(SQLException::class)
39-
override fun createAuthor(name: String, bio: String?): RowQuery<Author> {
40-
return object : RowQuery<Author>() {
41-
override fun execute(): Author {
42-
return conn.prepareStatement(createAuthor).use { stmt ->
43-
this.statement = stmt
44-
stmt.setString(1, name)
35+
override fun createAuthor(name: String, bio: String?): Author {
36+
return conn.prepareStatement(createAuthor).use { stmt ->
37+
stmt.setString(1, name)
4538
stmt.setString(2, bio)
4639

47-
val results = stmt.executeQuery()
48-
if (!results.next()) {
49-
throw SQLException("no rows in result set")
50-
}
51-
val ret = Author(
40+
val results = stmt.executeQuery()
41+
if (!results.next()) {
42+
throw SQLException("no rows in result set")
43+
}
44+
val ret = Author(
5245
results.getLong(1),
5346
results.getString(2),
5447
results.getString(3)
5548
)
56-
if (results.next()) {
57-
throw SQLException("expected one row in result set, but got many")
58-
}
59-
ret
60-
}
49+
if (results.next()) {
50+
throw SQLException("expected one row in result set, but got many")
6151
}
52+
ret
6253
}
6354
}
6455

6556
@Throws(SQLException::class)
66-
override fun deleteAuthor(id: Long): ExecuteQuery {
67-
return object : ExecuteQuery() {
68-
override fun execute() {
69-
conn.prepareStatement(deleteAuthor).use { stmt ->
70-
this.statement = stmt
71-
stmt.setLong(1, id)
72-
73-
stmt.execute()
74-
}
75-
}
57+
override fun deleteAuthor(id: Long) {
58+
conn.prepareStatement(deleteAuthor).use { stmt ->
59+
stmt.setLong(1, id)
60+
61+
stmt.execute()
7662
}
7763
}
7864

7965
@Throws(SQLException::class)
80-
override fun getAuthor(id: Long): RowQuery<Author> {
81-
return object : RowQuery<Author>() {
82-
override fun execute(): Author {
83-
return conn.prepareStatement(getAuthor).use { stmt ->
84-
this.statement = stmt
85-
stmt.setLong(1, id)
86-
87-
val results = stmt.executeQuery()
88-
if (!results.next()) {
89-
throw SQLException("no rows in result set")
90-
}
91-
val ret = Author(
66+
override fun getAuthor(id: Long): Author {
67+
return conn.prepareStatement(getAuthor).use { stmt ->
68+
stmt.setLong(1, id)
69+
70+
val results = stmt.executeQuery()
71+
if (!results.next()) {
72+
throw SQLException("no rows in result set")
73+
}
74+
val ret = Author(
9275
results.getLong(1),
9376
results.getString(2),
9477
results.getString(3)
9578
)
96-
if (results.next()) {
97-
throw SQLException("expected one row in result set, but got many")
98-
}
99-
ret
100-
}
79+
if (results.next()) {
80+
throw SQLException("expected one row in result set, but got many")
10181
}
82+
ret
10283
}
10384
}
10485

10586
@Throws(SQLException::class)
106-
override fun listAuthors(): ListQuery<Author> {
107-
return object : ListQuery<Author>() {
108-
override fun execute(): List<Author> {
109-
return conn.prepareStatement(listAuthors).use { stmt ->
110-
this.statement = stmt
111-
112-
val results = stmt.executeQuery()
113-
val ret = mutableListOf<Author>()
114-
while (results.next()) {
115-
ret.add(Author(
87+
override fun listAuthors(): List<Author> {
88+
return conn.prepareStatement(listAuthors).use { stmt ->
89+
90+
val results = stmt.executeQuery()
91+
val ret = mutableListOf<Author>()
92+
while (results.next()) {
93+
ret.add(Author(
11694
results.getLong(1),
11795
results.getString(2),
11896
results.getString(3)
11997
))
120-
}
121-
ret
122-
}
12398
}
99+
ret
124100
}
125101
}
126102

examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/Queries.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@ import java.sql.SQLException
77
import java.sql.Types
88
import java.time.OffsetDateTime
99

10-
import sqlc.runtime.ExecuteQuery
11-
import sqlc.runtime.ListQuery
12-
import sqlc.runtime.RowQuery
13-
1410
interface Queries {
1511
@Throws(SQLException::class)
16-
fun booksByTags(dollar1: List<String>): ListQuery<BooksByTagsRow>
12+
fun booksByTags(dollar1: List<String>): List<BooksByTagsRow>
1713

1814
@Throws(SQLException::class)
19-
fun booksByTitleYear(title: String, year: Int): ListQuery<Book>
15+
fun booksByTitleYear(title: String, year: Int): List<Book>
2016

2117
@Throws(SQLException::class)
22-
fun createAuthor(name: String): RowQuery<Author>
18+
fun createAuthor(name: String): Author
2319

2420
@Throws(SQLException::class)
2521
fun createBook(
@@ -29,29 +25,29 @@ interface Queries {
2925
title: String,
3026
year: Int,
3127
available: OffsetDateTime,
32-
tags: List<String>): RowQuery<Book>
28+
tags: List<String>): Book
3329

3430
@Throws(SQLException::class)
35-
fun deleteBook(bookId: Int): ExecuteQuery
31+
fun deleteBook(bookId: Int)
3632

3733
@Throws(SQLException::class)
38-
fun getAuthor(authorId: Int): RowQuery<Author>
34+
fun getAuthor(authorId: Int): Author
3935

4036
@Throws(SQLException::class)
41-
fun getBook(bookId: Int): RowQuery<Book>
37+
fun getBook(bookId: Int): Book
4238

4339
@Throws(SQLException::class)
4440
fun updateBook(
4541
title: String,
4642
tags: List<String>,
47-
bookId: Int): ExecuteQuery
43+
bookId: Int)
4844

4945
@Throws(SQLException::class)
5046
fun updateBookISBN(
5147
title: String,
5248
tags: List<String>,
5349
isbn: String,
54-
bookId: Int): ExecuteQuery
50+
bookId: Int)
5551

5652
}
5753

0 commit comments

Comments
 (0)