Skip to content

Commit

Permalink
Merge pull request #41 from andersio/anders/config-options
Browse files Browse the repository at this point in the history
Support setting recursive_triggers and synchronous.
  • Loading branch information
kpgalligan committed May 11, 2021
2 parents 24ce67c + cb26eda commit 519fdc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ data class DatabaseConfiguration(
val busyTimeout: Int = 5000,
val pageSize: Int? = null,
val basePath: String? = null,
)
val synchronousFlag: SynchronousFlag? = null,
val recursiveTriggers: Boolean = false
)
data class Logging(
val logger: Logger = WarningLogger,
val verboseDataCalls: Boolean = false
Expand Down Expand Up @@ -94,3 +96,7 @@ enum class JournalMode {
}
}
}

enum class SynchronousFlag(val value: Int) {
OFF(0), NORMAL(1), FULL(2), EXTRA(3);
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,15 @@ fun DatabaseConnection.updateJournalMode(value: JournalMode): JournalMode {
}

fun DatabaseConnection.updateForeignKeyConstraints(enabled: Boolean) {
val newValue = if (enabled) {
1
} else {
0
}
withStatement("PRAGMA foreign_keys=$newValue") { execute() }
withStatement("PRAGMA foreign_keys=${enabled.toInt()}") { execute() }
}

fun DatabaseConnection.updateSynchronousFlag(flag: SynchronousFlag) {
withStatement("PRAGMA synchronous=${flag.value}") { execute() }
}

fun DatabaseConnection.updateRecursiveTriggers(enabled: Boolean) {
withStatement("PRAGMA recursive_triggers=${enabled.toInt()}") { execute() }
}

private fun Boolean.toInt(): Int = if (this) 1 else 0
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class NativeDatabaseManager(private val path:String,
val conn = NativeDatabaseConnection(this, connectionPtrArg)
configuration.lifecycleConfig.onCreateConnection(conn)

if (configuration.extendedConfig.synchronousFlag != null) {
conn.updateSynchronousFlag(configuration.extendedConfig.synchronousFlag)
}

if (configuration.encryptionConfig.rekey == null) {
configuration.encryptionConfig.key?.let { conn.setCipherKey(it) }
} else {
Expand All @@ -69,9 +73,15 @@ class NativeDatabaseManager(private val path:String,
}
}

if(configuration.extendedConfig.foreignKeyConstraints){
conn.updateForeignKeyConstraints(true)
}
// These flags should be explicitly set on each connection at all times.
//
// "should set the foreign key enforcement flag [...] and not depend on the default setting."
// https://www.sqlite.org/pragma.html#pragma_foreign_keys
// "Recursive triggers may be turned on by default in future versions of SQLite."
// https://www.sqlite.org/pragma.html#pragma_recursive_triggers
conn.updateForeignKeyConstraints(configuration.extendedConfig.foreignKeyConstraints)
conn.updateRecursiveTriggers(configuration.extendedConfig.recursiveTriggers)


if(newConnection.value == 0){
conn.updateJournalMode(configuration.journalMode)
Expand Down

0 comments on commit 519fdc1

Please sign in to comment.