Skip to content

Commit 911919f

Browse files
committed
Support typealias of supported types(primitive types, String, ByteArray etc) in generated tables.
1 parent 179ce66 commit 911919f

7 files changed

Lines changed: 101 additions & 29 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# SQLlin Change Log
22

33
- Date format: YYYY-MM-dd
4+
-
5+
## 2.1.0 / 2025-11-xx
6+
7+
### sqllin-processor
8+
9+
* Support typealias of supported types(primitive types, String, ByteArray etc) in generated tables
410

511
## 2.0.0 / 2025-10-23
612

@@ -255,7 +261,7 @@ a runtime exception. Thanks for [@nbransby](https://github.com/nbransby).
255261

256262
* Add the new JVM target
257263
* **Breaking change**: Remove the public property: `DatabaseConnection#closed`
258-
* The Android (< 9) target supports to set the `journalMode` and `synchronousMode` now
264+
* The Android(< 9) target supports to set the `journalMode` and `synchronousMode` now
259265

260266
## v1.1.1 / 2023-08-12
261267

ROADMAP.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Support the key word REFERENCE
66
* Support JOIN sub-query
77
* Support Enum type
8-
* Support typealias for primitive types
98

109
## Medium Priority
1110

sample/src/androidMain/AndroidManifest.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.ctrip.sqllin.sample
1919
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.ExperimentalDSLDatabaseAPI
2223
import com.ctrip.sqllin.dsl.annotation.PrimaryKey
2324
import com.ctrip.sqllin.dsl.sql.clause.*
2425
import com.ctrip.sqllin.dsl.sql.clause.OrderByWay.DESC
@@ -36,6 +37,7 @@ import kotlinx.serialization.Serializable
3637

3738
object Sample {
3839

40+
@OptIn(ExperimentalDSLDatabaseAPI::class)
3941
private val db by lazy {
4042
Database(
4143
DSLDBConfiguration(
@@ -110,11 +112,13 @@ object Sample {
110112
}
111113
}
112114

115+
typealias MyInt = Int
116+
113117
@DBRow("person")
114118
@Serializable
115119
data class Person(
116120
@PrimaryKey val id: Long?,
117-
val age: Int?,
121+
val age: MyInt?,
118122
val name: String?,
119123
)
120124

@@ -130,7 +134,7 @@ data class Transcript(
130134
@Serializable
131135
data class Student(
132136
val name: String?,
133-
val age: Int?,
137+
val age: MyInt?,
134138
val math: Int,
135139
val english: Int,
136140
)

sqllin-dsl-test/src/androidMain/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
This file was deleted.

sqllin-dsl-test/src/commonMain/kotlin/com/ctrip/sqllin/dsl/test/Entities.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ import com.ctrip.sqllin.dsl.annotation.DBRow
2121
import com.ctrip.sqllin.dsl.annotation.PrimaryKey
2222
import kotlinx.serialization.Serializable
2323

24+
/**
25+
* Type aliases for testing typealias support in sqllin-processor
26+
*/
27+
typealias Price = Double
28+
typealias PageCount = Int
29+
typealias Age = Int
30+
typealias Grade = Int
31+
typealias StudentId = Long
32+
typealias CourseId = Long
33+
typealias Code = Int
34+
2435
/**
2536
* Book entity
2637
* @author Yuang Qiao
@@ -31,15 +42,15 @@ import kotlinx.serialization.Serializable
3142
data class Book(
3243
val name: String,
3344
val author: String,
34-
val price: Double,
35-
val pages: Int,
45+
val price: Price,
46+
val pages: PageCount,
3647
)
3748

3849
@DBRow("category")
3950
@Serializable
4051
data class Category(
4152
val name: String,
42-
val code: Int,
53+
val code: Code,
4354
)
4455

4556
@Serializable
@@ -72,30 +83,30 @@ data class NullTester(
7283
data class PersonWithId(
7384
@PrimaryKey val id: Long?,
7485
val name: String,
75-
val age: Int,
86+
val age: Age,
7687
)
7788

7889
@DBRow("product")
7990
@Serializable
8091
data class Product(
8192
@PrimaryKey val sku: String?,
8293
val name: String,
83-
val price: Double,
94+
val price: Price,
8495
)
8596

8697
@DBRow("student_with_autoincrement")
8798
@Serializable
8899
data class StudentWithAutoincrement(
89100
@PrimaryKey(isAutoincrement = true) val id: Long?,
90101
val studentName: String,
91-
val grade: Int,
102+
val grade: Grade,
92103
)
93104

94105
@DBRow("enrollment")
95106
@Serializable
96107
data class Enrollment(
97-
@CompositePrimaryKey val studentId: Long,
98-
@CompositePrimaryKey val courseId: Long,
108+
@CompositePrimaryKey val studentId: StudentId,
109+
@CompositePrimaryKey val courseId: CourseId,
99110
val semester: String,
100111
)
101112

sqllin-processor/src/main/kotlin/com/ctrip/sqllin/processor/ClauseProcessor.kt

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import java.io.OutputStreamWriter
3636
* - Mutable properties for UPDATE SET clauses
3737
* - Primary key metadata extraction from [@PrimaryKey][com.ctrip.sqllin.dsl.annotation.PrimaryKey]
3838
* and [@CompositePrimaryKey][com.ctrip.sqllin.dsl.annotation.CompositePrimaryKey] annotations
39+
* - Support for typealias of primitive types (resolves typealiases to their underlying types)
3940
*
4041
* The generated code provides compile-time safety for SQL DSL operations.
4142
*
@@ -164,7 +165,7 @@ class ClauseProcessor(
164165
}
165166
writer.write(nullableSymbol)
166167
writer.write(" get() = ${getSetClauseGetterValue(property)}\n")
167-
writer.write(" set(value) = ${appendFunction(elementName, property, isNotNull)}\n\n")
168+
writer.write(" set(value) = ${appendFunction(elementName, property)}\n\n")
168169
}
169170

170171
// Write the override instance for property `primaryKeyInfo`.
@@ -199,12 +200,27 @@ class ClauseProcessor(
199200

200201
/**
201202
* Maps a property's Kotlin type to the corresponding clause element type name.
203+
* Supports typealiases by resolving them to their underlying types.
202204
*
203205
* @return The clause type name (ClauseNumber, ClauseString, ClauseBoolean, ClauseBlob), or null if unsupported
204206
*/
205-
private fun getClauseElementTypeStr(property: KSPropertyDeclaration): String? = when (
206-
property.typeName
207-
) {
207+
private fun getClauseElementTypeStr(property: KSPropertyDeclaration): String? {
208+
val declaration = property.type.resolve().declaration
209+
return getClauseElementTypeStrByTypeName(declaration.typeName) ?: kotlin.run {
210+
if (declaration is KSTypeAlias)
211+
getClauseElementTypeStrByTypeName(declaration.typeName)
212+
else
213+
null
214+
}
215+
}
216+
217+
/**
218+
* Maps a fully qualified type name to its corresponding clause element type.
219+
*
220+
* @param typeName The fully qualified type name to map
221+
* @return The clause type name (ClauseNumber, ClauseString, ClauseBoolean, ClauseBlob), or null if unsupported
222+
*/
223+
private fun getClauseElementTypeStrByTypeName(typeName: String?): String? = when (typeName) {
208224
Int::class.qualifiedName,
209225
Long::class.qualifiedName,
210226
Short::class.qualifiedName,
@@ -228,12 +244,27 @@ class ClauseProcessor(
228244

229245
/**
230246
* Generates the default getter value for SetClause properties based on type.
247+
* Supports typealiases by resolving them to their underlying types.
231248
*
232249
* @return The default value string for the property type, or null if unsupported
233250
*/
234-
private fun getSetClauseGetterValue(property: KSPropertyDeclaration): String? = when (
235-
property.typeName
236-
) {
251+
private fun getSetClauseGetterValue(property: KSPropertyDeclaration): String? {
252+
val declaration = property.type.resolve().declaration
253+
return getDefaultValueByType(declaration.typeName) ?: kotlin.run {
254+
if (declaration is KSTypeAlias)
255+
getDefaultValueByType(declaration.typeName)
256+
else
257+
null
258+
}
259+
}
260+
261+
/**
262+
* Returns the default value string for a given type name.
263+
*
264+
* @param typeName The fully qualified type name
265+
* @return The default value string (e.g., "0" for Int, "false" for Boolean), or null if unsupported
266+
*/
267+
private fun getDefaultValueByType(typeName: String?): String? = when (typeName) {
237268
Int::class.qualifiedName -> "0"
238269
Long::class.qualifiedName -> "0L"
239270
Short::class.qualifiedName -> "0"
@@ -256,13 +287,30 @@ class ClauseProcessor(
256287

257288
/**
258289
* Generates the appropriate append function call for SetClause setters.
290+
* Supports typealiases by resolving them to their underlying types.
259291
*
260292
* @param elementName The serialized element name
261293
* @param property The property declaration
262-
* @param isNotNull Whether the property is non-nullable
263294
* @return The append function call string, or null if unsupported type
264295
*/
265-
private fun appendFunction(elementName: String, property: KSPropertyDeclaration, isNotNull: Boolean): String? = when (property.typeName) {
296+
private fun appendFunction(elementName: String, property: KSPropertyDeclaration): String? {
297+
val declaration = property.type.resolve().declaration
298+
return appendFunctionByTypeName(elementName, declaration.typeName) ?: kotlin.run {
299+
if (declaration is KSTypeAlias)
300+
appendFunctionByTypeName(elementName, declaration.typeName)
301+
else
302+
null
303+
}
304+
}
305+
306+
/**
307+
* Generates the append function call for a given type name.
308+
*
309+
* @param elementName The serialized element name
310+
* @param typeName The fully qualified type name
311+
* @return The append function call string, or null if unsupported type
312+
*/
313+
private fun appendFunctionByTypeName(elementName: String, typeName: String?): String? = when (typeName) {
266314
Int::class.qualifiedName,
267315
Long::class.qualifiedName,
268316
Short::class.qualifiedName,
@@ -285,4 +333,16 @@ class ClauseProcessor(
285333
*/
286334
private inline val KSPropertyDeclaration.typeName
287335
get() = type.resolve().declaration.qualifiedName?.asString()
336+
337+
/**
338+
* Extension property that resolves a type alias to its underlying fully qualified type name.
339+
*/
340+
private inline val KSTypeAlias.typeName
341+
get() = type.resolve().declaration.qualifiedName?.asString()
342+
343+
/**
344+
* Extension property that retrieves a declaration's fully qualified type name.
345+
*/
346+
private inline val KSDeclaration.typeName
347+
get() = qualifiedName?.asString()
288348
}

0 commit comments

Comments
 (0)