https://github.com/eygraber/sqldelight-androidx-driver
A SQLDelight Driver that wraps AndroidX Kotlin Multiplatform SQLite
https://github.com/eygraber/sqldelight-androidx-driver
androidx androidx-sqlite kotlin kotlin-multiplatform sqldelight sqlite
Last synced: about 1 month ago
JSON representation
A SQLDelight Driver that wraps AndroidX Kotlin Multiplatform SQLite
- Host: GitHub
- URL: https://github.com/eygraber/sqldelight-androidx-driver
- Owner: eygraber
- License: mit
- Created: 2025-01-13T15:56:59.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2025-04-08T12:43:32.000Z (about 2 months ago)
- Last Synced: 2025-04-08T13:45:02.080Z (about 2 months ago)
- Topics: androidx, androidx-sqlite, kotlin, kotlin-multiplatform, sqldelight, sqlite
- Language: Kotlin
- Homepage:
- Size: 180 KB
- Stars: 47
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: changelog_config.json
- License: LICENSE
Awesome Lists containing this project
README
# SqlDelight AndroidX Driver
`sqldelight-androidx-driver` provides a [SQLDelight] `SqlDriver` that wraps the [AndroidX Kotlin Multiplatform SQLite]
libraries.It works with any of the available implementations of AndroidX SQLite; see their documentation for more information.
## Gradle
```kotlin
repositories {
mavenCentral()
}dependencies {
implementation("com.eygraber:sqldelight-androidx-driver:0.0.10")
}
```## Usage
Assuming the following configuration:```kotlin
sqldelight {
databases {
create("Database")
}
}
```you get started by creating a `AndroidxSqliteDriver`:
```kotlin
Database(
AndroidxSqliteDriver(
driver = BundledSQLiteDriver(),
type = AndroidxSqliteDatabaseType.File(""),
schema = Database.Schema,
)
)
```on Android and JVM you can pass a `File`:
```kotlin
Database(
AndroidxSqliteDriver(
driver = BundledSQLiteDriver(),
type = AndroidxSqliteDatabaseType.File(File("my.db")),
schema = Database.Schema,
)
)
```and on Android you can pass a `Context` to create the file in the app's database directory:
```kotlin
Database(
AndroidxSqliteDriver(
driver = BundledSQLiteDriver(),
type = AndroidxSqliteDatabaseType.File(context, "my.db"),
schema = Database.Schema,
)
)
```If you want to provide `OpenFlags` to the bundled or native driver, you can use:
```kotlin
Database(
AndroidxSqliteDriver(
createConnection = { name ->
BundledSQLiteDriver().open(name, SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE)
},
type = AndroidxSqliteDatabaseType.File(""),
schema = Database.Schema,
)
)
```It will handle calling the `create` and `migrate` functions on your schema for you, and keep track of the database's version.
## Connection Pooling
By default, one connection will be used for both reading and writing, and only one thread can acquire that connection at a time.
If you have WAL enabled, you could (and should) set the amount of pooled reader connections that will be used:```kotlin
AndroidxSqliteDriver(
...,
readerConnections = 4,
...,
)
```On Android you can defer to the system to determine how many reader connections there should be[1]:
```kotlin
// Based on SQLiteGlobal.getWALConnectionPoolSize()
fun getWALConnectionPoolSize() {
val resources = Resources.getSystem()
val resId =
resources.getIdentifier("db_connection_pool_size", "integer", "android")
return if (resId != 0) {
resources.getInteger(resId)
} else {
2
}
}
```See [WAL & Dispatchers] for more information about how to configure dispatchers to use for reads and writes.
> [!NOTE]
> In-Memory and temporary databases will always use 0 reader connections i.e. there will be a single connection[1]: https://blog.p-y.wtf/parallelism-with-android-sqlite#heading-secondary-connections
[AndroidX Kotlin Multiplatform SQLite]: https://developer.android.com/kotlin/multiplatform/sqlite
[SQLDelight]: https://github.com/sqldelight/sqldelight
[WAL & Dispatchers]: https://blog.p-y.wtf/parallelism-with-android-sqlite#heading-wal-amp-dispatchers