Skip to content

Commit

Permalink
Refactor the SQL statements splicing, use the array attaching for Str…
Browse files Browse the repository at this point in the history
…ing params
  • Loading branch information
qiaoyuang committed Apr 19, 2024
1 parent c594567 commit b4ac6e3
Show file tree
Hide file tree
Showing 40 changed files with 206 additions and 198 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

- 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`
* Modified 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.

### 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 @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ public class ClauseString(
append(valueName)
append(' ')
append(symbol)
append(' ')
append('\'')
append(regex)
append('\'')
append(" ?")
}
return SelectCondition(sql)
return SelectCondition(sql, mutableListOf(regex))
}

private fun appendString(notNullSymbol: String, nullSymbol: String, str: String?): SelectCondition {
Expand All @@ -73,16 +70,12 @@ public class ClauseString(
val isNull = str == null
val symbol = if (isNull) nullSymbol else notNullSymbol
append(symbol)
append(' ')
if (str == null)
append(" NULL")
else {
append('\'')
append(str)
append('\'')
}
else
append(" ?")
}
return SelectCondition(sql)
return SelectCondition(sql, if (str == null) null else mutableListOf(str))
}

private fun appendClauseString(symbol: String, clauseString: ClauseString): SelectCondition {
Expand All @@ -97,7 +90,7 @@ public class ClauseString(
append('.')
append(clauseString.valueName)
}
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 @@ -18,7 +18,7 @@ package com.ctrip.sqllin.dsl.sql.clause

/**
* Abstract clause that could link conditions, include 'WHERE' and 'HAVING'
* @author yaquai
* @author yaqiao
*/

public sealed class ConditionClause<T>(private val selectCondition: SelectCondition) : SelectClause<T> {
Expand Down
Loading

0 comments on commit b4ac6e3

Please sign in to comment.