|
| 1 | +package net.samyn.kapper |
| 2 | + |
| 3 | +import java.sql.Connection |
| 4 | +import java.sql.ResultSet |
| 5 | +import kotlin.reflect.KClass |
| 6 | + |
| 7 | +/** |
| 8 | + * Execute a SQL statement with a RETURNING clause and map the results to a list of instances of the specified class. |
| 9 | + * |
| 10 | + * This function uses reflection to automatically map the result set columns to the properties of the specified class. |
| 11 | + * For advanced mappings, use the overloaded version with a custom `mapper` function. |
| 12 | + * |
| 13 | + * **Example**: |
| 14 | + * ```kotlin |
| 15 | + * data class Hero(val id: UUID, val name: String, val updatedAt: Instant) |
| 16 | + * |
| 17 | + * val heroes: List<Hero> = connection.executeReturning( |
| 18 | + * sql = """ |
| 19 | + * INSERT INTO heroes (id, name) VALUES (:id, :name) |
| 20 | + * ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name |
| 21 | + * RETURNING * |
| 22 | + * """, |
| 23 | + * "id" to UUID.randomUUID(), |
| 24 | + * "name" to "Superman" |
| 25 | + * ) |
| 26 | + * ``` |
| 27 | + * |
| 28 | + * @param sql The SQL statement to execute, including a RETURNING clause. |
| 29 | + * @param args Optional key-value pairs representing named parameters to substitute into the statement. |
| 30 | + * @return The rows returned by the RETURNING clause as a list of [T] instances. |
| 31 | + * @throws java.sql.SQLException If there's a database error. |
| 32 | + */ |
| 33 | +inline fun <reified T : Any> Connection.executeReturning( |
| 34 | + sql: String, |
| 35 | + vararg args: Pair<String, Any?>, |
| 36 | +): List<T> = executeReturning(T::class, sql, *args) |
| 37 | + |
| 38 | +/** |
| 39 | + * Execute a SQL statement with a RETURNING clause and map the results to a list of instances of the specified class |
| 40 | + * with a custom mapper. |
| 41 | + * |
| 42 | + * **Example**: |
| 43 | + * ```kotlin |
| 44 | + * val heroes: List<Hero> = connection.executeReturning( |
| 45 | + * sql = "INSERT INTO heroes (id, name) VALUES (:id, :name) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name RETURNING *", |
| 46 | + * mapper = { resultSet, _ -> |
| 47 | + * Hero( |
| 48 | + * id = resultSet.getObject("id", UUID::class.java), |
| 49 | + * name = resultSet.getString("name") |
| 50 | + * ) |
| 51 | + * }, |
| 52 | + * "id" to UUID.randomUUID(), |
| 53 | + * "name" to "Superman" |
| 54 | + * ) |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * @param sql The SQL statement to execute, including a RETURNING clause. |
| 58 | + * @param mapper Custom mapping function to transform the [ResultSet] into the target class. |
| 59 | + * @param args Optional parameters to be substituted in the SQL statement during execution. |
| 60 | + * @return The rows returned by the RETURNING clause as a list of [T] instances. |
| 61 | + */ |
| 62 | +inline fun <reified T : Any> Connection.executeReturning( |
| 63 | + sql: String, |
| 64 | + noinline mapper: (ResultSet, Map<String, Field>) -> T, |
| 65 | + vararg args: Pair<String, Any?>, |
| 66 | +): List<T> = executeReturning(T::class, sql, mapper, args.toMap()) |
| 67 | + |
| 68 | +/** |
| 69 | + * Execute a SQL statement with a RETURNING clause and map the results to a list of instances of the specified class. |
| 70 | + * |
| 71 | + * @param clazz The class to map the results to. |
| 72 | + * @param sql The SQL statement to execute, including a RETURNING clause. |
| 73 | + * @param args Optional parameters to be substituted in the SQL statement during execution. |
| 74 | + * @return The rows returned by the RETURNING clause as a list of [T] instances. |
| 75 | + */ |
| 76 | +fun <T : Any> Connection.executeReturning( |
| 77 | + clazz: KClass<T>, |
| 78 | + sql: String, |
| 79 | + vararg args: Pair<String, Any?>, |
| 80 | +): List<T> = Kapper.instance.executeReturning(clazz.java, this, sql, args.toMap()) |
| 81 | + |
| 82 | +/** |
| 83 | + * Execute a SQL statement with a RETURNING clause and map the results to a list of instances of the specified class |
| 84 | + * with a custom mapper. |
| 85 | + * |
| 86 | + * @param clazz The class to map the results to. |
| 87 | + * @param sql The SQL statement to execute, including a RETURNING clause. |
| 88 | + * @param mapper Custom mapping function to transform the [ResultSet] into the target class. |
| 89 | + * @param args Optional parameters to be substituted in the SQL statement during execution. |
| 90 | + * @return The rows returned by the RETURNING clause as a list of [T] instances. |
| 91 | + */ |
| 92 | +fun <T : Any> Connection.executeReturning( |
| 93 | + clazz: KClass<T>, |
| 94 | + sql: String, |
| 95 | + mapper: (ResultSet, Map<String, Field>) -> T, |
| 96 | + args: Map<String, Any?>, |
| 97 | +): List<T> = Kapper.instance.executeReturning(clazz.java, this, sql, mapper, args) |
| 98 | + |
| 99 | +/** |
| 100 | + * Execute a SQL statement with a RETURNING clause using an object and argument mapper functions, |
| 101 | + * and map the results to a list of instances of the specified class. |
| 102 | + * |
| 103 | + * The argument object type [A] and the returned row type [R] are intentionally separate, |
| 104 | + * allowing patterns such as passing a `CreateFoo` request and receiving a `Foo` result. |
| 105 | + * |
| 106 | + * **Example**: |
| 107 | + * ```kotlin |
| 108 | + * data class Hero(val id: UUID, val name: String) |
| 109 | + * |
| 110 | + * val hero = Hero(id = UUID.randomUUID(), name = "Superman") |
| 111 | + * val result: List<Hero> = connection.executeReturning( |
| 112 | + * sql = """ |
| 113 | + * INSERT INTO heroes (id, name) VALUES (:id, :name) |
| 114 | + * ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name |
| 115 | + * RETURNING * |
| 116 | + * """, |
| 117 | + * obj = hero, |
| 118 | + * "id" to Hero::id, |
| 119 | + * "name" to Hero::name |
| 120 | + * ) |
| 121 | + * ``` |
| 122 | + * |
| 123 | + * @param sql The SQL statement to execute, including a RETURNING clause. |
| 124 | + * @param obj The object containing the values to be used in the SQL statement. |
| 125 | + * @param args Argument mappers that extract values from the object for parameter substitution. |
| 126 | + * @return The rows returned by the RETURNING clause as a list of [R] instances. |
| 127 | + * @throws java.sql.SQLException If there's a database error. |
| 128 | + */ |
| 129 | +inline fun <reified R : Any, A : Any> Connection.executeReturning( |
| 130 | + sql: String, |
| 131 | + obj: A, |
| 132 | + vararg args: ArgMapper<A>, |
| 133 | +): List<R> = Kapper.instance.executeReturning(R::class.java, this, sql, obj, args.toMap()) |
| 134 | + |
| 135 | +/** |
| 136 | + * Execute a SQL statement with a RETURNING clause using an object, argument mapper functions, and a custom result mapper. |
| 137 | + * |
| 138 | + * The argument object type [A] and the returned row type [R] are intentionally separate, |
| 139 | + * allowing patterns such as passing a `CreateFoo` request and receiving a `Foo` result. |
| 140 | + * |
| 141 | + * @param sql The SQL statement to execute, including a RETURNING clause. |
| 142 | + * @param mapper Custom mapping function to transform the [ResultSet] into the target class. |
| 143 | + * @param obj The object containing the values to be used in the SQL statement. |
| 144 | + * @param args Argument mappers that extract values from the object for parameter substitution. |
| 145 | + * @return The rows returned by the RETURNING clause as a list of [R] instances. |
| 146 | + * @throws java.sql.SQLException If there's a database error. |
| 147 | + */ |
| 148 | +inline fun <reified R : Any, A : Any> Connection.executeReturning( |
| 149 | + sql: String, |
| 150 | + noinline mapper: (ResultSet, Map<String, Field>) -> R, |
| 151 | + obj: A, |
| 152 | + vararg args: ArgMapper<A>, |
| 153 | +): List<R> = Kapper.instance.executeReturning(R::class.java, this, sql, mapper, obj, args.toMap()) |
0 commit comments