@@ -5,10 +5,6 @@ package com.example.authors
5
5
import java.sql.Connection
6
6
import java.sql.SQLException
7
7
8
- import sqlc.runtime.ExecuteQuery
9
- import sqlc.runtime.ListQuery
10
- import sqlc.runtime.RowQuery
11
-
12
8
const val createAuthor = """ -- name: createAuthor :one
13
9
INSERT INTO authors (
14
10
name, bio
@@ -36,91 +32,71 @@ ORDER BY name
36
32
class QueriesImpl (private val conn : Connection ) : Queries {
37
33
38
34
@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)
45
38
stmt.setString(2 , bio)
46
39
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 (
52
45
results.getLong(1 ),
53
46
results.getString(2 ),
54
47
results.getString(3 )
55
48
)
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" )
61
51
}
52
+ ret
62
53
}
63
54
}
64
55
65
56
@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()
76
62
}
77
63
}
78
64
79
65
@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 (
92
75
results.getLong(1 ),
93
76
results.getString(2 ),
94
77
results.getString(3 )
95
78
)
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" )
101
81
}
82
+ ret
102
83
}
103
84
}
104
85
105
86
@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 (
116
94
results.getLong(1 ),
117
95
results.getString(2 ),
118
96
results.getString(3 )
119
97
))
120
- }
121
- ret
122
- }
123
98
}
99
+ ret
124
100
}
125
101
}
126
102
0 commit comments