feat: 完成r2dbc的支持

This commit is contained in:
wangyu 2024-07-13 00:48:49 +08:00
parent 8cdfe187d3
commit 53b74f194b
5 changed files with 94 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package group.flyfish.fluent.chain;
import group.flyfish.fluent.chain.common.PreSqlChain;
import group.flyfish.fluent.chain.select.SelectComposite;
import group.flyfish.fluent.operations.FluentSQLOperations;
import group.flyfish.fluent.operations.ReactiveFluentSQLOperations;
import group.flyfish.fluent.update.Update;
import group.flyfish.fluent.utils.sql.SFunction;
@ -61,4 +62,13 @@ public interface SQL {
static void bind(FluentSQLOperations operations) {
SQLImpl.bind(operations);
}
/**
* 绑定数据源上下文可自由切换实现
*
* @param operations jdbc操作
*/
static void bind(ReactiveFluentSQLOperations operations) {
SQLImpl.bind(operations);
}
}

View File

@ -236,7 +236,6 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
* @return 构建结果
*/
private String sql() {
Assert.notNull(SHARED_OPERATIONS, "未指定执行数据源!");
String sql = segments.stream().map(SQLSegment::get).collect(Collectors.joining(" "));
if (FluentSqlDebugger.enabled()) {
System.out.println("prepared sql: " + sql);
@ -310,11 +309,15 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
*
* @param <T>
*/
@RequiredArgsConstructor
private static class DefaultBoundEntitySpec<T> implements BoundEntitySpec<T> {
private final BoundSQLEntity<T> entity;
private DefaultBoundEntitySpec(BoundSQLEntity<T> entity) {
Assert.notNull(SHARED_OPERATIONS, "未指定执行数据源!");
this.entity = entity;
}
@Override
public T one() {
return SHARED_OPERATIONS.selectOne(entity);
@ -341,11 +344,15 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
*
* @param <T> 泛型
*/
@RequiredArgsConstructor
private static class DefaultReactiveBoundEntitySpec<T> implements ReactiveBoundEntitySpec<T> {
private final BoundSQLEntity<T> entity;
private DefaultReactiveBoundEntitySpec(BoundSQLEntity<T> entity) {
Assert.notNull(SHARED_REACTIVE_OPERATIONS, "未指定执行数据源!");
this.entity = entity;
}
@Override
public Mono<T> one() {
return SHARED_REACTIVE_OPERATIONS.selectOne(entity);

View File

@ -46,6 +46,12 @@
<artifactId>mysql-connector-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.asyncer</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>0.9.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>

View File

@ -1,19 +1,23 @@
package group.flyfish.fluent.operations;
import group.flyfish.fluent.chain.SQL;
import group.flyfish.fluent.entity.BoundSQLEntity;
import group.flyfish.fluent.entity.DataPage;
import group.flyfish.fluent.mapping.ReactiveSQLMappedRowMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.r2dbc.core.RowsFetchSpec;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RequiredArgsConstructor
public class R2dbcFluentSQLOperations implements ReactiveFluentSQLOperations {
private final DatabaseClient databaseClient;
public R2dbcFluentSQLOperations(DatabaseClient databaseClient) {
this.databaseClient = databaseClient;
SQL.bind(this);
}
/**
* 执行一条sql并且序列化为对象
* 注意如果查询不止一条该方法仅返回第一条数据

View File

@ -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());
}
}