Skip to content

Commit c5b466b

Browse files
add comments
1 parent adb5600 commit c5b466b

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
package org.ktorm.entity
1818

1919
import org.ktorm.database.Database
20-
import org.ktorm.schema.TypeReference
2120
import org.ktorm.schema.Table
21+
import org.ktorm.schema.TypeReference
2222
import java.io.ObjectInputStream
2323
import java.io.ObjectOutputStream
2424
import java.io.Serializable
2525
import java.lang.reflect.Proxy
26-
import java.sql.SQLException
2726
import kotlin.reflect.KClass
2827
import kotlin.reflect.full.isSubclassOf
2928
import kotlin.reflect.jvm.jvmErasure
@@ -162,7 +161,6 @@ public interface Entity<E : Entity<E>> : Serializable {
162161
* @see add
163162
* @see update
164163
*/
165-
@Throws(SQLException::class)
166164
public fun flushChanges(): Int
167165

168166
/**
@@ -187,7 +185,6 @@ public interface Entity<E : Entity<E>> : Serializable {
187185
* @see update
188186
* @see flushChanges
189187
*/
190-
@Throws(SQLException::class)
191188
public fun delete(): Int
192189

193190
/**

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import org.ktorm.schema.*
3838
*/
3939
@Suppress("UNCHECKED_CAST")
4040
public 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")
101101
public 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):
132132
public 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
*/
144144
public 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")
150155
internal 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+
*/
176185
internal 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+
*/
209224
private 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+
*/
221239
private 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+
*/
233254
private 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+
*/
274300
internal 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+
*/
306334
private 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+
*/
335366
private 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+
*/
344378
internal 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")
355392
private fun EntityImplementation.constructIdentityCondition(fromTable: Table<*>): ScalarExpression<Boolean> {
356393
val primaryKeys = fromTable.primaryKeys

0 commit comments

Comments
 (0)