diff --git a/project/gradle.properties b/project/gradle.properties index e9fac5f511..b16bd7aaa1 100644 --- a/project/gradle.properties +++ b/project/gradle.properties @@ -1,2 +1,2 @@ group=org.babyfish.jimmer -version=0.9.17 +version=0.9.18 diff --git a/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/JRepository.java b/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/JRepository.java index f4524ed051..078dc56848 100644 --- a/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/JRepository.java +++ b/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/JRepository.java @@ -5,13 +5,10 @@ import org.babyfish.jimmer.meta.ImmutableType; import org.babyfish.jimmer.meta.TypedProp; import org.babyfish.jimmer.Input; -import org.babyfish.jimmer.spring.repository.support.Utils; import org.babyfish.jimmer.sql.JSqlClient; import org.babyfish.jimmer.sql.ast.mutation.*; -import org.babyfish.jimmer.sql.ast.query.ConfigurableRootQuery; import org.babyfish.jimmer.sql.fetcher.Fetcher; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.springframework.core.annotation.AliasFor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -251,9 +248,9 @@ default Iterable saveAll(@NotNull Iterable entities) { default Iterable saveEntities(@NotNull Iterable entities) { return saveEntitiesCommand(entities) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } @@ -262,9 +259,9 @@ default Iterable saveEntities(@NotNull Iterable entities, Sa return saveEntitiesCommand(entities) .setMode(mode) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } @@ -273,9 +270,9 @@ default Iterable saveEntities(@NotNull Iterable entities, As return saveEntitiesCommand(entities) .setAssociatedModeAll(associatedMode) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } @@ -289,9 +286,9 @@ default Iterable saveEntities( .setMode(mode) .setAssociatedModeAll(associatedMode) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } @@ -299,9 +296,9 @@ default Iterable saveEntities( default Iterable saveInputs(@NotNull Iterable> entities) { return saveInputsCommand(entities) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } @@ -310,9 +307,9 @@ default Iterable saveInputs(@NotNull Iterable> entitie return saveInputsCommand(entities) .setMode(mode) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } @@ -321,9 +318,9 @@ default Iterable saveInputs(@NotNull Iterable> entitie return saveInputsCommand(entities) .setAssociatedModeAll(associatedMode) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } @@ -337,9 +334,9 @@ default Iterable saveInputs( .setMode(mode) .setAssociatedModeAll(associatedMode) .execute() - .getSimpleResults() + .getItems() .stream() - .map(SimpleSaveResult::getModifiedEntity) + .map(BatchSaveResult.Item::getModifiedEntity) .collect(Collectors.toList()); } diff --git a/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/KRepository.kt b/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/KRepository.kt index 2f03c707c7..ecedaad633 100644 --- a/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/KRepository.kt +++ b/project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/repository/KRepository.kt @@ -187,7 +187,7 @@ interface KRepository : PagingAndSortingRepository { sql.save(input, associatedMode, null, block).modifiedEntity override fun saveAll(entities: Iterable): List = - saveEntities(entities, SaveMode.UPSERT).simpleResults.map { it.modifiedEntity } + saveEntities(entities, SaveMode.UPSERT).items.map { it.modifiedEntity } fun saveEntities(entities: Iterable): KBatchSaveResult = saveEntities(entities, SaveMode.UPSERT) diff --git a/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KBatchSaveResult.kt b/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KBatchSaveResult.kt index 47200dd241..de14d353a3 100644 --- a/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KBatchSaveResult.kt +++ b/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KBatchSaveResult.kt @@ -1,6 +1,8 @@ package org.babyfish.jimmer.sql.kt.ast.mutation +import org.babyfish.jimmer.sql.ast.mutation.MutationResultItem + interface KBatchSaveResult : KMutationResult { - val simpleResults: List> + val items: List> } \ No newline at end of file diff --git a/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KSimpleSaveResult.kt b/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KSimpleSaveResult.kt index 02a880fcbd..6c265ba75d 100644 --- a/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KSimpleSaveResult.kt +++ b/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/KSimpleSaveResult.kt @@ -1,10 +1,9 @@ package org.babyfish.jimmer.sql.kt.ast.mutation -interface KSimpleSaveResult : KMutationResult { +import org.babyfish.jimmer.sql.ast.mutation.MutationResultItem - val originalEntity: E +interface KSimpleSaveResult : KMutationResult, MutationResultItem { - val modifiedEntity: E - - val isModified: Boolean + val isRowAffected: Boolean + get() = affectedRowCountMap.isNotEmpty() } \ No newline at end of file diff --git a/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/impl/KBatchSaveResultImpl.kt b/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/impl/KBatchSaveResultImpl.kt index aa032ce81b..08150a00a3 100644 --- a/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/impl/KBatchSaveResultImpl.kt +++ b/project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/ast/mutation/impl/KBatchSaveResultImpl.kt @@ -1,15 +1,13 @@ package org.babyfish.jimmer.sql.kt.ast.mutation.impl import org.babyfish.jimmer.sql.ast.mutation.BatchSaveResult -import org.babyfish.jimmer.sql.ast.mutation.SimpleSaveResult +import org.babyfish.jimmer.sql.ast.mutation.BatchSaveResult.Item import org.babyfish.jimmer.sql.kt.ast.mutation.KBatchSaveResult -import org.babyfish.jimmer.sql.kt.ast.mutation.KSimpleSaveResult internal class KBatchSaveResultImpl( javaResult: BatchSaveResult ) : KMutationResultImpl(javaResult), KBatchSaveResult { - @Suppress("UNCHECKED_CAST") - override val simpleResults: List> = - javaResult.simpleResults.map { KSimpleSaveResultImpl(it) } + override val items: List> = + javaResult.items } \ No newline at end of file diff --git a/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/common/AbstractMutationTest.kt b/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/common/AbstractMutationTest.kt index 5ad763c0d1..842107ce2c 100644 --- a/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/common/AbstractMutationTest.kt +++ b/project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/common/AbstractMutationTest.kt @@ -1,6 +1,7 @@ package org.babyfish.jimmer.sql.kt.common import org.babyfish.jimmer.sql.ast.impl.mutation.QueryReason +import org.babyfish.jimmer.sql.ast.mutation.MutationResultItem import org.babyfish.jimmer.sql.kt.ast.KExecutable import org.babyfish.jimmer.sql.kt.ast.mutation.KBatchSaveResult import org.babyfish.jimmer.sql.kt.ast.mutation.KMutationResult @@ -233,16 +234,16 @@ abstract class AbstractMutationTest : AbstractTest() { index: Int, block: EntityDSL.() -> Unit ): ExpectDSLWithResult { - val simpleSaveResult: KSimpleSaveResult<*> = if (index == 0) { + val item: MutationResultItem<*> = if (index == 0) { if (result is KSimpleSaveResult<*>) { result } else { - (result as KBatchSaveResult<*>).simpleResults[0] + (result as KBatchSaveResult<*>).items[0] } } else { - (result as KBatchSaveResult<*>).simpleResults[index] + (result as KBatchSaveResult<*>).items[index] } - block(EntityDSL(index, simpleSaveResult)) + block(EntityDSL(index, item)) return this } @@ -251,7 +252,7 @@ abstract class AbstractMutationTest : AbstractTest() { val actualEntityCount: Int = if (result is KSimpleSaveResult<*>) { 1 } else if (result is KBatchSaveResult<*>) { - result.simpleResults.size + result.items.size } else { 0 } @@ -374,12 +375,12 @@ abstract class AbstractMutationTest : AbstractTest() { protected class EntityDSL internal constructor( private val index: Int, - private val result: KSimpleSaveResult<*> + private val item: MutationResultItem<*> ) { fun original(json: String) { contentEquals( json, - result.originalEntity.toString(), + item.originalEntity.toString(), "originalEntities[$index]" ) } @@ -387,7 +388,7 @@ abstract class AbstractMutationTest : AbstractTest() { fun modified(json: String) { contentEquals( json, - result.modifiedEntity.toString(), + item.modifiedEntity.toString(), "modifiedEntities[$index]" ) } diff --git a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/BatchEntitySaveCommandImpl.java b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/BatchEntitySaveCommandImpl.java index 9d9e355a50..abfc401368 100644 --- a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/BatchEntitySaveCommandImpl.java +++ b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/BatchEntitySaveCommandImpl.java @@ -35,7 +35,7 @@ public BatchSaveResult execute(Connection con) { OptionsImpl options = options(); List entities = options.getArument(); if (entities.isEmpty()) { - return new BatchSaveResult<>(Collections.emptyList()); + return new BatchSaveResult<>(Collections.emptyMap(), Collections.emptyList()); } return options .getSqlClient() diff --git a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/Saver.java b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/Saver.java index ec138016db..d77549f9e5 100644 --- a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/Saver.java +++ b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/Saver.java @@ -62,7 +62,7 @@ public SimpleSaveResult save(E entity) { @SuppressWarnings("unchecked") public BatchSaveResult saveAll(Collection entities) { if (entities.isEmpty()) { - return new BatchSaveResult<>(Collections.emptyList()); + return new BatchSaveResult<>(Collections.emptyMap(), Collections.emptyList()); } ImmutableType immutableType = ImmutableType.get(entities.iterator().next().getClass()); MutationTrigger trigger = ctx.trigger; @@ -79,17 +79,16 @@ public BatchSaveResult saveAll(Collection entities) { } Iterator oldItr = entities.iterator(); Iterator newItr = newEntities.iterator(); - List> results = new ArrayList<>(entities.size()); + List> items = new ArrayList<>(entities.size()); while (oldItr.hasNext() && newItr.hasNext()) { - results.add( - new SimpleSaveResult<>( - ctx.affectedRowCountMap, + items.add( + new BatchSaveResult.Item<>( oldItr.next(), newItr.next() ) ); } - return new BatchSaveResult<>(results); + return new BatchSaveResult<>(ctx.affectedRowCountMap, items); } private void saveAllImpl(List drafts) { diff --git a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/AbstractMutationResult.java b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/AbstractMutationResult.java index 982fa5b626..7ce8aad4c7 100644 --- a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/AbstractMutationResult.java +++ b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/AbstractMutationResult.java @@ -8,9 +8,9 @@ public class AbstractMutationResult implements MutationResult { - protected int totalAffectedRowCount; + protected final int totalAffectedRowCount; - protected Map affectedRowCountMap; + protected final Map affectedRowCountMap; public AbstractMutationResult(Map affectedRowCountMap) { this.affectedRowCountMap = Collections.unmodifiableMap(affectedRowCountMap); diff --git a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/BatchSaveResult.java b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/BatchSaveResult.java index 09dc1632aa..e5ec048855 100644 --- a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/BatchSaveResult.java +++ b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/BatchSaveResult.java @@ -1,32 +1,28 @@ package org.babyfish.jimmer.sql.ast.mutation; +import org.jetbrains.annotations.NotNull; + import java.util.*; public class BatchSaveResult extends AbstractMutationResult { - private List> simpleResults; - - public BatchSaveResult( - List> simpleResults - ) { - this(mergedAffectedRowCount(simpleResults), simpleResults); - } + private List> items; public BatchSaveResult( Map affectedRowMap, - List> simpleResults + List> items ) { super(affectedRowMap); - this.simpleResults = Collections.unmodifiableList(simpleResults); + this.items = Collections.unmodifiableList(items); } - public List> getSimpleResults() { - return simpleResults; + public List> getItems() { + return items; } @Override public int hashCode() { - return Objects.hash(affectedRowCountMap, simpleResults); + return Objects.hash(affectedRowCountMap, items); } @Override @@ -35,7 +31,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; BatchSaveResult that = (BatchSaveResult) o; return affectedRowCountMap.equals(that.affectedRowCountMap) && - simpleResults.equals(that.simpleResults); + items.equals(that.items); } @Override @@ -43,25 +39,39 @@ public String toString() { return "BatchSaveResult{" + "totalAffectedRowCount=" + totalAffectedRowCount + ", affectedRowCountMap=" + affectedRowCountMap + - ", simpleResults=" + simpleResults + + ", simpleResults=" + items + '}'; } - private static Map mergedAffectedRowCount( - List> simpleResults - ) { - if (simpleResults.isEmpty()) { - return Collections.emptyMap(); + public static class Item implements MutationResultItem { + + private final E originalEntity; + + private final E modifiedEntity; + + public Item(E originalEntity, E modifiedEntity) { + this.originalEntity = originalEntity; + this.modifiedEntity = modifiedEntity; + } + + @NotNull + @Override + public E getOriginalEntity() { + return originalEntity; } - if (simpleResults.size() == 1) { - return simpleResults.get(0).getAffectedRowCountMap(); + + @NotNull + @Override + public E getModifiedEntity() { + return modifiedEntity; } - Map mergedMap = new HashMap<>(); - for (SimpleSaveResult result : simpleResults) { - for (Map.Entry e : result.getAffectedRowCountMap().entrySet()) { - mergedMap.merge(e.getKey(), e.getValue(), Integer::sum); - } + + @Override + public String toString() { + return "Item{" + + "originalEntity=" + originalEntity + + ", modifiedEntity=" + modifiedEntity + + '}'; } - return mergedMap; } } diff --git a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/MutationResultItem.kt b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/MutationResultItem.kt new file mode 100644 index 0000000000..6bb1cadf0f --- /dev/null +++ b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/MutationResultItem.kt @@ -0,0 +1,20 @@ +package org.babyfish.jimmer.sql.ast.mutation + +interface MutationResultItem { + + val originalEntity: E + + val modifiedEntity: E + + /** + * If it is true, that means the save object is changed, + * such as, + * - The id is assigned to generated value + * - Version is increased + * - The back reference of children of one-to-many association is set + * otherwise, the [originalEntity] and [modifiedEntity] + * should be same object. + */ + val isModified: Boolean + get() = originalEntity !== modifiedEntity +} \ No newline at end of file diff --git a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/SimpleSaveResult.java b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/SimpleSaveResult.java index 872c36f667..8e15dcea46 100644 --- a/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/SimpleSaveResult.java +++ b/project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/SimpleSaveResult.java @@ -1,12 +1,14 @@ package org.babyfish.jimmer.sql.ast.mutation; +import org.jetbrains.annotations.NotNull; + import java.util.Map; -public class SimpleSaveResult extends AbstractMutationResult { +public class SimpleSaveResult extends AbstractMutationResult implements MutationResultItem { - private E originalEntity; + private final E originalEntity; - private E modifiedEntity; + private final E modifiedEntity; public SimpleSaveResult( Map affectedRowCountMap, @@ -18,33 +20,18 @@ public SimpleSaveResult( this.modifiedEntity = modifiedEntity; } + @NotNull + @Override public E getOriginalEntity() { return originalEntity; } + @NotNull + @Override public E getModifiedEntity() { return modifiedEntity; } - /** - * If it is true, that means the save object is changed, - * such as, - *
    - *
  • The id is assigned to generated value
  • - *
  • Version is increased
  • - *
  • The back reference of children of one-to-many association is set
  • - *
- * otherwise, the {@link #getOriginalEntity()} and {@link #getModifiedEntity()} - * should be same object. - */ - public boolean isModified() { - return originalEntity != modifiedEntity; - } - - public boolean isRowAffected() { - return !affectedRowCountMap.isEmpty(); - } - @Override public int hashCode() { int hash = affectedRowCountMap.hashCode(); diff --git a/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/common/AbstractMutationTest.java b/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/common/AbstractMutationTest.java index 899025b4ae..2cdbdd0efc 100644 --- a/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/common/AbstractMutationTest.java +++ b/project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/common/AbstractMutationTest.java @@ -3,10 +3,7 @@ import org.apache.commons.lang3.builder.EqualsBuilder; import org.babyfish.jimmer.sql.ast.Executable; import org.babyfish.jimmer.sql.ast.impl.mutation.QueryReason; -import org.babyfish.jimmer.sql.ast.mutation.AffectedTable; -import org.babyfish.jimmer.sql.ast.mutation.BatchSaveResult; -import org.babyfish.jimmer.sql.ast.mutation.MutationResult; -import org.babyfish.jimmer.sql.ast.mutation.SimpleSaveResult; +import org.babyfish.jimmer.sql.ast.mutation.*; import org.babyfish.jimmer.sql.collection.TypedList; import org.babyfish.jimmer.sql.runtime.ExecutionPurpose; import org.junit.jupiter.api.Assertions; @@ -253,17 +250,17 @@ private ExpectDSLWithResult entity( int index, Consumer block ) { - SimpleSaveResult simpleSaveResult; + MutationResultItem item; if (index == 0) { if (result instanceof SimpleSaveResult) { - simpleSaveResult = (SimpleSaveResult) result; + item = (SimpleSaveResult) result; } else { - simpleSaveResult = ((BatchSaveResult) result).getSimpleResults().get(0); + item = ((BatchSaveResult) result).getItems().get(0); } } else { - simpleSaveResult = ((BatchSaveResult) result).getSimpleResults().get(index); + item = ((BatchSaveResult) result).getItems().get(index); } - block.accept(new EntityDSL(index, simpleSaveResult)); + block.accept(new EntityDSL(index, item)); return this; } @@ -274,7 +271,7 @@ public void close() { if (result instanceof SimpleSaveResult) { actualEntityCount = 1; } else if (result instanceof BatchSaveResult ){ - actualEntityCount = ((BatchSaveResult) result).getSimpleResults().size(); + actualEntityCount = ((BatchSaveResult) result).getItems().size(); } else { actualEntityCount = 0; } @@ -432,19 +429,19 @@ private static void rethrow(Throwable throwable) { protected static class EntityDSL { - private int index; + private final int index; - private SimpleSaveResult result; + private final MutationResultItem item; - EntityDSL(int index, SimpleSaveResult result) { + EntityDSL(int index, MutationResultItem item) { this.index = index; - this.result = result; + this.item = item; } public void original(String json) { Assertions.assertEquals( json.replace("--->", ""), - result.getOriginalEntity().toString(), + item.getOriginalEntity().toString(), "originalEntities[" + index + "]" ); } @@ -452,7 +449,7 @@ public void original(String json) { public void modified(String json) { Assertions.assertEquals( json.replace("--->", ""), - result.getModifiedEntity().toString(), + item.getModifiedEntity().toString(), "modifiedEntities[" + index + "]" ); }