@@ -24,7 +24,7 @@ fun <E : Entity<E>> Table<E>.add(entity: E): Int {
24
24
)
25
25
)
26
26
27
- val useGeneratedKey = primaryKey?.binding != null && entity.getPrimaryKeyValue(this ) == null
27
+ val useGeneratedKey = primaryKey?.binding != null && entity.implementation. getPrimaryKeyValue(this ) == null
28
28
29
29
expression.prepareStatement(autoGeneratedKeys = useGeneratedKey) { statement, logger ->
30
30
val effects = statement.executeUpdate().also { logger.debug(" Effects: {}" , it) }
@@ -35,24 +35,25 @@ fun <E : Entity<E>> Table<E>.add(entity: E): Int {
35
35
val generatedKey = primaryKey?.sqlType?.getResult(rs, 1 )
36
36
if (generatedKey != null ) {
37
37
logger.debug(" Generated Key: {}" , generatedKey)
38
- entity.setPrimaryKeyValue(this , generatedKey)
38
+ entity.implementation. setPrimaryKeyValue(this , generatedKey)
39
39
}
40
40
}
41
41
}
42
42
}
43
43
44
- entity.impl .fromTable = this
45
- entity.discardChanges ()
44
+ entity.implementation .fromTable = this
45
+ entity.implementation.doDiscardChanges ()
46
46
return effects
47
47
}
48
48
}
49
49
50
50
private fun Table <* >.findInsertColumns (entity : Entity <* >): Map <Column <* >, Any?> {
51
+ val implementation = entity.implementation
51
52
val assignments = LinkedHashMap <Column <* >, Any? > ()
52
53
53
54
for (column in columns) {
54
55
if (column is SimpleColumn && column.binding != null ) {
55
- val value = entity .getColumnValue(column)
56
+ val value = implementation .getColumnValue(column)
56
57
if (value != null ) {
57
58
assignments[column] = value
58
59
}
@@ -63,9 +64,9 @@ private fun Table<*>.findInsertColumns(entity: Entity<*>): Map<Column<*>, Any?>
63
64
}
64
65
65
66
@Suppress(" UNCHECKED_CAST" )
66
- internal fun EntityImpl .doFlushChanges (): Int {
67
- val fromTable = this .fromTable ? : kotlin. error(" The entity is not associated with any table yet." )
68
- val primaryKey = fromTable.primaryKey ? : kotlin. error(" Table ${fromTable.tableName} doesn't have a primary key." )
67
+ internal fun EntityImplementation .doFlushChanges (): Int {
68
+ val fromTable = this .fromTable?. takeIf { this .parent == null } ? : error(" The entity is not associated with any table yet." )
69
+ val primaryKey = fromTable.primaryKey ? : error(" Table ${fromTable.tableName} doesn't have a primary key." )
69
70
val assignments = findChangedColumns(fromTable).takeIf { it.isNotEmpty() } ? : return 0
70
71
71
72
val expression = AliasRemover .visit(
@@ -88,12 +89,12 @@ internal fun EntityImpl.doFlushChanges(): Int {
88
89
89
90
expression.prepareStatement { statement, logger ->
90
91
val effects = statement.executeUpdate().also { logger.debug(" Effects: {}" , it) }
91
- discardChanges ()
92
+ doDiscardChanges ()
92
93
return effects
93
94
}
94
95
}
95
96
96
- private fun EntityImpl .findChangedColumns (fromTable : Table <* >): Map <Column <* >, Any?> {
97
+ private fun EntityImplementation .findChangedColumns (fromTable : Table <* >): Map <Column <* >, Any?> {
97
98
val assignments = LinkedHashMap <Column <* >, Any? > ()
98
99
99
100
for (column in fromTable.columns) {
@@ -102,30 +103,34 @@ private fun EntityImpl.findChangedColumns(fromTable: Table<*>): Map<Column<*>, A
102
103
when (binding) {
103
104
is ReferenceBinding -> {
104
105
if (binding.onProperty.name in changedProperties) {
105
- val child = this [ binding.onProperty.name] as Entity <* >?
106
- assignments[column] = child?.getPrimaryKeyValue(binding.referenceTable)
106
+ val child = this .getProperty( binding.onProperty.name) as Entity <* >?
107
+ assignments[column] = child?.implementation?. getPrimaryKeyValue(binding.referenceTable)
107
108
}
108
109
}
109
110
is NestedBinding -> {
110
111
var anyChanged = false
111
112
var curr: Any? = this
112
113
113
114
for ((i, prop) in binding.withIndex()) {
114
- check(curr is Entity < * > ? )
115
+ check(curr is EntityImplementation ? )
115
116
116
- val changed = if (curr == null ) false else prop.name in curr.impl. changedProperties
117
+ val changed = if (curr == null ) false else prop.name in curr.changedProperties
117
118
118
119
if (changed && i > 0 ) {
119
120
check(curr != null )
120
121
121
- if (curr.impl. fromTable != null && curr.getRoot() != this ) {
122
+ if (curr.fromTable != null && curr.getRoot() != this ) {
122
123
val propPath = binding.subList(0 , i + 1 ).joinToString(separator = " ." , prefix = " this." ) { it.name }
123
124
throw IllegalStateException (" $propPath may be unexpectedly discarded after flushChanges, please save it to database first." )
124
125
}
125
126
}
126
127
127
128
anyChanged = anyChanged || changed
128
- curr = curr?.get(prop.name)
129
+
130
+ curr = curr?.getProperty(prop.name)
131
+ if (curr is Entity <* >) {
132
+ curr = curr.implementation
133
+ }
129
134
}
130
135
131
136
if (anyChanged) {
@@ -138,17 +143,17 @@ private fun EntityImpl.findChangedColumns(fromTable: Table<*>): Map<Column<*>, A
138
143
return assignments
139
144
}
140
145
141
- private tailrec fun Entity < * > .getRoot (): Entity < * > {
142
- val parent = this .impl. parent
146
+ private tailrec fun EntityImplementation .getRoot (): EntityImplementation {
147
+ val parent = this .parent
143
148
if (parent == null ) {
144
149
return this
145
150
} else {
146
151
return parent.getRoot()
147
152
}
148
153
}
149
154
150
- internal fun EntityImpl .doDiscardChanges () {
151
- val fromTable = this .fromTable ? : kotlin. error(" The entity is not associated with any table yet." )
155
+ internal fun EntityImplementation .doDiscardChanges () {
156
+ val fromTable = this .fromTable?. takeIf { this .parent == null } ? : error(" The entity is not associated with any table yet." )
152
157
153
158
for (column in fromTable.columns) {
154
159
val binding = column.binding?.takeIf { column is SimpleColumn } ? : continue
@@ -165,20 +170,23 @@ internal fun EntityImpl.doDiscardChanges() {
165
170
break
166
171
}
167
172
168
- check(curr is Entity < * > )
169
- curr.impl. changedProperties.remove(prop.name)
173
+ check(curr is EntityImplementation )
174
+ curr.changedProperties.remove(prop.name)
170
175
171
- curr = curr[prop.name]
176
+ curr = curr.getProperty(prop.name)
177
+ if (curr is Entity <* >) {
178
+ curr = curr.implementation
179
+ }
172
180
}
173
181
}
174
182
}
175
183
}
176
184
}
177
185
178
186
@Suppress(" UNCHECKED_CAST" )
179
- internal fun EntityImpl .doDelete (): Int {
180
- val fromTable = this .fromTable ? : kotlin. error(" The entity is not associated with any table yet." )
181
- val primaryKey = fromTable.primaryKey ? : kotlin. error(" Table ${fromTable.tableName} doesn't have a primary key." )
187
+ internal fun EntityImplementation .doDelete (): Int {
188
+ val fromTable = this .fromTable?. takeIf { this .parent == null } ? : error(" The entity is not associated with any table yet." )
189
+ val primaryKey = fromTable.primaryKey ? : error(" Table ${fromTable.tableName} doesn't have a primary key." )
182
190
183
191
val expression = AliasRemover .visit(
184
192
expr = DeleteExpression (
0 commit comments