-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for foreign key configuration
Fixes #5
- Loading branch information
Tamim Attafi
committed
Mar 26, 2024
1 parent
ab0d53b
commit db530a3
Showing
6 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
.../src/androidMain/kotlin/com/attafitamim/kabin/core/database/KabinDatabaseConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
package com.attafitamim.kabin.core.database | ||
|
||
import android.content.Context | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
|
||
private const val DEFAULT_CACHE_SIZE = 20 | ||
|
||
typealias OpenCallback = (db: SupportSQLiteDatabase) -> Unit | ||
|
||
private fun createDefaultCallback( | ||
foreignKeyConstraintsEnabled: Boolean | ||
): OpenCallback = { db -> | ||
db.setForeignKeyConstraintsEnabled(foreignKeyConstraintsEnabled) | ||
} | ||
|
||
actual class KabinDatabaseConfiguration( | ||
val context: Context, | ||
val name: String? = null, | ||
val cacheSize: Int = DEFAULT_CACHE_SIZE, | ||
val useNoBackupDirectory: Boolean = false, | ||
val windowSizeBytes: Long? = null, | ||
val foreignKeyConstraintsEnabled: Boolean = true, | ||
val onOpen: OpenCallback? = createDefaultCallback(foreignKeyConstraintsEnabled) | ||
) |
31 changes: 23 additions & 8 deletions
31
...ry/core/src/androidMain/kotlin/com/attafitamim/kabin/core/driver/DriverFactory.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
package com.attafitamim.kabin.core.driver | ||
|
||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import app.cash.sqldelight.async.coroutines.synchronous | ||
import app.cash.sqldelight.db.QueryResult | ||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.db.SqlSchema | ||
import app.cash.sqldelight.driver.android.AndroidSqliteDriver | ||
import com.attafitamim.kabin.core.database.KabinDatabaseConfiguration | ||
import com.attafitamim.kabin.core.database.OpenCallback | ||
|
||
private fun createCallback( | ||
schema: SqlSchema<QueryResult.Value<Unit>>, | ||
onOpen: OpenCallback?, | ||
) = object : AndroidSqliteDriver.Callback(schema) { | ||
override fun onOpen(db: SupportSQLiteDatabase) { | ||
onOpen?.invoke(db) | ||
} | ||
} | ||
|
||
actual fun KabinDatabaseConfiguration.createDriver( | ||
schema: SqlSchema<QueryResult.AsyncValue<Unit>>, | ||
): SqlDriver = AndroidSqliteDriver( | ||
schema.synchronous(), | ||
context, | ||
name, | ||
cacheSize = cacheSize, | ||
useNoBackupDirectory = useNoBackupDirectory, | ||
windowSizeBytes = windowSizeBytes | ||
) | ||
): SqlDriver { | ||
val synchronousSchema = schema.synchronous() | ||
return AndroidSqliteDriver( | ||
synchronousSchema, | ||
context, | ||
name, | ||
cacheSize = cacheSize, | ||
useNoBackupDirectory = useNoBackupDirectory, | ||
windowSizeBytes = windowSizeBytes, | ||
callback = createCallback(synchronousSchema, onOpen) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters