feat: 完成r2dbc的支持
This commit is contained in:
parent
8cdfe187d3
commit
53b74f194b
@ -3,6 +3,7 @@ package group.flyfish.fluent.chain;
|
|||||||
import group.flyfish.fluent.chain.common.PreSqlChain;
|
import group.flyfish.fluent.chain.common.PreSqlChain;
|
||||||
import group.flyfish.fluent.chain.select.SelectComposite;
|
import group.flyfish.fluent.chain.select.SelectComposite;
|
||||||
import group.flyfish.fluent.operations.FluentSQLOperations;
|
import group.flyfish.fluent.operations.FluentSQLOperations;
|
||||||
|
import group.flyfish.fluent.operations.ReactiveFluentSQLOperations;
|
||||||
import group.flyfish.fluent.update.Update;
|
import group.flyfish.fluent.update.Update;
|
||||||
import group.flyfish.fluent.utils.sql.SFunction;
|
import group.flyfish.fluent.utils.sql.SFunction;
|
||||||
|
|
||||||
@ -61,4 +62,13 @@ public interface SQL {
|
|||||||
static void bind(FluentSQLOperations operations) {
|
static void bind(FluentSQLOperations operations) {
|
||||||
SQLImpl.bind(operations);
|
SQLImpl.bind(operations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定数据源上下文,可自由切换实现
|
||||||
|
*
|
||||||
|
* @param operations jdbc操作
|
||||||
|
*/
|
||||||
|
static void bind(ReactiveFluentSQLOperations operations) {
|
||||||
|
SQLImpl.bind(operations);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,6 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
|
|||||||
* @return 构建结果
|
* @return 构建结果
|
||||||
*/
|
*/
|
||||||
private String sql() {
|
private String sql() {
|
||||||
Assert.notNull(SHARED_OPERATIONS, "未指定执行数据源!");
|
|
||||||
String sql = segments.stream().map(SQLSegment::get).collect(Collectors.joining(" "));
|
String sql = segments.stream().map(SQLSegment::get).collect(Collectors.joining(" "));
|
||||||
if (FluentSqlDebugger.enabled()) {
|
if (FluentSqlDebugger.enabled()) {
|
||||||
System.out.println("prepared sql: " + sql);
|
System.out.println("prepared sql: " + sql);
|
||||||
@ -310,11 +309,15 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
|
|||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
|
||||||
private static class DefaultBoundEntitySpec<T> implements BoundEntitySpec<T> {
|
private static class DefaultBoundEntitySpec<T> implements BoundEntitySpec<T> {
|
||||||
|
|
||||||
private final BoundSQLEntity<T> entity;
|
private final BoundSQLEntity<T> entity;
|
||||||
|
|
||||||
|
private DefaultBoundEntitySpec(BoundSQLEntity<T> entity) {
|
||||||
|
Assert.notNull(SHARED_OPERATIONS, "未指定执行数据源!");
|
||||||
|
this.entity = entity;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T one() {
|
public T one() {
|
||||||
return SHARED_OPERATIONS.selectOne(entity);
|
return SHARED_OPERATIONS.selectOne(entity);
|
||||||
@ -341,11 +344,15 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
|
|||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
|
||||||
private static class DefaultReactiveBoundEntitySpec<T> implements ReactiveBoundEntitySpec<T> {
|
private static class DefaultReactiveBoundEntitySpec<T> implements ReactiveBoundEntitySpec<T> {
|
||||||
|
|
||||||
private final BoundSQLEntity<T> entity;
|
private final BoundSQLEntity<T> entity;
|
||||||
|
|
||||||
|
private DefaultReactiveBoundEntitySpec(BoundSQLEntity<T> entity) {
|
||||||
|
Assert.notNull(SHARED_REACTIVE_OPERATIONS, "未指定执行数据源!");
|
||||||
|
this.entity = entity;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<T> one() {
|
public Mono<T> one() {
|
||||||
return SHARED_REACTIVE_OPERATIONS.selectOne(entity);
|
return SHARED_REACTIVE_OPERATIONS.selectOne(entity);
|
||||||
|
@ -46,6 +46,12 @@
|
|||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.asyncer</groupId>
|
||||||
|
<artifactId>r2dbc-mysql</artifactId>
|
||||||
|
<version>0.9.7</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
package group.flyfish.fluent.operations;
|
package group.flyfish.fluent.operations;
|
||||||
|
|
||||||
|
import group.flyfish.fluent.chain.SQL;
|
||||||
import group.flyfish.fluent.entity.BoundSQLEntity;
|
import group.flyfish.fluent.entity.BoundSQLEntity;
|
||||||
import group.flyfish.fluent.entity.DataPage;
|
import group.flyfish.fluent.entity.DataPage;
|
||||||
import group.flyfish.fluent.mapping.ReactiveSQLMappedRowMapper;
|
import group.flyfish.fluent.mapping.ReactiveSQLMappedRowMapper;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.r2dbc.core.DatabaseClient;
|
import org.springframework.r2dbc.core.DatabaseClient;
|
||||||
import org.springframework.r2dbc.core.RowsFetchSpec;
|
import org.springframework.r2dbc.core.RowsFetchSpec;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class R2dbcFluentSQLOperations implements ReactiveFluentSQLOperations {
|
public class R2dbcFluentSQLOperations implements ReactiveFluentSQLOperations {
|
||||||
|
|
||||||
private final DatabaseClient databaseClient;
|
private final DatabaseClient databaseClient;
|
||||||
|
|
||||||
|
public R2dbcFluentSQLOperations(DatabaseClient databaseClient) {
|
||||||
|
this.databaseClient = databaseClient;
|
||||||
|
SQL.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行一条sql,并且序列化为对象
|
* 执行一条sql,并且序列化为对象
|
||||||
* 注意,如果查询不止一条,该方法仅返回第一条数据
|
* 注意,如果查询不止一条,该方法仅返回第一条数据
|
||||||
|
@ -0,0 +1,62 @@
|
|||||||
|
package group.flyfish.framework;
|
||||||
|
|
||||||
|
import group.flyfish.fluent.chain.select.FetchSqlChain;
|
||||||
|
import group.flyfish.fluent.operations.R2dbcFluentSQLOperations;
|
||||||
|
import group.flyfish.framework.entity.SaasOrder;
|
||||||
|
import group.flyfish.framework.entity.SaasPlan;
|
||||||
|
import group.flyfish.framework.entity.SaasTenant;
|
||||||
|
import group.flyfish.framework.vo.TenantContext;
|
||||||
|
import io.asyncer.r2dbc.mysql.MySqlConnectionConfiguration;
|
||||||
|
import io.asyncer.r2dbc.mysql.MySqlConnectionFactory;
|
||||||
|
import io.r2dbc.spi.ConnectionFactory;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.r2dbc.core.DatabaseClient;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import static group.flyfish.fluent.chain.SQL.select;
|
||||||
|
import static group.flyfish.fluent.chain.select.SelectComposite.composite;
|
||||||
|
import static group.flyfish.fluent.query.Query.where;
|
||||||
|
|
||||||
|
public class FluentR2dbcTest {
|
||||||
|
|
||||||
|
private FetchSqlChain sql;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 静态测试demo
|
||||||
|
* 实际测试请根据自己的数据库字段书写实体
|
||||||
|
*
|
||||||
|
* @throws SQLException sql异常
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testSql() {
|
||||||
|
MySqlConnectionConfiguration configuration = MySqlConnectionConfiguration.builder()
|
||||||
|
.host("localhost")
|
||||||
|
.port(3306)
|
||||||
|
.database("epi_project")
|
||||||
|
.user("root")
|
||||||
|
.password("Unicom#2018")
|
||||||
|
.build();
|
||||||
|
ConnectionFactory connectionFactory = MySqlConnectionFactory.from(configuration);
|
||||||
|
DatabaseClient databaseClient = DatabaseClient.builder().connectionFactory(connectionFactory)
|
||||||
|
.build();
|
||||||
|
new R2dbcFluentSQLOperations(databaseClient);
|
||||||
|
|
||||||
|
// 缓存构建结果
|
||||||
|
this.sql = select(
|
||||||
|
// 查询租户全量字段
|
||||||
|
composite(SaasTenant::getId, SaasTenant::getName, SaasTenant::getIdentifier, SaasTenant::getDatasource,
|
||||||
|
SaasTenant::getStorage, SaasTenant::getStatus, SaasTenant::getEnable),
|
||||||
|
// 查询套餐
|
||||||
|
composite(SaasOrder::getQuotaConfig, SaasOrder::getOrderTime, SaasOrder::getExpireTime,
|
||||||
|
SaasOrder::getOrderType))
|
||||||
|
.from(SaasTenant.class)
|
||||||
|
.leftJoin(SaasOrder.class).on(where(SaasOrder::getTenantId).eq(SaasTenant::getId))
|
||||||
|
.leftJoin(SaasPlan.class).on(where(SaasPlan::getId).eq(SaasOrder::getPlanId))
|
||||||
|
.matching(where(SaasTenant::getEnable).eq(true));
|
||||||
|
|
||||||
|
System.out.println(sql.as(TenantContext.class).reactive().all()
|
||||||
|
.collectList()
|
||||||
|
.block());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user