Skip to content
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 @@ -17,6 +17,12 @@ enum class Status(val value: String) {
}
}

data class CheckingAccount (
val id: Int,
val depositAmount: Int?,
val depositRate: Double?
)

data class City (
val slug: String,
val name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import java.sql.Types
import java.time.LocalDateTime

interface Queries {
@Throws(SQLException::class)
fun createCheckingAccount(depositAmount: Int?, depositRate: Double?): CheckingAccount?

@Throws(SQLException::class)
fun createCity(name: String, slug: String): City?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import java.sql.Statement
import java.sql.Types
import java.time.LocalDateTime

const val createCheckingAccount = """-- name: createCheckingAccount :one
INSERT INTO checking_accounts (deposit_amount, deposit_rate)
VALUES (?, ?) RETURNING id, deposit_amount, deposit_rate
"""

const val createCity = """-- name: createCity :one
INSERT INTO city (
name,
Expand Down Expand Up @@ -101,6 +106,28 @@ data class VenueCountByCityRow (

class QueriesImpl(private val conn: Connection) : Queries {

@Throws(SQLException::class)
override fun createCheckingAccount(depositAmount: Int?, depositRate: Double?): CheckingAccount? {
return conn.prepareStatement(createCheckingAccount).use { stmt ->
if (depositAmount != null) stmt.setInt(1, depositAmount) else stmt.setNull(1, Types.INTEGER)
if (depositRate != null) stmt.setDouble(2, depositRate) else stmt.setNull(2, Types.DOUBLE)

val results = stmt.executeQuery()
if (!results.next()) {
return null
}
val ret = CheckingAccount(
results.getInt(1),
results.getInt(2),
results.getDouble(3)
)
if (results.next()) {
throw SQLException("expected one row in result set, but got many")
}
ret
}
}

// Create a new city. The slug must be unique.
// This is the second line of the comment
// This is the third line
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- name: CreateCheckingAccount :one
INSERT INTO checking_accounts (deposit_amount, deposit_rate)
VALUES ($1, $2) RETURNING *;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE checking_accounts
(
id SERIAL PRIMARY KEY,
deposit_amount INT,
deposit_rate DOUBLE PRECISION
);