Skip to content

Commit

Permalink
Merge pull request #41 from anboralabs/stable-room-interface
Browse files Browse the repository at this point in the history
Added compatible stable methods with Room interface.
  • Loading branch information
dalgarins authored Nov 11, 2023
2 parents f6477a1 + 09dcee0 commit 4e17d4b
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ Please check which android room version are you using and select the perfect fit
- Which version use?

| Room | SpatiaRoom | LWGeom functions |
|------------------|------------|--------------------|
| ---------------- | ---------- | ------------------ |
| 2.3.0 | 0.2.3 | :no_entry: |
| >= 2.4.2 < 2.5.0 | 0.2.4 | :no_entry: |
| >= 2.5.0 | 0.2.7 | :white_check_mark: |
| >= 2.5.0 | 0.2.8 | :white_check_mark: |

- Android Room >= 2.5.0

```gradle
dependencies {
implementation 'com.github.anboralabs:spatia-room:0.2.7'
implementation 'com.github.anboralabs:spatia-room:0.2.8'
}
```

Expand Down
4 changes: 2 additions & 2 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 33
compileSdkVersion 34

defaultConfig {
applicationId "co.anbora.labs.spatiaroom"
minSdkVersion 21
targetSdkVersion 33
targetSdkVersion 34
versionCode 1
versionName "1.0"

Expand Down
6 changes: 3 additions & 3 deletions spatia-room/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'maven-publish'

android {
compileSdkVersion 33
compileSdkVersion 34

defaultConfig {
minSdkVersion 21
targetSdkVersion 33
targetSdkVersion 34

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down Expand Up @@ -61,7 +61,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.anboralabs'
artifactId = 'spatia-room'
version = '0.2.7'
version = '0.2.8'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package co.anbora.labs.spatia.builder

import android.content.Context
import android.content.Intent
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.AutoMigrationSpec
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper
import co.anbora.labs.spatia.db.SpatiaHelperFactory
import java.io.File
import java.io.InputStream
import java.util.concurrent.Callable
import java.util.concurrent.Executor
import java.util.concurrent.TimeUnit

class SpatiaBuilder<T : RoomDatabase> (
context: Context,
klass: Class<T>,
name: String?
): SpatiaRoom.Builder<T> {

private val templateDb = "spatia_db_template.sqlite"

private val roomBuilder = if (name != null) {
Room.databaseBuilder(
context.applicationContext,
Expand All @@ -36,6 +40,40 @@ class SpatiaBuilder<T : RoomDatabase> (
return this
}

override fun createFromAsset(
databaseFilePath: String,
callback: RoomDatabase.PrepackagedDatabaseCallback
): SpatiaRoom.Builder<T> {
roomBuilder.createFromAsset(databaseFilePath, callback)
return this
}

override fun createFromFile(databaseFile: File): SpatiaRoom.Builder<T> {
roomBuilder.createFromFile(databaseFile)
return this
}

override fun createFromFile(
databaseFile: File,
callback: RoomDatabase.PrepackagedDatabaseCallback
): SpatiaRoom.Builder<T> {
roomBuilder.createFromFile(databaseFile, callback)
return this
}

override fun createFromInputStream(inputStreamCallable: Callable<InputStream>): SpatiaRoom.Builder<T> {
roomBuilder.createFromInputStream(inputStreamCallable)
return this
}

override fun createFromInputStream(
inputStreamCallable: Callable<InputStream>,
callback: RoomDatabase.PrepackagedDatabaseCallback
): SpatiaRoom.Builder<T> {
roomBuilder.createFromInputStream(inputStreamCallable, callback)
return this
}

override fun openHelperFactory(factory: SupportSQLiteOpenHelper.Factory?): SpatiaRoom.Builder<T> {
roomBuilder.openHelperFactory(factory)
return this
Expand All @@ -46,6 +84,11 @@ class SpatiaBuilder<T : RoomDatabase> (
return this
}

override fun addAutoMigrationSpec(autoMigrationSpec: AutoMigrationSpec): SpatiaRoom.Builder<T> {
roomBuilder.addAutoMigrationSpec(autoMigrationSpec)
return this
}

override fun allowMainThreadQueries(): SpatiaRoom.Builder<T> {
roomBuilder.allowMainThreadQueries()
return this
Expand Down Expand Up @@ -91,6 +134,19 @@ class SpatiaBuilder<T : RoomDatabase> (
return this
}

override fun setQueryCallback(
queryCallback: RoomDatabase.QueryCallback,
executor: Executor
): SpatiaRoom.Builder<T> {
roomBuilder.setQueryCallback(queryCallback, executor)
return this
}

override fun addTypeConverter(typeConverter: Any): SpatiaRoom.Builder<T> {
roomBuilder.addTypeConverter(typeConverter)
return this
}

override fun build(): T = roomBuilder.build()


Expand Down
193 changes: 193 additions & 0 deletions spatia-room/src/main/java/co/anbora/labs/spatia/builder/SpatiaRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@ package co.anbora.labs.spatia.builder

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import androidx.annotation.IntRange
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.ExperimentalRoomApi
import androidx.room.MultiInstanceInvalidationService
import androidx.room.ProvidedTypeConverter
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.RoomDatabase.Builder
import androidx.room.RoomDatabase.Callback
import androidx.room.RoomDatabase.JournalMode
import androidx.room.RoomDatabase.QueryCallback
import androidx.room.migration.AutoMigrationSpec
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteOpenHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import java.io.File
import java.io.InputStream
import java.util.concurrent.Callable
import java.util.concurrent.Executor
import java.util.concurrent.TimeUnit

object SpatiaRoom {

Expand Down Expand Up @@ -67,6 +82,146 @@ object SpatiaRoom {
@Deprecated(message = "This API is experimental. It may be changed in the future without notice.", level = DeprecationLevel.WARNING)
fun createFromAsset(databaseFilePath: String): Builder<T>

/**
* Configures Room to create and open the database using a pre-packaged database located in
* the application 'assets/' folder.
*
* Room does not open the pre-packaged database, instead it copies it into the internal
* app database folder and then opens it. The pre-packaged database file must be located in
* the "assets/" folder of your application. For example, the path for a file located in
* "assets/databases/products.db" would be "databases/products.db".
*
* The pre-packaged database schema will be validated. It might be best to create your
* pre-packaged database schema utilizing the exported schema files generated when
* [Database.exportSchema] is enabled.
*
* This method is not supported for an in memory database [Builder].
*
* @param databaseFilePath The file path within the 'assets/' directory of where the
* database file is located.
* @param callback The pre-packaged callback.
*
* @return This builder instance.
*/
@SuppressLint("BuilderSetStyle") // To keep naming consistency.
fun createFromAsset(
databaseFilePath: String,
callback: RoomDatabase.PrepackagedDatabaseCallback
): Builder<T>

/**
* Configures Room to create and open the database using a pre-packaged database file.
*
* Room does not open the pre-packaged database, instead it copies it into the internal
* app database folder and then opens it. The given file must be accessible and the right
* permissions must be granted for Room to copy the file.
*
* The pre-packaged database schema will be validated. It might be best to create your
* pre-packaged database schema utilizing the exported schema files generated when
* [Database.exportSchema] is enabled.
*
* The [Callback.onOpen] method can be used as an indicator
* that the pre-packaged database was successfully opened by Room and can be cleaned up.
*
* This method is not supported for an in memory database [Builder].
*
* @param databaseFile The database file.
*
* @return This builder instance.
*/
fun createFromFile(databaseFile: File): Builder<T>

/**
* Configures Room to create and open the database using a pre-packaged database file.
*
* Room does not open the pre-packaged database, instead it copies it into the internal
* app database folder and then opens it. The given file must be accessible and the right
* permissions must be granted for Room to copy the file.
*
* The pre-packaged database schema will be validated. It might be best to create your
* pre-packaged database schema utilizing the exported schema files generated when
* [Database.exportSchema] is enabled.
*
* The [Callback.onOpen] method can be used as an indicator
* that the pre-packaged database was successfully opened by Room and can be cleaned up.
*
* This method is not supported for an in memory database [Builder].
*
* @param databaseFile The database file.
* @param callback The pre-packaged callback.
*
* @return This builder instance.
*/
@SuppressLint("BuilderSetStyle", "StreamFiles") // To keep naming consistency.
fun createFromFile(
databaseFile: File,
callback: RoomDatabase.PrepackagedDatabaseCallback
): Builder<T>

/**
* Configures Room to create and open the database using a pre-packaged database via an
* [InputStream].
*
* This is useful for processing compressed database files. Room does not open the
* pre-packaged database, instead it copies it into the internal app database folder, and
* then open it. The [InputStream] will be closed once Room is done consuming it.
*
* The pre-packaged database schema will be validated. It might be best to create your
* pre-packaged database schema utilizing the exported schema files generated when
* [Database.exportSchema] is enabled.
*
* The [Callback.onOpen] method can be used as an indicator
* that the pre-packaged database was successfully opened by Room and can be cleaned up.
*
* This method is not supported for an in memory database [Builder].
*
* @param inputStreamCallable A callable that returns an InputStream from which to copy
* the database. The callable will be invoked in a thread from
* the Executor set via [setQueryExecutor]. The
* callable is only invoked if Room needs to create and open the
* database from the pre-package database, usually the first time
* it is created or during a destructive migration.
*
* @return This builder instance.
*/
@SuppressLint("BuilderSetStyle") // To keep naming consistency.
fun createFromInputStream(
inputStreamCallable: Callable<InputStream>
): Builder<T>

/**
* Configures Room to create and open the database using a pre-packaged database via an
* [InputStream].
*
* This is useful for processing compressed database files. Room does not open the
* pre-packaged database, instead it copies it into the internal app database folder, and
* then open it. The [InputStream] will be closed once Room is done consuming it.
*
* The pre-packaged database schema will be validated. It might be best to create your
* pre-packaged database schema utilizing the exported schema files generated when
* [Database.exportSchema] is enabled.
*
* The [Callback.onOpen] method can be used as an indicator
* that the pre-packaged database was successfully opened by Room and can be cleaned up.
*
* This method is not supported for an in memory database [Builder].
*
* @param inputStreamCallable A callable that returns an InputStream from which to copy
* the database. The callable will be invoked in a thread from
* the Executor set via [setQueryExecutor]. The
* callable is only invoked if Room needs to create and open the
* database from the pre-package database, usually the first time
* it is created or during a destructive migration.
* @param callback The pre-packaged callback.
*
* @return This builder instance.
*/
@SuppressLint("BuilderSetStyle", "LambdaLast") // To keep naming consistency.
fun createFromInputStream(
inputStreamCallable: Callable<InputStream>,
callback: RoomDatabase.PrepackagedDatabaseCallback
): Builder<T>

/**
* Sets the database factory. If not set, it defaults to
* [FrameworkSQLiteOpenHelperFactory].
Expand Down Expand Up @@ -100,6 +255,16 @@ object SpatiaRoom {
*/
fun addMigrations(vararg migrations: Migration): Builder<T>

/**
* Adds an auto migration spec to the builder.
*
* @param autoMigrationSpec The auto migration object that is annotated with
* [AutoMigrationSpec] and is declared in an [AutoMigration] annotation.
* @return This builder instance.
*/
@Suppress("MissingGetterMatchingBuilder")
fun addAutoMigrationSpec(autoMigrationSpec: AutoMigrationSpec): Builder<T>

/**
* Disables the main thread query check for Room.
*
Expand Down Expand Up @@ -278,6 +443,34 @@ object SpatiaRoom {
*/
fun addCallback(callback: RoomDatabase.Callback): Builder<T>

/**
* Sets a [QueryCallback] to be invoked when queries are executed.
*
* The callback is invoked whenever a query is executed, note that adding this callback
* has a small cost and should be avoided in production builds unless needed.
*
* A use case for providing a callback is to allow logging executed queries. When the
* callback implementation logs then it is recommended to use an immediate executor.
*
* @param queryCallback The query callback.
* @param executor The executor on which the query callback will be invoked.
* @return This builder instance.
*/
@Suppress("MissingGetterMatchingBuilder")
fun setQueryCallback(
queryCallback: QueryCallback,
executor: Executor
): Builder<T>

/**
* Adds a type converter instance to this database.
*
* @param typeConverter The converter. It must be an instance of a class annotated with
* [ProvidedTypeConverter] otherwise Room will throw an exception.
* @return This builder instance.
*/
fun addTypeConverter(typeConverter: Any): Builder<T>

/**
* Creates the databases and initializes it.
*
Expand Down

0 comments on commit 4e17d4b

Please sign in to comment.