Skip to content

Commit

Permalink
Move locale to configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov committed Mar 18, 2024
1 parent 756b9be commit dfb53eb
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.common.api

import kotlin.jvm.JvmInline

/**
* ICU Locale ID
*/
@JvmInline
public value class Locale(
public val icuId: String,
) {
public companion object {
public val EN_US: Locale = Locale("en_US")
}
}
5 changes: 3 additions & 2 deletions sqlite-open-helper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ android {
}

dependencies {
api(projects.commonApi)
api(projects.sqliteCommonApi)
annotationProcessor(libs.androidx.room.compiler)
kspAndroid(libs.androidx.room.compiler)
kspAndroidTest(libs.androidx.room.compiler)
Expand All @@ -67,6 +65,7 @@ dependencies {
kotlin {
androidTarget()
jvm()
linuxX64()

sourceSets {
androidMain.dependencies {
Expand All @@ -87,6 +86,8 @@ kotlin {
runtimeOnly(libs.junit.jupiter.engine)
}
commonMain.dependencies {
api(projects.commonApi)
api(projects.sqliteCommonApi)
}
commonTest.dependencies {
implementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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
Expand All @@ -35,6 +36,7 @@ import ru.pixnews.wasm.sqlite.open.helper.path.DatabasePathResolver
*/
internal class WasmSqliteOpenHelperFactory(
private val pathResolver: DatabasePathResolver,
private val defaultLocale: Locale,
private val sqliteCapi: SqliteCapi,
private val debugConfig: SQLiteDebug = SQLiteDebug(),
private val configurationOptions: List<ConfigurationOptions> = emptyList(),
Expand All @@ -46,6 +48,7 @@ internal class WasmSqliteOpenHelperFactory(
val bindings = GraalNativeBindings(sqliteCapi, logger)
return CallbackSqliteOpenHelper(
pathResolver = pathResolver,
defaultLocale = defaultLocale,
debugConfig = debugConfig,
rootLogger = logger,
name = configuration.name,
Expand All @@ -58,6 +61,7 @@ internal class WasmSqliteOpenHelperFactory(

private class CallbackSqliteOpenHelper<CP : Sqlite3ConnectionPtr, SP : Sqlite3StatementPtr, WP : Sqlite3WindowPtr>(
pathResolver: DatabasePathResolver,
defaultLocale: Locale,
debugConfig: SQLiteDebug,
rootLogger: Logger,
name: String?,
Expand All @@ -67,6 +71,7 @@ internal class WasmSqliteOpenHelperFactory(
windowBindings: SqlOpenHelperWindowBindings<WP>,
) : WasmSqliteOpenHelper<CP, SP, WP>(
pathResolver = pathResolver,
defaultLocale = defaultLocale,
debugConfig = debugConfig,
rootLogger = rootLogger,
databaseName = name,
Expand All @@ -91,8 +96,12 @@ internal class WasmSqliteOpenHelperFactory(

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

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

configurationOptions.forEach { option ->
config = option.apply(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public fun <E : SqliteEmbedderConfig> WasmSqliteOpenHelperFactory(
}
return WasmSqliteOpenHelperFactory(
pathResolver = config.pathResolver,
defaultLocale = config.locale,
sqliteCapi = embedder.createCapi(commonConfig, config.embedderConfig),
debugConfig = config.debugConfigBlock.build(),
rootLogger = commonConfig.logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package ru.pixnews.wasm.sqlite.open.helper.dsl

import ru.pixnews.wasm.sqlite.open.helper.ConfigurationOptions
import ru.pixnews.wasm.sqlite.open.helper.WasmSqliteOpenHelperDsl
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.SqliteEmbedderConfig
import ru.pixnews.wasm.sqlite.open.helper.path.DatabasePathResolver
Expand All @@ -19,6 +20,7 @@ public class WasmSqliteOpenHelperFactoryConfigBlock<E : SqliteEmbedderConfig> {
internal var debugConfigBlock: DebugConfigBlock = DebugConfigBlock()
private set
public var pathResolver: DatabasePathResolver = JvmDatabasePathResolver()
public val locale: Locale = Locale.EN_US
internal var configurationOptions: List<ConfigurationOptions> = emptyList()
private set
internal var embedderConfig: E.() -> Unit = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import ru.pixnews.wasm.sqlite.open.helper.SqliteDatabaseConfiguration
import ru.pixnews.wasm.sqlite.open.helper.base.CursorWindow
import ru.pixnews.wasm.sqlite.open.helper.base.DatabaseErrorHandler
import ru.pixnews.wasm.sqlite.open.helper.base.DefaultDatabaseErrorHandler
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.common.api.clear
import ru.pixnews.wasm.sqlite.open.helper.common.api.contains
Expand All @@ -51,7 +52,6 @@ import ru.pixnews.wasm.sqlite.open.helper.internal.interop.Sqlite3WindowPtr
import java.io.File
import java.io.FileFilter
import java.io.IOException
import java.util.Locale

/**
* Exposes methods to manage a SQLite database.
Expand Down Expand Up @@ -1260,10 +1260,10 @@ internal class SQLiteDatabase<
* for this is that there is no collator available for the locale you requested.
* In this case the database remains unchanged.
*/
override fun setLocale(locale: Locale) = synchronized(lock) {
override fun setLocale(locale: java.util.Locale) = synchronized(lock) {
val pool = requireConnectionPoolLocked()
val oldLocale = configurationLocked.locale
configurationLocked.locale = locale
configurationLocked.locale = Locale(locale.toString())
try {
pool.reconfigure(configurationLocked)
} catch (@Suppress("TooGenericExceptionCaught") ex: RuntimeException) {
Expand Down Expand Up @@ -1630,9 +1630,10 @@ internal class SQLiteDatabase<
bindings: SqlOpenHelperNativeBindings<CP, SP, WP>,
windowBindings: SqlOpenHelperWindowBindings<WP>,
debugConfig: SQLiteDebug,
locale: Locale,
logger: Logger,
): SQLiteDatabase<CP, SP, WP> {
val configuration = SqliteDatabaseConfiguration(path, flags)
val configuration = SqliteDatabaseConfiguration(path, flags, locale)
val db = SQLiteDatabase(configuration, debugConfig, logger, bindings, windowBindings, factory, errorHandler)
db.open()
return db
Expand Down Expand Up @@ -1679,13 +1680,15 @@ internal class SQLiteDatabase<
factory: CursorFactory<CP, SP, WP>?,
bindings: SqlOpenHelperNativeBindings<CP, SP, WP>,
windowBindings: SqlOpenHelperWindowBindings<WP>,
locale: Locale,
debugConfig: SQLiteDebug,
logger: Logger,
): SQLiteDatabase<CP, SP, WP> = openOrCreateDatabase(
path = file.path,
factory = factory,
bindings = bindings,
windowBindings = windowBindings,
locale = locale,
debugConfig = debugConfig,
logger = logger,
)
Expand All @@ -1699,6 +1702,7 @@ internal class SQLiteDatabase<
bindings: SqlOpenHelperNativeBindings<CP, SP, WP>,
windowBindings: SqlOpenHelperWindowBindings<WP>,
debugConfig: SQLiteDebug,
locale: Locale,
logger: Logger,
): SQLiteDatabase<CP, SP, WP> = openDatabase(
path = path,
Expand All @@ -1708,6 +1712,7 @@ internal class SQLiteDatabase<
bindings = bindings,
windowBindings = windowBindings,
debugConfig = debugConfig,
locale = locale,
logger = logger,
)

Expand All @@ -1722,6 +1727,7 @@ internal class SQLiteDatabase<
bindings: SqlOpenHelperNativeBindings<CP, SP, WP>,
windowBindings: SqlOpenHelperWindowBindings<WP>,
debugConfig: SQLiteDebug,
locale: Locale,
logger: Logger,
): SQLiteDatabase<CP, SP, WP> =
openDatabase(
Expand All @@ -1732,6 +1738,7 @@ internal class SQLiteDatabase<
bindings = bindings,
windowBindings = windowBindings,
debugConfig = debugConfig,
locale = locale,
logger = logger,
)

Expand Down Expand Up @@ -1804,6 +1811,7 @@ internal class SQLiteDatabase<
factory: CursorFactory<CP, SP, WP>?,
bindings: SqlOpenHelperNativeBindings<CP, SP, WP>,
windowBindings: SqlOpenHelperWindowBindings<WP>,
locale: Locale,
debugConfig: SQLiteDebug,
logger: Logger,
): SQLiteDatabase<CP, SP, WP> = openDatabase(
Expand All @@ -1813,6 +1821,7 @@ internal class SQLiteDatabase<
bindings = bindings,
windowBindings = windowBindings,
debugConfig = debugConfig,
locale = locale,
logger = logger,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ru.pixnews.wasm.sqlite.open.helper.OpenFlags.Companion.ENABLE_WRITE_AHEAD
import ru.pixnews.wasm.sqlite.open.helper.OpenFlags.Companion.OPEN_READONLY
import ru.pixnews.wasm.sqlite.open.helper.SqliteDatabaseConfiguration
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.common.api.or
import ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteDatabase.CursorFactory
Expand Down Expand Up @@ -61,6 +62,7 @@ internal abstract class WasmSqliteOpenHelper<
WP : Sqlite3WindowPtr,
>(
private val pathResolver: DatabasePathResolver,
private val defaultLocale: Locale,
private val debugConfig: SQLiteDebug,
rootLogger: Logger,
override val databaseName: String?,
Expand Down Expand Up @@ -215,13 +217,14 @@ internal abstract class WasmSqliteOpenHelper<
bindings = bindings,
windowBindings = windowBindings,
debugConfig = debugConfig,
locale = defaultLocale,
logger = logger,
)
} else {
try {
val path = pathResolver.getDatabasePath(databaseName.toString()).path
if (DEBUG_STRICT_READONLY && !writable) {
val configuration = createConfiguration(path, OPEN_READONLY)
val configuration = createConfiguration(path, defaultLocale, OPEN_READONLY)
db = SQLiteDatabase.openDatabase(
configuration = configuration,
factory = factory,
Expand All @@ -234,7 +237,7 @@ internal abstract class WasmSqliteOpenHelper<
} else {
var flags = if (enableWriteAheadLogging) ENABLE_WRITE_AHEAD_LOGGING else OpenFlags(0U)
flags = flags or CREATE_IF_NECESSARY
val configuration = createConfiguration(path, flags)
val configuration = createConfiguration(path, defaultLocale, flags)
db = SQLiteDatabase.openDatabase(
configuration = configuration,
factory = factory,
Expand All @@ -251,7 +254,7 @@ internal abstract class WasmSqliteOpenHelper<
}
logger.e(ex) { "Couldn't open $databaseName for writing (will try read-only):" }
val path = pathResolver.getDatabasePath(databaseName.toString()).path
val configuration = createConfiguration(path, OPEN_READONLY)
val configuration = createConfiguration(path, defaultLocale, OPEN_READONLY)
db = SQLiteDatabase.openDatabase(
configuration = configuration,
factory = factory,
Expand Down Expand Up @@ -421,8 +424,9 @@ internal abstract class WasmSqliteOpenHelper<
*/
protected open fun createConfiguration(
path: String,
defaultLocale: Locale,
openFlags: OpenFlags,
): SqliteDatabaseConfiguration = SqliteDatabaseConfiguration(path, openFlags)
): SqliteDatabaseConfiguration = SqliteDatabaseConfiguration(path, openFlags, defaultLocale)

companion object {
// When true, getReadableDatabase returns a read-only database if it is just being opened.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ru.pixnews.wasm.sqlite.open.helper.common.api.SqliteUintBitMask
import ru.pixnews.wasm.sqlite.open.helper.common.api.clear
import ru.pixnews.wasm.sqlite.open.helper.common.api.or
import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteOpenFlags
import kotlin.jvm.JvmInline

@JvmInline
public value class OpenFlags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@

package ru.pixnews.wasm.sqlite.open.helper

/*
* 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 ru.pixnews.wasm.sqlite.open.helper.internal.SQLiteDatabase
import java.util.Locale
import java.util.regex.Pattern
import ru.pixnews.wasm.sqlite.open.helper.common.api.Locale

/**
* Describes how to configure a database.
Expand Down Expand Up @@ -57,7 +48,6 @@ public class SqliteDatabaseConfiguration {
/**
* The database locale.
*
* Default is the value returned by [Locale.getDefault].
*/
public var locale: Locale? = null

Expand All @@ -82,18 +72,20 @@ public class SqliteDatabaseConfiguration {
*
* @param path The database path.
* @param openFlags Open flags for the database, such as [SQLiteDatabase.OPEN_READWRITE].
* @param defaultLocale Initial locale
*/
public constructor(
path: String = MEMORY_DB_PATH,
openFlags: OpenFlags,
defaultLocale: Locale,
) {
this.path = path
this.openFlags = openFlags
label = stripPathForLogs(path)

// Set default values for optional parameters.
maxSqlCacheSize = @Suppress("MagicNumber") 25
locale = Locale.getDefault()
locale = defaultLocale
}

/**
Expand Down Expand Up @@ -131,13 +123,13 @@ public class SqliteDatabaseConfiguration {

// The pattern we use to strip email addresses from database paths
// when constructing a label to use in log messages.
private val EMAIL_IN_DB_PATTERN: Pattern = Pattern.compile("[\\w\\.\\-]+@[\\w\\.\\-]+")
private val EMAIL_IN_DB_PATTERN: Regex = Regex("""[\w.\-]+@[\w.\-]+""")

private fun stripPathForLogs(path: String): String {
if (path.indexOf('@') == -1) {
return path
}
return EMAIL_IN_DB_PATTERN.matcher(path).replaceAll("XX@YY")
return EMAIL_IN_DB_PATTERN.replace(path, "XX@YY")
}
}
}

0 comments on commit dfb53eb

Please sign in to comment.