Skip to content

Commit 519fdc1

Browse files
authored
Merge pull request #41 from andersio/anders/config-options
Support setting recursive_triggers and synchronous.
2 parents 24ce67c + cb26eda commit 519fdc1

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/DatabaseConfiguration.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ data class DatabaseConfiguration(
3636
val busyTimeout: Int = 5000,
3737
val pageSize: Int? = null,
3838
val basePath: String? = null,
39-
)
39+
val synchronousFlag: SynchronousFlag? = null,
40+
val recursiveTriggers: Boolean = false
41+
)
4042
data class Logging(
4143
val logger: Logger = WarningLogger,
4244
val verboseDataCalls: Boolean = false
@@ -94,3 +96,7 @@ enum class JournalMode {
9496
}
9597
}
9698
}
99+
100+
enum class SynchronousFlag(val value: Int) {
101+
OFF(0), NORMAL(1), FULL(2), EXTRA(3);
102+
}

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/DatabaseConnection.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ fun DatabaseConnection.updateJournalMode(value: JournalMode): JournalMode {
105105
}
106106

107107
fun DatabaseConnection.updateForeignKeyConstraints(enabled: Boolean) {
108-
val newValue = if (enabled) {
109-
1
110-
} else {
111-
0
112-
}
113-
withStatement("PRAGMA foreign_keys=$newValue") { execute() }
108+
withStatement("PRAGMA foreign_keys=${enabled.toInt()}") { execute() }
109+
}
110+
111+
fun DatabaseConnection.updateSynchronousFlag(flag: SynchronousFlag) {
112+
withStatement("PRAGMA synchronous=${flag.value}") { execute() }
114113
}
114+
115+
fun DatabaseConnection.updateRecursiveTriggers(enabled: Boolean) {
116+
withStatement("PRAGMA recursive_triggers=${enabled.toInt()}") { execute() }
117+
}
118+
119+
private fun Boolean.toInt(): Int = if (this) 1 else 0

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/native/NativeDatabaseManager.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class NativeDatabaseManager(private val path:String,
5858
val conn = NativeDatabaseConnection(this, connectionPtrArg)
5959
configuration.lifecycleConfig.onCreateConnection(conn)
6060

61+
if (configuration.extendedConfig.synchronousFlag != null) {
62+
conn.updateSynchronousFlag(configuration.extendedConfig.synchronousFlag)
63+
}
64+
6165
if (configuration.encryptionConfig.rekey == null) {
6266
configuration.encryptionConfig.key?.let { conn.setCipherKey(it) }
6367
} else {
@@ -69,9 +73,15 @@ class NativeDatabaseManager(private val path:String,
6973
}
7074
}
7175

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

7686
if(newConnection.value == 0){
7787
conn.updateJournalMode(configuration.journalMode)

0 commit comments

Comments
 (0)