Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ INSERT INTO authors (
RETURNING id, name, bio
"""

data class CreateAuthorParams (
val name: String,
val bio: String?
)

const val deleteAuthor = """-- name: deleteAuthor :exec
DELETE FROM authors
WHERE id = ?
Expand All @@ -37,10 +32,10 @@ ORDER BY name
class QueriesImpl(private val conn: Connection) {

@Throws(SQLException::class)
fun createAuthor(arg: CreateAuthorParams): Author {
fun createAuthor(name: String, bio: String?): Author {
return conn.prepareStatement(createAuthor).use { stmt ->
stmt.setString(1, arg.name)
stmt.setString(2, arg.bio)
stmt.setString(1, name)
stmt.setString(2, bio)

val results = stmt.executeQuery()
if (!results.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ SELECT book_id, author_id, isbn, booktype, title, year, available, tags FROM boo
WHERE title = ? AND year = ?
"""

data class BooksByTitleYearParams (
val title: String,
val year: Int
)

const val createAuthor = """-- name: createAuthor :one
INSERT INTO authors (name) VALUES (?)
RETURNING author_id, name
Expand All @@ -63,16 +58,6 @@ INSERT INTO books (
RETURNING book_id, author_id, isbn, booktype, title, year, available, tags
"""

data class CreateBookParams (
val authorId: Int,
val isbn: String,
val booktype: BookType,
val title: String,
val year: Int,
val available: OffsetDateTime,
val tags: List<String>
)

const val deleteBook = """-- name: deleteBook :exec
DELETE FROM books
WHERE book_id = ?
Expand All @@ -94,31 +79,18 @@ SET title = ?, tags = ?
WHERE book_id = ?
"""

data class UpdateBookParams (
val title: String,
val tags: List<String>,
val bookId: Int
)

const val updateBookISBN = """-- name: updateBookISBN :exec
UPDATE books
SET title = ?, tags = ?, isbn = ?
WHERE book_id = ?
"""

data class UpdateBookISBNParams (
val title: String,
val tags: List<String>,
val isbn: String,
val bookId: Int
)

class QueriesImpl(private val conn: Connection) {

@Throws(SQLException::class)
fun booksByTags(dollar_1: List<String>): List<BooksByTagsRow> {
fun booksByTags(dollar1: List<String>): List<BooksByTagsRow> {
return conn.prepareStatement(booksByTags).use { stmt ->
stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar_1.toTypedArray()))
stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar1.toTypedArray()))

val results = stmt.executeQuery()
val ret = mutableListOf<BooksByTagsRow>()
Expand All @@ -136,10 +108,10 @@ class QueriesImpl(private val conn: Connection) {
}

@Throws(SQLException::class)
fun booksByTitleYear(arg: BooksByTitleYearParams): List<Book> {
fun booksByTitleYear(title: String, year: Int): List<Book> {
return conn.prepareStatement(booksByTitleYear).use { stmt ->
stmt.setString(1, arg.title)
stmt.setInt(2, arg.year)
stmt.setString(1, title)
stmt.setInt(2, year)

val results = stmt.executeQuery()
val ret = mutableListOf<Book>()
Expand Down Expand Up @@ -180,15 +152,22 @@ class QueriesImpl(private val conn: Connection) {
}

@Throws(SQLException::class)
fun createBook(arg: CreateBookParams): Book {
fun createBook(
authorId: Int,
isbn: String,
booktype: BookType,
title: String,
year: Int,
available: OffsetDateTime,
tags: List<String>): Book {
return conn.prepareStatement(createBook).use { stmt ->
stmt.setInt(1, arg.authorId)
stmt.setString(2, arg.isbn)
stmt.setObject(3, arg.booktype.value, Types.OTHER)
stmt.setString(4, arg.title)
stmt.setInt(5, arg.year)
stmt.setObject(6, arg.available)
stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray()))
stmt.setInt(1, authorId)
stmt.setString(2, isbn)
stmt.setObject(3, booktype.value, Types.OTHER)
stmt.setString(4, title)
stmt.setInt(5, year)
stmt.setObject(6, available)
stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray()))

val results = stmt.executeQuery()
if (!results.next()) {
Expand Down Expand Up @@ -267,23 +246,30 @@ class QueriesImpl(private val conn: Connection) {
}

@Throws(SQLException::class)
fun updateBook(arg: UpdateBookParams) {
fun updateBook(
title: String,
tags: List<String>,
bookId: Int) {
conn.prepareStatement(updateBook).use { stmt ->
stmt.setString(1, arg.title)
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray()))
stmt.setInt(3, arg.bookId)
stmt.setString(1, title)
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray()))
stmt.setInt(3, bookId)

stmt.execute()
}
}

@Throws(SQLException::class)
fun updateBookISBN(arg: UpdateBookISBNParams) {
fun updateBookISBN(
title: String,
tags: List<String>,
isbn: String,
bookId: Int) {
conn.prepareStatement(updateBookISBN).use { stmt ->
stmt.setString(1, arg.title)
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray()))
stmt.setString(3, arg.isbn)
stmt.setInt(4, arg.bookId)
stmt.setString(1, title)
stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray()))
stmt.setString(3, isbn)
stmt.setInt(4, bookId)

stmt.execute()
}
Expand Down
17 changes: 12 additions & 5 deletions examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ import java.time.LocalDateTime

interface Queries {
@Throws(SQLException::class)
fun createCity(arg: CreateCityParams): City
fun createCity(name: String, slug: String): City

@Throws(SQLException::class)
fun createVenue(arg: CreateVenueParams): Int
fun createVenue(
slug: String,
name: String,
city: String,
spotifyPlaylist: String,
status: Status,
statuses: List<Status>,
tags: List<String>): Int

@Throws(SQLException::class)
fun deleteVenue(slug: String)
Expand All @@ -21,7 +28,7 @@ interface Queries {
fun getCity(slug: String): City

@Throws(SQLException::class)
fun getVenue(arg: GetVenueParams): Venue
fun getVenue(slug: String, city: String): Venue

@Throws(SQLException::class)
fun listCities(): List<City>
Expand All @@ -30,10 +37,10 @@ interface Queries {
fun listVenues(city: String): List<Venue>

@Throws(SQLException::class)
fun updateCityName(arg: UpdateCityNameParams)
fun updateCityName(name: String, slug: String)

@Throws(SQLException::class)
fun updateVenueName(arg: UpdateVenueNameParams): Int
fun updateVenueName(name: String, slug: String): Int

@Throws(SQLException::class)
fun venueCountByCity(): List<VenueCountByCityRow>
Expand Down
77 changes: 27 additions & 50 deletions examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ INSERT INTO city (
) RETURNING slug, name
"""

data class CreateCityParams (
val name: String,
val slug: String
)

const val createVenue = """-- name: createVenue :one
INSERT INTO venue (
slug,
Expand All @@ -44,16 +39,6 @@ INSERT INTO venue (
) RETURNING id
"""

data class CreateVenueParams (
val slug: String,
val name: String,
val city: String,
val spotifyPlaylist: String,
val status: Status,
val statuses: List<Status>,
val tags: List<String>
)

const val deleteVenue = """-- name: deleteVenue :exec
DELETE FROM venue
WHERE slug = ? AND slug = ?
Expand All @@ -71,11 +56,6 @@ FROM venue
WHERE slug = ? AND city = ?
"""

data class GetVenueParams (
val slug: String,
val city: String
)

const val listCities = """-- name: listCities :many
SELECT slug, name
FROM city
Expand All @@ -95,23 +75,13 @@ SET name = ?
WHERE slug = ?
"""

data class UpdateCityNameParams (
val name: String,
val slug: String
)

const val updateVenueName = """-- name: updateVenueName :one
UPDATE venue
SET name = ?
WHERE slug = ?
RETURNING id
"""

data class UpdateVenueNameParams (
val name: String,
val slug: String
)

const val venueCountByCity = """-- name: venueCountByCity :many
SELECT
city,
Expand All @@ -133,10 +103,10 @@ class QueriesImpl(private val conn: Connection) : Queries {
// This is the third line

@Throws(SQLException::class)
override fun createCity(arg: CreateCityParams): City {
override fun createCity(name: String, slug: String): City {
return conn.prepareStatement(createCity).use { stmt ->
stmt.setString(1, arg.name)
stmt.setString(2, arg.slug)
stmt.setString(1, name)
stmt.setString(2, slug)

val results = stmt.executeQuery()
if (!results.next()) {
Expand All @@ -154,15 +124,22 @@ class QueriesImpl(private val conn: Connection) : Queries {
}

@Throws(SQLException::class)
override fun createVenue(arg: CreateVenueParams): Int {
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, arg.slug)
stmt.setString(2, arg.name)
stmt.setString(3, arg.city)
stmt.setString(4, arg.spotifyPlaylist)
stmt.setObject(5, arg.status.value, Types.OTHER)
stmt.setArray(6, conn.createArrayOf("status", arg.statuses.map { v -> v.value }.toTypedArray()))
stmt.setArray(7, conn.createArrayOf("text", arg.tags.toTypedArray()))
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()) {
Expand Down Expand Up @@ -207,10 +184,10 @@ class QueriesImpl(private val conn: Connection) : Queries {
}

@Throws(SQLException::class)
override fun getVenue(arg: GetVenueParams): Venue {
override fun getVenue(slug: String, city: String): Venue {
return conn.prepareStatement(getVenue).use { stmt ->
stmt.setString(1, arg.slug)
stmt.setString(2, arg.city)
stmt.setString(1, slug)
stmt.setString(2, city)

val results = stmt.executeQuery()
if (!results.next()) {
Expand Down Expand Up @@ -277,20 +254,20 @@ class QueriesImpl(private val conn: Connection) : Queries {
}

@Throws(SQLException::class)
override fun updateCityName(arg: UpdateCityNameParams) {
override fun updateCityName(name: String, slug: String) {
conn.prepareStatement(updateCityName).use { stmt ->
stmt.setString(1, arg.name)
stmt.setString(2, arg.slug)
stmt.setString(1, name)
stmt.setString(2, slug)

stmt.execute()
}
}

@Throws(SQLException::class)
override fun updateVenueName(arg: UpdateVenueNameParams): Int {
override fun updateVenueName(name: String, slug: String): Int {
return conn.prepareStatement(updateVenueName).use { stmt ->
stmt.setString(1, arg.name)
stmt.setString(2, arg.slug)
stmt.setString(1, name)
stmt.setString(2, slug)

val results = stmt.executeQuery()
if (!results.next()) {
Expand Down
Loading