Skip to content

Commit

Permalink
Merge pull request #45 from qiaoyuang/main
Browse files Browse the repository at this point in the history
Update version to 1.2.0
  • Loading branch information
qiaoyuang authored Sep 19, 2023
2 parents 3893d79 + b83586c commit 22f17ad
Show file tree
Hide file tree
Showing 25 changed files with 257 additions and 287 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

- Date format: YYYY-MM-dd

## v1.2.0 / 2023-09-19

### sqllin-dsl

* Add the new JVM target

### sqllin-driver

* Add the new JVM target
* Breaking change: Remove the public property: `DatabaseConnection#closed`
* The Android (<= 9) target supports to set the `journalMode` and `synchronousMode` now

## v1.1.1 / 2023-08-12

### All
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SQLlin supports these platforms:

- Multiplatform Common
- Android (6.0+)
- JVM (Java 11+, since `1.2.0`)
- iOS (x64, arm64, simulatorArm64)
- macOS (x64, arm64)
- watchOS (x64, arm32, arm64, simulatorArm64, deviceArm64)
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ SQLlin 支持如下平台:

- Multiplatform Common
- Android (6.0+)
- JVM (Java 11+, since `1.2.0`)
- iOS (x64, arm64, simulatorArm64)
- macOS (x64, arm64)
- watchOS (x64, arm32, arm64, simulatorArm64, deviceArm64)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=1.1.1
VERSION=1.2.0
GROUP=com.ctrip.kotlin

kotlinVersion=1.9.0
Expand Down
2 changes: 2 additions & 0 deletions sqllin-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ _sqllin-driver_ use the _New Native Driver_.
Whatever, [SQLiter](https://github.com/touchlab/SQLiter) still is a good project. I referred to a lot of designs and code
details from it and use them in _New Native Driver_ in _sqllin-driver_ .

Since `1.2.0`, SQLlin started to support JVM target, and it's base on [sqlite-jdbc](https://github.com/xerial/sqlite-jdbc).

## Basic usage

I don't recommend you use _sqllin-driver_ in your application projects directly, but if you want to develop your own SQLite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ internal class AndroidDatabaseConnection(private val database: SQLiteDatabase) :

override fun close() = database.close()

@Deprecated(
message = "The property closed has been deprecated, please use the isClosed to replace it",
replaceWith = ReplaceWith("isClosed")
)
override val closed: Boolean
get() = !database.isOpen

override val isClosed: Boolean
get() = !database.isOpen
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ internal value class AndroidDatabasePath(val context: Context) : DatabasePath
public actual fun openDatabase(config: DatabaseConfiguration): DatabaseConnection {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && config.inMemory)
return AndroidDatabaseConnection(createInMemory(config.toAndroidOpenParams()))
val helper = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
val isEqualsOrHigherThanAndroidP = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
val helper = if (isEqualsOrHigherThanAndroidP)
AndroidDBHelper(config)
else
OldAndroidDBHelper(config)
val database = if (config.isReadOnly)
helper.readableDatabase
else
helper.writableDatabase
return AndroidDatabaseConnection(database)
val connection = AndroidDatabaseConnection(database)
if (!isEqualsOrHigherThanAndroidP) {
connection.updateSynchronousMode(config.synchronousMode)
connection.updateJournalMode(config.journalMode)
}
return connection
}

private class OldAndroidDBHelper(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2023 Ctrip.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ctrip.sqllin.driver.platform

/**
* The tools with Android implementation
* @author yaqiao
*/

internal actual inline val separatorChar: Char
get() = '/'
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,5 @@ public interface DatabaseConnection {

public fun close()

@Deprecated(
message = "The property closed has been deprecated, please use the isClosed to replace it",
replaceWith = ReplaceWith("isClosed")
)
public val closed: Boolean

public val isClosed: Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.ctrip.sqllin.driver

import com.ctrip.sqllin.driver.platform.separatorChar
import kotlin.jvm.JvmInline

/**
* SQLite extension function
* @author yaqiao
Expand Down Expand Up @@ -62,4 +65,108 @@ public inline fun <T> DatabaseConnection.withQuery(
}
}

public expect fun deleteDatabase(path: DatabasePath, name: String): Boolean
public expect fun deleteDatabase(path: DatabasePath, name: String): Boolean

internal infix fun DatabaseConnection.updateSynchronousMode(mode: SynchronousMode) {
val currentJournalMode = withQuery("PRAGMA synchronous;") {
it.next()
it.getInt(0)
}
if (currentJournalMode != mode.value)
execSQL("PRAGMA synchronous=${mode.value};")
}

internal infix fun DatabaseConnection.updateJournalMode(mode: JournalMode) {
val currentJournalMode = withQuery("PRAGMA journal_mode;") {
it.next()
it.getString(0)
}
if (!currentJournalMode.equals(mode.name, ignoreCase = true))
withQuery("PRAGMA journal_mode=${mode.name};") {}
}

internal fun DatabaseConnection.migrateIfNeeded(
create: (DatabaseConnection) -> Unit,
upgrade: (DatabaseConnection, Int, Int) -> Unit,
version: Int,
) = withTransaction {
val initialVersion = withQuery("PRAGMA user_version;") {
it.next()
it.getInt(0)
}
if (initialVersion == 0) {
create(this)
execSQL("PRAGMA user_version = $version;")
} else if (initialVersion != version) {
if (initialVersion > version) {
throw IllegalStateException("Database version $initialVersion newer than config version $version")
}
upgrade(this, initialVersion, version)
execSQL("PRAGMA user_version = $version;")
}
}

internal fun DatabaseConfiguration.diskOrMemoryPath(): String =
if (inMemory) {
if (name.isBlank())
":memory:"
else
"file:$name?mode=memory&cache=shared"
} else {
require(name.isNotBlank()) { "Database name cannot be blank" }
getDatabaseFullPath((path as StringDatabasePath).pathString, name)
}

internal fun getDatabaseFullPath(dirPath: String, name: String): String {
val param = when {
dirPath.isEmpty() -> name
name.isEmpty() -> dirPath
else -> join(dirPath, name)
}
return fixSlashes(param)
}

private fun join(prefix: String, suffix: String): String {
val haveSlash = (prefix.isNotEmpty() && prefix.last() == separatorChar)
|| (suffix.isNotEmpty() && suffix.first() == separatorChar)
return buildString {
append(prefix)
if (!haveSlash)
append(separatorChar)
append(suffix)
}
}

private fun fixSlashes(origPath: String): String {
// Remove duplicate adjacent slashes.
var lastWasSlash = false
val newPath = origPath.toCharArray()
val length = newPath.size
var newLength = 0
val initialIndex = if (origPath.startsWith("file://", true)) 7 else 0
for (i in initialIndex ..< length) {
val ch = newPath[i]
if (ch == separatorChar) {
if (!lastWasSlash) {
newPath[newLength++] = separatorChar
lastWasSlash = true
}
} else {
newPath[newLength++] = ch
lastWasSlash = false
}
}
// Remove any trailing slash (unless this is the root of the file system).
if (lastWasSlash && newLength > 1) {
newLength--
}

// Reuse the original string if possible.
return if (newLength != length) buildString(newLength) {
append(newPath)
setLength(newLength)
} else origPath
}

@JvmInline
internal value class StringDatabasePath(val pathString: String) : DatabasePath
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@

package com.ctrip.sqllin.driver.platform

import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi

/**
* The tools with platform-specific implementation
* @author yaqiao
*/

@OptIn(ExperimentalForeignApi::class)
internal expect fun bytesToString(bv: CPointer<ByteVar>): String

internal expect val separatorChar: Char
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ internal class ConcurrentDatabaseConnection(private val delegateConnection: Data
delegateConnection.close()
}

@Deprecated(
message = "The property closed has been deprecated, please use the isClosed to replace it",
replaceWith = ReplaceWith("isClosed")
)
override val closed: Boolean
get() = delegateConnection.isClosed
override val isClosed: Boolean
get() = delegateConnection.isClosed
}
Loading

0 comments on commit 22f17ad

Please sign in to comment.