Skip to content

Commit 222762d

Browse files
Merge branch 'v3.4.x' of https://github.com/vincentlauvlwj/Ktorm into v3.4.x
2 parents c4efbc8 + c649e14 commit 222762d

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

detekt.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ complexity:
5050
threshold: 4
5151
ComplexInterface:
5252
active: true
53-
threshold: 11
53+
threshold: 12
5454
includeStaticDeclarations: false
5555
ComplexMethod:
5656
active: true

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

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ import kotlin.reflect.jvm.jvmErasure
4444
*
4545
* ### Creating Entity Objects
4646
*
47-
* As everyone knows, interfaces cannot be instantiated, so Ktorm provides [Entity.create] functions for us to
48-
* create entity objects. Those functions generate implementations for entity interfaces via JDK dynamic proxy
47+
* As everyone knows, interfaces cannot be instantiated, so Ktorm provides a [Entity.create] function for us to
48+
* create entity objects. This function generate implementations for entity interfaces via JDK dynamic proxy
4949
* and create their instances.
5050
*
51-
* If you don't like creating objects by [Entity.create] functions, Ktorm also provides an abstract factory class
51+
* In case you don't like creating objects by [Entity.create], Ktorm also provides an abstract factory class
5252
* [Entity.Factory]. This class overloads the `invoke` operator of Kotlin, so we just need to add a companion
5353
* object to our entity class extending from [Entity.Factory], then entity objects can be created just like there
5454
* is a constructor: `val department = Department()`.
@@ -71,7 +71,7 @@ import kotlin.reflect.jvm.jvmErasure
7171
* - For [Boolean] type, the default value is `false`.
7272
* - For [Char] type, the default value is `\u0000`.
7373
* - For number types (such as [Int], [Long], [Double], etc), the default value is zero.
74-
* - For the [String] type, the default value is the empty string.
74+
* - For [String] type, the default value is an empty string.
7575
* - For entity types, the default value is a new-created entity object which is empty.
7676
* - For enum types, the default value is the first value of the enum, whose ordinal is 0.
7777
* - For array types, the default value is a new-created empty array.
@@ -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
@@ -209,6 +208,27 @@ public interface Entity<E : Entity<E>> : Serializable {
209208
*/
210209
public fun copy(): E
211210

211+
/**
212+
* Indicate whether some other object is "equal to" this entity.
213+
* Two entities are equal only if they have the same [entityClass] and [properties].
214+
*
215+
* @since 3.4.0
216+
*/
217+
public override fun equals(other: Any?): Boolean
218+
219+
/**
220+
* Return a hash code value for this entity.
221+
*
222+
* @since 3.4.0
223+
*/
224+
public override fun hashCode(): Int
225+
226+
/**
227+
* Return a string representation of this table.
228+
* The format is like `Employee{id=1, name=Eric, job=contributor, hireDate=2021-05-05, salary=50}`.
229+
*/
230+
public override fun toString(): String
231+
212232
/**
213233
* Companion object provides functions to create entity instances.
214234
*/

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-core/src/test/kotlin/org/ktorm/entity/EntityTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ class EntityTest : BaseTest() {
522522
val employee = database.employees.find { it.id eq 2 } ?: return
523523
val copy = employee.copy()
524524

525-
assert(employee.hireDate == copy.hireDate)
525+
assert(employee == copy)
526+
assert(employee !== copy)
526527
assert(employee.hireDate !== copy.hireDate) // should not be the same instance because of deep copy.
527528
assert(copy.manager?.implementation?.parent === copy.implementation) // should keep the parent relationship.
528529
}

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)