feat: 暂存修改

This commit is contained in:
wangyu 2024-07-11 17:57:20 +08:00
parent 75d302a510
commit a2afbd8753
33 changed files with 341 additions and 63 deletions

View File

@ -33,6 +33,11 @@
<groupId>javax.persistence</groupId> <groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId> <artifactId>persistence-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>

View File

@ -5,6 +5,7 @@ import group.flyfish.fluent.chain.common.HandleSqlChain;
import group.flyfish.fluent.chain.common.PreSqlChain; import group.flyfish.fluent.chain.common.PreSqlChain;
import group.flyfish.fluent.chain.select.AfterOrderSqlChain; import group.flyfish.fluent.chain.select.AfterOrderSqlChain;
import group.flyfish.fluent.chain.select.AfterWhereSqlChain; 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.chain.update.AfterSetSqlChain;
import group.flyfish.fluent.debug.FluentSqlDebugger; import group.flyfish.fluent.debug.FluentSqlDebugger;
import group.flyfish.fluent.entity.SQLEntity; import group.flyfish.fluent.entity.SQLEntity;
@ -39,6 +40,7 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
// 共享的操作 // 共享的操作
private static FluentSQLOperations SHARED_OPERATIONS; private static FluentSQLOperations SHARED_OPERATIONS;
// 参数map有序 // 参数map有序
private final List<Object> parameters = new ArrayList<>(); private final List<Object> parameters = new ArrayList<>();
@ -46,7 +48,7 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
private Class<?> primaryClass; private Class<?> primaryClass;
// sql实体提供者 // sql实体提供者
private final Supplier<SQLEntity> entity = wrap(this::entity); private final Supplier<SQLEntity> entity = wrap(this::toEntity);
/** /**
* 绑定实现类 * 绑定实现类
@ -163,6 +165,7 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
* @param query 条件 * @param query 条件
* @return 结果 * @return 结果
*/ */
@Override
public AfterWhereSqlChain matching(Query query) { public AfterWhereSqlChain matching(Query query) {
if (withoutParameter(query)) return this; if (withoutParameter(query)) return this;
return concat("WHERE").concat(query); return concat("WHERE").concat(query);
@ -195,7 +198,9 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
* @param clazz 结果类 * @param clazz 结果类
* @param <T> 泛型 * @param <T> 泛型
*/ */
@Override
public <T> T one(Class<T> clazz) { public <T> T one(Class<T> clazz) {
limit(1);
return SHARED_OPERATIONS.selectOne(entity.get(), clazz); return SHARED_OPERATIONS.selectOne(entity.get(), clazz);
} }
@ -222,7 +227,17 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
*/ */
@Override @Override
public int execute() { public int execute() {
return SHARED_OPERATIONS.execute(entity()); return SHARED_OPERATIONS.execute(toEntity());
}
/**
* 获取实体做下一步的事情
*
* @return 结果
*/
@Override
public SQLEntity toEntity() {
return SQLEntity.of(wrap(this::sql), wrap(this::parsedParameters));
} }
/** /**
@ -265,13 +280,13 @@ final class SQLImpl extends ConcatSegment<SQLImpl> implements SQLOperations, Pre
return false; return false;
} }
/** @Override
* 将本实体转换为sql实体 public PieceSqlChain limit(int count) {
* return concat("LIMIT").concat(String.valueOf(count));
* @return 转换结果
*/
private SQLEntity entity() {
return SQLEntity.of(wrap(this::sql), wrap(this::parsedParameters));
} }
@Override
public PieceSqlChain offset(int rows) {
return concat("OFFSET").concat(String.valueOf(rows));
}
} }

View File

@ -1,5 +1,7 @@
package group.flyfish.fluent.chain.common; package group.flyfish.fluent.chain.common;
import group.flyfish.fluent.entity.SQLEntity;
/** /**
* 可执行的sql * 可执行的sql
* *
@ -13,4 +15,11 @@ public interface ExecutableSql {
* @return 更新条数 * @return 更新条数
*/ */
int execute(); int execute();
/**
* 转换为SQL实体
*
* @return 结果
*/
SQLEntity toEntity();
} }

View File

@ -1,47 +1,10 @@
package group.flyfish.fluent.chain.select; package group.flyfish.fluent.chain.select;
import group.flyfish.fluent.chain.common.ExecutableSql;
import java.util.List;
/** /**
* order做完后支持的操作 * order做完后支持的操作
* *
* @author wangyu * @author wangyu
*/ */
public interface AfterOrderSqlChain extends ExecutableSql { public interface AfterOrderSqlChain extends PieceSqlChain {
/**
* 执行并获取结果
*
* @param <T> 泛型
* @return 单一结果值
*/
<T> T one();
/**
* 执行并获取结果
*
* @param clazz 结果类
* @param <T> 泛型
* @return 单一结果值
*/
<T> T one(Class<T> clazz);
/**
* 执行并获取多条结果以主表class为结果
*
* @param <T> 结果泛型
* @return 结果列表
*/
<T> List<T> list();
/**
* 执行并获取多条结果
*
* @param clazz 结果类
* @param <T> 结果泛型
* @return 结果列表
*/
<T> List<T> list(Class<T> clazz);
} }

View File

@ -0,0 +1,42 @@
package group.flyfish.fluent.chain.select;
import group.flyfish.fluent.chain.common.ExecutableSql;
import java.util.List;
public interface FetchSqlChain extends ExecutableSql {
/**
* 执行并获取结果
*
* @param <T> 泛型
* @return 单一结果值
*/
<T> T one();
/**
* 执行并获取结果
*
* @param clazz 结果类
* @param <T> 泛型
* @return 单一结果值
*/
<T> T one(Class<T> clazz);
/**
* 执行并获取多条结果以主表class为结果
*
* @param <T> 结果泛型
* @return 结果列表
*/
<T> List<T> list();
/**
* 执行并获取多条结果
*
* @param clazz 结果类
* @param <T> 结果泛型
* @return 结果列表
*/
<T> List<T> list(Class<T> clazz);
}

View File

@ -0,0 +1,25 @@
package group.flyfish.fluent.chain.select;
/**
* 支持分片的sql链
*
* @author wangyu
*/
public interface PieceSqlChain extends FetchSqlChain {
/**
* 限制返回条数
*
* @param count 条数
* @return 结果
*/
PieceSqlChain limit(int count);
/**
* 跳过多少行
*
* @param rows 行数
* @return 结果
*/
PieceSqlChain offset(int rows);
}

View File

@ -0,0 +1,22 @@
package group.flyfish.fluent.entity;
import lombok.Data;
import java.util.List;
/**
* 数据分页
*
* @author wangyu
*/
@Data
public class DataPage<T> {
private List<T> list;
private long total;
private int size = 10;
private int page = 1;
}

View File

@ -1,5 +1,6 @@
package group.flyfish.fluent.operations; package group.flyfish.fluent.operations;
import group.flyfish.fluent.entity.DataPage;
import group.flyfish.fluent.entity.SQLEntity; import group.flyfish.fluent.entity.SQLEntity;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -35,6 +36,16 @@ public interface FluentSQLOperations {
*/ */
<T> List<T> select(SQLEntity entity, Class<T> clazz); <T> List<T> select(SQLEntity entity, Class<T> clazz);
/**
* 分页查询
*
* @param entity sql实体
* @param clazz 目标类型
* @param <T> 目标泛型
* @return 返回的分页对象
*/
<T> DataPage<T> selectPage(SQLEntity entity, Class<T> clazz);
/** /**
* 直接执行sql根据update count返回更新行数如果是查询永远返回0 * 直接执行sql根据update count返回更新行数如果是查询永远返回0
* *

View File

@ -0,0 +1,56 @@
package group.flyfish.fluent.operations;
import group.flyfish.fluent.entity.DataPage;
import group.flyfish.fluent.entity.SQLEntity;
import org.springframework.lang.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* sql query操作
*
* @author wangyu
*/
public interface ReactiveFluentSQLOperations {
/**
* 执行一条sql并且序列化为对象
* 注意如果查询不止一条该方法仅返回第一条数据
* 如果没有结果将返回null
*
* @param entity sql实体
* @param clazz 目标类型
* @param <T> 目标泛型
* @return 查询结果
*/
@Nullable
<T> Mono<T> selectOne(SQLEntity entity, Class<T> clazz);
/**
* 执行一条sql并且查询出所有行
*
* @param entity sql实体
* @param clazz 目标类型
* @param <T> 目标泛型
* @return 返回的列表
*/
<T> Flux<T> select(SQLEntity entity, Class<T> clazz);
/**
* 分页查询
*
* @param entity sql实体
* @param clazz 目标类型
* @param <T> 目标泛型
* @return 返回的分页对象
*/
<T> Mono<DataPage<T>> selectPage(SQLEntity entity, Class<T> clazz);
/**
* 直接执行sql根据update count返回更新行数如果是查询永远返回0
*
* @param entity sql实体
* @return 更新行数
*/
Mono<Integer> execute(SQLEntity entity);
}

View File

@ -24,7 +24,17 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>group.flyfish.framework</groupId> <groupId>group.flyfish.framework</groupId>
<artifactId>fluent-sql-spring-jdbc</artifactId> <artifactId>fluent-sql-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-r2dbc</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<optional>true</optional>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,13 +1,20 @@
package group.flyfish.fluent.autoconfigure; package group.flyfish.fluent.autoconfigure;
import group.flyfish.fluent.autoconfigure.FluentSqlAutoConfiguration.JdbcFluentSqlAutoConfigure;
import group.flyfish.fluent.autoconfigure.FluentSqlAutoConfiguration.R2dbcFluentSqlAutoConfigure;
import group.flyfish.fluent.operations.FluentSQLOperations; import group.flyfish.fluent.operations.FluentSQLOperations;
import group.flyfish.fluent.operations.JdbcTemplateFluentSQLOperations; import group.flyfish.fluent.operations.JdbcTemplateFluentSQLOperations;
import group.flyfish.fluent.operations.R2dbcFluentSQLOperations;
import group.flyfish.fluent.operations.ReactiveFluentSQLOperations;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.r2dbc.core.DatabaseClient;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -17,8 +24,28 @@ import javax.sql.DataSource;
* @author wangyu * @author wangyu
*/ */
@AutoConfigureAfter(DataSourceAutoConfiguration.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class)
@Import({R2dbcFluentSqlAutoConfigure.class, JdbcFluentSqlAutoConfigure.class})
public class FluentSqlAutoConfiguration { public class FluentSqlAutoConfiguration {
@ConditionalOnClass(DatabaseClient.class)
static class R2dbcFluentSqlAutoConfigure {
/**
* 动态注入初始化的bean完成注入配置
*
* @param databaseClient 从spring r2dbc注入
*/
@Bean
@ConditionalOnMissingBean(ReactiveFluentSQLOperations.class)
@ConditionalOnBean(DatabaseClient.class)
public ReactiveFluentSQLOperations fluentSQLOperations(DatabaseClient databaseClient) {
return new R2dbcFluentSQLOperations(databaseClient);
}
}
@ConditionalOnClass(DataSource.class)
static class JdbcFluentSqlAutoConfigure {
/** /**
* 动态注入初始化的bean完成注入配置 * 动态注入初始化的bean完成注入配置
* *
@ -30,4 +57,5 @@ public class FluentSqlAutoConfiguration {
public FluentSQLOperations fluentSQLOperations(DataSource dataSource) { public FluentSQLOperations fluentSQLOperations(DataSource dataSource) {
return new JdbcTemplateFluentSQLOperations(new JdbcTemplate(dataSource)); return new JdbcTemplateFluentSQLOperations(new JdbcTemplate(dataSource));
} }
}
} }

View File

@ -9,7 +9,7 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>fluent-sql-spring-jdbc</artifactId> <artifactId>fluent-sql-spring</artifactId>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
@ -24,6 +24,12 @@
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId> <artifactId>spring-jdbc</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-r2dbc</artifactId>
<optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@ -1,6 +1,7 @@
package group.flyfish.fluent.operations; package group.flyfish.fluent.operations;
import group.flyfish.fluent.chain.SQL; import group.flyfish.fluent.chain.SQL;
import group.flyfish.fluent.entity.DataPage;
import group.flyfish.fluent.entity.SQLEntity; import group.flyfish.fluent.entity.SQLEntity;
import group.flyfish.fluent.mapping.SQLMappedRowMapper; import group.flyfish.fluent.mapping.SQLMappedRowMapper;
import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.EmptyResultDataAccessException;
@ -41,7 +42,7 @@ public class JdbcTemplateFluentSQLOperations implements FluentSQLOperations {
@SuppressWarnings("all") @SuppressWarnings("all")
public <T> T selectOne(SQLEntity entity, Class<T> clazz) { public <T> T selectOne(SQLEntity entity, Class<T> clazz) {
try { try {
String sql = entity.getSql().concat(" limit 1"); String sql = entity.getSql();
if (ClassUtils.isPrimitiveOrWrapper(clazz)) { if (ClassUtils.isPrimitiveOrWrapper(clazz)) {
return jdbcOperations.queryForObject(sql, clazz, entity.getParameters()); return jdbcOperations.queryForObject(sql, clazz, entity.getParameters());
} }
@ -67,6 +68,18 @@ public class JdbcTemplateFluentSQLOperations implements FluentSQLOperations {
return jdbcOperations.query(sql, new SQLMappedRowMapper<>(clazz), entity.getParameters()); return jdbcOperations.query(sql, new SQLMappedRowMapper<>(clazz), entity.getParameters());
} }
/**
* 分页查询
*
* @param entity sql实体
* @param clazz 目标类型
* @return 返回的分页对象
*/
@Override
public <T> DataPage<T> selectPage(SQLEntity entity, Class<T> clazz) {
return null;
}
/** /**
* 直接执行sql根据update count返回更新行数如果是查询永远返回0 * 直接执行sql根据update count返回更新行数如果是查询永远返回0
* *

View File

@ -0,0 +1,63 @@
package group.flyfish.fluent.operations;
import group.flyfish.fluent.entity.DataPage;
import group.flyfish.fluent.entity.SQLEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.r2dbc.core.DatabaseClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RequiredArgsConstructor
public class R2dbcFluentSQLOperations implements ReactiveFluentSQLOperations {
private final DatabaseClient databaseClient;
/**
* 执行一条sql并且序列化为对象
* 注意如果查询不止一条该方法仅返回第一条数据
* 如果没有结果将返回null
*
* @param entity sql实体
* @param clazz 目标类型
* @return 查询结果
*/
@Override
public <T> Mono<T> selectOne(SQLEntity entity, Class<T> clazz) {
return null;
}
/**
* 执行一条sql并且查询出所有行
*
* @param entity sql实体
* @param clazz 目标类型
* @return 返回的列表
*/
@Override
public <T> Flux<T> select(SQLEntity entity, Class<T> clazz) {
return null;
}
/**
* 分页查询
*
* @param entity sql实体
* @param clazz 目标类型
* @return 返回的分页对象
*/
@Override
public <T> Mono<DataPage<T>> selectPage(SQLEntity entity, Class<T> clazz) {
return null;
}
/**
* 直接执行sql根据update count返回更新行数如果是查询永远返回0
*
* @param entity sql实体
* @return 更新行数
*/
@Override
public Mono<Integer> execute(SQLEntity entity) {
return null;
}
}

View File

@ -35,7 +35,7 @@ public class FluentJdbcTest {
public void testSql() throws SQLException, JsonProcessingException { public void testSql() throws SQLException, JsonProcessingException {
DataSource dataSource = new SimpleDriverDataSource( DataSource dataSource = new SimpleDriverDataSource(
new Driver(), new Driver(),
"jdbc:mysql://127.0.0.1:3306/epi_project?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=Asia/Shanghai", "jdbc:mysql://127.0.0.1:3306/epi_project?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true",
"root", "root",
"Unicom#2018" // "oI3WtMO8h%mSYARp" "Unicom#2018" // "oI3WtMO8h%mSYARp"
); );

14
pom.xml
View File

@ -29,7 +29,7 @@
<modules> <modules>
<module>fluent-sql-core</module> <module>fluent-sql-core</module>
<module>fluent-sql-spring-jdbc</module> <module>fluent-sql-spring</module>
<module>fluent-sql-annotations</module> <module>fluent-sql-annotations</module>
<module>fluent-sql-spring-boot-starter</module> <module>fluent-sql-spring-boot-starter</module>
</modules> </modules>
@ -88,7 +88,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>group.flyfish.framework</groupId> <groupId>group.flyfish.framework</groupId>
<artifactId>fluent-sql-spring-jdbc</artifactId> <artifactId>fluent-sql-spring</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -121,6 +121,11 @@
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-r2dbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.mybatis</groupId> <groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId> <artifactId>mybatis</artifactId>
@ -153,6 +158,11 @@
<version>8.0.29</version> <version>8.0.29</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.6.7</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>