JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.34k stars 696 forks source link

SQLite Android status #578

Open lamba92 opened 5 years ago

lamba92 commented 5 years ago

I may be mistaken but, no JDBC driver works for SQLite on Android, which means no SQLite Exposed on Android.

Here's a list of issues related to that problem:

SQLDroid is out of question since the jdbc postfix is not one of the registered here, unless adding something like:

registerDialect(SqlDroidDialect.dialectName) { MysqlDialect() }

Anybody has a solution so far? I was happy to try Exposed on Android as well to replace Room but so far no success 😢

Digging a little deep I noticed that the problem resides in the metadata.url in the Database class here. metadata.url returns null instead of the actual URL and since everything is evaluated lazily the error message gets a little confused. This happens with the SQLDroid drivers as well and I was not able to figure out why 😕

Tapac commented 5 years ago

@lamba92 Do you have any suggestion about how I could test Exposed against android sqlite?

lamba92 commented 5 years ago

I'd say a Gradle module with the Android plugin. You can launch an activity and initialize something like this:

class CacheTransactionManagerImpl @Inject constructor(val context: Context) : CacheTransactionManager {

    private val database by lazy {
        Database.connectSQLite(File(context.cacheDir, "localCache.sqlite").absolutePath)
                .also {
                    serializableTransaction(it) { SchemaUtils.createMissingTablesAndColumns(...) }
                }
    }

    override fun <T> execute(block: Transaction.() -> T) = serializableTransaction(database, function = block)

}

You can either provide a context using DI or just creating it during execution.

As soon as some R/W operation occurs, all the lazy variables will be triggered until metadata.url is requested, returns null and Kotlin throws NPE.

lisonge commented 2 years ago

What is it now

toth-istvan-zoltan commented 2 years ago

@lisonge We do use Exposed on Android. I've ported the SQLDroid driver and fixed the few problems Exposed had with it.

You can find some basic documents here: https://zakadabar.io/en/Documentation/guides/android/Introduction.md. This is an Apache 2.0 project, you can use the dependency from Maven Central as we do or copy the code.

You can see a working example in this recipe: Database Transfer to Android.

lisonge commented 2 years ago

@toth-istvan-zoltan

now I use room , and a custom encapsulation framework that like Exposed to solve stackoverflow/71995278 on room