Skip to content

Some optimization #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ jobs:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-33
key: avd-34

- name: Create AVD and Generate Snapshot for Caching
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 33
api-level: 34
target: google_apis
arch: x86_64
profile: pixel_6
Expand All @@ -88,10 +88,10 @@ jobs:
disable-animations: false
script: echo "Generated AVD snapshot for caching."

- name: Run Android 13 Instrumented Tests
- name: Run Android 14 Instrumented Tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 33
api-level: 34
target: google_apis
arch: x86_64
profile: pixel_6
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ jobs:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-33
key: avd-34

- name: Create AVD and Generate Snapshot for Caching
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 33
api-level: 34
target: google_apis
arch: x86_64
profile: pixel_6
Expand All @@ -83,10 +83,10 @@ jobs:
disable-animations: false
script: echo "Generated AVD snapshot for caching."

- name: Run Android 13 Instrumented Tests
- name: Run Android 14 Instrumented Tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 33
api-level: 34
target: google_apis
arch: x86_64
profile: pixel_6
Expand Down
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Date format: YYYY-MM-dd

## v1.3.0 / 2024-04-16
## v1.3.0 / 2024-04-21

### All

Expand All @@ -13,7 +13,8 @@
* 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)
* Performance optimization, use `ArrayDeque` to replace the LinkedList for SQL statements management (self-implemented).
* The parameter `enableSimpleSQLLog` of the `Database`'s constructors of is `false` by default.

### sqllin-driver

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ object Sample {
upgrade = { _, _, _ ->
// You must write SQL to String when the database is created or upgraded
}
)
),
enableSimpleSQLLog = true,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import kotlinx.coroutines.sync.withLock

public class Database(
configuration: DatabaseConfiguration,
private val enableSimpleSQLLog: Boolean = true,
private val enableSimpleSQLLog: Boolean = false,
) {

public constructor(
name: String,
path: DatabasePath,
version: Int,
enableSimpleSQLLog: Boolean = true,
enableSimpleSQLLog: Boolean = false,
) : this(
DatabaseConfiguration(
name = name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class ClauseBoolean(
}
append(valueName)
if (bool)
append(" > ")
append('>')
else
append(" <= ")
append("<=")
append(0)
}
return SelectCondition(sql, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ public class ClauseNumber(
append('.')
}
append(valueName)
append(' ')
append(symbol)
append(' ')
append(number)
}
return SelectCondition(sql, null)
Expand All @@ -121,10 +119,14 @@ public class ClauseNumber(
append('.')
}
append(valueName)
append(' ')
append(if (number == null) nullSymbol else notNullSymbol)
append(' ')
append(number ?: "NULL")
if (number == null){
append(nullSymbol)
append(" NULL")

} else {
append(notNullSymbol)
append(number)
}
}
return SelectCondition(sql, null)
}
Expand All @@ -134,9 +136,7 @@ public class ClauseNumber(
append(table.tableName)
append('.')
append(valueName)
append(' ')
append(symbol)
append(' ')
append(clauseNumber.table.tableName)
append('.')
append(clauseNumber.valueName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public class ClauseString(
append('.')
}
append(valueName)
append(' ')
append(symbol)
append(" ?")
append('?')
}
return SelectCondition(sql, mutableListOf(regex))
}
Expand All @@ -66,14 +65,13 @@ public class ClauseString(
append('.')
}
append(valueName)
append(' ')
val isNull = str == null
val symbol = if (isNull) nullSymbol else notNullSymbol
append(symbol)
if (str == null)
append(" NULL")
else
append(" ?")
append('?')
}
return SelectCondition(sql, if (str == null) null else mutableListOf(str))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ internal class CompleteOrderByClause<T>(private val column2WayMap: Map<ClauseEle
append(' ')
append(way.str)
val hasNext = iterator.hasNext()
val symbol = if (hasNext) ',' else ' '
append(symbol)
if (hasNext)
append(',')
} while (hasNext)
}
}
Expand Down Expand Up @@ -99,14 +99,12 @@ internal class SimpleOrderByClause<T>(private val columns: Iterable<ClauseElemen
append(',')
append(iterator.next().valueName)
}
append(' ')
}
}
}
public fun <T> ORDER_BY(vararg elements: ClauseElement): OrderByClause<T> =
SimpleOrderByClause(elements.toList())

@Suppress("NOTHING_TO_INLINE")
public inline infix fun <T> WhereSelectStatement<T>.ORDER_BY(column: ClauseElement): OrderBySelectStatement<T> =
ORDER_BY(listOf(column))

Expand All @@ -115,7 +113,6 @@ public infix fun <T> WhereSelectStatement<T>.ORDER_BY(columns: Iterable<ClauseEl
container changeLastStatement it
}

@Suppress("NOTHING_TO_INLINE")
public inline infix fun <T> HavingSelectStatement<T>.ORDER_BY(column: ClauseElement): OrderBySelectStatement<T> =
ORDER_BY(listOf(column))

Expand All @@ -124,7 +121,6 @@ public infix fun <T> HavingSelectStatement<T>.ORDER_BY(columns: Iterable<ClauseE
container changeLastStatement it
}

@Suppress("NOTHING_TO_INLINE")
public inline infix fun <T> GroupBySelectStatement<T>.ORDER_BY(column: ClauseElement): OrderBySelectStatement<T> =
ORDER_BY(listOf(column))

Expand All @@ -133,7 +129,6 @@ public infix fun <T> GroupBySelectStatement<T>.ORDER_BY(columns: Iterable<Clause
container changeLastStatement it
}

@Suppress("NOTHING_TO_INLINE")
public inline infix fun <T> JoinSelectStatement<T>.ORDER_BY(column: ClauseElement): OrderBySelectStatement<T> =
ORDER_BY(listOf(column))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal object Select : Operation {
connection: DatabaseConnection,
container: StatementContainer,
): OrderBySelectStatement<T> =
OrderBySelectStatement(buildSQL(table, clause, isDistinct, deserializer), deserializer, connection, container, mutableListOf())
OrderBySelectStatement(buildSQL(table, clause, isDistinct, deserializer), deserializer, connection, container, null)

fun <T> select(
table: Table<T>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class DatabaseExecuteEngine(
when (it) {
is SingleStatement -> {
if (enableSimpleSQLLog)
println("SQL String: ${it.sqlStr}")
it.printlnSQL()
it.execute()
}
is TransactionStatementsGroup -> it.execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public sealed class SingleStatement(

internal val params: Array<String>?
get() = parameters?.toTypedArray()

internal fun printlnSQL() {
print("SQL String: $sqlStr")
parameters?.let {
println("; Parameters: $it")
} ?: println()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class TransactionStatementsGroup(
override fun execute() = databaseConnection.withTransaction {
statementList.forEach {
if (enableSimpleSQLLog)
println("SQL String: ${it.sqlStr}")
it.printlnSQL()
it.execute()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CommonBasicTest(private val path: DatabasePath) {
close()
}

fun testInsert() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testInsert() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
database {
BookTable { bookTable ->
Expand All @@ -65,7 +65,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(book, statement?.getResults()?.firstOrNull())
}

fun testDelete() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testDelete() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book1 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
var statement: SelectStatement<Book>? = null
Expand All @@ -92,7 +92,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(true, statement2!!.getResults().isEmpty())
}

fun testUpdate() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testUpdate() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book1 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
var statement: SelectStatement<Book>? = null
Expand Down Expand Up @@ -126,7 +126,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(true, newResult!!.getResults().any { it == newBook2 })
}

fun testSelectWhereClause() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testSelectWhereClause() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book0 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book1 = Book(name = "Kotlin Cookbook", author = "Ken Kousen", pages = 251, price = 37.72)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
Expand All @@ -149,7 +149,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(book1, statementOfWhere2?.getResults()?.firstOrNull())
}

fun testSelectOrderByClause() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testSelectOrderByClause() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book0 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book1 = Book(name = "Kotlin Cookbook", author = "Ken Kousen", pages = 251, price = 37.72)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
Expand Down Expand Up @@ -209,7 +209,7 @@ class CommonBasicTest(private val path: DatabasePath) {
}
}

fun testSelectLimitAndOffsetClause() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testSelectLimitAndOffsetClause() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book0 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book1 = Book(name = "Kotlin Cookbook", author = "Ken Kousen", pages = 251, price = 37.72)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
Expand All @@ -229,7 +229,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(1, statementOfLimitAndOffset?.getResults()?.size)
}

fun testGroupByAndHavingClause() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testGroupByAndHavingClause() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book0 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book1 = Book(name = "Kotlin Cookbook", author = "Ken Kousen", pages = 251, price = 37.72)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
Expand Down Expand Up @@ -261,7 +261,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals("Ken Kousen", resultOfGroupByAndHaving.first().author)
}

fun testUnionSelect() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testUnionSelect() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book0 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book1 = Book(name = "Kotlin Cookbook", author = "Ken Kousen", pages = 251, price = 37.72)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
Expand All @@ -286,7 +286,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(2, statement!!.getResults().count { it == book3 })
}

fun testFunction() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testFunction() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
val book0 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book1 = Book(name = "Kotlin Cookbook", author = "Ken Kousen", pages = 251, price = 37.72)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
Expand Down Expand Up @@ -326,7 +326,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(book0.author, selectStatement8?.getResults()?.first()?.author)
}

fun testJoinClause() = Database(getDefaultDBConfig()).databaseAutoClose { database ->
fun testJoinClause() = Database(getDefaultDBConfig(), true).databaseAutoClose { database ->
var crossJoinStatement: SelectStatement<CrossJoiner>? = null
var innerJoinStatement: SelectStatement<Joiner>? = null
var naturalInnerJoinStatement: SelectStatement<Joiner>? = null
Expand Down Expand Up @@ -372,7 +372,7 @@ class CommonBasicTest(private val path: DatabasePath) {
fun testConcurrency() = runBlocking(Dispatchers.Default) {
val book1 = Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96)
val book2 = Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95)
val database = Database(getDefaultDBConfig())
val database = Database(getDefaultDBConfig(), true)
launch {
var statement: SelectStatement<Book>? = null
database suspendedScope {
Expand Down
Loading