From 2867a883fa448ba49ce4f8614e9ff79dd6d614af Mon Sep 17 00:00:00 2001 From: Alexey Illarionov Date: Tue, 19 Mar 2024 17:53:22 +0300 Subject: [PATCH] Clenup code, move more to common (#20) --- .../WasmSqliteOpenHelperFactoryBuilder.kt | 12 + .../helper/internal/SQLiteConnectionPool.kt | 6 +- .../open/helper/internal/SQLiteCursor.kt | 5 +- .../helper/internal/SQLiteCursorDriver.kt | 41 ---- .../internal/SQLiteCursorDriverListener.kt | 19 -- .../open/helper/internal/SQLiteDatabase.kt | 210 ++++-------------- .../internal/SQLiteDirectCursorDriver.kt | 57 ----- .../open/helper/internal/SQLiteProgram.kt | 6 +- .../open/helper/internal/SQLiteQuery.kt | 11 +- .../open/helper/internal/SQLiteSession.kt | 2 +- .../open/helper/internal/SQLiteStatement.kt | 44 ++-- .../internal/interop/GraalNativeBindings.kt | 2 +- .../sqlite/open/helper/internal/CloseGuard.kt | 20 +- .../open/helper/internal/ConflictAlgorithm.kt | 0 .../open/helper/internal/SQLiteGlobal.kt | 7 - .../helper/internal/SQLiteStatementInfo.kt | 7 - .../helper/internal/SQLiteStatementType.kt | 26 +-- .../sqlite/open/helper/internal}/TextExt.kt | 10 +- .../internal/interop/LocalizedComparator.kt | 0 .../interop/SqlOpenHelperNativeBindings.kt | 0 .../helper/internal/interop/Sqlite3Ptr.kt | 0 21 files changed, 95 insertions(+), 390 deletions(-) delete mode 100644 sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteCursorDriver.kt delete mode 100644 sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteCursorDriverListener.kt delete mode 100644 sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteDirectCursorDriver.kt rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/CloseGuard.kt (90%) rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ConflictAlgorithm.kt (100%) rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteGlobal.kt (90%) rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementInfo.kt (79%) rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementType.kt (87%) rename sqlite-open-helper/src/{androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ext => commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal}/TextExt.kt (61%) rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/LocalizedComparator.kt (100%) rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/SqlOpenHelperNativeBindings.kt (100%) rename sqlite-open-helper/src/{androidMain => commonMain}/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/Sqlite3Ptr.kt (100%) diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/WasmSqliteOpenHelperFactoryBuilder.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/WasmSqliteOpenHelperFactoryBuilder.kt index 9b2b2d06..69c74033 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/WasmSqliteOpenHelperFactoryBuilder.kt +++ b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/WasmSqliteOpenHelperFactoryBuilder.kt @@ -12,6 +12,8 @@ import ru.pixnews.wasm.sqlite.open.helper.dsl.WasmSqliteOpenHelperFactoryConfigB import ru.pixnews.wasm.sqlite.open.helper.embedder.SqliteEmbedder import ru.pixnews.wasm.sqlite.open.helper.embedder.SqliteEmbedderConfig import ru.pixnews.wasm.sqlite.open.helper.embedder.WasmSqliteCommonConfig +import ru.pixnews.wasm.sqlite.open.helper.internal.CloseGuard +import ru.pixnews.wasm.sqlite.open.helper.internal.CloseGuard.Reporter import ru.pixnews.wasm.sqlite.open.helper.path.JvmDatabasePathResolver /** @@ -28,6 +30,9 @@ public fun WasmSqliteOpenHelperFactory( val commonConfig = object : WasmSqliteCommonConfig { override val logger: Logger = config.logger } + + setupCloseGuard(config.logger) + return WasmSqliteOpenHelperFactory( pathResolver = config.pathResolver, defaultLocale = config.locale, @@ -37,3 +42,10 @@ public fun WasmSqliteOpenHelperFactory( configurationOptions = config.configurationOptions, ) } + +private fun setupCloseGuard(rootLogger: Logger) { + val logger = rootLogger.withTag("SQLite") + CloseGuard.reporter = Reporter { message, allocationSite -> + logger.w(allocationSite, message::toString) + } +} diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteConnectionPool.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteConnectionPool.kt index fad904a4..4010db77 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteConnectionPool.kt +++ b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteConnectionPool.kt @@ -202,9 +202,9 @@ internal class SQLiteConnectionPool { - /** - * Executes the query returning a Cursor over the result set. - * - * @param factory The CursorFactory to use when creating the Cursors, or - * null if standard SQLiteCursors should be returned. - * @return a Cursor over the result set - */ - fun query(factory: CursorFactory?, bindArgs: List): Cursor - - /** - * Set new bind arguments. These will take effect in cursorRequeried(). - * - * @param bindArgs the new arguments - */ - fun setBindArguments(bindArgs: List) -} diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteCursorDriverListener.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteCursorDriverListener.kt deleted file mode 100644 index 836c6375..00000000 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteCursorDriverListener.kt +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file - * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - * SPDX-License-Identifier: Apache-2.0 - */ - -package ru.pixnews.wasm.sqlite.open.helper.internal - -internal interface SQLiteCursorDriverListener { - /** - * Called by a SQLiteCursor when it is released. - */ - fun cursorDeactivated() - - /** - * Called by a SQLiteCursor when it it closed to destroy this object as well. - */ - fun cursorClosed() -} diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteDatabase.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteDatabase.kt index 60228249..ae7eee57 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteDatabase.kt +++ b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteDatabase.kt @@ -40,11 +40,12 @@ import ru.pixnews.wasm.sqlite.open.helper.common.api.or import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteConnectionPool.Companion.CONNECTION_FLAG_PRIMARY_CONNECTION_AFFINITY import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteConnectionPool.Companion.CONNECTION_FLAG_READ_ONLY import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteProgram.Companion.bindAllArgsAsStrings +import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteSession.Companion.TRANSACTION_MODE_EXCLUSIVE +import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteSession.Companion.TRANSACTION_MODE_IMMEDIATE 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 java.io.File -import java.io.FileFilter import java.io.IOException import kotlin.collections.Map.Entry @@ -167,10 +168,10 @@ internal class SQLiteDatabase> = mutableListOf() // has attached databases. query sqlite to get the list of attached databases. - rawQuery("pragma database_list;").use { cursor -> + rawQueryWithFactory("pragma database_list;").use { cursor -> while (cursor.moveToNext()) { // sqlite returns a row for each database in the returned list of databases. // in each row, @@ -341,18 +342,6 @@ internal class SQLiteDatabase * */ - override fun beginTransaction() = beginTransaction( - null, - SQLiteSession.TRANSACTION_MODE_EXCLUSIVE, - ) + override fun beginTransaction() = beginTransaction(null, TRANSACTION_MODE_EXCLUSIVE) /** * Begins a transaction in IMMEDIATE mode. Transactions can be nested. When @@ -402,10 +388,7 @@ internal class SQLiteDatabase * */ - override fun beginTransactionNonExclusive() = beginTransaction( - null, - SQLiteSession.TRANSACTION_MODE_IMMEDIATE, - ) + override fun beginTransactionNonExclusive() = beginTransaction(null, TRANSACTION_MODE_IMMEDIATE) /** * Begins a transaction in EXCLUSIVE mode. @@ -436,10 +419,7 @@ internal class SQLiteDatabase): Cursor = rawQueryWithFactory( - cursorFactory = null, sql = query, selectionArgs = bindArgs.toList(), - cancellationSignal = null, ) /** @@ -743,13 +708,12 @@ internal class SQLiteDatabase + sql = supportQuery.sql, + cancellationSignal = signal, + cursorFactory = { query -> supportQuery.bindTo(query) SQLiteCursor(query, logger) }, - sql = supportQuery.sql, - selectionArgs = listOf(), - cancellationSignal = signal, ) /** @@ -762,46 +726,24 @@ internal class SQLiteDatabase?, sql: String, selectionArgs: List = listOf(), cancellationSignal: CancellationSignal? = null, + cursorFactory: ((SQLiteQuery) -> Cursor)? = null, ): Cursor = useReference { - val driver: SQLiteCursorDriver = SQLiteDirectCursorDriver( - database = this, - sql = sql, - cancellationSignal = cancellationSignal, - rootLogger = logger, - ) - return driver.query(cursorFactory, selectionArgs) + val query = SQLiteQuery(this, sql, selectionArgs, cancellationSignal, logger) + try { + return cursorFactory?.invoke(query) ?: SQLiteCursor(query, logger) + } catch (@Suppress("TooGenericExceptionCaught") ex: RuntimeException) { + query.close() + throw ex + } } - /** - * Runs the provided SQL and returns a [Cursor] over the result set. - * - * @param sql the SQL query. The SQL string must not be ; terminated - * @param selectionArgs You may include ?s in where clause in the query, - * which will be replaced by the values from selectionArgs. - * @param cancellationSignal A signal to cancel the operation in progress, or null if none. - * If the operation is canceled, then [OperationCanceledException] will be thrown - * when the query is executed. - * @return A [Cursor] object, which is positioned before the first entry. Note that - * [Cursor]s are not synchronized, see the documentation for more details. - */ - fun rawQuery( - sql: String, - selectionArgs: List = listOf(), - cancellationSignal: CancellationSignal? = null, - ): Cursor = rawQueryWithFactory( - cursorFactory = null, - sql = sql, - selectionArgs = selectionArgs, - cancellationSignal = cancellationSignal, - ) - /** * General method for inserting a row into the database. * @@ -895,29 +837,6 @@ internal class SQLiteDatabase = emptyList()): Int = useReference { - SQLiteStatement( - this, - "DELETE FROM $table${(if (whereClause.isNotEmpty()) " WHERE $whereClause" else "")}", - whereArgs, - ).use { - it.executeUpdateDelete() - } - } - /** * Convenience method for deleting rows in the database. * @@ -1304,38 +1223,25 @@ internal class SQLiteDatabase = checkNotNull(connectionPoolLocked) { "The database '${configurationLocked.label}' is not open." } + override fun toString(): String = "SQLiteDatabase: $path" + /** * Utility method to run the query on the db and return the value in the * first column of the first row. */ - fun longForQuery( + private fun longForQuery( query: String, selectionArgs: List = emptyList(), - ): Long = compileStatement(query).use { prog -> - longForQuery(prog = prog, selectionArgs) + ): Long = compileStatement(query).use { statement: SupportSQLiteStatement -> + statement.bindAllArgsAsStrings(selectionArgs) + statement.simpleQueryForLong() } - /** - * Used to allow returning sub-classes of [Cursor] when calling query. - */ - internal fun interface CursorFactory { - /** - * See [SQLiteCursor.SQLiteCursor]. - */ - fun newCursor( - db: SQLiteDatabase, - masterQuery: SQLiteCursorDriver?, - query: SQLiteQuery, - ): Cursor - } - - companion object { + internal companion object { private const val TAG = "SQLiteDatabase" /** @@ -1377,34 +1283,6 @@ internal class SQLiteDatabase candidate.name.startsWith(prefix) } - dir.listFiles(filter)?.forEach { masterJournal -> - deleted = deleted or masterJournal.delete() - } - } - return deleted - } - @Suppress("NestedBlockDepth") private fun ensureFile(path: String, logger: Logger) { val file = File(path) @@ -1465,15 +1343,6 @@ internal class SQLiteDatabase): Long { - prog.bindAllArgsAsStrings(selectionArgs) - return prog.simpleQueryForLong() - } - /** * Convenience method for inserting a row into the database. * @@ -1524,5 +1393,16 @@ internal class SQLiteDatabase( - private val database: SQLiteDatabase, - private val sql: String, - private val cancellationSignal: CancellationSignal?, - rootLogger: Logger, -) : SQLiteCursorDriver { - private val logger: Logger = rootLogger.withTag("SQLiteDirectCursorDriver") - private var query: SQLiteQuery? = null - - override fun query( - factory: SQLiteDatabase.CursorFactory?, - bindArgs: List, - ): Cursor { - val query = SQLiteQuery(database, sql, bindArgs, cancellationSignal) - val cursor: Cursor - try { - cursor = factory?.newCursor(database, this, query) ?: SQLiteCursor(query, logger) - } catch (@Suppress("TooGenericExceptionCaught") ex: RuntimeException) { - query.close() - throw ex - } - - this.query = query - return cursor - } - - override fun setBindArguments(bindArgs: List) { - requireNotNull(query) { "query() not called" }.bindAllArgsAsStrings(bindArgs) - } - - override fun toString(): String = "SQLiteDirectCursorDriver: $sql" -} diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteProgram.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteProgram.kt index ad373736..0c895cb9 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteProgram.kt +++ b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteProgram.kt @@ -15,6 +15,7 @@ package ru.pixnews.wasm.sqlite.open.helper.internal import androidx.core.os.CancellationSignal import androidx.sqlite.db.SupportSQLiteProgram +import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteDatabase.Companion.getThreadDefaultConnectionFlags import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteStatementType.STATEMENT_ABORT import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteStatementType.STATEMENT_BEGIN import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteStatementType.STATEMENT_COMMIT @@ -43,7 +44,7 @@ internal abstract class SQLiteProgram internal constructor( get() = database.threadSession protected val connectionFlags: Int - get() = database.getThreadDefaultConnectionFlags(readOnly) + get() = getThreadDefaultConnectionFlags(readOnly) init { when (val statementType = SQLiteStatementType.getSqlStatementType(this.sql)) { @@ -57,7 +58,7 @@ internal abstract class SQLiteProgram internal constructor( val assumeReadOnly = (statementType == SQLiteStatementType.STATEMENT_SELECT) val info = database.threadSession.prepare( this.sql, - database.getThreadDefaultConnectionFlags(assumeReadOnly), + getThreadDefaultConnectionFlags(assumeReadOnly), cancellationSignalForPrepare, ) readOnly = info.readOnly @@ -106,7 +107,6 @@ internal abstract class SQLiteProgram internal constructor( * * @param bindArgs the String array of bind args, none of which must be null. */ - @JvmStatic fun SupportSQLiteProgram.bindAllArgsAsStrings(bindArgs: List) { for (i in bindArgs.size downTo 1) { val arg = bindArgs[i - 1] diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteQuery.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteQuery.kt index 6d3f0596..c27a72d7 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteQuery.kt +++ b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteQuery.kt @@ -15,8 +15,8 @@ package ru.pixnews.wasm.sqlite.open.helper.internal import android.database.sqlite.SQLiteDatabaseCorruptException import android.database.sqlite.SQLiteException -import android.util.Log import androidx.core.os.CancellationSignal +import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger import ru.pixnews.wasm.sqlite.open.helper.internal.cursor.CursorWindow /** @@ -32,7 +32,10 @@ internal class SQLiteQuery( query: String, bindArgs: List, private val cancellationSignal: CancellationSignal?, + rootLogger: Logger, ) : SQLiteProgram(db, query, bindArgs, cancellationSignal) { + private val logger = rootLogger.withTag("SQLiteQuery") + /** * Reads rows into a buffer. * @@ -70,15 +73,11 @@ internal class SQLiteQuery( onCorruption() throw ex } catch (ex: SQLiteException) { - Log.e(TAG, "exception: ${ex.message}; query: $sql") + logger.e { "exception: ${ex.message}; query: $sql" } throw ex } } } override fun toString(): String = "SQLiteQuery: $sql" - - private companion object { - private const val TAG = "SQLiteQuery" - } } diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteSession.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteSession.kt index ca5bacf5..d53ba1ef 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteSession.kt +++ b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteSession.kt @@ -310,7 +310,7 @@ internal class SQLiteSession executeHandleCorruption(block: () -> R): R = useReference { try { - return session.executeForString(sql, bindArgs, connectionFlags, null) + return block() } catch (ex: SQLiteDatabaseCorruptException) { onCorruption() throw ex diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/GraalNativeBindings.kt b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/GraalNativeBindings.kt index b39b7316..1e18c4a5 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/GraalNativeBindings.kt +++ b/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/GraalNativeBindings.kt @@ -33,7 +33,7 @@ import ru.pixnews.wasm.sqlite.open.helper.common.api.or import ru.pixnews.wasm.sqlite.open.helper.embedder.SqliteCapi import ru.pixnews.wasm.sqlite.open.helper.embedder.SqliteCapi.SqliteDbReadonlyResult import ru.pixnews.wasm.sqlite.open.helper.internal.cursor.NativeCursorWindow -import ru.pixnews.wasm.sqlite.open.helper.internal.ext.encodedNullTerminatedStringLength +import ru.pixnews.wasm.sqlite.open.helper.internal.encodedNullTerminatedStringLength import ru.pixnews.wasm.sqlite.open.helper.internal.interop.GraalNativeBindings.CopyRowResult.CPR_FULL import ru.pixnews.wasm.sqlite.open.helper.internal.interop.GraalNativeBindings.CopyRowResult.CPR_OK import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteColumnType.Companion.SQLITE3_TEXT diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/CloseGuard.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/CloseGuard.kt similarity index 90% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/CloseGuard.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/CloseGuard.kt index 8de22df7..371a2c81 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/CloseGuard.kt +++ b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/CloseGuard.kt @@ -6,14 +6,6 @@ package ru.pixnews.wasm.sqlite.open.helper.internal -/* - * Original Copyrights: - * Copyright (C) 2017-2024 requery.io - * Copyright (C) 2005-2012 The Android Open Source Project - * Licensed under the Apache License, Version 2.0 (the "License") - */ - -import android.util.Log import kotlin.concurrent.Volatile /** @@ -105,15 +97,6 @@ import kotlin.concurrent.Volatile internal class CloseGuard private constructor() { private var allocationSite: Throwable? = null - /** - * Default Reporter which reports CloseGuard violations to the log. - */ - private object DefaultReporter : Reporter { - override fun report(message: String?, allocationSite: Throwable?) { - Log.w("SQLite", message, allocationSite) - } - } - /** * If CloseGuard is enabled, `open` initializes the instance * with a warning that the caller should have explicitly called the @@ -182,7 +165,7 @@ internal class CloseGuard private constructor() { * Hook for customizing how CloseGuard issues are reported. */ @Volatile - private var _reporter: Reporter = DefaultReporter + private var _reporter: Reporter = Reporter { _, _ -> } var reporter: Reporter? /** @@ -206,7 +189,6 @@ internal class CloseGuard private constructor() { * instance to warn on failure to close. * If CloseGuard is disabled, a non-null no-op instance is returned. */ - @JvmStatic fun get(): CloseGuard { if (!ENABLED) { return NOOP diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ConflictAlgorithm.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ConflictAlgorithm.kt similarity index 100% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ConflictAlgorithm.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ConflictAlgorithm.kt diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteGlobal.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteGlobal.kt similarity index 90% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteGlobal.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteGlobal.kt index d614d341..9495ce78 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteGlobal.kt +++ b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteGlobal.kt @@ -6,13 +6,6 @@ package ru.pixnews.wasm.sqlite.open.helper.internal -/* - * Original Copyrights: - * Copyright (C) 2017-2024 requery.io - * Copyright (C) 2005-2012 The Android Open Source Project - * Licensed under the Apache License, Version 2.0 (the "License") - */ - /** * Provides access to SQLite functions that affect all database connection, * such as memory management. diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementInfo.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementInfo.kt similarity index 79% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementInfo.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementInfo.kt index bf156086..f48647f9 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementInfo.kt +++ b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementInfo.kt @@ -6,13 +6,6 @@ package ru.pixnews.wasm.sqlite.open.helper.internal -/* - * Original Copyrights: - * Copyright (C) 2017-2024 requery.io - * Copyright (C) 2005-2012 The Android Open Source Project - * Licensed under the Apache License, Version 2.0 (the "License") - */ - /** * Describes a SQLite statement. * diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementType.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementType.kt similarity index 87% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementType.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementType.kt index 0b87d522..09e9107d 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementType.kt +++ b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/SQLiteStatementType.kt @@ -6,15 +6,6 @@ package ru.pixnews.wasm.sqlite.open.helper.internal -/* - * Original Copyrights: - * Copyright (C) 2017-2024 requery.io - * Copyright (C) 2005-2012 The Android Open Source Project - * Licensed under the Apache License, Version 2.0 (the "License") - */ - -import androidx.annotation.VisibleForTesting - internal enum class SQLiteStatementType { STATEMENT_SELECT, STATEMENT_UPDATE, @@ -78,7 +69,6 @@ internal enum class SQLiteStatementType { * @param sql sql statement to check * @return index of the SQL statement start, skipping leading comments */ - @VisibleForTesting @Suppress("CyclomaticComplexMethod", "IDENTIFIER_LENGTH") internal fun statementStartIndex(sql: String): Int { var inSingleLineComment = false @@ -89,20 +79,20 @@ internal enum class SQLiteStatementType { val c = sql[i] when { inSingleLineComment -> if (c == '\n') { - inSingleLineComment = false - } + inSingleLineComment = false + } inMultiLineComment -> if (c == '*' && i + 1 < sql.length && sql[i + 1] == '/') { - inMultiLineComment = false - } + inMultiLineComment = false + } c == '-' -> if (i + 1 < sql.length && sql[i + 1] == '-') { - inSingleLineComment = true - } + inSingleLineComment = true + } c == '/' -> if (i + 1 < sql.length && sql[i + 1] == '*') { - inMultiLineComment = true - } + inMultiLineComment = true + } c != '\n' && c != '\r' && c != ' ' && c != '\t' -> { statementStartIndex = i diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ext/TextExt.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/TextExt.kt similarity index 61% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ext/TextExt.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/TextExt.kt index c5954ffe..c6e9e531 100644 --- a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/ext/TextExt.kt +++ b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/TextExt.kt @@ -4,17 +4,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -package ru.pixnews.wasm.sqlite.open.helper.internal.ext +package ru.pixnews.wasm.sqlite.open.helper.internal import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi -import java.io.ByteArrayOutputStream - -internal fun String.encodeToNullTerminatedByteArray(): ByteArray { - val os = ByteArrayOutputStream(this.length) - this.encodeToByteArray().let { os.write(it, 0, it.size) } - os.write(0) - return os.toByteArray() -} // TODO: merge with MemoryUtil @InternalWasmSqliteHelperApi diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/LocalizedComparator.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/LocalizedComparator.kt similarity index 100% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/LocalizedComparator.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/LocalizedComparator.kt diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/SqlOpenHelperNativeBindings.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/SqlOpenHelperNativeBindings.kt similarity index 100% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/SqlOpenHelperNativeBindings.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/SqlOpenHelperNativeBindings.kt diff --git a/sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/Sqlite3Ptr.kt b/sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/Sqlite3Ptr.kt similarity index 100% rename from sqlite-open-helper/src/androidMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/Sqlite3Ptr.kt rename to sqlite-open-helper/src/commonMain/kotlin/ru/pixnews/wasm/sqlite/open/helper/internal/interop/Sqlite3Ptr.kt