How to print query sql on SQLDelight

Problem

How to print query sql on SQLDelight

Problem

We use SQLDelight on our KMM project to operate sqlite database, and I want to print the query sql it executed during development, I search the internet, but didn’t get the answer, and yesterday, I find the solution by accident.

Solution

In SQLDelight 1.5.3, there is a file LogSqliteDriver.kt which located in runtime/src/commonMain/kotlin/com/squareup/sqldelight/logs/LogSqliteDriver.kt when we create SQLDelight driver we can wrap it in LogSqliteDriver like below:# Original function
actual fun createDriver(): SqlDriver {
   return NativeSqliteDriver(AppDatabase.Schema, "test.db")
}# Wrap with LogSqliteDriver
actual fun createDriver(): SqlDriver {
   return LogSqliteDriver(sqlDriver= NativeSqliteDriver(AppDatabase.Schema, "test.db")) {log ->
       // print the log
       Napier.d(log)
   }
}