Skip to content

Commit a803fda

Browse files
authored
Merge pull request #108 from qiaoyuang/main
Add New SQL CREATE DSL APIs
2 parents 028c624 + bcf8323 commit a803fda

File tree

22 files changed

+627
-77
lines changed

22 files changed

+627
-77
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
* Update `Kotlin`'s version to `2.2.20`
1010
* Remove the Desuger configuration
1111

12+
### sqllin-dsl
13+
14+
* Optimized performance for SQL assembly
15+
* New experimental API: `DatabaseScope#CREATE`
16+
* New experimental API: `DatabaseScope#DROP`
17+
* New experimental API: `DatabaseSceop#ALERT`
18+
1219
### sqllin-driver
1320

1421
* Update the `sqlite-jdbc`'s version to `3.50.3.0`

ROADMAP.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SQLlin Roadmap
2+
3+
## High Priority
4+
5+
* Support the key word REFERENCE
6+
* Support JOIN sub-query
7+
* Fix the bug of storing ByteArray in DSL
8+
9+
## Medium Priority
10+
11+
* Support WASM platform
12+
13+
## Low Priority
14+
15+
* Support store instances of kotlinx.datetime

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=1.4.4
1+
VERSION=2.0.0
22
GROUP_ID=com.ctrip.kotlin
33

44
#Maven Publishing Information

sample/src/commonMain/kotlin/com/ctrip/sqllin/sample/Sample.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717
package com.ctrip.sqllin.sample
1818

19-
import com.ctrip.sqllin.driver.DatabaseConfiguration
19+
import com.ctrip.sqllin.dsl.DSLDBConfiguration
2020
import com.ctrip.sqllin.dsl.Database
2121
import com.ctrip.sqllin.dsl.annotation.DBRow
22+
import com.ctrip.sqllin.dsl.annotation.PrimaryKey
2223
import com.ctrip.sqllin.dsl.sql.clause.*
2324
import com.ctrip.sqllin.dsl.sql.clause.OrderByWay.DESC
2425
import com.ctrip.sqllin.dsl.sql.statement.SelectStatement
@@ -30,34 +31,33 @@ import kotlinx.serialization.Serializable
3031

3132
/**
3233
* Sample
33-
* @author yaqiao
34+
* @author Yuang Qiao
3435
*/
3536

3637
object Sample {
3738

3839
private val db by lazy {
3940
Database(
40-
DatabaseConfiguration(
41+
DSLDBConfiguration(
4142
name = "Person.db",
4243
path = databasePath,
4344
version = 1,
4445
create = {
45-
// You must write SQL to String when the database is created or upgraded
46-
it.execSQL("CREATE TABLE person (id INTEGER PRIMARY KEY AUTOINCREMENT, age INTEGER, name TEXT);")
47-
it.execSQL("CREATE TABLE transcript (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, math INTEGER NOT NULL, english INTEGER NOT NULL);")
46+
PersonTable {
47+
CREATE()
48+
}
49+
this CREATE TranscriptTable
4850
},
49-
upgrade = { _, _, _ ->
50-
// You must write SQL to String when the database is created or upgraded
51-
}
51+
upgrade = { _, _ -> }
5252
),
5353
enableSimpleSQLLog = true,
5454
)
5555
}
5656

5757
fun sample() {
58-
val tom = Person(age = 4, name = "Tom")
59-
val jerry = Person(age = 3, name = "Jerry")
60-
val jack = Person(age = 8, name = "Jack")
58+
val tom = Person(id = 0, age = 4, name = "Tom")
59+
val jerry = Person(id = 1, age = 3, name = "Jerry")
60+
val jack = Person(id = 2, age = 8, name = "Jack")
6161

6262
lateinit var selectStatement: SelectStatement<Person>
6363
db {
@@ -113,13 +113,15 @@ object Sample {
113113
@DBRow("person")
114114
@Serializable
115115
data class Person(
116+
@PrimaryKey val id: Long?,
116117
val age: Int?,
117118
val name: String?,
118119
)
119120

120121
@DBRow("transcript")
121122
@Serializable
122123
data class Transcript(
124+
@PrimaryKey val id: Long?,
123125
val name: String?,
124126
val math: Int,
125127
val english: Int,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ package com.ctrip.sqllin.driver
1818

1919
/**
2020
* SQLite Cursor common abstract
21-
* @author yaqiao
21+
* @author Yuang Qiao
2222
*/
2323

24-
@OptIn(ExperimentalStdlibApi::class)
2524
public interface CommonCursor : AutoCloseable {
2625

2726
public fun getInt(columnIndex: Int): Int

sqllin-dsl-test/src/commonTest/kotlin/com/ctrip/sqllin/dsl/test/CommonBasicTest.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.ctrip.sqllin.dsl.test
1818

19-
import com.ctrip.sqllin.driver.DatabaseConfiguration
2019
import com.ctrip.sqllin.driver.DatabasePath
20+
import com.ctrip.sqllin.dsl.DSLDBConfiguration
2121
import com.ctrip.sqllin.dsl.Database
2222
import com.ctrip.sqllin.dsl.sql.X
2323
import com.ctrip.sqllin.dsl.sql.clause.*
@@ -41,9 +41,6 @@ class CommonBasicTest(private val path: DatabasePath) {
4141

4242
companion object {
4343
const val DATABASE_NAME = "BookStore.db"
44-
const val SQL_CREATE_BOOK = "create table book (id integer primary key autoincrement, name text, author text, pages integer, price real)"
45-
const val SQL_CREATE_CATEGORY = "create table category (id integer primary key autoincrement, name text, code integer)"
46-
const val SQL_CREATE_NULL_TESTER = "create table NullTester (id integer primary key autoincrement, paramInt integer, paramString text, paramDouble real)"
4744
}
4845

4946
private inline fun Database.databaseAutoClose(block: (Database) -> Unit) = try {
@@ -429,12 +426,12 @@ class CommonBasicTest(private val path: DatabasePath) {
429426
}
430427

431428
fun testNullValue() {
432-
val config = DatabaseConfiguration(
429+
val config = DSLDBConfiguration(
433430
name = DATABASE_NAME,
434431
path = path,
435432
version = 1,
436433
create = {
437-
it.execSQL(SQL_CREATE_NULL_TESTER)
434+
CREATE(NullTesterTable)
438435
}
439436
)
440437
Database(config, true).databaseAutoClose { database ->
@@ -493,14 +490,14 @@ class CommonBasicTest(private val path: DatabasePath) {
493490
}
494491
}
495492

496-
private fun getDefaultDBConfig(): DatabaseConfiguration =
497-
DatabaseConfiguration(
493+
private fun getDefaultDBConfig(): DSLDBConfiguration =
494+
DSLDBConfiguration (
498495
name = DATABASE_NAME,
499496
path = path,
500497
version = 1,
501498
create = {
502-
it.execSQL(SQL_CREATE_BOOK)
503-
it.execSQL(SQL_CREATE_CATEGORY)
499+
CREATE(BookTable)
500+
CREATE(CategoryTable)
504501
}
505502
)
506503
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2025 Ctrip.com.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ctrip.sqllin.dsl
18+
19+
import com.ctrip.sqllin.driver.DatabaseConfiguration
20+
import com.ctrip.sqllin.driver.DatabasePath
21+
import com.ctrip.sqllin.driver.JournalMode
22+
import com.ctrip.sqllin.driver.SynchronousMode
23+
24+
/**
25+
* DSL database configuration
26+
* @author Yuang Qiao
27+
*/
28+
29+
public data class DSLDBConfiguration(
30+
val name: String,
31+
val path: DatabasePath,
32+
val version: Int,
33+
val isReadOnly: Boolean = false,
34+
val inMemory: Boolean = false,
35+
val journalMode: JournalMode = JournalMode.WAL,
36+
val synchronousMode: SynchronousMode = SynchronousMode.NORMAL,
37+
val busyTimeout: Int = 5000,
38+
val lookasideSlotSize: Int = 0,
39+
val lookasideSlotCount: Int = 0,
40+
val create: DatabaseScope.() -> Unit = {},
41+
val upgrade: DatabaseScope.(oldVersion: Int, newVersion: Int) -> Unit = { _, _ -> },
42+
) {
43+
internal infix fun convertToDatabaseConfiguration(enableSimpleSQLLog: Boolean): DatabaseConfiguration = DatabaseConfiguration(
44+
name,
45+
path,
46+
version,
47+
isReadOnly,
48+
inMemory,
49+
journalMode,
50+
synchronousMode,
51+
busyTimeout,
52+
lookasideSlotSize,
53+
lookasideSlotCount,
54+
create = {
55+
val database = Database(it, enableSimpleSQLLog)
56+
database {
57+
create()
58+
}
59+
},
60+
upgrade = { databaseConnection, oldVersion, newVersion ->
61+
val database = Database(databaseConnection, enableSimpleSQLLog)
62+
database {
63+
upgrade(oldVersion, newVersion)
64+
}
65+
}
66+
)
67+
}

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,20 @@
1616

1717
package com.ctrip.sqllin.dsl
1818

19-
import com.ctrip.sqllin.driver.DatabaseConfiguration
20-
import com.ctrip.sqllin.driver.DatabasePath
21-
import com.ctrip.sqllin.driver.openDatabase
19+
import com.ctrip.sqllin.driver.DatabaseConnection
2220
import kotlinx.coroutines.sync.Mutex
2321
import kotlinx.coroutines.sync.withLock
2422

2523
/**
2624
* Database object
27-
* @author yaqiao
25+
* @author Yuang Qiao
2826
*/
2927

30-
public class Database(
31-
configuration: DatabaseConfiguration,
28+
public class Database internal constructor(
29+
private val databaseConnection: DatabaseConnection,
3230
private val enableSimpleSQLLog: Boolean = false,
3331
) {
3432

35-
public constructor(
36-
name: String,
37-
path: DatabasePath,
38-
version: Int,
39-
enableSimpleSQLLog: Boolean = false,
40-
) : this(
41-
DatabaseConfiguration(
42-
name = name,
43-
path = path,
44-
version = version,
45-
),
46-
enableSimpleSQLLog,
47-
)
48-
49-
private val databaseConnection = openDatabase(configuration)
50-
5133
/**
5234
* Close the database connection.
5335
*/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2025 Ctrip.com.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ctrip.sqllin.dsl
18+
19+
import com.ctrip.sqllin.driver.DatabaseConfiguration
20+
import com.ctrip.sqllin.driver.DatabasePath
21+
import com.ctrip.sqllin.driver.openDatabase
22+
23+
/**
24+
* Factory functions for Database
25+
* @author Yuang Qiao
26+
*/
27+
28+
public fun Database(
29+
configuration: DatabaseConfiguration,
30+
enableSimpleSQLLog: Boolean = false,
31+
): Database = Database(openDatabase(configuration), enableSimpleSQLLog)
32+
33+
public fun Database(
34+
name: String,
35+
path: DatabasePath,
36+
version: Int,
37+
enableSimpleSQLLog: Boolean = false,
38+
): Database = Database(
39+
DatabaseConfiguration(
40+
name = name,
41+
path = path,
42+
version = version,
43+
),
44+
enableSimpleSQLLog
45+
)
46+
47+
public fun Database(
48+
dsldbConfiguration: DSLDBConfiguration,
49+
enableSimpleSQLLog: Boolean = false,
50+
): Database = Database(
51+
configuration = dsldbConfiguration convertToDatabaseConfiguration enableSimpleSQLLog,
52+
enableSimpleSQLLog = enableSimpleSQLLog,
53+
)

0 commit comments

Comments
 (0)