anboralabs / spatia-room

Implementation of spatia lite database to android component Room
MIT License
41 stars 4 forks source link

I can't open a database from assets #4

Closed guillermolc closed 3 years ago

guillermolc commented 3 years ago

Good morning, I am using the library, but I had to make some changes in the SpatiaBuilder.kt class, since I need to load a database from the assets folder, but from what I see I do not incorporate this function.

So the change I made was to pass in the instance the name of the file in assets and set it in

class SpBuilder<T : RoomDatabase?> ( context: Context, klass: Class, name: String, nameDbAsset: String < ------- HERE ): SpRoom.Builder {

private val roomBuilder = Room.databaseBuilder(
    context.applicationContext,
    klass,
    name
).createFromAsset(nameDbAsset). < ------- HERE
    .openHelperFactory(SpatiaHelperFactory())

override fun openHelperFactory(factory: SupportSQLiteOpenHelper.Factory?): SpRoom.Builder<T> {
    roomBuilder.openHelperFactory(factory)
    return this
}

override fun addMigrations(vararg migrations: Migration): SpRoom.Builder<T> {
    roomBuilder.addMigrations(*migrations)
    return this
}

override fun allowMainThreadQueries(): SpRoom.Builder<T> {
    roomBuilder.allowMainThreadQueries()
    return this
}

override fun setJournalMode(journalMode: RoomDatabase.JournalMode): SpRoom.Builder<T> {
    roomBuilder.setJournalMode(journalMode)
    return this
}

override fun setQueryExecutor(executor: Executor): SpRoom.Builder<T> {
    roomBuilder.setQueryExecutor(executor)
    return this
}

override fun setTransactionExecutor(executor: Executor): SpRoom.Builder<T> {
    roomBuilder.setTransactionExecutor(executor)
    return this
}

override fun enableMultiInstanceInvalidation(): SpRoom.Builder<T> {
    roomBuilder.enableMultiInstanceInvalidation()
    return this
}

override fun fallbackToDestructiveMigration(): SpRoom.Builder<T> {
    roomBuilder.fallbackToDestructiveMigration()
    return this
}

override fun fallbackToDestructiveMigrationOnDowngrade(): SpRoom.Builder<T> {
    roomBuilder.fallbackToDestructiveMigrationOnDowngrade()
    return this
}

override fun fallbackToDestructiveMigrationFrom(vararg startVersions: Int): SpRoom.Builder<T> {
    roomBuilder.fallbackToDestructiveMigrationFrom(*startVersions)
    return this
}

override fun addCallback(callback: RoomDatabase.Callback): SpRoom.Builder<T> {
    roomBuilder.addCallback(callback)
    return this
}

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

}

I hope you can make the change, greetings.

dalgarins commented 3 years ago

@guillermo-lagos, It's not possible with current implementation, the method was deleted to avoid issues with spatialite functions, the database that you are importing is a spatialite db?

Approach to deal the issue: 1- Create migrations to insert all data that you want. 2- Modify the library, (I would like to know more about your problem before add that method.)

mrclayman commented 3 years ago

I am in the same/similar position as @guillermo-lagos . I have a pre-populated Spatialite database that I want to include in the app's assets and be able to open it in the app from there. At the moment, the database contains about 5,5k polygons and weighs just under 50 MB, which basically forbids the migrations approach.

In my case, the database will be read-only, though, so really all I need is to call the createFromAsset() and I should be able use it as a regular Room database (if my understanding is correct).

guillermolc commented 3 years ago

So far the only solution I have found is to copy the file directly to the database directory of my app and then load the instance, to make use of it

dalgarins commented 3 years ago
val instance = SpatiaRoom.databaseBuilder(
                            context.applicationContext,
                            AppDatabase::class.java,
                            DB_NAME
                        ).createFromAsset("my_template.db")
                            .build()

@guillermo-lagos and @mrclayman thanks for testing, the feature will be available on version: 0.1.1