Skip to content

Commit 6da726a

Browse files
authored
Merge pull request #82 from qiaoyuang/main
Refactor the SQL statements splicing method
2 parents 573a9d4 + 088738e commit 6da726a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+246
-402
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
- Date format: YYYY-MM-dd
44

5+
## v1.3.0 / 2024-04-16
6+
7+
### All
8+
9+
* Update `Kotlin`'s version to `1.9.23`
10+
11+
### sqllin-dsl
12+
13+
* Update `kotlinx.coroutines`'s version to `1.8.0`
14+
* Update `kotlinx.serialization`'s version to `1.6.3`
15+
* 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.
16+
* Performance optimization, use `ArrayDeque` to replace the LinkedList for SQL statements management (self-implemented)
17+
18+
### sqllin-driver
19+
20+
* Update the `sqlite-jdbc`'s version to `3.45.3.0`
21+
22+
### sqllin-processor
23+
24+
* Update `KSP`'s version to `1.9.23-1.0.20`
25+
526
## v1.2.4 / 2024-01-05
627

728
### All

gradle.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
VERSION=1.2.4
1+
VERSION=1.3.0
22
GROUP=com.ctrip.kotlin
33

4-
kotlinVersion=1.9.22
5-
kspVersion=1.9.22-1.0.16
6-
serializationVersion=1.6.2
7-
coroutinesVersion=1.7.3
4+
kotlinVersion=1.9.23
5+
kspVersion=1.9.23-1.0.20
6+
serializationVersion=1.6.3
7+
coroutinesVersion=1.8.0
88
androidxAnnotationVersion=1.7.1
99

1010
#Maven Publish Information
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Tue Mar 08 15:11:46 CST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

sqllin-driver/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ kotlin {
9595

9696
val jvmMain by getting {
9797
dependencies {
98-
implementation("org.xerial:sqlite-jdbc:3.44.1.0")
98+
implementation("org.xerial:sqlite-jdbc:3.45.3.0")
9999
}
100100
}
101101
}

sqllin-driver/src/androidMain/kotlin/com/ctrip/sqllin/driver/AndroidDatabaseConnection.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ import android.database.sqlite.SQLiteDatabase
2525

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

28-
override fun execSQL(sql: String, bindParams: Array<Any?>?) =
28+
override fun execSQL(sql: String, bindParams: Array<out Any?>?) =
2929
if (bindParams == null)
3030
database.execSQL(sql)
3131
else
3232
database.execSQL(sql, bindParams)
3333

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

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

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

4040
override fun beginTransaction() = database.beginTransaction()
4141
override fun endTransaction() = database.endTransaction()

sqllin-driver/src/commonMain/kotlin/com/ctrip/sqllin/driver/DatabaseConnection.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ package com.ctrip.sqllin.driver
2323

2424
public interface DatabaseConnection {
2525

26-
public fun execSQL(sql: String, bindParams: Array<Any?>? = null)
27-
public fun executeInsert(sql: String, bindParams: Array<Any?>? = null)
28-
public fun executeUpdateDelete(sql: String, bindParams: Array<Any?>? = null)
26+
public fun execSQL(sql: String, bindParams: Array<out Any?>? = null)
27+
public fun executeInsert(sql: String, bindParams: Array<out Any?>? = null)
28+
public fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>? = null)
2929

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

3232
public fun beginTransaction()
3333
public fun setTransactionSuccessful()

sqllin-driver/src/jvmMain/kotlin/com/ctrip/sqllin/driver/AbstractJdbcDatabaseConnection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal abstract class AbstractJdbcDatabaseConnection : DatabaseConnection {
2929

3030
abstract fun createStatement(sql: String): PreparedStatement
3131

32-
protected fun bindParamsToSQL(sql: String, bindParams: Array<Any?>?): PreparedStatement = createStatement(sql).apply {
32+
protected fun bindParamsToSQL(sql: String, bindParams: Array<out Any?>?): PreparedStatement = createStatement(sql).apply {
3333
bindParams?.run {
3434
require(isNotEmpty()) { "Empty bindArgs" }
3535
forEachIndexed { index, any ->

sqllin-driver/src/jvmMain/kotlin/com/ctrip/sqllin/driver/ConcurrentDatabaseConnection.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ internal class ConcurrentDatabaseConnection(private val delegateConnection: Data
2828

2929
private val accessLock = ReentrantLock()
3030

31-
override fun execSQL(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
31+
override fun execSQL(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
3232
delegateConnection.execSQL(sql, bindParams)
3333
}
3434

35-
override fun executeInsert(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
35+
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
3636
delegateConnection.executeInsert(sql, bindParams)
3737
}
3838

39-
override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
39+
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
4040
delegateConnection.executeUpdateDelete(sql, bindParams)
4141
}
4242

43-
override fun query(sql: String, bindParams: Array<String?>?): CommonCursor = accessLock.withLock {
43+
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor = accessLock.withLock {
4444
delegateConnection.query(sql, bindParams)
4545
}
4646

sqllin-driver/src/jvmMain/kotlin/com/ctrip/sqllin/driver/JdbcDatabaseConnection.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ import java.util.concurrent.atomic.AtomicBoolean
2929

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

32-
override fun execSQL(sql: String, bindParams: Array<Any?>?) {
32+
override fun execSQL(sql: String, bindParams: Array<out Any?>?) {
3333
bindParamsToSQL(sql, bindParams).use {
3434
it.execute()
3535
}
3636
}
3737

38-
override fun executeInsert(sql: String, bindParams: Array<Any?>?) {
38+
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) {
3939
executeUpdate(sql, bindParams)
4040
}
4141

42-
override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) {
42+
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) {
4343
executeUpdate(sql, bindParams)
4444
}
4545

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

50-
override fun query(sql: String, bindParams: Array<String?>?): CommonCursor {
50+
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor {
5151
val statement = connection.prepareStatement(sql)
5252
bindParams?.forEachIndexed { index, str ->
5353
str?.let {

sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/ConcurrentDatabaseConnection.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ internal class ConcurrentDatabaseConnection(
3030

3131
private val accessLock = Lock()
3232

33-
override fun execSQL(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
33+
override fun execSQL(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
3434
delegateConnection.execSQL(sql, bindParams)
3535
}
3636

37-
override fun executeInsert(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
37+
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
3838
delegateConnection.executeInsert(sql, bindParams)
3939
}
4040

41-
override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) = accessLock.withLock {
41+
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) = accessLock.withLock {
4242
delegateConnection.executeUpdateDelete(sql, bindParams)
4343
}
4444

45-
override fun query(sql: String, bindParams: Array<String?>?): CommonCursor = accessLock.withLock {
45+
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor = accessLock.withLock {
4646
delegateConnection.query(sql, bindParams)
4747
}
4848

sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/NativeDatabaseConnection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal abstract class NativeDatabaseConnection : DatabaseConnection {
2525

2626
abstract fun createStatement(sql: String): SQLiteStatement
2727

28-
protected fun bindParamsToSQL(sql: String, bindParams: Array<Any?>?): SQLiteStatement = createStatement(sql).apply {
28+
protected fun bindParamsToSQL(sql: String, bindParams: Array<out Any?>?): SQLiteStatement = createStatement(sql).apply {
2929
bindParams?.run {
3030
require(isNotEmpty()) { "Empty bindArgs" }
3131
forEachIndexed { index, any ->

sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/RealDatabaseConnection.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal class RealDatabaseConnection(
3737

3838
private data class Transaction(val isSuccessful: Boolean)
3939

40-
override fun execSQL(sql: String, bindParams: Array<Any?>?) =
40+
override fun execSQL(sql: String, bindParams: Array<out Any?>?) =
4141
if (bindParams == null) {
4242
database.rawExecSql(sql)
4343
} else {
@@ -49,7 +49,7 @@ internal class RealDatabaseConnection(
4949
}
5050
}
5151

52-
override fun executeInsert(sql: String, bindParams: Array<Any?>?) {
52+
override fun executeInsert(sql: String, bindParams: Array<out Any?>?) {
5353
val statement = bindParamsToSQL(sql, bindParams)
5454
try {
5555
statement.executeInsert()
@@ -58,7 +58,7 @@ internal class RealDatabaseConnection(
5858
}
5959
}
6060

61-
override fun executeUpdateDelete(sql: String, bindParams: Array<Any?>?) {
61+
override fun executeUpdateDelete(sql: String, bindParams: Array<out Any?>?) {
6262
val statement = bindParamsToSQL(sql, bindParams)
6363
try {
6464
statement.executeUpdateDelete()
@@ -67,7 +67,7 @@ internal class RealDatabaseConnection(
6767
}
6868
}
6969

70-
override fun query(sql: String, bindParams: Array<String?>?): CommonCursor {
70+
override fun query(sql: String, bindParams: Array<out String?>?): CommonCursor {
7171
val statement = createStatement(sql)
7272
bindParams?.forEachIndexed { index, str ->
7373
str?.let {

sqllin-dsl/doc/getting-start-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
id("com.google.devtools.ksp")
1515
}
1616

17-
val sqllinVersion = "1.2.4"
17+
val sqllinVersion = "1.3.0"
1818

1919
kotlin {
2020
// ......

sqllin-dsl/doc/getting-start.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
id("com.google.devtools.ksp")
1717
}
1818

19-
val sqllinVersion = "1.2.4"
19+
val sqllinVersion = "1.3.0"
2020

2121
kotlin {
2222
// ......
@@ -30,10 +30,10 @@ kotlin {
3030
implementation("com.ctrip.kotlin:sqllin-driver:$sqllinVersion")
3131

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

3535
// Since 1.2.2, sqllin-dsl depends on kotlinx.coroutines
36-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
36+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
3737
}
3838
}
3939
// ......

sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/DatabaseScope.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public class DatabaseScope internal constructor(
8686
}
8787

8888
private fun <T> addSelectStatement(statement: SelectStatement<T>) {
89-
if (unionSelectStatementGroupStack.isNotEmpty)
90-
(unionSelectStatementGroupStack.top as UnionSelectStatementGroup<T>).addSelectStatement(statement)
89+
if (unionSelectStatementGroupStack.isNotEmpty())
90+
(unionSelectStatementGroupStack.last() as UnionSelectStatementGroup<T>).addSelectStatement(statement)
9191
else
9292
addStatement(statement)
9393
}
@@ -124,7 +124,7 @@ public class DatabaseScope internal constructor(
124124
*/
125125

126126
public infix fun Table<*>.DELETE(x: X) {
127-
val statement = Delete.deleteAllEntity(this, databaseConnection)
127+
val statement = Delete.deleteAllEntities(this, databaseConnection)
128128
addStatement(statement)
129129
}
130130

@@ -223,9 +223,9 @@ public class DatabaseScope internal constructor(
223223
* The 'UNION' clause of Select.
224224
*/
225225

226-
private val unionSelectStatementGroupStack by lazy { Stack<UnionSelectStatementGroup<*>>() }
226+
private val unionSelectStatementGroupStack by lazy { ArrayDeque<UnionSelectStatementGroup<*>>() }
227227

228-
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executiveEngine
228+
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.lastOrNull() ?: transactionStatementsGroup ?: executiveEngine
229229

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

254254
public fun <T> beginUnion() {
255-
unionSelectStatementGroupStack.push(UnionSelectStatementGroup<T>())
255+
unionSelectStatementGroupStack.add(UnionSelectStatementGroup<T>())
256256
}
257257

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

263263
public fun <T> endUnion(selectStatement: SelectStatement<T>?) {
264-
unionSelectStatementGroupStack.pop()
264+
unionSelectStatementGroupStack.removeLast()
265265
selectStatement?.let { addSelectStatement(it) }
266266
}
267267

sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/sql/clause/BaseJoinClause.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public sealed class JoinClause<R>(vararg tables: Table<*>) : BaseJoinClause<R>(*
4747
public infix fun <R> JoinStatementWithoutCondition<R>.ON(condition: SelectCondition): JoinSelectStatement<R> =
4848
convertToJoinSelectStatement(condition)
4949

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

sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/sql/clause/ClauseBoolean.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ public class ClauseBoolean(
3636
append('.')
3737
}
3838
append(valueName)
39-
append(' ')
4039
if (bool)
41-
append('>')
40+
append(" > ")
4241
else
43-
append("<=")
44-
append(' ')
42+
append(" <= ")
4543
append(0)
4644
}
47-
return SelectCondition(sql)
45+
return SelectCondition(sql, null)
4846
}
4947

5048
override fun hashCode(): Int = valueName.hashCode() + table.tableName.hashCode()

sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/sql/clause/ClauseNumber.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class ClauseNumber(
8181
append(symbol)
8282
} while (hasNext)
8383
}
84-
return SelectCondition(sql)
84+
return SelectCondition(sql, null)
8585
}
8686

8787
internal infix fun between(range: LongRange): SelectCondition {
@@ -96,7 +96,7 @@ public class ClauseNumber(
9696
append(" AND ")
9797
append(range.last)
9898
}
99-
return SelectCondition(sql)
99+
return SelectCondition(sql, null)
100100
}
101101

102102
private fun appendNumber(symbol: String, number: Number): SelectCondition {
@@ -111,7 +111,7 @@ public class ClauseNumber(
111111
append(' ')
112112
append(number)
113113
}
114-
return SelectCondition(sql)
114+
return SelectCondition(sql, null)
115115
}
116116

117117
private fun appendNullableNumber(notNullSymbol: String, nullSymbol: String, number: Number?): SelectCondition {
@@ -126,7 +126,7 @@ public class ClauseNumber(
126126
append(' ')
127127
append(number ?: "NULL")
128128
}
129-
return SelectCondition(sql)
129+
return SelectCondition(sql, null)
130130
}
131131

132132
private fun appendClauseNumber(symbol: String, clauseNumber: ClauseNumber): SelectCondition {
@@ -141,7 +141,7 @@ public class ClauseNumber(
141141
append('.')
142142
append(clauseNumber.valueName)
143143
}
144-
return SelectCondition(sql)
144+
return SelectCondition(sql, null)
145145
}
146146

147147
override fun hashCode(): Int = valueName.hashCode() + table.tableName.hashCode()

0 commit comments

Comments
 (0)