Skip to content

Commit

Permalink
Better API
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed May 31, 2022
1 parent feaedcd commit fc90db2
Show file tree
Hide file tree
Showing 22 changed files with 600 additions and 113 deletions.
6 changes: 3 additions & 3 deletions example/jimmer-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ plugins {
}

group 'org.babyfish.jimmer.example.core'
version '0.0.19'
version '0.0.20'

repositories {
mavenCentral()
}

dependencies {

implementation 'org.babyfish.jimmer:jimmer-core:0.0.19'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.19'
implementation 'org.babyfish.jimmer:jimmer-core:0.0.20'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.20'

implementation 'javax.validation:validation-api:2.0.1.Final'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void bookDemo() {

private static void treeNodeDemo() {

// 第一步,从头构建全新的数据
// First step, create new object from scratch
TreeNode treeNode = TreeNodeDraft.$.produce(root -> {
root.setName("Root").addIntoChildNodes(food -> {
food
Expand All @@ -65,14 +65,14 @@ private static void treeNodeDemo() {
});
});

// 第二步,基于现有数据对象,做某些“变更”,创建新的数据对象。
// Second step, make some "changes" based on the existing object to get a new object.
TreeNode newTreeNode = TreeNodeDraft.$.produce(
// highlight-next-line
treeNode, // 现有的数据对象
treeNode, // existing object
root -> {
root
.childNodes(true).get(0)
.childNodes(true).get(0)
.childNodes(true).get(0) // Food
.childNodes(true).get(0) // Drink
.childNodes(true).get(0) // Coco Cola
.setName("Coco Cola plus");
}
);
Expand Down
6 changes: 3 additions & 3 deletions example/jimmer-sql-graphql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'org.babyfish.jimmer.sql.example'
version = '0.0.19'
version = '0.0.20'
sourceCompatibility = '1.8'

repositories {
Expand All @@ -14,8 +14,8 @@ repositories {

dependencies {

implementation 'org.babyfish.jimmer:jimmer-sql:0.0.19'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.19'
implementation 'org.babyfish.jimmer:jimmer-sql:0.0.20'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.20'

implementation 'org.springframework.boot:spring-boot-starter-graphql'
implementation 'org.springframework.boot:spring-boot-starter-web'
Expand Down
6 changes: 3 additions & 3 deletions example/jimmer-sql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ plugins {
}

group 'org.babyfish.jimmer.example.sql'
version '0.0.19'
version '0.0.20'

repositories {
mavenCentral()
}

dependencies {

implementation 'org.babyfish.jimmer:jimmer-sql:0.0.19'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.19'
implementation 'org.babyfish.jimmer:jimmer-sql:0.0.20'
annotationProcessor 'org.babyfish.jimmer:jimmer-apt:0.0.20'

implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.shell:spring-shell-starter:2.0.1.RELEASE'
Expand Down
2 changes: 1 addition & 1 deletion project/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
allprojects {
group = "org.babyfish.jimmer"
version = "0.0.19"
version = "0.0.20"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,47 @@ public interface Associations {

Associations reverse();

default int save(Object sourceId, Object targetId) {
return saveCommand(sourceId, targetId).execute();
}

default int batchSave(Collection<Object> sourceIds, Collection<Object> targetIds) {
return batchSaveCommand(sourceIds, targetIds).execute();
}

default int batchSave(Collection<Tuple2<Object, Object>> idPairs) {
return batchSaveCommand(idPairs).execute();
}

AssociationSaveCommand saveCommand(
Object sourceId,
Object targetId
);

AssociationSaveCommand saveCommand(
AssociationSaveCommand batchSaveCommand(
Collection<Object> sourceIds,
Collection<Object> targetIds
);

AssociationSaveCommand saveCommand(
AssociationSaveCommand batchSaveCommand(
Collection<Tuple2<Object, Object>> idPairs
);

default int delete(Object sourceId, Object targetId) {
return deleteCommand(sourceId, targetId).execute();
}

default int delete(Collection<Object> sourceIds, Collection<Object> targetIds) {
return deleteCommand(sourceIds, targetIds).execute();
default int batchDelete(Collection<Object> sourceIds, Collection<Object> targetIds) {
return batchDeleteCommand(sourceIds, targetIds).execute();
}

default int delete(Collection<Tuple2<Object, Object>> idPairs) {
return deleteCommand(idPairs).execute();
default int batchDelete(Collection<Tuple2<Object, Object>> idPairs) {
return batchDeleteCommand(idPairs).execute();
}

Executable<Integer> deleteCommand(Object sourceId, Object targetId);

Executable<Integer> deleteCommand(Collection<Object> sourceIds, Collection<Object> targetIds);
Executable<Integer> batchDeleteCommand(Collection<Object> sourceIds, Collection<Object> targetIds);

Executable<Integer> deleteCommand(Collection<Tuple2<Object, Object>> idPairs);
Executable<Integer> batchDeleteCommand(Collection<Tuple2<Object, Object>> idPairs);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package org.babyfish.jimmer.sql;

import org.babyfish.jimmer.sql.ast.mutation.*;
import org.babyfish.jimmer.sql.fetcher.Fetcher;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface Entities {

<E> E findById(Class<E> entityType, Object id);

<E> List<E> findByIds(Class<E> entityType, Collection<Object> ids);

<ID, E> Map<ID, E> findMapByIds(Class<E> entityType, Collection<ID> ids);

<E> E findById(Fetcher<E> fetcher, Object id);

<E> List<E> findByIds(Fetcher<E> fetcher, Collection<Object> ids);

<ID, E> Map<ID, E> findMapByIds(Fetcher<E> fetcher, Collection<ID> ids);

default <E> SimpleSaveResult<E> save(E entity) {
return saveCommand(entity).execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public ListLoaderImpl(SqlClient sqlClient, ImmutableProp prop, BiConsumer<Sortab

@Override
public Executable<List<T>> loadCommand(S source, int limit, int offset) {
if (source instanceof Collection<?>) {
throw new IllegalArgumentException(
"source cannot be collection, do you want to call 'batchLoadCommand'?"
);
}
return new SingleCommand<>(
sqlClient,
prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public ReferenceLoaderImpl(

@Override
public Executable<T> loadCommand(S source) {
if (source instanceof Collection<?>) {
throw new IllegalArgumentException(
"source cannot be collection, do you want to call 'batchLoadCommand'?"
);
}
return new SingleCommand<>(
sqlClient,
prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ public interface Expression<T> extends Selection<T> {

Predicate in(Collection<T> values);

Predicate in(T ... values);

Predicate notIn(Collection<T> values);

Predicate notIn(T ... values);

Predicate in(TypedSubQuery<T> subQuery);

Predicate notIn(TypedSubQuery<T> subQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,11 @@ default Predicate in(Collection<T> values) {
return new InCollectionPredicate(this, values, false);
}

@Override
default Predicate in(T ... values) {
return in(Arrays.asList(values));
}

@Override
default Predicate notIn(Collection<T> values) {
return new InCollectionPredicate(this, values, true);
}

@Override
default Predicate notIn(T ... values) {
return notIn(Arrays.asList(values));
}

@Override
default Predicate in(TypedSubQuery<T> subQuery) {
return new InSubQueryPredicate(this, subQuery, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,47 @@ public Associations reverse() {

@Override
public AssociationSaveCommand saveCommand(Object sourceId, Object targetId) {
if (sourceId instanceof Collection<?> || targetId instanceof Collection<?>) {
throw new IllegalArgumentException(
"sourceId or targetId cannot be collection, do you want to call 'batchSaveCommand'?"
);
}
return new AssociationSaveCommandImpl(
saveExecutable(validateAndZip(sourceId, targetId))
);
}

@Override
public AssociationSaveCommand saveCommand(Collection<Object> sourceIds, Collection<Object> targetIds) {
public AssociationSaveCommand batchSaveCommand(Collection<Object> sourceIds, Collection<Object> targetIds) {
return new AssociationSaveCommandImpl(
saveExecutable(validateAndZip(sourceIds, targetIds))
);
}

@Override
public AssociationSaveCommand saveCommand(Collection<Tuple2<Object, Object>> idPairs) {
public AssociationSaveCommand batchSaveCommand(Collection<Tuple2<Object, Object>> idPairs) {
return new AssociationSaveCommandImpl(
saveExecutable(validate(idPairs))
);
}

@Override
public Executable<Integer> deleteCommand(Object sourceId, Object targetId) {
if (sourceId instanceof Collection<?> || targetId instanceof Collection<?>) {
throw new IllegalArgumentException(
"sourceId or targetId cannot be collection, do you want to call 'batchDeleteCommand'?"
);
}
return deleteExecutable(validateAndZip(sourceId, targetId));
}

@Override
public Executable<Integer> deleteCommand(Collection<Object> sourceIds, Collection<Object> targetIds) {
public Executable<Integer> batchDeleteCommand(Collection<Object> sourceIds, Collection<Object> targetIds) {
return deleteExecutable(validateAndZip(sourceIds, targetIds));
}

@Override
public Executable<Integer> deleteCommand(Collection<Tuple2<Object, Object>> idPairs) {
public Executable<Integer> batchDeleteCommand(Collection<Tuple2<Object, Object>> idPairs) {
return deleteExecutable(validate(idPairs));
}

Expand All @@ -88,6 +98,7 @@ private Executable<Integer> deleteExecutable(Collection<Tuple2<Object, Object>>
);
}

@SuppressWarnings("unchecked")
private static Collection<Tuple2<Object, Object>> validateAndZip(Object sourceId, Object targetId) {
return Collections.singleton(
new Tuple2<>(
Expand Down
Loading

0 comments on commit fc90db2

Please sign in to comment.