Skip to content

Commit

Permalink
Merge pull request #82 from qiaoyuang/main
Browse files Browse the repository at this point in the history
Refactor the SQL statements splicing method
  • Loading branch information
qiaoyuang authored Apr 19, 2024
2 parents 573a9d4 + 088738e commit 6da726a
Show file tree
Hide file tree
Showing 46 changed files with 246 additions and 402 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

- Date format: YYYY-MM-dd

## v1.3.0 / 2024-04-16

### All

* Update `Kotlin`'s version to `1.9.23`

### sqllin-dsl

* Update `kotlinx.coroutines`'s version to `1.8.0`
* Update `kotlinx.serialization`'s version to `1.6.3`
* Modify the SQL statements' splicing method, that fixed the [issue#77](https://github.com/ctripcorp/SQLlin/issues/77) that users can't read/write special symbols as the values in SQL statements.
* Performance optimization, use `ArrayDeque` to replace the LinkedList for SQL statements management (self-implemented)

### sqllin-driver

* Update the `sqlite-jdbc`'s version to `3.45.3.0`

### sqllin-processor

* Update `KSP`'s version to `1.9.23-1.0.20`

## v1.2.4 / 2024-01-05

### All
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
VERSION=1.2.4
VERSION=1.3.0
GROUP=com.ctrip.kotlin

kotlinVersion=1.9.22
kspVersion=1.9.22-1.0.16
serializationVersion=1.6.2
coroutinesVersion=1.7.3
kotlinVersion=1.9.23
kspVersion=1.9.23-1.0.20
serializationVersion=1.6.3
coroutinesVersion=1.8.0
androidxAnnotationVersion=1.7.1

#Maven Publish Information
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Mar 08 15:11:46 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
2 changes: 1 addition & 1 deletion sqllin-driver/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ kotlin {

val jvmMain by getting {
dependencies {
implementation("org.xerial:sqlite-jdbc:3.44.1.0")
implementation("org.xerial:sqlite-jdbc:3.45.3.0")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import android.database.sqlite.SQLiteDatabase

internal class AndroidDatabaseConnection(private val database: SQLiteDatabase) : DatabaseConnection {

override fun execSQL(sql: String, bindParams: Array<Any?>?) =
override fun execSQL(sql: String, bindParams: Array<out Any?>?) =
if (bindParams == null)
database.execSQL(sql)
else
database.execSQL(sql, bindParams)

override fun executeInsert(sql: String, bindParams: Array<Any?>?) = execSQL(sql, bindParams)
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) = execSQL(sql, bindParams)

override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) = execSQL(sql, bindParams)
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) = execSQL(sql, bindParams)

override fun query(sql: String, bindParams: Array<String?>?): CommonCursor = AndroidCursor(database.rawQuery(sql, bindParams))
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor = AndroidCursor(database.rawQuery(sql, bindParams))

override fun beginTransaction() = database.beginTransaction()
override fun endTransaction() = database.endTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ package com.ctrip.sqllin.driver

public interface DatabaseConnection {

public fun execSQL(sql: String, bindParams: Array<Any?>? = null)
public fun executeInsert(sql: String, bindParams: Array<Any?>? = null)
public fun executeUpdateDelete(sql: String, bindParams: Array<Any?>? = null)
public fun execSQL(sql: String, bindParams: Array<out Any?>? = null)
public fun executeInsert(sql: String, bindParams: Array<out Any?>? = null)
public fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>? = null)

public fun query(sql: String, bindParams: Array<String?>? = null): CommonCursor
public fun query(sql: String, bindParams: Array<out String?>? = null): CommonCursor

public fun beginTransaction()
public fun setTransactionSuccessful()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal abstract class AbstractJdbcDatabaseConnection : DatabaseConnection {

abstract fun createStatement(sql: String): PreparedStatement

protected fun bindParamsToSQL(sql: String, bindParams: Array<Any?>?): PreparedStatement = createStatement(sql).apply {
protected fun bindParamsToSQL(sql: String, bindParams: Array<out Any?>?): PreparedStatement = createStatement(sql).apply {
bindParams?.run {
require(isNotEmpty()) { "Empty bindArgs" }
forEachIndexed { index, any ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ internal class ConcurrentDatabaseConnection(private val delegateConnection: Data

private val accessLock = ReentrantLock()

override fun execSQL(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
override fun execSQL(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
delegateConnection.execSQL(sql, bindParams)
}

override fun executeInsert(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
delegateConnection.executeInsert(sql, bindParams)
}

override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
delegateConnection.executeUpdateDelete(sql, bindParams)
}

override fun query(sql: String, bindParams: Array<String?>?): CommonCursor = accessLock.withLock {
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor = accessLock.withLock {
delegateConnection.query(sql, bindParams)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ import java.util.concurrent.atomic.AtomicBoolean

internal class JdbcDatabaseConnection(private val connection: Connection) : AbstractJdbcDatabaseConnection() {

override fun execSQL(sql: String, bindParams: Array<Any?>?) {
override fun execSQL(sql: String, bindParams: Array<out Any?>?) {
bindParamsToSQL(sql, bindParams).use {
it.execute()
}
}

override fun executeInsert(sql: String, bindParams: Array<Any?>?) {
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) {
executeUpdate(sql, bindParams)
}

override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) {
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) {
executeUpdate(sql, bindParams)
}

private fun executeUpdate(sql: String, bindParams: Array<Any?>?): Int = bindParamsToSQL(sql, bindParams).use {
private fun executeUpdate(sql: String, bindParams: Array<out Any?>?): Int = bindParamsToSQL(sql, bindParams).use {
it.executeUpdate()
}

override fun query(sql: String, bindParams: Array<String?>?): CommonCursor {
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor {
val statement = connection.prepareStatement(sql)
bindParams?.forEachIndexed { index, str ->
str?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ internal class ConcurrentDatabaseConnection(

private val accessLock = Lock()

override fun execSQL(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
override fun execSQL(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
delegateConnection.execSQL(sql, bindParams)
}

override fun executeInsert(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
delegateConnection.executeInsert(sql, bindParams)
}

override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
delegateConnection.executeUpdateDelete(sql, bindParams)
}

override fun query(sql: String, bindParams: Array<String?>?): CommonCursor = accessLock.withLock {
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor = accessLock.withLock {
delegateConnection.query(sql, bindParams)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal abstract class NativeDatabaseConnection : DatabaseConnection {

abstract fun createStatement(sql: String): SQLiteStatement

protected fun bindParamsToSQL(sql: String, bindParams: Array<Any?>?): SQLiteStatement = createStatement(sql).apply {
protected fun bindParamsToSQL(sql: String, bindParams: Array<out Any?>?): SQLiteStatement = createStatement(sql).apply {
bindParams?.run {
require(isNotEmpty()) { "Empty bindArgs" }
forEachIndexed { index, any ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class RealDatabaseConnection(

private data class Transaction(val isSuccessful: Boolean)

override fun execSQL(sql: String, bindParams: Array<Any?>?) =
override fun execSQL(sql: String, bindParams: Array<out Any?>?) =
if (bindParams == null) {
database.rawExecSql(sql)
} else {
Expand All @@ -49,7 +49,7 @@ internal class RealDatabaseConnection(
}
}

override fun executeInsert(sql: String, bindParams: Array<Any?>?) {
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) {
val statement = bindParamsToSQL(sql, bindParams)
try {
statement.executeInsert()
Expand All @@ -58,7 +58,7 @@ internal class RealDatabaseConnection(
}
}

override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) {
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) {
val statement = bindParamsToSQL(sql, bindParams)
try {
statement.executeUpdateDelete()
Expand All @@ -67,7 +67,7 @@ internal class RealDatabaseConnection(
}
}

override fun query(sql: String, bindParams: Array<String?>?): CommonCursor {
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor {
val statement = createStatement(sql)
bindParams?.forEachIndexed { index, str ->
str?.let {
Expand Down
2 changes: 1 addition & 1 deletion sqllin-dsl/doc/getting-start-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {
id("com.google.devtools.ksp")
}

val sqllinVersion = "1.2.4"
val sqllinVersion = "1.3.0"

kotlin {
// ......
Expand Down
6 changes: 3 additions & 3 deletions sqllin-dsl/doc/getting-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ plugins {
id("com.google.devtools.ksp")
}

val sqllinVersion = "1.2.4"
val sqllinVersion = "1.3.0"

kotlin {
// ......
Expand All @@ -30,10 +30,10 @@ kotlin {
implementation("com.ctrip.kotlin:sqllin-driver:$sqllinVersion")

// The sqllin-dsl serialization and deserialization depends on kotlinx-serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.6.3")

// Since 1.2.2, sqllin-dsl depends on kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
}
}
// ......
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public class DatabaseScope internal constructor(
}

private fun <T> addSelectStatement(statement: SelectStatement<T>) {
if (unionSelectStatementGroupStack.isNotEmpty)
(unionSelectStatementGroupStack.top as UnionSelectStatementGroup<T>).addSelectStatement(statement)
if (unionSelectStatementGroupStack.isNotEmpty())
(unionSelectStatementGroupStack.last() as UnionSelectStatementGroup<T>).addSelectStatement(statement)
else
addStatement(statement)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ public class DatabaseScope internal constructor(
*/

public infix fun Table<*>.DELETE(x: X) {
val statement = Delete.deleteAllEntity(this, databaseConnection)
val statement = Delete.deleteAllEntities(this, databaseConnection)
addStatement(statement)
}

Expand Down Expand Up @@ -223,9 +223,9 @@ public class DatabaseScope internal constructor(
* The 'UNION' clause of Select.
*/

private val unionSelectStatementGroupStack by lazy { Stack<UnionSelectStatementGroup<*>>() }
private val unionSelectStatementGroupStack by lazy { ArrayDeque<UnionSelectStatementGroup<*>>() }

private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executiveEngine
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.lastOrNull() ?: transactionStatementsGroup ?: executiveEngine

public inline fun <T> Table<T>.UNION(block: Table<T>.(Table<T>) -> Unit): FinalSelectStatement<T> {
beginUnion<T>()
Expand All @@ -252,16 +252,16 @@ public class DatabaseScope internal constructor(
}

public fun <T> beginUnion() {
unionSelectStatementGroupStack.push(UnionSelectStatementGroup<T>())
unionSelectStatementGroupStack.add(UnionSelectStatementGroup<T>())
}

public fun <T> createUnionSelectStatement(isUnionAll: Boolean): FinalSelectStatement<T> {
check(unionSelectStatementGroupStack.isNotEmpty) { "Please invoke the 'beginUnion' before you invoke this function!!!" }
return (unionSelectStatementGroupStack.top as UnionSelectStatementGroup<T>).unionStatements(isUnionAll)
check(unionSelectStatementGroupStack.isNotEmpty()) { "Please invoke the 'beginUnion' before you invoke this function!!!" }
return (unionSelectStatementGroupStack.last() as UnionSelectStatementGroup<T>).unionStatements(isUnionAll)
}

public fun <T> endUnion(selectStatement: SelectStatement<T>?) {
unionSelectStatementGroupStack.pop()
unionSelectStatementGroupStack.removeLast()
selectStatement?.let { addSelectStatement(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public sealed class JoinClause<R>(vararg tables: Table<*>) : BaseJoinClause<R>(*
public infix fun <R> JoinStatementWithoutCondition<R>.ON(condition: SelectCondition): JoinSelectStatement<R> =
convertToJoinSelectStatement(condition)

@Suppress("NOTHING_TO_INLINE")
public inline infix fun <R> JoinStatementWithoutCondition<R>.USING(clauseElement: ClauseElement): JoinSelectStatement<R> =
USING(listOf(clauseElement))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ public class ClauseBoolean(
append('.')
}
append(valueName)
append(' ')
if (bool)
append('>')
append(" > ")
else
append("<=")
append(' ')
append(" <= ")
append(0)
}
return SelectCondition(sql)
return SelectCondition(sql, null)
}

override fun hashCode(): Int = valueName.hashCode() + table.tableName.hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class ClauseNumber(
append(symbol)
} while (hasNext)
}
return SelectCondition(sql)
return SelectCondition(sql, null)
}

internal infix fun between(range: LongRange): SelectCondition {
Expand All @@ -96,7 +96,7 @@ public class ClauseNumber(
append(" AND ")
append(range.last)
}
return SelectCondition(sql)
return SelectCondition(sql, null)
}

private fun appendNumber(symbol: String, number: Number): SelectCondition {
Expand All @@ -111,7 +111,7 @@ public class ClauseNumber(
append(' ')
append(number)
}
return SelectCondition(sql)
return SelectCondition(sql, null)
}

private fun appendNullableNumber(notNullSymbol: String, nullSymbol: String, number: Number?): SelectCondition {
Expand All @@ -126,7 +126,7 @@ public class ClauseNumber(
append(' ')
append(number ?: "NULL")
}
return SelectCondition(sql)
return SelectCondition(sql, null)
}

private fun appendClauseNumber(symbol: String, clauseNumber: ClauseNumber): SelectCondition {
Expand All @@ -141,7 +141,7 @@ public class ClauseNumber(
append('.')
append(clauseNumber.valueName)
}
return SelectCondition(sql)
return SelectCondition(sql, null)
}

override fun hashCode(): Int = valueName.hashCode() + table.tableName.hashCode()
Expand Down
Loading

0 comments on commit 6da726a

Please sign in to comment.