Skip to content

Commit

Permalink
New API after some problems found buring doc refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Nov 17, 2024
1 parent cb320c2 commit 2569f74
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 114 deletions.
2 changes: 1 addition & 1 deletion project/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=org.babyfish.jimmer
version=0.9.17
version=0.9.18
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -251,9 +248,9 @@ default <S extends E> Iterable<S> saveAll(@NotNull Iterable<S> entities) {
default <S extends E> Iterable<S> saveEntities(@NotNull Iterable<S> entities) {
return saveEntitiesCommand(entities)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

Expand All @@ -262,9 +259,9 @@ default <S extends E> Iterable<S> saveEntities(@NotNull Iterable<S> entities, Sa
return saveEntitiesCommand(entities)
.setMode(mode)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

Expand All @@ -273,9 +270,9 @@ default <S extends E> Iterable<S> saveEntities(@NotNull Iterable<S> entities, As
return saveEntitiesCommand(entities)
.setAssociatedModeAll(associatedMode)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

Expand All @@ -289,19 +286,19 @@ default <S extends E> Iterable<S> saveEntities(
.setMode(mode)
.setAssociatedModeAll(associatedMode)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

@NotNull
default <S extends E> Iterable<S> saveInputs(@NotNull Iterable<Input<S>> entities) {
return saveInputsCommand(entities)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

Expand All @@ -310,9 +307,9 @@ default <S extends E> Iterable<S> saveInputs(@NotNull Iterable<Input<S>> entitie
return saveInputsCommand(entities)
.setMode(mode)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

Expand All @@ -321,9 +318,9 @@ default <S extends E> Iterable<S> saveInputs(@NotNull Iterable<Input<S>> entitie
return saveInputsCommand(entities)
.setAssociatedModeAll(associatedMode)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

Expand All @@ -337,9 +334,9 @@ default <S extends E> Iterable<S> saveInputs(
.setMode(mode)
.setAssociatedModeAll(associatedMode)
.execute()
.getSimpleResults()
.getItems()
.stream()
.map(SimpleSaveResult::getModifiedEntity)
.map(BatchSaveResult.Item::getModifiedEntity)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ interface KRepository<E: Any, ID: Any> : PagingAndSortingRepository<E, ID> {
sql.save(input, associatedMode, null, block).modifiedEntity

override fun <S : E> saveAll(entities: Iterable<S>): List<S> =
saveEntities(entities, SaveMode.UPSERT).simpleResults.map { it.modifiedEntity }
saveEntities(entities, SaveMode.UPSERT).items.map { it.modifiedEntity }

fun <S : E> saveEntities(entities: Iterable<S>): KBatchSaveResult<S> =
saveEntities(entities, SaveMode.UPSERT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.babyfish.jimmer.sql.kt.ast.mutation

import org.babyfish.jimmer.sql.ast.mutation.MutationResultItem

interface KBatchSaveResult<E: Any> : KMutationResult {

val simpleResults: List<KSimpleSaveResult<E>>
val items: List<MutationResultItem<E>>
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.babyfish.jimmer.sql.kt.ast.mutation

interface KSimpleSaveResult<E: Any> : KMutationResult {
import org.babyfish.jimmer.sql.ast.mutation.MutationResultItem

val originalEntity: E
interface KSimpleSaveResult<E: Any> : KMutationResult, MutationResultItem<E> {

val modifiedEntity: E

val isModified: Boolean
val isRowAffected: Boolean
get() = affectedRowCountMap.isNotEmpty()
}
Original file line number Diff line number Diff line change
@@ -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<E: Any>(
javaResult: BatchSaveResult<E>
) : KMutationResultImpl(javaResult), KBatchSaveResult<E> {

@Suppress("UNCHECKED_CAST")
override val simpleResults: List<KSimpleSaveResult<E>> =
javaResult.simpleResults.map { KSimpleSaveResultImpl(it) }
override val items: List<Item<E>> =
javaResult.items
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -374,20 +375,20 @@ 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]"
)
}

fun modified(json: String) {
contentEquals(
json,
result.modifiedEntity.toString(),
item.modifiedEntity.toString(),
"modifiedEntities[$index]"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public BatchSaveResult<E> execute(Connection con) {
OptionsImpl options = options();
List<E> entities = options.getArument();
if (entities.isEmpty()) {
return new BatchSaveResult<>(Collections.emptyList());
return new BatchSaveResult<>(Collections.emptyMap(), Collections.emptyList());
}
return options
.getSqlClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public <E> SimpleSaveResult<E> save(E entity) {
@SuppressWarnings("unchecked")
public <E> BatchSaveResult<E> saveAll(Collection<E> 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;
Expand All @@ -79,17 +79,16 @@ public <E> BatchSaveResult<E> saveAll(Collection<E> entities) {
}
Iterator<E> oldItr = entities.iterator();
Iterator<E> newItr = newEntities.iterator();
List<SimpleSaveResult<E>> results = new ArrayList<>(entities.size());
List<BatchSaveResult.Item<E>> 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<DraftSpi> drafts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

public class AbstractMutationResult implements MutationResult {

protected int totalAffectedRowCount;
protected final int totalAffectedRowCount;

protected Map<AffectedTable, Integer> affectedRowCountMap;
protected final Map<AffectedTable, Integer> affectedRowCountMap;

public AbstractMutationResult(Map<AffectedTable, Integer> affectedRowCountMap) {
this.affectedRowCountMap = Collections.unmodifiableMap(affectedRowCountMap);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package org.babyfish.jimmer.sql.ast.mutation;

import org.jetbrains.annotations.NotNull;

import java.util.*;

public class BatchSaveResult<E> extends AbstractMutationResult {

private List<SimpleSaveResult<E>> simpleResults;

public BatchSaveResult(
List<SimpleSaveResult<E>> simpleResults
) {
this(mergedAffectedRowCount(simpleResults), simpleResults);
}
private List<Item<E>> items;

public BatchSaveResult(
Map<AffectedTable, Integer> affectedRowMap,
List<SimpleSaveResult<E>> simpleResults
List<Item<E>> items
) {
super(affectedRowMap);
this.simpleResults = Collections.unmodifiableList(simpleResults);
this.items = Collections.unmodifiableList(items);
}

public List<SimpleSaveResult<E>> getSimpleResults() {
return simpleResults;
public List<Item<E>> getItems() {
return items;
}

@Override
public int hashCode() {
return Objects.hash(affectedRowCountMap, simpleResults);
return Objects.hash(affectedRowCountMap, items);
}

@Override
Expand All @@ -35,33 +31,47 @@ 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
public String toString() {
return "BatchSaveResult{" +
"totalAffectedRowCount=" + totalAffectedRowCount +
", affectedRowCountMap=" + affectedRowCountMap +
", simpleResults=" + simpleResults +
", simpleResults=" + items +
'}';
}

private static <E> Map<AffectedTable, Integer> mergedAffectedRowCount(
List<SimpleSaveResult<E>> simpleResults
) {
if (simpleResults.isEmpty()) {
return Collections.emptyMap();
public static class Item<E> implements MutationResultItem<E> {

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<AffectedTable, Integer> mergedMap = new HashMap<>();
for (SimpleSaveResult<?> result : simpleResults) {
for (Map.Entry<AffectedTable, Integer> e : result.getAffectedRowCountMap().entrySet()) {
mergedMap.merge(e.getKey(), e.getValue(), Integer::sum);
}

@Override
public String toString() {
return "Item{" +
"originalEntity=" + originalEntity +
", modifiedEntity=" + modifiedEntity +
'}';
}
return mergedMap;
}
}
Loading

0 comments on commit 2569f74

Please sign in to comment.