Skip to content

Commit 372fe09

Browse files
update entity dml doc to use extension properties
1 parent c6b6198 commit 372fe09

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

docs/source/en/entity-dml.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ related_path: zh-cn/entity-dml.html
66

77
# Entity Manipulation
88

9-
In addition to querying, sequence APIs also support the manipulation of entity objects. We need to get a sequence object firstly via `sequenceOf`:
9+
In addition to querying, sequence APIs also support the manipulation of entity objects. We need to define some extension properties first, creating sequence objects via `sequenceOf`:
1010

1111
```kotlin
12-
val sequence = database.sequenceOf(Employees)
12+
val Database.departments get() = this.sequenceOf(Departments)
13+
val Database.employees get() = this.sequenceOf(Employees)
1314
```
1415

1516
## Insert
@@ -28,10 +29,10 @@ val employee = Employee {
2829
job = "trainee"
2930
hireDate = LocalDate.now()
3031
salary = 50
31-
department = database.sequenceOf(Departments).find { it.name eq "tech" }
32+
department = database.departments.find { it.name eq "tech" }
3233
}
3334

34-
sequence.add(employee)
35+
database.employees.add(employee)
3536
```
3637

3738
In this example, we create an employee object and fill its properties with some initial values. Please note the property `department`, whose value is an entity object just obtained from the database via sequence APIs. When we call the `add` function, the ID of the referenced entity will be saved into `Employees` table. The generated SQL is as follows:
@@ -69,7 +70,7 @@ interface Entity<E : Entity<E>> : Serializable {
6970
It can be seen that there is a `flushChanges` function in the `Entity` interface. This function updates all the changes of the current entity into the database and returns the affected record number after the update completes. Typical usage is to obtain entity objects via sequence APIs first, then modify their property values according to our requirements, finally call the `flushChanges` function to save the modifications.
7071

7172
```kotlin
72-
val employee = sequence.find { it.id eq 5 } ?: return
73+
val employee = database.employees.find { it.id eq 5 } ?: return
7374
employee.job = "engineer"
7475
employee.salary = 100
7576
employee.flushChanges()
@@ -97,7 +98,7 @@ Using `flushChanges`, we also need to note that:
9798
`Entity` interface also provides a `delete` function, which deletes the entity object in the database, and returns the affected record number after the deletion completes. Typical usage is to obtain entity objects via sequence APIs first, then call the `delete` function to delete them according to our requirements.
9899

99100
```kotlin
100-
val employee = sequence.find { it.id eq 5 } ?: return
101+
val employee = database.employees.find { it.id eq 5 } ?: return
101102
employee.delete()
102103
```
103104

@@ -115,7 +116,7 @@ Similar to `flushChanges`, we also need to note that:
115116
There are also some other functions that can delete entities, they are `removeIf` and `clear`. While `removeIf` deletes records in the table that matches a given condition, and `clear` deletes all records in a table. Here, we use `removeIf` to delete all the employees in department 1:
116117

117118
```kotlin
118-
sequence.removeIf { it.departmentId eq 1 }
119+
database.employees.removeIf { it.departmentId eq 1 }
119120
```
120121

121122
Generated SQL:

docs/source/zh-cn/entity-dml.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ related_path: en/entity-dml.html
66

77
# 实体增删改
88

9-
除了查询以外,序列 API 还支持实体对象的增删改操作。当然,我们首先要调用 `sequenceOf` 获得一个序列对象
9+
除了查询以外,序列 API 还支持实体对象的增删改操作。当然,我们首先要定义一些扩展属性,它们使用 `sequenceOf` 函数创建序列对象
1010

1111
```kotlin
12-
val sequence = database.sequenceOf(Employees)
12+
val Database.departments get() = this.sequenceOf(Departments)
13+
val Database.employees get() = this.sequenceOf(Employees)
1314
```
1415

1516
## 插入
@@ -28,10 +29,10 @@ val employee = Employee {
2829
job = "trainee"
2930
hireDate = LocalDate.now()
3031
salary = 50
31-
department = database.sequenceOf(Departments).find { it.name eq "tech" }
32+
department = database.departments.find { it.name eq "tech" }
3233
}
3334

34-
sequence.add(employee)
35+
database.employees.add(employee)
3536
```
3637

3738
在上面的例子中,我们创建了一个员工对象,并为它的各个属性设置了初始值。值得注意的是 `department` 这个属性,它是员工所属的部门,它的值是通过序列 API 从数据库中查询获得的实体对象,在调用 `add` 函数的时候,它的 ID 会被保存在 `Employees` 表中。生成的 SQL 如下:
@@ -69,7 +70,7 @@ interface Entity<E : Entity<E>> : Serializable {
6970
可以看到里面有一个 `flushChanges` 函数,它的功能正是将实体对象的修改更新到数据库,执行后返回受影响的记录数。典型用法是先使用序列 API 从数据库中获取实体对象,然后按需修改它们的属性值,最后再调用 `flushChanges` 保存这些修改。
7071

7172
```kotlin
72-
val employee = sequence.find { it.id eq 5 } ?: return
73+
val employee = database.employees.find { it.id eq 5 } ?: return
7374
employee.job = "engineer"
7475
employee.salary = 100
7576
employee.flushChanges()
@@ -97,7 +98,7 @@ update t_employee set job = ?, salary = ? where id = ?
9798
`Entity` 接口中还有一个 `delete` 函数,它的功能是从数据库中删除该实体对象,执行后返回受影响的记录数。典型用法是先使用序列 API 从数据库中获取实体对象,然后根据条件按需调用 `delete` 函数将其删除:
9899

99100
````kotlin
100-
val employee = sequence.find { it.id eq 5 } ?: return
101+
val employee = database.employees.find { it.id eq 5 } ?: return
101102
employee.delete()
102103
````
103104

@@ -115,7 +116,7 @@ delete from t_employee where id = ?
115116
最后,序列 API 还提供了 `removeIf``clear` 两个函数,`removeIf` 可以删除表中符合条件的记录,`clear` 可以删除表中的所有记录。下面使用 `removeIf` 删除部门 1 中的所有员工:
116117

117118
```kotlin
118-
sequence.removeIf { it.departmentId eq 1 }
119+
database.employees.removeIf { it.departmentId eq 1 }
119120
```
120121

121122
生成 SQL:

0 commit comments

Comments
 (0)