mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-04 15:28:20 +08:00
[v0.2.6] 版本更新
- [U] 优化 ConditionalBuilder 的使用方式。 - [A] 为 TableQueryBuilder 添加分表查询limit方法。 - [A] 添加 SQLFunction 类,用于对SQL结果直接进行处理,且不需要在方法内处理异常,交由 ExceptionHandler 进行处理。
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<version>0.2.5</version>
|
<version>0.2.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.api;
|
package cc.carm.lib.easysql.api;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -7,13 +8,12 @@ import java.sql.SQLException;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SQLAction 是用于承载SQL语句并进行处理、返回的基本类。
|
* SQLAction 是用于承载SQL语句并进行处理、返回的基本类。
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>同步执行 {@link #execute()}, {@link #execute(Function, BiConsumer)}
|
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, BiConsumer)}
|
||||||
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
||||||
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
||||||
*
|
*
|
||||||
@@ -23,8 +23,6 @@ import java.util.function.Function;
|
|||||||
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <b>注意: 无论是否异步,都不需要自行关闭ResultSet,本API已自动关闭</b>
|
|
||||||
*
|
|
||||||
* @param <T> 需要返回的类型
|
* @param <T> 需要返回的类型
|
||||||
* @author CarmJos
|
* @author CarmJos
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
@@ -74,20 +72,6 @@ public interface SQLAction<T> {
|
|||||||
*/
|
*/
|
||||||
@NotNull T execute() throws SQLException;
|
@NotNull T execute() throws SQLException;
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行语句并处理返回值
|
|
||||||
*
|
|
||||||
* @param function 处理方法
|
|
||||||
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
|
|
||||||
* @param <R> 需要返回的内容
|
|
||||||
* @return 指定类型数据
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
default <R> R execute(@NotNull Function<T, R> function, @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
|
||||||
T value = execute(exceptionHandler);
|
|
||||||
if (value == null) return null;
|
|
||||||
else return function.apply(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行语句并返回值
|
* 执行语句并返回值
|
||||||
@@ -97,14 +81,44 @@ public interface SQLAction<T> {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||||
if (exceptionHandler == null) exceptionHandler = defaultExceptionHandler();
|
return execute(t -> t, exceptionHandler);
|
||||||
T value = null;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行语句并处理返回值
|
||||||
|
*
|
||||||
|
* @param function 处理方法
|
||||||
|
* @param <R> 需要返回的内容
|
||||||
|
* @return 指定类型数据
|
||||||
|
* @throws SQLException 当SQL操作出现问题时抛出
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException {
|
||||||
try {
|
try {
|
||||||
value = execute();
|
T value = execute();
|
||||||
|
return function.apply(value);
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
exceptionHandler.accept(exception, this);
|
throw new SQLException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行语句并处理返回值
|
||||||
|
*
|
||||||
|
* @param function 处理方法
|
||||||
|
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
|
||||||
|
* @param <R> 需要返回的内容
|
||||||
|
* @return 指定类型数据
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
||||||
|
@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||||
|
try {
|
||||||
|
return executeFunction(function);
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
handleException(exceptionHandler, exception);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,6 +145,12 @@ public interface SQLAction<T> {
|
|||||||
*/
|
*/
|
||||||
void executeAsync(@Nullable Consumer<T> success, @Nullable BiConsumer<SQLException, SQLAction<T>> failure);
|
void executeAsync(@Nullable Consumer<T> success, @Nullable BiConsumer<SQLException, SQLAction<T>> failure);
|
||||||
|
|
||||||
|
|
||||||
|
default void handleException(@Nullable BiConsumer<SQLException, SQLAction<T>> handler, SQLException exception) {
|
||||||
|
if (handler == null) handler = defaultExceptionHandler();
|
||||||
|
handler.accept(exception, this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return 默认的异常处理器
|
* @return 默认的异常处理器
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,7 +1,51 @@
|
|||||||
package cc.carm.lib.easysql.api.action.query;
|
package cc.carm.lib.easysql.api.action.query;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import cc.carm.lib.easysql.api.SQLAction;
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQLQueryAction 是用于承载SQL查询语句并进行处理、返回并自动关闭连接的基本类。
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, BiConsumer)}
|
||||||
|
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
||||||
|
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
||||||
|
*
|
||||||
|
* <li>异步执行 {@link #executeAsync(Consumer, BiConsumer)}
|
||||||
|
* <br>异步执行时将提供成功与异常两种处理方式
|
||||||
|
* <br>可自行选择是否对数据或异常进行处理
|
||||||
|
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <b>注意: 无论是否异步,都不需要自行关闭ResultSet,本API已自动关闭</b>
|
||||||
|
*
|
||||||
|
* @author CarmJos
|
||||||
|
* @since 0.2.6
|
||||||
|
*/
|
||||||
public interface QueryAction extends SQLAction<SQLQuery> {
|
public interface QueryAction extends SQLAction<SQLQuery> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行语句并处理返回值
|
||||||
|
*
|
||||||
|
* @param function 处理方法
|
||||||
|
* @param <R> 需要返回的内容
|
||||||
|
* @return 指定类型数据
|
||||||
|
* @throws SQLException 当SQL操作出现问题时抛出
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
default <R> R executeFunction(@NotNull SQLFunction<SQLQuery, R> function)
|
||||||
|
throws SQLException {
|
||||||
|
try (SQLQuery value = execute()) {
|
||||||
|
return function.apply(value);
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
throw new SQLException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package cc.carm.lib.easysql.api.function;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public interface SQLFunction<T, R> {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
R apply(T t) throws SQLException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,14 +5,14 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.2.5</version>
|
<version>0.2.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>easysql-beecp</artifactId>
|
<artifactId>easysql-beecp</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>10-EasySQL-BeeCP</name>
|
<name>12-EasySQL-BeeCP</name>
|
||||||
<description>EasySQL的应用部分。此为BeeCP版本。</description>
|
<description>EasySQL的应用部分。此为BeeCP版本。</description>
|
||||||
<url>https://github.com/CarmJos/EasySQL</url>
|
<url>https://github.com/CarmJos/EasySQL</url>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.2.5</version>
|
<version>0.2.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -49,16 +49,11 @@ public class EasySQLDemo {
|
|||||||
.build().execute(query -> {
|
.build().execute(query -> {
|
||||||
//可以直接进行数据处理
|
//可以直接进行数据处理
|
||||||
ResultSet result = query.getResultSet();
|
ResultSet result = query.getResultSet();
|
||||||
try {
|
return result.next() ? UUIDUtil.toUUID(result.getString("uuid")) : null;
|
||||||
if (result != null && result.next()) {
|
|
||||||
return UUIDUtil.toUUID(result.getString("uuid"));
|
|
||||||
}
|
|
||||||
} catch (SQLException ignored) {
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}, (exception, action) -> {
|
}, (exception, action) -> {
|
||||||
// 处理异常,不想处理直接填null
|
// 处理异常,不想处理直接填null
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sqlQueryAsync(SQLManager sqlManager) {
|
public void sqlQueryAsync(SQLManager sqlManager) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.2.5</version>
|
<version>0.2.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.2.5</version>
|
<version>0.2.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>0.2.5</version>
|
<version>0.2.6</version>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>easysql-api</module>
|
<module>easysql-api</module>
|
||||||
|
|||||||
Reference in New Issue
Block a user