Debugging
Viewing generated SQL
For each supported database there is a method to directly generate SQL in that dialect
for the purpose of inspection.
These methods can be used without instantiating a DataSource
.
This makes them perfect for locally inspecting generated SQL in your IDE or in unit tests.
Dialect | Method | Import |
---|---|---|
MySQL | PerformableSql::generateMysqlSql | import io.koalaql.mysql.generateMysqlSql |
Postgres | PerformableSql::generatePostgresSql | import io.koalaql.postgres.generatePostgresSql |
H2 | PerformableSql::generateH2Sql | import io.koalaql.h2.generateH2Sql |
Example
Here is an example of using generatePostgresSql
to print some generated SQL
- Code
- Output
val generated: CompiledSql? = CustomerTable
.where(CustomerTable.id eq 123)
.generatePostgresSql()
println(generated?.parameterizedSql)
SELECT T0."id" c0
, T0."shop" c1
, T0."name" c2
, T0."spent" c3
FROM "Customer" T0
WHERE T0."id" = ?
note
The return type is nullable because certain Koala statements may be no-ops. An example is attempting to insert with an empty list of values.
Using a DataSource
The methods above are handy if you want to quickly see the generated SQL for a dialect.
If you want to see the exact SQL generated by a DataSource
at runtime,
you can use the generateSql
method.
- Code
- Output
val generated: CompiledSql? = CustomerTable
.where(CustomerTable.id eq 123)
.generateSql(db) /* pass our DataSource */
println(generated?.parameterizedSql)
SELECT T0."id" c0
, T0."shop" c1
, T0."name" c2
, T0."spent" c3
FROM "Customer" T0
WHERE T0."id" = ?