-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathQueriesImpl.kt
More file actions
304 lines (267 loc) · 7.44 KB
/
QueriesImpl.kt
File metadata and controls
304 lines (267 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.15.0
package com.example.ondeck.postgresql
import java.sql.Connection
import java.sql.SQLException
import java.sql.Statement
import java.sql.Types
import java.time.LocalDateTime
const val createCity = """-- name: createCity :one
INSERT INTO city (
name,
slug
) VALUES (
?,
?
) RETURNING slug, name
"""
const val createVenue = """-- name: createVenue :one
INSERT INTO venue (
slug,
name,
city,
created_at,
spotify_playlist,
status,
statuses,
tags
) VALUES (
?,
?,
?,
NOW(),
?,
?,
?,
?
) RETURNING id
"""
const val deleteVenue = """-- name: deleteVenue :exec
DELETE FROM venue
WHERE slug = ? AND slug = ?
"""
const val getCity = """-- name: getCity :one
SELECT slug, name
FROM city
WHERE slug = ?
"""
const val getVenue = """-- name: getVenue :one
SELECT id, status, statuses, slug, name, city, spotify_playlist, songkick_id, tags, created_at
FROM venue
WHERE slug = ? AND city = ?
"""
const val listCities = """-- name: listCities :many
SELECT slug, name
FROM city
ORDER BY name
"""
const val listVenues = """-- name: listVenues :many
SELECT id, status, statuses, slug, name, city, spotify_playlist, songkick_id, tags, created_at
FROM venue
WHERE city = ?
ORDER BY name
"""
const val updateCityName = """-- name: updateCityName :exec
UPDATE city
SET name = ?
WHERE slug = ?
"""
const val updateVenueName = """-- name: updateVenueName :one
UPDATE venue
SET name = ?
WHERE slug = ?
RETURNING id
"""
const val venueCountByCity = """-- name: venueCountByCity :many
SELECT
city,
count(*)
FROM venue
GROUP BY 1
ORDER BY 1
"""
data class VenueCountByCityRow (
val city: String,
val count: Long
)
class QueriesImpl(private val conn: Connection) : Queries {
// Create a new city. The slug must be unique.
// This is the second line of the comment
// This is the third line
@Throws(SQLException::class)
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()) {
return null
}
val ret = City(
results.getString(1),
results.getString(2)
)
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}
@Throws(SQLException::class)
override fun createVenue(
slug: String,
name: String,
city: String,
spotifyPlaylist: String,
status: Status,
statuses: List<Status>,
tags: List<String>): Int? {
return conn.prepareStatement(createVenue).use { stmt ->
stmt.setString(1, slug)
stmt.setString(2, name)
stmt.setString(3, city)
stmt.setString(4, spotifyPlaylist)
stmt.setObject(5, status.value, Types.OTHER)
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()) {
return null
}
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) {
conn.prepareStatement(deleteVenue).use { stmt ->
stmt.setString(1, slug)
stmt.setString(2, slug)
stmt.execute()
}
}
@Throws(SQLException::class)
override fun getCity(slug: String): City? {
return conn.prepareStatement(getCity).use { stmt ->
stmt.setString(1, slug)
val results = stmt.executeQuery()
if (!results.next()) {
return null
}
val ret = City(
results.getString(1),
results.getString(2)
)
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): Venue? {
return conn.prepareStatement(getVenue).use { stmt ->
stmt.setString(1, slug)
stmt.setString(2, city)
val results = stmt.executeQuery()
if (!results.next()) {
return null
}
val ret = Venue(
results.getInt(1),
Status.lookup(results.getString(2))!!,
(results.getArray(3).array as Array<String>).map { v -> Status.lookup(v)!! }.toList(),
results.getString(4),
results.getString(5),
results.getString(6),
results.getString(7),
results.getString(8),
(results.getArray(9).array as Array<String>).toList(),
results.getObject(10, LocalDateTime::class.java)
)
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}
@Throws(SQLException::class)
override fun listCities(): List<City> {
return conn.prepareStatement(listCities).use { stmt ->
val results = stmt.executeQuery()
val ret = mutableListOf<City>()
while (results.next()) {
ret.add(City(
results.getString(1),
results.getString(2)
))
}
ret
}
}
@Throws(SQLException::class)
override fun listVenues(city: String): List<Venue> {
return conn.prepareStatement(listVenues).use { stmt ->
stmt.setString(1, city)
val results = stmt.executeQuery()
val ret = mutableListOf<Venue>()
while (results.next()) {
ret.add(Venue(
results.getInt(1),
Status.lookup(results.getString(2))!!,
(results.getArray(3).array as Array<String>).map { v -> Status.lookup(v)!! }.toList(),
results.getString(4),
results.getString(5),
results.getString(6),
results.getString(7),
results.getString(8),
(results.getArray(9).array as Array<String>).toList(),
results.getObject(10, LocalDateTime::class.java)
))
}
ret
}
}
@Throws(SQLException::class)
override fun updateCityName(name: String, slug: String) {
conn.prepareStatement(updateCityName).use { stmt ->
stmt.setString(1, name)
stmt.setString(2, slug)
stmt.execute()
}
}
@Throws(SQLException::class)
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()) {
return null
}
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(): List<VenueCountByCityRow> {
return conn.prepareStatement(venueCountByCity).use { stmt ->
val results = stmt.executeQuery()
val ret = mutableListOf<VenueCountByCityRow>()
while (results.next()) {
ret.add(VenueCountByCityRow(
results.getString(1),
results.getLong(2)
))
}
ret
}
}
}