Alexander Ioffe
07/08/2024, 8:33 AM// Uses Kotlinx Serialization, not Reflection!
@Serializable
data class Person(val id: Int, val firstName: String, val lastName: String)
// Run a Query, decode into a Data Class
val person: List<Person> = Sql("SELECT * FROM Person WHERE id = $id").queryOf<Person>().runOn(ctx)
Terpal-SQL does not use JVM reflection to construct or encode objects. It uses the Kotlinx-Serialization library in order to produce encoders and decoders. This makes it ideal for future extensions for non-JVM targets such as Kotlin multiplatform (please let me know if you are interested in using Terpal with KMP!).
https://github.com/deusaquilus/terpal-sqlCLOVIS
07/08/2024, 10:52 AMAlexander Ioffe
07/08/2024, 11:53 AM$dollar $sign $variables
before they are spliced into the parent string and puts them into a separate data-structure. There’s a more detailed explanation here:
https://github.com/deusaquilus/terpalCLOVIS
07/08/2024, 2:07 PMCLOVIS
07/08/2024, 2:08 PMThis is a very powerful feature that allows libraries to create DSLs that are both safe and easy to use. Sadly Kotlin does not have it.yet 👀 https://kotlinlang.slack.com/archives/CT0G9SD7Z/p1718216286102869
CLOVIS
07/08/2024, 2:10 PMAlexander Ioffe
07/08/2024, 3:31 PMio.exoquery.terpal.Interpolator
is missing. Then if you really try to be clever and include terpal-runtime (I.e the module that contains Interpolator
) in the dependencies without the terpal plugin, you’ll get the following error the second Sql
is called:Alexander Ioffe
07/08/2024, 3:33 PMCLOVIS
07/08/2024, 3:34 PMCLOVIS
07/08/2024, 3:34 PMInterpolator.interpolate
?Alexander Ioffe
07/08/2024, 3:39 PMInterpolator.interpolate
with bad strings I guess.Alexander Ioffe
07/08/2024, 3:39 PMCLOVIS
07/08/2024, 3:41 PMinterpolate
takes a string + the list of parameters, and bakes the query, right?
I can't look at the code right now, so I don't know if you've done it, but just in case: it should be annotated with an PossibleInjectionApi
opt-in or similar to make it really easy to auditAlexander Ioffe
07/08/2024, 3:41 PMCLOVIS
07/08/2024, 3:42 PMCLOVIS
07/08/2024, 3:43 PMCLOVIS
07/08/2024, 3:44 PMAlexander Ioffe
07/08/2024, 3:44 PMAlexander Ioffe
07/08/2024, 3:46 PMSql("...")
string clause. A sql.Param
and a sql.Statement
. If you try to splice a different datatype e.g. Int, Long, Float, Double, etc... it will attempt to wrap it in a sql.Param
object at compile-time. If it can't do that it will fail to compile with the following message:Alexander Ioffe
07/08/2024, 3:47 PMSerializer
or mark it as contextual (which tells Terpal-SQL that it needs to find a JDBC encoder for it).CLOVIS
07/08/2024, 3:48 PMAlexander Ioffe
07/08/2024, 3:48 PM