You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sqllin-dsl/doc/getting-start.md
+109-4Lines changed: 109 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ plugins {
16
16
id("com.google.devtools.ksp")
17
17
}
18
18
19
-
val sqllinVersion ="1.4.4"
19
+
val sqllinVersion ="2.0.0"
20
20
21
21
kotlin {
22
22
// ......
@@ -126,15 +126,52 @@ val database = Database(
126
126
)
127
127
```
128
128
129
-
Note, because of limitation by Android Framework, the `inMemory`, `busyTimeout`, `lookasideSlotSize`, `lookasideSlotCount`
129
+
Note, because of limitation by Android Framework, the `inMemory`, `busyTimeout`, `lookasideSlotSize`, `lookasideSlotCount`
130
130
only work on Android 9 and higher. And, because [sqlite-jdbc](https://github.com/xerial/sqlite-jdbc)(SQLlin is based on it on JVM) doesn't support
131
131
`sqlite3_config()`, the `lookasideSlotSize` and `lookasideSlotCount` don't work on JVM target.
132
132
133
-
Now, the operations that change database structure haven't been supported by DSL yet. So, you need to write these SQL statements by string
134
-
as in `create` and `upgrade` parameters.
133
+
### Using DSLDBConfiguration for Type-Safe Schema Management
134
+
135
+
Alternatively, you can use `DSLDBConfiguration` which allows you to use the type-safe SQL DSL in the `create` and `upgrade` callbacks instead of raw SQL strings:
136
+
137
+
```kotlin
138
+
importcom.ctrip.sqllin.driver.DSLDBConfiguration
139
+
importcom.ctrip.sqllin.dsl.Database
140
+
141
+
val database =Database(
142
+
DSLDBConfiguration(
143
+
name ="Person.db",
144
+
path = getGlobalDatabasePath(),
145
+
version =1,
146
+
isReadOnly =false,
147
+
inMemory =false,
148
+
journalMode =JournalMode.WAL,
149
+
synchronousMode =SynchronousMode.NORMAL,
150
+
busyTimeout =5000,
151
+
lookasideSlotSize =0,
152
+
lookasideSlotCount =0,
153
+
create = {
154
+
// Use type-safe DSL instead of raw SQL
155
+
CREATE(PersonTable)
156
+
},
157
+
upgrade = { oldVersion, newVersion ->
158
+
when (oldVersion) {
159
+
1-> {
160
+
// Example: Add a new column in version 2
161
+
PersonTableALERT_ADD_COLUMNPersonTable.email
162
+
}
163
+
}
164
+
}
165
+
)
166
+
)
167
+
```
168
+
169
+
With `DSLDBConfiguration`, you can use CREATE, DROP, and ALTER operations directly in the callbacks, making schema management more type-safe and maintainable. The DSL operations available in these callbacks are the same as those available in regular `database { }` blocks.
135
170
136
171
Usually, you just need to create one `Database` instance in your component lifecycle. So, you need to close database manually when the lifecycle ended:
137
172
173
+
> Notice: `DSLDBConfiguration` is experimental, but it will completely replace `DatabaseConfiguration` when it is stable. That means _sqllin-dsl_ will not support to use `DatabaseConfiguration` to create `Database` instances in the future versions.
174
+
138
175
```kotlin
139
176
overridefunonDestroy() {
140
177
database.close()
@@ -167,6 +204,74 @@ name as table name, for example, `Person`'s default table name is "Person".
167
204
In _sqllin-dsl_, objects are serialized to SQL and deserialized from cursor depend on _kotlinx.serialization_. So, you also need to add the `@Serializable` onto your data classes. Therefore, if
168
205
you want to ignore some properties when serialization or deserialization and `Table` classes generation, you can annotate your properties with `kotlinx.serialization.Transient`.
169
206
207
+
### Defining Primary Keys
208
+
209
+
SQLlin provides annotations to define primary keys for your database tables.
210
+
211
+
#### Single Primary Key with @PrimaryKey
212
+
213
+
Use `@PrimaryKey` to mark a single property as the primary key:
-**For `Long` primary keys with auto-increment**: The property **must** be declared as nullable (`Long?`). This maps to SQLite's `INTEGER PRIMARY KEY` which acts as an alias for the internal `rowid`. When inserting a new record with `id = null`, SQLite automatically generates the ID.
233
+
234
+
-**For other types (String, Int, etc.)**: The property **must** be non-nullable. You must provide a unique value when inserting:
The `autoIncrement` parameter enables stricter auto-incrementing behavior (using `AUTOINCREMENT` keyword), ensuring row IDs are never reused. This is only meaningful for `Long?` properties.
247
+
248
+
#### Composite Primary Key with @CompositePrimaryKey
249
+
250
+
Use `@CompositePrimaryKey` when your table's primary key consists of multiple columns:
0 commit comments