Skip to content

Commit

Permalink
Use ArrayDeque to replace the LinkedList (self-implemented)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaoyuang committed Apr 19, 2024
1 parent b4ac6e3 commit 088738e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 210 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

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

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 @@ -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 @@ -25,25 +25,23 @@ internal class DatabaseExecuteEngine(
private val enableSimpleSQLLog: Boolean,
) : StatementContainer {

private lateinit var statementsLinkedList: StatementLinkedList<ExecutableStatement>
private val statementList = ArrayDeque<ExecutableStatement>()

override infix fun changeLastStatement(statement: SingleStatement) {
if (statementsLinkedList.lastStatement is UpdateStatementWithoutWhereClause<*>
|| statementsLinkedList.lastStatement is SelectStatement<*>)
statementsLinkedList.resetLastStatement(statement)
else
if (statementList.lastOrNull() is UpdateStatementWithoutWhereClause<*>
|| statementList.lastOrNull() is SelectStatement<*>) {
statementList.removeLast()
statementList.add(statement)
} else
throw IllegalStateException("Current statement can't append clause.")
}

infix fun addStatement(statement: ExecutableStatement) {
if (::statementsLinkedList.isInitialized)
statementsLinkedList.addStatement(statement)
else
statementsLinkedList = StatementLinkedList(statement)
statementList.add(statement)
}

fun executeAllStatement() {
statementsLinkedList.forEach {
statementList.forEach {
when (it) {
is SingleStatement -> {
if (enableSimpleSQLLog)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ internal class TransactionStatementsGroup(
private val enableSimpleSQLLog: Boolean,
) : ExecutableStatement, StatementContainer {

private lateinit var statementList: StatementLinkedList<SingleStatement>
private val statementList = ArrayDeque<SingleStatement>()

infix fun addStatement(statement: SingleStatement) {
if (this::statementList.isInitialized)
statementList.addStatement(statement)
else
statementList = StatementLinkedList(statement)
statementList.add(statement)
}

override fun execute() = databaseConnection.withTransaction {
Expand All @@ -47,10 +44,11 @@ internal class TransactionStatementsGroup(
}

override infix fun changeLastStatement(statement: SingleStatement) {
if (statementList.lastStatement is UpdateStatementWithoutWhereClause<*>
|| statementList.lastStatement is SelectStatement<*>)
statementList resetLastStatement statement
else
if (statementList.lastOrNull() is UpdateStatementWithoutWhereClause<*>
|| statementList.lastOrNull() is SelectStatement<*>) {
statementList.removeLast()
statementList.add(statement)
} else
throw IllegalStateException("Current statement can't append clause.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,31 @@ package com.ctrip.sqllin.dsl.sql.statement

internal class UnionSelectStatementGroup<T> : StatementContainer {

private var statementLinkedList: StatementLinkedList<SelectStatement<T>>? = null
private val statementList = ArrayDeque<SelectStatement<T>>()

infix fun addSelectStatement(selectStatement: SelectStatement<T>) {
if (statementLinkedList != null)
statementLinkedList!!.addStatement(selectStatement)
else
statementLinkedList = StatementLinkedList(selectStatement)
statementList.add(selectStatement)
}

internal fun unionStatements(isUnionAll: Boolean): FinalSelectStatement<T> {
require(statementLinkedList?.hasNext() == true) { "Please write at least two 'select' statements on 'UNION' scope" }
var firstStatement: SelectStatement<T>? = null
require(statementList.isNotEmpty()) { "Please write at least two 'select' statements on 'UNION' scope" }
var parameters: MutableList<String>? = null
val unionSqlStr = buildString {
statementLinkedList!!.run {
val unionKeyWord = if (isUnionAll) " UNION ALL " else " UNION "
do {
val next = next()
append(next.sqlStr)
val hasNext = hasNext()
if (firstStatement == null) {
firstStatement = next
if (!hasNext)
throw IllegalStateException("Please write at least two 'select' statements on 'UNION' scope")
}
if (parameters == null) {
parameters = next.parameters
} else next.parameters?.let {
parameters!!.addAll(it)
}
if (hasNext)
append(unionKeyWord)
} while (hasNext)
check(statementList.size > 1) { "Please write at least two 'select' statements on 'UNION' scope" }
val unionKeyWord = if (isUnionAll) " UNION ALL " else " UNION "
statementList.forEachIndexed { index, statement ->
append(statement.sqlStr)
if (parameters == null)
parameters = statement.parameters
else statement.parameters?.let {
parameters!!.addAll(it)
}
if (index != statementList.lastIndex)
append(unionKeyWord)
}
}

return firstStatement!!.run {
return statementList.first().run {
FinalSelectStatement(
sqlStr = unionSqlStr,
deserializer = deserializer,
Expand All @@ -72,9 +60,10 @@ internal class UnionSelectStatementGroup<T> : StatementContainer {

@Suppress("UNCHECKED_CAST")
override fun changeLastStatement(statement: SingleStatement) {
if (statementLinkedList?.lastStatement is SelectStatement<*>)
statementLinkedList!!.resetLastStatement(statement as SelectStatement<T>)
else
if (statementList.lastOrNull() is SelectStatement<*>) {
statementList.removeLast()
statementList.add(statement as SelectStatement<T>)
} else
throw IllegalStateException("Current statement can't append clause")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data class TestPrimitiveTypeForKSP(
val testULong: ULong,
val testUShort: UShort,
val testUByte: UByte,
val testBoolean: Boolean,
val testChar: Char,
val testBoolean: Boolean?,
val testChar: Char?,
val testString: String,
)

0 comments on commit 088738e

Please sign in to comment.