Skip to content

Commit

Permalink
Merge cursor window (#18)
Browse files Browse the repository at this point in the history
* Remove GraalWindowBindings

* Cleanup AbstractWindowsCursor

* Cleanup SQLiteCursor

* Remove CallbackSqliteOpenHelper

* Cleanup SQLiteDatabase.kt
  • Loading branch information
illarionov authored Mar 19, 2024
1 parent 98ce16e commit 811cc3e
Show file tree
Hide file tree
Showing 25 changed files with 558 additions and 1,537 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
Expand All @@ -18,6 +19,13 @@ plugins {
kotlin {
explicitApi = ExplicitApiMode.Warning

@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
freeCompilerArgs.addAll(
"-Xexpect-actual-classes",
)
}

sourceSets {
all {
languageSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ package ru.pixnews.wasm.sqlite.open.helper
*/

import androidx.sqlite.db.SupportSQLiteOpenHelper
import ru.pixnews.wasm.sqlite.open.helper.base.DatabaseErrorHandler
import ru.pixnews.wasm.sqlite.open.helper.common.api.Locale
import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger
import ru.pixnews.wasm.sqlite.open.helper.embedder.SqliteCapi
import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteDatabase
import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteDebug
import ru.pixnews.wasm.sqlite.open.helper.internal.WasmSqliteOpenHelper
import ru.pixnews.wasm.sqlite.open.helper.internal.interop.GraalNativeBindings
import ru.pixnews.wasm.sqlite.open.helper.internal.interop.SqlOpenHelperNativeBindings
import ru.pixnews.wasm.sqlite.open.helper.internal.interop.Sqlite3ConnectionPtr
import ru.pixnews.wasm.sqlite.open.helper.internal.interop.Sqlite3StatementPtr
import ru.pixnews.wasm.sqlite.open.helper.path.DatabasePathResolver

/**
Expand All @@ -43,71 +38,15 @@ internal class WasmSqliteOpenHelperFactory(

override fun create(configuration: SupportSQLiteOpenHelper.Configuration): SupportSQLiteOpenHelper {
val bindings = GraalNativeBindings(sqliteCapi, logger)
return CallbackSqliteOpenHelper(
return WasmSqliteOpenHelper(
pathResolver = pathResolver,
defaultLocale = defaultLocale,
debugConfig = debugConfig,
callback = configuration.callback,
configurationOptions = configurationOptions,
rootLogger = logger,
name = configuration.name,
cb = configuration.callback,
ops = configurationOptions,
databaseName = configuration.name,
bindings = bindings,
)
}

private class CallbackSqliteOpenHelper<CP : Sqlite3ConnectionPtr, SP : Sqlite3StatementPtr>(
pathResolver: DatabasePathResolver,
defaultLocale: Locale,
debugConfig: SQLiteDebug,
rootLogger: Logger,
name: String?,
cb: SupportSQLiteOpenHelper.Callback,
ops: Iterable<ConfigurationOptions>,
bindings: SqlOpenHelperNativeBindings<CP, SP>,
) : WasmSqliteOpenHelper<CP, SP>(
pathResolver = pathResolver,
defaultLocale = defaultLocale,
debugConfig = debugConfig,
rootLogger = rootLogger,
databaseName = name,
factory = null,
version = cb.version,
errorHandler = CallbackDatabaseErrorHandler(cb),
bindings = bindings,
) {
private val callback: SupportSQLiteOpenHelper.Callback = cb
private val configurationOptions = ops

override fun onConfigure(db: SQLiteDatabase<CP, SP>) = callback.onConfigure(db)

override fun onCreate(db: SQLiteDatabase<CP, SP>) = callback.onCreate(db)

override fun onUpgrade(db: SQLiteDatabase<CP, SP>, oldVersion: Int, newVersion: Int) =
callback.onUpgrade(db, oldVersion, newVersion)

override fun onDowngrade(db: SQLiteDatabase<CP, SP>, oldVersion: Int, newVersion: Int): Unit =
callback.onDowngrade(db, oldVersion, newVersion)

override fun onOpen(db: SQLiteDatabase<CP, SP>) = callback.onOpen(db)

override fun createConfiguration(
path: String,
defaultLocale: Locale,
openFlags: OpenFlags,
): SqliteDatabaseConfiguration {
var config = super.createConfiguration(path, defaultLocale, openFlags)

configurationOptions.forEach { option ->
config = option.apply(config)
}

return config
}
}

private class CallbackDatabaseErrorHandler(
private val callback: SupportSQLiteOpenHelper.Callback,
) : DatabaseErrorHandler {
override fun onCorruption(dbObj: SQLiteDatabase<*, *>) = callback.onCorruption(dbObj)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ package ru.pixnews.wasm.sqlite.open.helper.base

import android.database.CharArrayBuffer
import android.database.StaleDataException
import ru.pixnews.wasm.sqlite.open.helper.internal.interop.NativeCursorWindow
import ru.pixnews.wasm.sqlite.open.helper.internal.cursor.CursorWindow
import ru.pixnews.wasm.sqlite.open.helper.internal.cursor.NativeCursorWindow

/**
* A base class for Cursors that store their data in [android.database.CursorWindow]s.
Expand Down Expand Up @@ -66,12 +67,12 @@ internal abstract class AbstractWindowedCursor(

override fun getBlob(column: Int): ByteArray {
checkPosition()
return window!!.getBlob(pos, column) ?: byteArrayOf()
return requireWindow().getBlob(pos, column) ?: byteArrayOf()
}

override fun getString(column: Int): String? {
checkPosition()
return window!!.getString(pos, column)
return requireWindow().getString(pos, column)
}

/**
Expand Down Expand Up @@ -107,64 +108,55 @@ internal abstract class AbstractWindowedCursor(

override fun getShort(column: Int): Short {
checkPosition()
return window!!.getShort(pos, column)
return requireWindow().getShort(pos, column)
}

override fun getInt(column: Int): Int {
checkPosition()
return window!!.getInt(pos, column)
return requireWindow().getInt(pos, column)
}

override fun getLong(column: Int): Long {
checkPosition()
return window!!.getLong(pos, column)
return requireWindow().getLong(pos, column)
}

override fun getFloat(column: Int): Float {
checkPosition()
return window!!.getFloat(pos, column)
return requireWindow().getFloat(pos, column)
}

override fun getDouble(column: Int): Double {
checkPosition()
return window!!.getDouble(pos, column)
return requireWindow().getDouble(pos, column)
}

override fun isNull(column: Int): Boolean {
return window!!.getType(pos, column) == NativeCursorWindow.CursorFieldType.NULL
return requireWindow().getType(pos, column) == NativeCursorWindow.CursorFieldType.NULL
}

override fun getType(column: Int): Int {
return window!!.getType(pos, column).id
return requireWindow().getType(pos, column).id
}

override fun checkPosition() {
super.checkPosition()
if (window == null) {
throw StaleDataException(
"Attempting to access a closed CursorWindow." +
"Most probable cause: cursor is deactivated prior to calling this method.",
)
}
requireWindow()
}

/**
* Returns true if the cursor has an associated cursor window.
*
* @return True if the cursor has an associated cursor window.
*/
fun hasWindow(): Boolean = window != null
private fun requireWindow(): CursorWindow = window ?: throw StaleDataException(
"Attempting to access a closed CursorWindow." +
"Most probable cause: cursor is deactivated prior to calling this method.",
)

/**
* If there is a window, clear it. Otherwise, creates a new window.
* Creates a new window.
*
* @param name The window name.
* @hide
*/
protected fun clearOrCreateWindow(name: String?) {
window?.clear() ?: run {
window = windowFactory(name)
}
protected fun createWindow(name: String?) {
window = windowFactory(name)
}

override fun onDeactivateOrClose() {
Expand Down
Loading

0 comments on commit 811cc3e

Please sign in to comment.