Skip to content

Commit c2b34ce

Browse files
update comments for entity attachment
1 parent d2547bd commit c2b34ce

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

ktorm-core/src/main/kotlin/org/ktorm/entity/Entity.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,14 @@ public interface Entity<E : Entity<E>> : Serializable {
150150
* Using this function, we need to note that:
151151
*
152152
* 1. This function requires a primary key specified in the table object via [Table.primaryKey],
153-
* otherwise Ktorm doesn’t know how to identify entity objects, then throws an exception.
153+
* otherwise Ktorm doesn’t know how to identify entity objects and will throw an exception.
154154
*
155-
* 2. The entity object calling this function must **be associated with a table** first. In Ktorm’s implementation,
156-
* every entity object holds a reference `fromTable`, that means this object is associated with the table or was
157-
* obtained from it. For entity objects obtained by sequence APIs, their `fromTable` references point to the current
158-
* table object they are obtained from. But for entity objects created by [Entity.create] or [Entity.Factory], their
159-
* `fromTable` references are `null` initially, so we can not call [flushChanges] on them. But once we use them with
160-
* [add] or [update] function of entity sequences, Ktorm will modify their `fromTable` to the current table object,
161-
* then we can call [flushChanges] on them afterwards.
155+
* 2. The entity object calling this function must be ATTACHED to the database first. In Ktorm’s implementation,
156+
* every entity object holds a reference `fromDatabase`. For entity objects obtained by sequence APIs, their
157+
* `fromDatabase` references point to the database they are obtained from. For entity objects created by
158+
* [Entity.create] or [Entity.Factory], their `fromDatabase` references are `null` initially, so we can not call
159+
* [flushChanges] on them. But once we use them with [add] or [update] function, `fromDatabase` will be modified
160+
* to the current database, so we will be able to call [flushChanges] on them afterwards.
162161
*
163162
* @see add
164163
* @see update
@@ -182,7 +181,7 @@ public interface Entity<E : Entity<E>> : Serializable {
182181
* 1. The function requires a primary key specified in the table object via [Table.primaryKey],
183182
* otherwise, Ktorm doesn’t know how to identify entity objects.
184183
*
185-
* 2. The entity object calling this function must **be associated with a table** first.
184+
* 2. The entity object calling this function must be ATTACHED to the database first.
186185
*
187186
* @see add
188187
* @see update

ktorm-core/src/main/kotlin/org/ktorm/entity/EntityDml.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import org.ktorm.schema.*
3030
* not to set the primary key’s value beforehand, otherwise, if you do that, the given value will be inserted
3131
* into the database, and no keys generated.
3232
*
33-
* Note that after calling this function, the [entity] will **be associated with the current table**.
33+
* Note that after calling this function, the [entity] will be ATTACHED to the current database.
3434
*
3535
* @see Entity.flushChanges
3636
* @see Entity.delete
@@ -91,7 +91,7 @@ public fun <E : Entity<E>, T : Table<E>> EntitySequence<E, T>.add(entity: E): In
9191
/**
9292
* Update properties of the given entity to the database and return the affected record number.
9393
*
94-
* Note that after calling this function, the [entity] will **be associated with the current table**.
94+
* Note that after calling this function, the [entity] will be ATTACHED to the current database.
9595
*
9696
* @see Entity.flushChanges
9797
* @see Entity.delete
@@ -148,10 +148,10 @@ public fun <E : Any, T : BaseTable<E>> EntitySequence<E, T>.clear(): Int {
148148

149149
@Suppress("UNCHECKED_CAST")
150150
internal fun EntityImplementation.doFlushChanges(): Int {
151-
check(parent == null) { "The entity is not associated with any database yet." }
151+
check(parent == null) { "The entity is not attached to any database yet." }
152152

153-
val fromDatabase = fromDatabase ?: error("The entity is not associated with any database yet.")
154-
val fromTable = fromTable ?: error("The entity is not associated with any table yet.")
153+
val fromDatabase = fromDatabase ?: error("The entity is not attached to any database yet.")
154+
val fromTable = fromTable ?: error("The entity is not attached to any database yet.")
155155
checkUnexpectedDiscarding(fromTable)
156156

157157
val assignments = findChangedColumns(fromTable).takeIf { it.isNotEmpty() } ?: return 0
@@ -174,10 +174,10 @@ internal fun EntityImplementation.doFlushChanges(): Int {
174174

175175
@Suppress("UNCHECKED_CAST")
176176
internal fun EntityImplementation.doDelete(): Int {
177-
check(parent == null) { "The entity is not associated with any database yet." }
177+
check(parent == null) { "The entity is not attached to any database yet." }
178178

179-
val fromDatabase = fromDatabase ?: error("The entity is not associated with any database yet.")
180-
val fromTable = fromTable ?: error("The entity is not associated with any table yet.")
179+
val fromDatabase = fromDatabase ?: error("The entity is not attached to any database yet.")
180+
val fromTable = fromTable ?: error("The entity is not attached to any database yet.")
181181

182182
val expression = AliasRemover.visit(
183183
expr = DeleteExpression(
@@ -272,8 +272,8 @@ private fun EntityImplementation.findChangedColumns(fromTable: Table<*>): Map<Co
272272
}
273273

274274
internal fun EntityImplementation.doDiscardChanges() {
275-
check(parent == null) { "The entity is not associated with any database yet." }
276-
val fromTable = fromTable ?: error("The entity is not associated with any table yet.")
275+
check(parent == null) { "The entity is not attached to any database yet." }
276+
val fromTable = fromTable ?: error("The entity is not attached to any database yet.")
277277

278278
for (column in fromTable.columns) {
279279
val binding = column.binding ?: continue

ktorm-global/src/main/kotlin/org/ktorm/global/EntitySequence.kt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ public fun <E : Any, T : BaseTable<E>> T.asSequence(withReferences: Boolean = tr
3030
}
3131

3232
/**
33-
* Insert the given entity into this table and return the affected record number. Only non-null properties
34-
* are inserted.
33+
* Insert the given entity into this sequence and return the affected record number.
3534
*
3635
* If we use an auto-increment key in our table, we need to tell Ktorm which is the primary key by calling
37-
* [Table.primaryKey] function while registering columns, then this function will obtain the generated key
38-
* from the database and fill it into the corresponding property after the insertion completes. But this
39-
* requires us not to set the primary key’s value beforehand, otherwise, if you do that, the given value
40-
* will be inserted into the database, and no keys generated.
36+
* [Table.primaryKey] while registering columns, then this function will obtain the generated key from the
37+
* database and fill it into the corresponding property after the insertion completes. But this requires us
38+
* not to set the primary key’s value beforehand, otherwise, if you do that, the given value will be inserted
39+
* into the database, and no keys generated.
4140
*
42-
* Note that after calling this function, the [entity] will **be associated with the current table**.
41+
* Note that after calling this function, the [entity] will be ATTACHED to the current database.
4342
*
4443
* @see Entity.flushChanges
4544
* @see Entity.delete
@@ -53,16 +52,15 @@ public fun <E : Entity<E>> Table<E>.add(entity: E): Int {
5352
}
5453

5554
/**
56-
* Insert the given entity into this table and return the affected record number. Only non-null properties
57-
* are inserted.
55+
* Insert the given entity into this sequence and return the affected record number.
5856
*
5957
* If we use an auto-increment key in our table, we need to tell Ktorm which is the primary key by calling
60-
* [Table.primaryKey] function while registering columns, then this function will obtain the generated key
61-
* from the database and fill it into the corresponding property after the insertion completes. But this
62-
* requires us not to set the primary key’s value beforehand, otherwise, if you do that, the given value
63-
* will be inserted into the database, and no keys generated.
58+
* [Table.primaryKey] while registering columns, then this function will obtain the generated key from the
59+
* database and fill it into the corresponding property after the insertion completes. But this requires us
60+
* not to set the primary key’s value beforehand, otherwise, if you do that, the given value will be inserted
61+
* into the database, and no keys generated.
6462
*
65-
* Note that after calling this function, the [entity] will **be associated with the current table**.
63+
* Note that after calling this function, the [entity] will be ATTACHED to the current database.
6664
*
6765
* @see Entity.flushChanges
6866
* @see Entity.delete
@@ -72,9 +70,9 @@ public fun <E : Entity<E>> Table<E>.addEntity(entity: E): Int {
7270
}
7371

7472
/**
75-
* Update the non-null properties of the given entity to the database and return the affected record number.
73+
* Update properties of the given entity to the database and return the affected record number.
7674
*
77-
* Note that after calling this function, the [entity] will **be associated with the current table**.
75+
* Note that after calling this function, the [entity] will be ATTACHED to the current database.
7876
*
7977
* @see Entity.flushChanges
8078
* @see Entity.delete

0 commit comments

Comments
 (0)