Can I use Room Persistence for a different type of Database?

好久不见. 提交于 2021-02-08 02:01:37

问题


I know that room is an abstraction layer over SQLite, I just want to know if I can use it for a different type of Database. I want to use Room to a special version of SQLite with geospatial support, is it possible? I really can't find an answer about this one.

Android Spatialite

Thanks in advance!


回答1:


Yes, if you are willing to write the bridge code for it.

One of the methods that you can call on a RoomDatabase.Builder is openHelperFactory(). This takes an instance of SupportSQLiteOpenHelper.Factory, and this is what Room uses for interacting with the underlying database implementation. By default, Room uses FrameworkSQLiteOpenHelper.Factory, but via openHelperFactory() you can provide your own.

This, in turn, will require you to implement other interfaces on other classes, such as SupportSQLiteDatabase. This is basically a wrapper, with an API that somewhat resembles the framework API. You translate those calls to the SQLite implementation that you wish to use.

I did this for SQLCipher for Android, in the form of the SafeRoom library. Right now, my implementation is a bit clunky, due to some limitations in SQLCipher for Android that hopefully will be lifted soon. You would create the same sort of classes for your particular SQLite implementation. Then, plug your SupportSQLiteHelper.Factory into your RoomDatabase.Builder, and now Room will talk to your database implementation.

Note that you cannot actually extend Room itself. For example, you cannot invent new Room annotations that know about geospatial stuff. And the Room compiler may have issues with your geospatial SQL, if that SQL breaks what Room is expecting (e.g., new keywords). It seems like what they added are "just" a lot of functions, so hopefully Room will behave OK.




回答2:


You could use my current implementation of spatialite for Android room:

https://github.com/anboralabs/spatia-room



来源:https://stackoverflow.com/questions/53366380/can-i-use-room-persistence-for-a-different-type-of-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!