@@ -38,7 +38,7 @@ import org.ktorm.schema.*
3838 */
3939@Suppress(" UNCHECKED_CAST" )
4040public fun <E : Entity <E >, T : Table <E >> EntitySequence <E , T >.add (entity : E ): Int {
41- checkIfSequenceModified ()
41+ checkForDml ()
4242 entity.implementation.checkUnexpectedDiscarding(sourceTable)
4343
4444 val assignments = entity.findInsertColumns(sourceTable).takeIf { it.isNotEmpty() } ? : return 0
@@ -99,7 +99,7 @@ public fun <E : Entity<E>, T : Table<E>> EntitySequence<E, T>.add(entity: E): In
9999 */
100100@Suppress(" UNCHECKED_CAST" )
101101public fun <E : Entity <E >, T : Table <E >> EntitySequence <E , T >.update (entity : E ): Int {
102- checkIfSequenceModified ()
102+ checkForDml ()
103103 entity.implementation.checkUnexpectedDiscarding(sourceTable)
104104
105105 val assignments = entity.findUpdateColumns(sourceTable).takeIf { it.isNotEmpty() } ? : return 0
@@ -132,7 +132,7 @@ public fun <E : Entity<E>, T : Table<E>> EntitySequence<E, T>.update(entity: E):
132132public fun <E : Any , T : BaseTable <E >> EntitySequence <E , T >.removeIf (
133133 predicate : (T ) -> ColumnDeclaring <Boolean >
134134): Int {
135- checkIfSequenceModified ()
135+ checkForDml ()
136136 return database.delete(sourceTable, predicate)
137137}
138138
@@ -142,10 +142,15 @@ public fun <E : Any, T : BaseTable<E>> EntitySequence<E, T>.removeIf(
142142 * @since 2.7
143143 */
144144public fun <E : Any , T : BaseTable <E >> EntitySequence <E , T >.clear (): Int {
145- checkIfSequenceModified ()
145+ checkForDml ()
146146 return database.deleteAll(sourceTable)
147147}
148148
149+ /* *
150+ * Update the property changes of this entity into the database and return the affected record number.
151+ *
152+ * This function is the implementation of [Entity.flushChanges].
153+ */
149154@Suppress(" UNCHECKED_CAST" )
150155internal fun EntityImplementation.doFlushChanges (): Int {
151156 check(parent == null ) { " The entity is not attached to any database yet." }
@@ -172,7 +177,11 @@ internal fun EntityImplementation.doFlushChanges(): Int {
172177 return fromDatabase.executeUpdate(expression).also { doDiscardChanges() }
173178}
174179
175- @Suppress(" UNCHECKED_CAST" )
180+ /* *
181+ * Delete this entity in the database and return the affected record number.
182+ *
183+ * This function is the implementation of [Entity.delete].
184+ */
176185internal fun EntityImplementation.doDelete (): Int {
177186 check(parent == null ) { " The entity is not attached to any database yet." }
178187
@@ -189,7 +198,10 @@ internal fun EntityImplementation.doDelete(): Int {
189198 return fromDatabase.executeUpdate(expression)
190199}
191200
192- private fun EntitySequence <* , * >.checkIfSequenceModified () {
201+ /* *
202+ * Check if this sequence can be used for entity manipulations.
203+ */
204+ private fun EntitySequence <* , * >.checkForDml () {
193205 val isModified = expression.where != null
194206 || expression.groupBy.isNotEmpty()
195207 || expression.having != null
@@ -206,6 +218,9 @@ private fun EntitySequence<*, *>.checkIfSequenceModified() {
206218 }
207219}
208220
221+ /* *
222+ * Return columns associated with their values for insert.
223+ */
209224private fun Entity <* >.findInsertColumns (table : Table <* >): Map <Column <* >, Any?> {
210225 val assignments = LinkedHashMap <Column <* >, Any? > ()
211226
@@ -218,6 +233,9 @@ private fun Entity<*>.findInsertColumns(table: Table<*>): Map<Column<*>, Any?> {
218233 return assignments
219234}
220235
236+ /* *
237+ * Return columns associated with their values for update.
238+ */
221239private fun Entity <* >.findUpdateColumns (table : Table <* >): Map <Column <* >, Any?> {
222240 val assignments = LinkedHashMap <Column <* >, Any? > ()
223241
@@ -230,6 +248,9 @@ private fun Entity<*>.findUpdateColumns(table: Table<*>): Map<Column<*>, Any?> {
230248 return assignments
231249}
232250
251+ /* *
252+ * Return changed columns associated with their values.
253+ */
233254private fun EntityImplementation.findChangedColumns (fromTable : Table <* >): Map <Column <* >, Any?> {
234255 val assignments = LinkedHashMap <Column <* >, Any? > ()
235256
@@ -271,6 +292,11 @@ private fun EntityImplementation.findChangedColumns(fromTable: Table<*>): Map<Co
271292 return assignments
272293}
273294
295+ /* *
296+ * Clear the tracked property changes of this entity.
297+ *
298+ * This function is the implementation of [Entity.discardChanges].
299+ */
274300internal fun EntityImplementation.doDiscardChanges () {
275301 check(parent == null ) { " The entity is not attached to any database yet." }
276302 val fromTable = fromTable ? : error(" The entity is not attached to any database yet." )
@@ -302,7 +328,9 @@ internal fun EntityImplementation.doDiscardChanges() {
302328 }
303329}
304330
305- // Add check to avoid bug #10
331+ /* *
332+ * Check to avoid unexpected discarding of changed properties, fix bug #10.
333+ */
306334private fun EntityImplementation.checkUnexpectedDiscarding (fromTable : Table <* >) {
307335 for (column in fromTable.columns) {
308336 if (column.binding !is NestedBinding ) continue
@@ -332,6 +360,9 @@ private fun EntityImplementation.checkUnexpectedDiscarding(fromTable: Table<*>)
332360 }
333361}
334362
363+ /* *
364+ * Return the root parent of this entity.
365+ */
335366private tailrec fun EntityImplementation.getRoot (): EntityImplementation {
336367 val parent = this .parent
337368 if (parent == null ) {
@@ -341,6 +372,9 @@ private tailrec fun EntityImplementation.getRoot(): EntityImplementation {
341372 }
342373}
343374
375+ /* *
376+ * Clear all changes for this entity.
377+ */
344378internal fun Entity <* >.clearChangesRecursively () {
345379 implementation.changedProperties.clear()
346380
@@ -351,6 +385,9 @@ internal fun Entity<*>.clearChangesRecursively() {
351385 }
352386}
353387
388+ /* *
389+ * Construct the identity condition `where primaryKey = ?` for the table.
390+ */
354391@Suppress(" UNCHECKED_CAST" )
355392private fun EntityImplementation.constructIdentityCondition (fromTable : Table <* >): ScalarExpression <Boolean > {
356393 val primaryKeys = fromTable.primaryKeys
0 commit comments