1
mirror of https://github.com/CarmJos/EasySQL.git synced 2026-06-05 09:01:26 +08:00

Compare commits

...

4 Commits

15 changed files with 163 additions and 64 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<version>0.2.8</version>
<version>0.2.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -3,6 +3,8 @@ package cc.carm.lib.easysql.api;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import cc.carm.lib.easysql.api.function.SQLFunction;
import cc.carm.lib.easysql.api.function.SQLHandler;
import cc.carm.lib.easysql.api.function.impl.DefaultSQLExceptionHandler;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -20,7 +22,8 @@ import java.util.UUID;
* <li>异步执行 {@link #executeAsync(SQLHandler, SQLExceptionHandler)}
* <br>异步执行时将提供成功与异常两种处理方式
* <br>可自行选择是否对数据或异常进行处理
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}
* <br>若有特殊需要,可通过{@link #setExceptionHandler(SQLExceptionHandler)} 方法修改默认的处理器</li>
* </ul>
*
* @param <T> 需要返回的类型
@@ -87,19 +90,15 @@ public interface SQLAction<T> {
/**
* 执行语句并处理返回值
*
* @param function 处理方法
* @param <R> 需要返回的内容
* @param function 处理方法
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
* @param <R> 需要返回的内容
* @return 指定类型数据
* @throws SQLException 当SQL操作出现问题时抛出
*/
@Nullable
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException {
try {
T value = execute();
return function.apply(value);
} catch (SQLException exception) {
throw new SQLException(exception);
}
default <R> R execute(@NotNull SQLFunction<T, R> function,
@Nullable SQLExceptionHandler exceptionHandler) {
return execute(function, null, exceptionHandler);
}
/**
@@ -111,16 +110,53 @@ public interface SQLAction<T> {
* @return 指定类型数据
*/
@Nullable
@Contract("_,!null,_ -> !null")
default <R> R execute(@NotNull SQLFunction<T, R> function,
@Nullable R defaultResult,
@Nullable SQLExceptionHandler exceptionHandler) {
try {
return executeFunction(function);
return executeFunction(function, defaultResult);
} catch (SQLException exception) {
handleException(exceptionHandler, exception);
return null;
}
}
/**
* 执行语句并处理返回值
*
* @param function 处理方法
* @param <R> 需要返回的内容
* @return 指定类型数据
* @throws SQLException 当SQL操作出现问题时抛出
*/
@Nullable
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException {
return executeFunction(function, null);
}
/**
* 执行语句并处理返回值
*
* @param defaultResult 默认结果,若处理后的结果为null,则返回该值
* @param function 处理方法
* @param <R> 需要返回的内容
* @return 指定类型数据
* @throws SQLException 当SQL操作出现问题时抛出
*/
@Nullable
@Contract("_,!null -> !null")
default <R> R executeFunction(@NotNull SQLFunction<T, R> function,
@Nullable R defaultResult) throws SQLException {
try {
T value = execute();
R result = function.apply(value);
return result == null ? defaultResult : result;
} catch (SQLException exception) {
throw new SQLException(exception);
}
}
/**
* 异步执行SQL语句,采用默认异常处理,无需返回值。
*/
@@ -155,12 +191,18 @@ public interface SQLAction<T> {
* @return 默认的异常处理器
*/
default SQLExceptionHandler defaultExceptionHandler() {
return (exception, action) -> {
getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
getManager().getLogger().severe(exception.getLocalizedMessage());
exception.printStackTrace();
};
return DefaultSQLExceptionHandler.get(getManager().getLogger());
}
/**
* 设定通用的异常处理器。
* <br> 在使用 {@link #execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。
* <br> 若该方法传入参数为 null,则会使用 {@link #defaultExceptionHandler()} 。
*
* @param handler 异常处理器
*/
default void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
DefaultSQLExceptionHandler.setCustomHandler(handler);
}
}
@@ -16,6 +16,10 @@ public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
*/
SQLUpdateBatchAction addBatch(@NotNull String sql);
default @NotNull String getSQLContent() {
return getSQLContents().get(0);
}
List<String> getSQLContents();
@Override
@@ -5,6 +5,7 @@ import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import cc.carm.lib.easysql.api.function.SQLFunction;
import cc.carm.lib.easysql.api.function.SQLHandler;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -34,16 +35,20 @@ public interface QueryAction extends SQLAction<SQLQuery> {
/**
* 执行语句并处理返回值
*
* @param function 处理方法
* @param <R> 需要返回的内容
* @param defaultResult 默认结果,若处理后的结果为null,则返回该值
* @param function 处理方法
* @param <R> 需要返回的内容
* @return 指定类型数据
* @throws SQLException 当SQL操作出现问题时抛出
*/
@Nullable
default <R> R executeFunction(@NotNull SQLFunction<SQLQuery, R> function)
throws SQLException {
@Contract("!null, _ -> !null")
default <R> R executeFunction(@Nullable R defaultResult,
@NotNull SQLFunction<SQLQuery, R> function) throws SQLException {
try (SQLQuery value = execute()) {
return function.apply(value);
R result = function.apply(value);
return result == null ? defaultResult : result;
} catch (SQLException exception) {
throw new SQLException(exception);
}
@@ -0,0 +1,6 @@
package cc.carm.lib.easysql.api.builder;
import cc.carm.lib.easysql.api.SQLBuilder;
public interface TableAlertBuilder extends SQLBuilder {
}
@@ -9,6 +9,6 @@ import java.sql.SQLException;
public interface SQLFunction<T, R> {
@Nullable
R apply(T t) throws SQLException;
R apply(@NotNull T t) throws SQLException;
}
@@ -0,0 +1,46 @@
package cc.carm.lib.easysql.api.function.impl;
import cc.carm.lib.easysql.api.SQLAction;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.logging.Logger;
public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
private static @Nullable SQLExceptionHandler customDefaultHandler = null;
public static void setCustomHandler(@Nullable SQLExceptionHandler handler) {
DefaultSQLExceptionHandler.customDefaultHandler = handler;
}
public static @Nullable SQLExceptionHandler getCustomHandler() {
return customDefaultHandler;
}
public static @NotNull SQLExceptionHandler get(Logger logger) {
if (getCustomHandler() != null) return getCustomHandler();
else return new DefaultSQLExceptionHandler(logger);
}
private final Logger logger;
public DefaultSQLExceptionHandler(Logger logger) {
this.logger = logger;
}
protected Logger getLogger() {
return logger;
}
@Override
public void accept(SQLException exception, SQLAction<?> sqlAction) {
getLogger().severe("Error when execute [" + sqlAction.getSQLContent() + "]");
getLogger().severe(exception.getLocalizedMessage());
exception.printStackTrace();
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.8</version>
<version>0.2.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.8</version>
<version>0.2.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -19,6 +19,7 @@ public class EasySQLDemo {
.addColumn("email", "VARCHAR(32)")
.addColumn("phone", "VARCHAR(16)")
.addColumn("registerTime", "DATETIME NOT NULL")
.addColumn("INDEX `phone`") // 添加索引
.build().execute(null /* 不处理错误 */);
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.8</version>
<version>0.2.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.8</version>
<version>0.2.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -8,8 +8,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiConsumer;
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
@@ -20,7 +20,7 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
protected @NotNull String sqlContent;
protected @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler = null;
protected static @Nullable SQLExceptionHandler exceptionHandler = null;
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
this(manager, sql, System.currentTimeMillis());
@@ -16,43 +16,38 @@ public class SQLUpdateBatchActionImpl
extends AbstractSQLAction<List<Integer>>
implements SQLUpdateBatchAction {
List<String> sqlContents = new ArrayList<>();
List<String> sqlContents = new ArrayList<>();
public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
super(manager, sql);
this.sqlContents.add(sql);
}
public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
super(manager, sql);
this.sqlContents.add(sql);
}
@Override
public @NotNull String getSQLContent() {
return this.sqlContents.get(0);
}
@Override
public @NotNull List<String> getSQLContents() {
return this.sqlContents;
}
@Override
public @NotNull List<String> getSQLContents() {
return this.sqlContents;
}
@Override
public SQLUpdateBatchAction addBatch(@NotNull String sql) {
this.sqlContents.add(sql);
return this;
}
@Override
public SQLUpdateBatchAction addBatch(@NotNull String sql) {
this.sqlContents.add(sql);
return this;
}
@Override
public @NotNull List<Integer> execute() throws SQLException {
Connection connection = getManager().getConnection();
Statement statement = connection.createStatement();
outputDebugMessage();
for (String content : this.sqlContents) {
statement.addBatch(content);
}
int[] executed = statement.executeBatch();
List<Integer> returnedValues = Arrays.stream(executed).boxed().collect(Collectors.toList());
@Override
public @NotNull List<Integer> execute() throws SQLException {
Connection connection = getManager().getConnection();
Statement statement = connection.createStatement();
outputDebugMessage();
for (String content : this.sqlContents) {
statement.addBatch(content);
}
int[] executed = statement.executeBatch();
List<Integer> returnedValues = Arrays.stream(executed).boxed().collect(Collectors.toList());
statement.close();
connection.close();
statement.close();
connection.close();
return returnedValues;
}
return returnedValues;
}
}
+1 -1
View File
@@ -16,7 +16,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<packaging>pom</packaging>
<version>0.2.8</version>
<version>0.2.10</version>
<modules>
<module>easysql-api</module>