Skip to main content

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.

DialectMethodImport
MySQLPerformableSql::generateMysqlSqlimport io.koalaql.mysql.generateMysqlSql
PostgresPerformableSql::generatePostgresSqlimport io.koalaql.postgres.generatePostgresSql
H2PerformableSql::generateH2Sqlimport io.koalaql.h2.generateH2Sql

Example

Here is an example of using generatePostgresSql to print some generated SQL

val generated: CompiledSql? = CustomerTable
.where(CustomerTable.id eq 123)
.generatePostgresSql()

println(generated?.parameterizedSql)
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.

val generated: CompiledSql? = CustomerTable
.where(CustomerTable.id eq 123)
.generateSql(db) /* pass our DataSource */

println(generated?.parameterizedSql)