diff --git a/examples/sqlc.yaml b/examples/sqlc.yaml index 8a129bf..f70a3e8 100644 --- a/examples/sqlc.yaml +++ b/examples/sqlc.yaml @@ -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 diff --git a/examples/src/main/kotlin/com/example/ondeck/postgresql/Models.kt b/examples/src/main/kotlin/com/example/ondeck/postgresql/Models.kt index 66749ba..a73f1ef 100644 --- a/examples/src/main/kotlin/com/example/ondeck/postgresql/Models.kt +++ b/examples/src/main/kotlin/com/example/ondeck/postgresql/Models.kt @@ -4,6 +4,7 @@ package com.example.ondeck.postgresql +import java.math.BigDecimal import java.time.LocalDateTime // Venues can be either open or closed @@ -22,6 +23,12 @@ data class City ( val name: String ) +data class Count ( + val slug: String, + val count: BigDecimal, + val increments: BigDecimal? +) + // Venues are places where muisc happens data class Venue ( val id: Int, diff --git a/examples/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt b/examples/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt index dbcd3c8..23a0c06 100644 --- a/examples/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt +++ b/examples/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt @@ -4,6 +4,7 @@ package com.example.ondeck.postgresql +import java.math.BigDecimal import java.sql.Connection import java.sql.SQLException import java.sql.Statement @@ -14,6 +15,12 @@ interface Queries { @Throws(SQLException::class) fun createCity(name: String, slug: String): City? + @Throws(SQLException::class) + fun createCounts( + slug: String, + count: BigDecimal, + increments: BigDecimal?): Count? + @Throws(SQLException::class) fun createVenue( slug: String, @@ -36,6 +43,9 @@ interface Queries { @Throws(SQLException::class) fun listCities(): List + @Throws(SQLException::class) + fun listCounts(): List + @Throws(SQLException::class) fun listVenues(city: String): List diff --git a/examples/src/main/kotlin/com/example/ondeck/postgresql/QueriesImpl.kt b/examples/src/main/kotlin/com/example/ondeck/postgresql/QueriesImpl.kt index 1cdeefd..aa4237a 100644 --- a/examples/src/main/kotlin/com/example/ondeck/postgresql/QueriesImpl.kt +++ b/examples/src/main/kotlin/com/example/ondeck/postgresql/QueriesImpl.kt @@ -4,6 +4,7 @@ package com.example.ondeck.postgresql +import java.math.BigDecimal import java.sql.Connection import java.sql.SQLException import java.sql.Statement @@ -20,6 +21,18 @@ INSERT INTO city ( ) RETURNING slug, name """ +const val createCounts = """-- name: createCounts :one +INSERT INTO counts ( + slug, + count, + increments +) VALUES ( +?, +?, +? +) RETURNING slug, count, increments +""" + const val createVenue = """-- name: createVenue :one INSERT INTO venue ( slug, @@ -65,6 +78,11 @@ FROM city ORDER BY name """ +const val listCounts = """-- name: listCounts :many +SELECT slug, count, increments +FROM counts +""" + const val listVenues = """-- name: listVenues :many SELECT id, status, statuses, slug, name, city, spotify_playlist, songkick_id, tags, created_at FROM venue @@ -126,6 +144,32 @@ class QueriesImpl(private val conn: Connection) : Queries { } } + @Throws(SQLException::class) + override fun createCounts( + slug: String, + count: BigDecimal, + increments: BigDecimal?): Count? { + return conn.prepareStatement(createCounts).use { stmt -> + stmt.setString(1, slug) + stmt.setBigDecimal(2, count) + stmt.setBigDecimal(3, increments) + + val results = stmt.executeQuery() + if (!results.next()) { + return null + } + val ret = Count( + results.getString(1), + results.getBigDecimal(2), + results.getBigDecimal(3) + ) + if (results.next()) { + throw SQLException("expected one row in result set, but got many") + } + ret + } + } + @Throws(SQLException::class) override fun createVenue( slug: String, @@ -231,6 +275,23 @@ class QueriesImpl(private val conn: Connection) : Queries { } } + @Throws(SQLException::class) + override fun listCounts(): List { + return conn.prepareStatement(listCounts).use { stmt -> + + val results = stmt.executeQuery() + val ret = mutableListOf() + while (results.next()) { + ret.add(Count( + results.getString(1), + results.getBigDecimal(2), + results.getBigDecimal(3) + )) + } + ret + } + } + @Throws(SQLException::class) override fun listVenues(city: String): List { return conn.prepareStatement(listVenues).use { stmt -> diff --git a/examples/src/main/resources/ondeck/postgresql/query/numeric.sql b/examples/src/main/resources/ondeck/postgresql/query/numeric.sql new file mode 100644 index 0000000..371e965 --- /dev/null +++ b/examples/src/main/resources/ondeck/postgresql/query/numeric.sql @@ -0,0 +1,14 @@ +-- name: ListCounts :many +SELECT * +FROM counts; + +-- name: CreateCounts :one +INSERT INTO counts ( + slug, + count, + increments +) VALUES ( +$1, +$2, +$3 +) RETURNING *; \ No newline at end of file diff --git a/examples/src/main/resources/ondeck/postgresql/schema/0004_numeric.sql b/examples/src/main/resources/ondeck/postgresql/schema/0004_numeric.sql new file mode 100644 index 0000000..939aafd --- /dev/null +++ b/examples/src/main/resources/ondeck/postgresql/schema/0004_numeric.sql @@ -0,0 +1,5 @@ +CREATE TABLE counts ( + slug text PRIMARY KEY, + count numeric NOT NULL, + increments numeric +); \ No newline at end of file