Skip to content

Update examples with big decimal changes #2

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 @@ -4,6 +4,7 @@

package com.example.ondeck.postgresql

import java.math.BigDecimal
import java.time.LocalDateTime

// Venues can be either open or closed
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions examples/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -36,6 +43,9 @@ interface Queries {
@Throws(SQLException::class)
fun listCities(): List<City>

@Throws(SQLException::class)
fun listCounts(): List<Count>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -231,6 +275,23 @@ class QueriesImpl(private val conn: Connection) : Queries {
}
}

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

val results = stmt.executeQuery()
val ret = mutableListOf<Count>()
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<Venue> {
return conn.prepareStatement(listVenues).use { stmt ->
Expand Down
14 changes: 14 additions & 0 deletions examples/src/main/resources/ondeck/postgresql/query/numeric.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- name: ListCounts :many
SELECT *
FROM counts;

-- name: CreateCounts :one
INSERT INTO counts (
slug,
count,
increments
) VALUES (
$1,
$2,
$3
) RETURNING *;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE counts (
slug text PRIMARY KEY,
count numeric NOT NULL,
increments numeric
);