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..e4f38c1 100644 --- a/examples/src/main/kotlin/com/example/ondeck/postgresql/Models.kt +++ b/examples/src/main/kotlin/com/example/ondeck/postgresql/Models.kt @@ -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 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..89ca81a 100644 --- a/examples/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt +++ b/examples/src/main/kotlin/com/example/ondeck/postgresql/Queries.kt @@ -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? 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..0ba0d0a 100644 --- a/examples/src/main/kotlin/com/example/ondeck/postgresql/QueriesImpl.kt +++ b/examples/src/main/kotlin/com/example/ondeck/postgresql/QueriesImpl.kt @@ -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, @@ -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 diff --git a/examples/src/main/resources/ondeck/postgresql/query/checking_account.sql b/examples/src/main/resources/ondeck/postgresql/query/checking_account.sql new file mode 100644 index 0000000..7b39016 --- /dev/null +++ b/examples/src/main/resources/ondeck/postgresql/query/checking_account.sql @@ -0,0 +1,3 @@ +-- name: CreateCheckingAccount :one +INSERT INTO checking_accounts (deposit_amount, deposit_rate) +VALUES ($1, $2) RETURNING *; diff --git a/examples/src/main/resources/ondeck/postgresql/schema/0004_checking_account.sql b/examples/src/main/resources/ondeck/postgresql/schema/0004_checking_account.sql new file mode 100644 index 0000000..84022fb --- /dev/null +++ b/examples/src/main/resources/ondeck/postgresql/schema/0004_checking_account.sql @@ -0,0 +1,6 @@ +CREATE TABLE checking_accounts +( + id SERIAL PRIMARY KEY, + deposit_amount INT, + deposit_rate DOUBLE PRECISION +);