diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/SQLImpl.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/SQLImpl.java index 01a9b7e..41c49b2 100644 --- a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/SQLImpl.java +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/SQLImpl.java @@ -3,11 +3,15 @@ package group.flyfish.fluent.chain; import group.flyfish.fluent.chain.common.AfterJoinSqlChain; import group.flyfish.fluent.chain.common.HandleSqlChain; import group.flyfish.fluent.chain.common.PreSqlChain; +import group.flyfish.fluent.chain.execution.BoundEntity; +import group.flyfish.fluent.chain.execution.BoundProxy; +import group.flyfish.fluent.chain.execution.ReactiveBoundEntity; import group.flyfish.fluent.chain.select.AfterOrderSqlChain; import group.flyfish.fluent.chain.select.AfterWhereSqlChain; import group.flyfish.fluent.chain.select.PieceSqlChain; import group.flyfish.fluent.chain.update.AfterSetSqlChain; import group.flyfish.fluent.debug.FluentSqlDebugger; +import group.flyfish.fluent.entity.DataPage; import group.flyfish.fluent.entity.SQLEntity; import group.flyfish.fluent.operations.FluentSQLOperations; import group.flyfish.fluent.query.JoinCandidate; @@ -21,7 +25,10 @@ import group.flyfish.fluent.utils.sql.ConcatSegment; import group.flyfish.fluent.utils.sql.EntityNameUtils; import group.flyfish.fluent.utils.sql.SFunction; import group.flyfish.fluent.utils.sql.SqlNameUtils; +import lombok.RequiredArgsConstructor; import org.springframework.util.Assert; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import java.util.ArrayList; import java.util.Arrays; @@ -149,14 +156,24 @@ final class SQLImpl extends ConcatSegment implements SQLOperations, Pre return concat("ON").concat(query); } + /** + * 下一步操作 + * + * @return 结果 + */ + @Override + public HandleSqlChain then() { + return this; + } + /** * 不带连接条件 * * @return 处理链 */ @Override - public HandleSqlChain then() { - return this; + public BoundProxy next() { + return new DefaultBoundProxy<>(SQLEntity.of(primaryClass, this::sql, this::parsedParameters)); } /** @@ -187,57 +204,14 @@ final class SQLImpl extends ConcatSegment implements SQLOperations, Pre } - @Override - public T one() { - return one(SqlNameUtils.cast(primaryClass)); - } - - /** - * 执行并获取结果 - * - * @param clazz 结果类 - * @param 泛型 - */ - @Override - public T one(Class clazz) { - limit(1); - return SHARED_OPERATIONS.selectOne(entity.get(), clazz); - } - - @Override - public List list() { - return list(SqlNameUtils.cast(primaryClass)); - } - - /** - * 执行并获取多条结果 - * - * @param clazz 结果类 - * @return 结果列表 - */ - @Override - public List list(Class clazz) { - return SHARED_OPERATIONS.select(entity.get(), clazz); - } - - /** - * 执行并获取更新条数 - * - * @return 更新条数 - */ - @Override - public int execute() { - return SHARED_OPERATIONS.execute(toEntity()); - } - /** * 获取实体,做下一步的事情 * * @return 结果 */ @Override - public SQLEntity toEntity() { - return SQLEntity.of(wrap(this::sql), wrap(this::parsedParameters)); + public BoundProxy as(Class type) { + return new DefaultBoundProxy<>(SQLEntity.of(type, wrap(this::sql), wrap(this::parsedParameters))); } /** @@ -289,4 +263,71 @@ final class SQLImpl extends ConcatSegment implements SQLOperations, Pre public PieceSqlChain offset(int rows) { return concat("OFFSET").concat(String.valueOf(rows)); } + + @RequiredArgsConstructor + private static class DefaultBoundProxy implements BoundProxy { + + private final SQLEntity entity; + + @Override + public BoundEntity block() { + return new DefaultBoundEntity<>(); + } + + @Override + public ReactiveBoundEntity reactive() { + return new DefaultReactiveBoundEntity<>(); + } + } + + /** + * 默认的绑定实体 + * + * @param + */ + private static class DefaultBoundEntity implements BoundEntity { + + @Override + public T one() { + return null; + } + + @Override + public List all() { + return List.of(); + } + + @Override + public DataPage page() { + return null; + } + + @Override + public int execute() { + return 0; + } + } + + private static class DefaultReactiveBoundEntity implements ReactiveBoundEntity { + + @Override + public Mono one() { + return null; + } + + @Override + public Flux all() { + return null; + } + + @Override + public Mono> page() { + return null; + } + + @Override + public Mono execute() { + return null; + } + } } diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/common/ExecutableSql.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/common/ExecutableSql.java index 9fd8917..c5d0bf6 100644 --- a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/common/ExecutableSql.java +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/common/ExecutableSql.java @@ -1,6 +1,6 @@ package group.flyfish.fluent.chain.common; -import group.flyfish.fluent.entity.SQLEntity; +import group.flyfish.fluent.chain.execution.BoundProxy; /** * 可执行的sql @@ -10,16 +10,10 @@ import group.flyfish.fluent.entity.SQLEntity; public interface ExecutableSql { /** - * 执行并获取更新条数 + * 进入下一步,以主表作为输出结果 * - * @return 更新条数 + * @param 泛型 + * @return 绑定操作 */ - int execute(); - - /** - * 转换为SQL实体 - * - * @return 结果 - */ - SQLEntity toEntity(); + BoundProxy next(); } diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/BoundEntity.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/BoundEntity.java new file mode 100644 index 0000000..6bd6212 --- /dev/null +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/BoundEntity.java @@ -0,0 +1,49 @@ +package group.flyfish.fluent.chain.execution; + +import group.flyfish.fluent.entity.DataPage; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; + +import java.util.List; + +/** + * 已经绑定的实体 + * + * @author wangyu + */ +public interface BoundEntity { + + + /** + * 执行一条sql,并且序列化为对象 + * 注意,如果查询不止一条,该方法仅返回第一条数据 + * 如果没有结果,将返回null + * + * @return 查询结果 + */ + @Nullable + T one(); + + /** + * 执行一条sql,并且查询出所有行 + * + * @return 返回的列表 + */ + @NonNull + List all(); + + /** + * 分页查询 + * + * @return 返回的分页对象 + */ + @NonNull + DataPage page(); + + /** + * 直接执行sql,根据update count返回更新行数,如果是查询,永远返回0 + * + * @return 更新行数 + */ + int execute(); +} diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/BoundProxy.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/BoundProxy.java new file mode 100644 index 0000000..782498f --- /dev/null +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/BoundProxy.java @@ -0,0 +1,24 @@ +package group.flyfish.fluent.chain.execution; + +/** + * 绑定状态下的代理 + * + * @author wangyu + */ +public interface BoundProxy { + + /** + * 阻塞的数据库操作 + * + * @return 结果 + */ + BoundEntity block(); + + /** + * 异步数据库逻辑 + * + * @return 结果 + */ + ReactiveBoundEntity reactive(); + +} diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/ReactiveBoundEntity.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/ReactiveBoundEntity.java new file mode 100644 index 0000000..bac1f09 --- /dev/null +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/execution/ReactiveBoundEntity.java @@ -0,0 +1,48 @@ +package group.flyfish.fluent.chain.execution; + +import group.flyfish.fluent.entity.DataPage; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * 已经绑定的异步实体 + * + * @author wangyu + */ +public interface ReactiveBoundEntity { + + /** + * 执行一条sql,并且序列化为对象 + * 注意,如果查询不止一条,该方法仅返回第一条数据 + * 如果没有结果,将返回null + * + * @return 查询结果 + */ + @Nullable + Mono one(); + + /** + * 执行一条sql,并且查询出所有行 + * + * @return 返回的列表 + */ + @NonNull + Flux all(); + + /** + * 分页查询 + * + * @return 返回的分页对象 + */ + @NonNull + Mono> page(); + + /** + * 直接执行sql,根据update count返回更新行数,如果是查询,永远返回0 + * + * @return 更新行数 + */ + Mono execute(); +} diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/select/FetchSqlChain.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/select/FetchSqlChain.java index 2ae8212..0df60d4 100644 --- a/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/select/FetchSqlChain.java +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/chain/select/FetchSqlChain.java @@ -1,42 +1,16 @@ package group.flyfish.fluent.chain.select; import group.flyfish.fluent.chain.common.ExecutableSql; +import group.flyfish.fluent.chain.execution.BoundProxy; import java.util.List; public interface FetchSqlChain extends ExecutableSql { /** - * 执行并获取结果 + * 转换为SQL实体 * - * @param 泛型 - * @return 单一结果值 + * @return 结果 */ - T one(); - - /** - * 执行并获取结果 - * - * @param clazz 结果类 - * @param 泛型 - * @return 单一结果值 - */ - T one(Class clazz); - - /** - * 执行并获取多条结果,以主表class为结果 - * - * @param 结果泛型 - * @return 结果列表 - */ - List list(); - - /** - * 执行并获取多条结果 - * - * @param clazz 结果类 - * @param 结果泛型 - * @return 结果列表 - */ - List list(Class clazz); + BoundProxy as(Class type); } diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/entity/SQLEntity.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/entity/SQLEntity.java index 7505e56..943c483 100644 --- a/fluent-sql-core/src/main/java/group/flyfish/fluent/entity/SQLEntity.java +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/entity/SQLEntity.java @@ -1,38 +1,51 @@ package group.flyfish.fluent.entity; +import lombok.Getter; +import org.springframework.lang.NonNull; + import java.util.function.Supplier; /** * sql运行实体 * + * @param 结果类型泛型 * @author wangyu */ -public class SQLEntity { +public class SQLEntity { private static final Supplier EMPTY_PARAMETERS = () -> new Object[]{}; // sql提供者 - private Supplier sqlProvider; + @NonNull + private final Supplier sql; // sql参数表提供者 - private Supplier parametersProvider; + private final Supplier parameters; - public static SQLEntity of(Supplier sqlProvider) { - return of(sqlProvider, EMPTY_PARAMETERS); + // 结果类型 + @NonNull + @Getter + private final Class resultType; + + private SQLEntity(Class resultType, Supplier sql, Supplier parameters) { + this.resultType = resultType; + this.sql = sql; + this.parameters = parameters; } - public static SQLEntity of(Supplier sqlProvider, Supplier parametersProvider) { - SQLEntity entity = new SQLEntity(); - entity.sqlProvider = sqlProvider; - entity.parametersProvider = parametersProvider; - return entity; + public static SQLEntity of(Class resultType, Supplier sqlProvider) { + return of(resultType, sqlProvider, EMPTY_PARAMETERS); + } + + public static SQLEntity of(Class resultType, Supplier sql, Supplier parameters) { + return new SQLEntity(resultType, sql, parameters); } public String getSql() { - return sqlProvider.get(); + return sql.get(); } public Object[] getParameters() { - return parametersProvider.get(); + return parameters.get(); } } diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/FluentSQLOperations.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/FluentSQLOperations.java index ec971b3..06f8581 100644 --- a/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/FluentSQLOperations.java +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/FluentSQLOperations.java @@ -19,32 +19,29 @@ public interface FluentSQLOperations { * 如果没有结果,将返回null * * @param entity sql实体 - * @param clazz 目标类型 * @param 目标泛型 * @return 查询结果 */ @Nullable - T selectOne(SQLEntity entity, Class clazz); + T selectOne(SQLEntity entity); /** * 执行一条sql,并且查询出所有行 * * @param entity sql实体 - * @param clazz 目标类型 * @param 目标泛型 * @return 返回的列表 */ - List select(SQLEntity entity, Class clazz); + List select(SQLEntity entity); /** * 分页查询 * * @param entity sql实体 - * @param clazz 目标类型 * @param 目标泛型 * @return 返回的分页对象 */ - DataPage selectPage(SQLEntity entity, Class clazz); + DataPage selectPage(SQLEntity entity); /** * 直接执行sql,根据update count返回更新行数,如果是查询,永远返回0 @@ -52,5 +49,5 @@ public interface FluentSQLOperations { * @param entity sql实体 * @return 更新行数 */ - int execute(SQLEntity entity); + int execute(SQLEntity entity); } diff --git a/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/ReactiveFluentSQLOperations.java b/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/ReactiveFluentSQLOperations.java index bf03931..f6568f2 100644 --- a/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/ReactiveFluentSQLOperations.java +++ b/fluent-sql-core/src/main/java/group/flyfish/fluent/operations/ReactiveFluentSQLOperations.java @@ -19,32 +19,29 @@ public interface ReactiveFluentSQLOperations { * 如果没有结果,将返回null * * @param entity sql实体 - * @param clazz 目标类型 * @param 目标泛型 * @return 查询结果 */ @Nullable - Mono selectOne(SQLEntity entity, Class clazz); + Mono selectOne(SQLEntity entity); /** * 执行一条sql,并且查询出所有行 * * @param entity sql实体 - * @param clazz 目标类型 * @param 目标泛型 * @return 返回的列表 */ - Flux select(SQLEntity entity, Class clazz); + Flux select(SQLEntity entity); /** * 分页查询 * * @param entity sql实体 - * @param clazz 目标类型 * @param 目标泛型 * @return 返回的分页对象 */ - Mono> selectPage(SQLEntity entity, Class clazz); + Mono> selectPage(SQLEntity entity); /** * 直接执行sql,根据update count返回更新行数,如果是查询,永远返回0 @@ -52,5 +49,5 @@ public interface ReactiveFluentSQLOperations { * @param entity sql实体 * @return 更新行数 */ - Mono execute(SQLEntity entity); + Mono execute(SQLEntity entity); }