Skip to content

Update examples with nullable enum #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 2 additions & 3 deletions examples/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
version: '2'
plugins:
- name: kt
wasm:
url: https://downloads.sqlc.dev/plugin/sqlc-gen-kotlin_1.2.0.wasm
sha256: 22b437ecaea66417bbd3b958339d9868ba89368ce542c936c37305acf373104b
process:
cmd: ../plugin/plugin
sql:
- schema: src/main/resources/authors/postgresql/schema.sql
queries: src/main/resources/authors/postgresql/query.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum class BooksBookType(val value: String) {

companion object {
private val map = BooksBookType.values().associateBy(BooksBookType::value)
fun lookup(value: String) = map[value]
fun lookup(value: String?) = map[value]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum class BookType(val value: String) {

companion object {
private val map = BookType.values().associateBy(BookType::value)
fun lookup(value: String) = map[value]
fun lookup(value: String?) = map[value]
}
}

Expand Down
18 changes: 17 additions & 1 deletion examples/src/main/kotlin/com/example/ondeck/mysql/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ package com.example.ondeck.mysql
import java.sql.Timestamp
import java.time.Instant

enum class PeopleMood(val value: String) {
HAPPY("happy"),
SAD("sad");

companion object {
private val map = PeopleMood.values().associateBy(PeopleMood::value)
fun lookup(value: String?) = map[value]
}
}

enum class VenueStatus(val value: String) {
OPEN("open"),
CLOSED("closed");

companion object {
private val map = VenueStatus.values().associateBy(VenueStatus::value)
fun lookup(value: String) = map[value]
fun lookup(value: String?) = map[value]
}
}

Expand All @@ -22,6 +32,12 @@ data class City (
val name: String
)

data class Person (
val id: Long,
val name: String,
val mood: PeopleMood?
)

// Venues are places where muisc happens
data class Venue (
val id: Long,
Expand Down
6 changes: 6 additions & 0 deletions examples/src/main/kotlin/com/example/ondeck/mysql/Queries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface Queries {
@Throws(SQLException::class)
fun createCity(name: String, slug: String)

@Throws(SQLException::class)
fun createPerson(name: String, mood: PeopleMood?)

@Throws(SQLException::class)
fun createVenue(
slug: String,
Expand All @@ -36,6 +39,9 @@ interface Queries {
@Throws(SQLException::class)
fun listCities(): List<City>

@Throws(SQLException::class)
fun listPeople(): List<Person>

@Throws(SQLException::class)
fun listVenues(city: String): List<Venue>

Expand Down
42 changes: 42 additions & 0 deletions examples/src/main/kotlin/com/example/ondeck/mysql/QueriesImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ INSERT INTO city (
)
"""

const val createPerson = """-- name: createPerson :exec
INSERT INTO people (
name,
mood
) VALUES (
?,
?
)
"""

const val createVenue = """-- name: createVenue :execresult
INSERT INTO venue (
slug,
Expand Down Expand Up @@ -65,6 +75,11 @@ FROM city
ORDER BY name
"""

const val listPeople = """-- name: listPeople :many
SELECT id, name, mood
FROM people
"""

const val listVenues = """-- name: listVenues :many
SELECT id, status, statuses, slug, name, city, spotify_playlist, songkick_id, tags, created_at
FROM venue
Expand Down Expand Up @@ -110,6 +125,16 @@ class QueriesImpl(private val conn: Connection) : Queries {
}
}

@Throws(SQLException::class)
override fun createPerson(name: String, mood: PeopleMood?) {
conn.prepareStatement(createPerson).use { stmt ->
stmt.setString(1, name)
stmt.setString(2, mood?.value)

stmt.execute()
}
}

@Throws(SQLException::class)
override fun createVenue(
slug: String,
Expand Down Expand Up @@ -213,6 +238,23 @@ class QueriesImpl(private val conn: Connection) : Queries {
}
}

@Throws(SQLException::class)
override fun listPeople(): List<Person> {
return conn.prepareStatement(listPeople).use { stmt ->

val results = stmt.executeQuery()
val ret = mutableListOf<Person>()
while (results.next()) {
ret.add(Person(
results.getLong(1),
results.getString(2),
PeopleMood.lookup(results.getString(3))
))
}
ret
}
}

@Throws(SQLException::class)
override fun listVenues(city: String): List<Venue> {
return conn.prepareStatement(listVenues).use { stmt ->
Expand Down
18 changes: 17 additions & 1 deletion examples/src/main/kotlin/com/example/ondeck/postgresql/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ package com.example.ondeck.postgresql

import java.time.LocalDateTime

enum class Mood(val value: String) {
HAPPY("happy"),
SAD("sad");

companion object {
private val map = Mood.values().associateBy(Mood::value)
fun lookup(value: String?) = map[value]
}
}

// Venues can be either open or closed
enum class Status(val value: String) {
OPEN("op!en"),
CLOSED("clo@sed");

companion object {
private val map = Status.values().associateBy(Status::value)
fun lookup(value: String) = map[value]
fun lookup(value: String?) = map[value]
}
}

Expand All @@ -22,6 +32,12 @@ data class City (
val name: String
)

data class Person (
val id: Int,
val name: String,
val mood: Mood?
)

// Venues are places where muisc happens
data class Venue (
val id: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface Queries {
@Throws(SQLException::class)
fun createCity(name: String, slug: String): City?

@Throws(SQLException::class)
fun createPerson(name: String, mood: Mood?): Int?

@Throws(SQLException::class)
fun createVenue(
slug: String,
Expand All @@ -36,6 +39,9 @@ interface Queries {
@Throws(SQLException::class)
fun listCities(): List<City>

@Throws(SQLException::class)
fun listPeople(): List<Person>

@Throws(SQLException::class)
fun listVenues(city: String): List<Venue>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ INSERT INTO city (
) RETURNING slug, name
"""

const val createPerson = """-- name: createPerson :one
INSERT INTO people (
name,
mood
) VALUES (
?,
?
) RETURNING id
"""

const val createVenue = """-- name: createVenue :one
INSERT INTO venue (
slug,
Expand Down Expand Up @@ -65,6 +75,11 @@ FROM city
ORDER BY name
"""

const val listPeople = """-- name: listPeople :many
SELECT id, name, mood
FROM people
"""

const val listVenues = """-- name: listVenues :many
SELECT id, status, statuses, slug, name, city, spotify_playlist, songkick_id, tags, created_at
FROM venue
Expand Down Expand Up @@ -126,6 +141,24 @@ class QueriesImpl(private val conn: Connection) : Queries {
}
}

@Throws(SQLException::class)
override fun createPerson(name: String, mood: Mood?): Int? {
return conn.prepareStatement(createPerson).use { stmt ->
stmt.setString(1, name)
stmt.setObject(2, mood?.value, Types.OTHER)

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 createVenue(
slug: String,
Expand Down Expand Up @@ -231,6 +264,23 @@ class QueriesImpl(private val conn: Connection) : Queries {
}
}

@Throws(SQLException::class)
override fun listPeople(): List<Person> {
return conn.prepareStatement(listPeople).use { stmt ->

val results = stmt.executeQuery()
val ret = mutableListOf<Person>()
while (results.next()) {
ret.add(Person(
results.getInt(1),
results.getString(2),
Mood.lookup(results.getString(3))
))
}
ret
}
}

@Throws(SQLException::class)
override fun listVenues(city: String): List<Venue> {
return conn.prepareStatement(listVenues).use { stmt ->
Expand Down
12 changes: 12 additions & 0 deletions examples/src/main/resources/ondeck/mysql/query/people.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- name: ListPeople :many
SELECT *
FROM people;

-- name: CreatePerson :exec
INSERT INTO people (
name,
mood
) VALUES (
?,
?
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE people (
id SERIAL primary key,
name varchar(255) NOT NULL,
mood ENUM('happy', 'sad')
);
12 changes: 12 additions & 0 deletions examples/src/main/resources/ondeck/postgresql/query/people.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- name: ListPeople :many
SELECT *
FROM people;

-- name: CreatePerson :one
INSERT INTO people (
name,
mood
) VALUES (
$1,
$2
) RETURNING id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TYPE mood AS ENUM ('happy', 'sad');

CREATE TABLE people (
id SERIAL primary key,
name varchar(255) NOT NULL,
mood mood
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.ondeck.postgresql

import com.example.dbtest.PostgresDbTestExtension
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension

Expand Down Expand Up @@ -42,5 +43,14 @@ class QueriesImplTest {
assertEquals(venue.id, id)

q.deleteVenue(venue.slug)

val person1Id = q.createPerson("Jane", null)
val person2Id = q.createPerson("John", Mood.HAPPY)
val people = q.listPeople()
assertEquals(2, people.size)
val person1 = people.first { it.id == person1Id }
val person2 = people.first { it.id == person2Id }
assertNull(person1.mood)
assertEquals(Mood.HAPPY, person2.mood)
}
}