mirror of
https://github.com/CarmJos/EasySQL.git
synced 2024-09-19 21:35:47 +00:00
feat(function): 添加 andThen 与 compose 方法
。
This commit is contained in:
parent
298a5c4e81
commit
4be85f5481
@ -17,6 +17,7 @@ public interface PreparedSQLUpdateAction<T extends Number> extends SQLUpdateActi
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedSQLUpdateAction}
|
||||
* @since 0.4.0
|
||||
*/
|
||||
PreparedSQLUpdateAction<T> setParams(@Nullable Iterable<Object> params);
|
||||
|
||||
|
@ -35,6 +35,7 @@ public interface PreparedSQLUpdateBatchAction<T extends Number> extends SQLActio
|
||||
* @param keyTypeClass 自增序列的数字类型
|
||||
* @param <N> 自增键序列类型 {@link Number}
|
||||
* @return {@link SQLUpdateAction}
|
||||
* @since 0.4.0
|
||||
*/
|
||||
<N extends Number> PreparedSQLUpdateBatchAction<N> returnGeneratedKeys(Class<N> keyTypeClass);
|
||||
|
||||
|
@ -18,6 +18,7 @@ public interface SQLUpdateAction<T extends Number> extends SQLAction<T> {
|
||||
* @param keyTypeClass 自增序列的数字类型
|
||||
* @param <N> 自增键序列类型 {@link Number}
|
||||
* @return {@link SQLUpdateAction}
|
||||
* @since 0.4.0
|
||||
*/
|
||||
<N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass);
|
||||
|
||||
|
@ -4,6 +4,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface SQLFunction<T, R> {
|
||||
@ -11,4 +12,23 @@ public interface SQLFunction<T, R> {
|
||||
@Nullable
|
||||
R apply(@NotNull T t) throws SQLException;
|
||||
|
||||
default <V> SQLFunction<V, R> compose(@NotNull SQLFunction<? super V, ? extends T> before) {
|
||||
Objects.requireNonNull(before);
|
||||
return (V v) -> {
|
||||
T t = before.apply(v);
|
||||
if (t == null) return null;
|
||||
else return apply(t);
|
||||
};
|
||||
}
|
||||
|
||||
default <V> SQLFunction<T, V> then(@NotNull SQLFunction<? super R, ? extends V> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (T t) -> {
|
||||
R r = apply(t);
|
||||
if (r == null) return null;
|
||||
else return after.apply(r);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,23 +6,38 @@ import cc.carm.lib.easysql.tests.*;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class EasySQLTest {
|
||||
|
||||
protected SQLManager sqlManager;
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
@Before
|
||||
public void initDatabase() {
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setDriverClassName("org.h2.Driver");
|
||||
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
|
||||
|
||||
SQLManager sqlManager = new SQLManagerImpl(new HikariDataSource(config), "test");
|
||||
sqlManager.setDebugMode(true);
|
||||
this.sqlManager = new SQLManagerImpl(new HikariDataSource(config), "test");
|
||||
this.sqlManager.setDebugMode(true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void shutdownDatabase() {
|
||||
if (sqlManager.getDataSource() instanceof HikariDataSource) {
|
||||
//Close bee connection pool
|
||||
((HikariDataSource) sqlManager.getDataSource()).close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
|
||||
print("加载测试类...");
|
||||
Set<TestHandler> tests = new LinkedHashSet<>();
|
||||
@ -62,10 +77,6 @@ public class EasySQLTest {
|
||||
success, (tests.size() - success)
|
||||
);
|
||||
|
||||
if (sqlManager.getDataSource() instanceof HikariDataSource) {
|
||||
//Close bee connection pool
|
||||
((HikariDataSource) sqlManager.getDataSource()).close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user