|
| 1 | +// Code generated by sqlc. DO NOT EDIT. |
| 2 | +// versions: |
| 3 | +// sqlc v1.15.0 |
| 4 | + |
| 5 | +package com.example.authors.postgresql |
| 6 | + |
| 7 | +import java.sql.Connection |
| 8 | +import java.sql.SQLException |
| 9 | +import java.sql.Statement |
| 10 | + |
| 11 | +const val createAuthor = """-- name: createAuthor :one |
| 12 | +INSERT INTO authors ( |
| 13 | + name, bio |
| 14 | +) VALUES ( |
| 15 | + ?, ? |
| 16 | +) |
| 17 | +RETURNING id, name, bio |
| 18 | +""" |
| 19 | + |
| 20 | +const val deleteAuthor = """-- name: deleteAuthor :exec |
| 21 | +DELETE FROM authors |
| 22 | +WHERE id = ? |
| 23 | +""" |
| 24 | + |
| 25 | +const val getAuthor = """-- name: getAuthor :one |
| 26 | +SELECT id, name, bio FROM authors |
| 27 | +WHERE id = ? LIMIT 1 |
| 28 | +""" |
| 29 | + |
| 30 | +const val listAuthors = """-- name: listAuthors :many |
| 31 | +SELECT id, name, bio FROM authors |
| 32 | +ORDER BY name |
| 33 | +""" |
| 34 | + |
| 35 | +class QueriesImpl(private val conn: Connection) : Queries { |
| 36 | + |
| 37 | + @Throws(SQLException::class) |
| 38 | + override fun createAuthor(name: String, bio: String?): Author? { |
| 39 | + return conn.prepareStatement(createAuthor).use { stmt -> |
| 40 | + stmt.setString(1, name) |
| 41 | + stmt.setString(2, bio) |
| 42 | + |
| 43 | + val results = stmt.executeQuery() |
| 44 | + if (!results.next()) { |
| 45 | + return null |
| 46 | + } |
| 47 | + val ret = Author( |
| 48 | + results.getLong(1), |
| 49 | + results.getString(2), |
| 50 | + results.getString(3) |
| 51 | + ) |
| 52 | + if (results.next()) { |
| 53 | + throw SQLException("expected one row in result set, but got many") |
| 54 | + } |
| 55 | + ret |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Throws(SQLException::class) |
| 60 | + override fun deleteAuthor(id: Long) { |
| 61 | + conn.prepareStatement(deleteAuthor).use { stmt -> |
| 62 | + stmt.setLong(1, id) |
| 63 | + |
| 64 | + stmt.execute() |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Throws(SQLException::class) |
| 69 | + override fun getAuthor(id: Long): Author? { |
| 70 | + return conn.prepareStatement(getAuthor).use { stmt -> |
| 71 | + stmt.setLong(1, id) |
| 72 | + |
| 73 | + val results = stmt.executeQuery() |
| 74 | + if (!results.next()) { |
| 75 | + return null |
| 76 | + } |
| 77 | + val ret = Author( |
| 78 | + results.getLong(1), |
| 79 | + results.getString(2), |
| 80 | + results.getString(3) |
| 81 | + ) |
| 82 | + if (results.next()) { |
| 83 | + throw SQLException("expected one row in result set, but got many") |
| 84 | + } |
| 85 | + ret |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Throws(SQLException::class) |
| 90 | + override fun listAuthors(): List<Author> { |
| 91 | + return conn.prepareStatement(listAuthors).use { stmt -> |
| 92 | + |
| 93 | + val results = stmt.executeQuery() |
| 94 | + val ret = mutableListOf<Author>() |
| 95 | + while (results.next()) { |
| 96 | + ret.add(Author( |
| 97 | + results.getLong(1), |
| 98 | + results.getString(2), |
| 99 | + results.getString(3) |
| 100 | + )) |
| 101 | + } |
| 102 | + ret |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
| 107 | + |
0 commit comments