1
mirror of https://github.com/CarmJos/EasySQL.git synced 2026-06-04 15:28:20 +08:00

[v0.2.10] [A] 添加含默认值的SQL执行函数。

This commit is contained in:
2022-01-24 17:17:47 +08:00
parent 3e05e5764b
commit d29d06053c
14 changed files with 110 additions and 60 deletions
+1 -1
View File
@@ -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.9</version> <version>0.2.10</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -4,11 +4,11 @@ import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import cc.carm.lib.easysql.api.function.SQLFunction; import cc.carm.lib.easysql.api.function.SQLFunction;
import cc.carm.lib.easysql.api.function.SQLHandler; import cc.carm.lib.easysql.api.function.SQLHandler;
import cc.carm.lib.easysql.api.function.impl.DefaultSQLExceptionHandler; import cc.carm.lib.easysql.api.function.impl.DefaultSQLExceptionHandler;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
/** /**
@@ -22,7 +22,8 @@ import java.util.UUID;
* <li>异步执行 {@link #executeAsync(SQLHandler, SQLExceptionHandler)} * <li>异步执行 {@link #executeAsync(SQLHandler, SQLExceptionHandler)}
* <br>异步执行时将提供成功与异常两种处理方式 * <br>异步执行时将提供成功与异常两种处理方式
* <br>可自行选择是否对数据或异常进行处理 * <br>可自行选择是否对数据或异常进行处理
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li> * <br>默认的异常处理器为 {@link #defaultExceptionHandler()}
* <br>若有特殊需要,可通过{@link #setExceptionHandler(SQLExceptionHandler)} 方法修改默认的处理器</li>
* </ul> * </ul>
* *
* @param <T> 需要返回的类型 * @param <T> 需要返回的类型
@@ -97,8 +98,24 @@ public interface SQLAction<T> {
@Nullable @Nullable
default <R> R execute(@NotNull SQLFunction<T, R> function, default <R> R execute(@NotNull SQLFunction<T, R> function,
@Nullable SQLExceptionHandler exceptionHandler) { @Nullable SQLExceptionHandler exceptionHandler) {
return execute(function, null, exceptionHandler);
}
/**
* 执行语句并处理返回值
*
* @param function 处理方法
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
* @param <R> 需要返回的内容
* @return 指定类型数据
*/
@Nullable
@Contract("_,!null,_ -> !null")
default <R> R execute(@NotNull SQLFunction<T, R> function,
@Nullable R defaultResult,
@Nullable SQLExceptionHandler exceptionHandler) {
try { try {
return executeFunction(function); return executeFunction(function, defaultResult);
} catch (SQLException exception) { } catch (SQLException exception) {
handleException(exceptionHandler, exception); handleException(exceptionHandler, exception);
return null; return null;
@@ -115,9 +132,26 @@ public interface SQLAction<T> {
*/ */
@Nullable @Nullable
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException { 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 { try {
T value = execute(); T value = execute();
return function.apply(value); R result = function.apply(value);
return result == null ? defaultResult : result;
} catch (SQLException exception) { } catch (SQLException exception) {
throw new SQLException(exception); throw new SQLException(exception);
} }
@@ -157,8 +191,7 @@ public interface SQLAction<T> {
* @return 默认的异常处理器 * @return 默认的异常处理器
*/ */
default SQLExceptionHandler defaultExceptionHandler() { default SQLExceptionHandler defaultExceptionHandler() {
return Optional.ofNullable(DefaultSQLExceptionHandler.getCustomHandler()) return DefaultSQLExceptionHandler.get(getManager().getLogger());
.orElse(new DefaultSQLExceptionHandler(getManager()));
} }
/** /**
@@ -16,6 +16,10 @@ public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
*/ */
SQLUpdateBatchAction addBatch(@NotNull String sql); SQLUpdateBatchAction addBatch(@NotNull String sql);
default @NotNull String getSQLContent() {
return getSQLContents().get(0);
}
List<String> getSQLContents(); List<String> getSQLContents();
@Override @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.SQLExceptionHandler;
import cc.carm.lib.easysql.api.function.SQLFunction; import cc.carm.lib.easysql.api.function.SQLFunction;
import cc.carm.lib.easysql.api.function.SQLHandler; import cc.carm.lib.easysql.api.function.SQLHandler;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -34,16 +35,20 @@ public interface QueryAction extends SQLAction<SQLQuery> {
/** /**
* 执行语句并处理返回值 * 执行语句并处理返回值
* *
* @param function 处理方法 * @param defaultResult 默认结果,若处理后的结果为null,则返回该值
* @param <R> 需要返回的内容 * @param function 处理方法
* @param <R> 需要返回的内容
* @return 指定类型数据 * @return 指定类型数据
* @throws SQLException 当SQL操作出现问题时抛出 * @throws SQLException 当SQL操作出现问题时抛出
*/ */
@Nullable @Nullable
default <R> R executeFunction(@NotNull SQLFunction<SQLQuery, R> function) @Contract("!null, _ -> !null")
throws SQLException { default <R> R executeFunction(@Nullable R defaultResult,
@NotNull SQLFunction<SQLQuery, R> function) throws SQLException {
try (SQLQuery value = execute()) { try (SQLQuery value = execute()) {
return function.apply(value); R result = function.apply(value);
return result == null ? defaultResult : result;
} catch (SQLException exception) { } catch (SQLException exception) {
throw new 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> { public interface SQLFunction<T, R> {
@Nullable @Nullable
R apply(T t) throws SQLException; R apply(@NotNull T t) throws SQLException;
} }
@@ -1,11 +1,12 @@
package cc.carm.lib.easysql.api.function.impl; package cc.carm.lib.easysql.api.function.impl;
import cc.carm.lib.easysql.api.SQLAction; import cc.carm.lib.easysql.api.SQLAction;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler; import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Logger;
public class DefaultSQLExceptionHandler implements SQLExceptionHandler { public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
@@ -19,20 +20,25 @@ public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
return customDefaultHandler; return customDefaultHandler;
} }
private final SQLManager sqlManager; public static @NotNull SQLExceptionHandler get(Logger logger) {
if (getCustomHandler() != null) return getCustomHandler();
public DefaultSQLExceptionHandler(SQLManager manager) { else return new DefaultSQLExceptionHandler(logger);
this.sqlManager = manager;
} }
protected SQLManager getManager() { private final Logger logger;
return sqlManager;
public DefaultSQLExceptionHandler(Logger logger) {
this.logger = logger;
}
protected Logger getLogger() {
return logger;
} }
@Override @Override
public void accept(SQLException exception, SQLAction<?> sqlAction) { public void accept(SQLException exception, SQLAction<?> sqlAction) {
getManager().getLogger().severe("Error when execute [" + sqlAction.getSQLContent() + "]"); getLogger().severe("Error when execute [" + sqlAction.getSQLContent() + "]");
getManager().getLogger().severe(exception.getLocalizedMessage()); getLogger().severe(exception.getLocalizedMessage());
exception.printStackTrace(); exception.printStackTrace();
} }
+1 -1
View File
@@ -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.9</version> <version>0.2.10</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -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.9</version> <version>0.2.10</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -19,6 +19,7 @@ public class EasySQLDemo {
.addColumn("email", "VARCHAR(32)") .addColumn("email", "VARCHAR(32)")
.addColumn("phone", "VARCHAR(16)") .addColumn("phone", "VARCHAR(16)")
.addColumn("registerTime", "DATETIME NOT NULL") .addColumn("registerTime", "DATETIME NOT NULL")
.addColumn("INDEX `phone`") // 添加索引
.build().execute(null /* 不处理错误 */); .build().execute(null /* 不处理错误 */);
} }
+1 -1
View File
@@ -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.9</version> <version>0.2.10</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -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.9</version> <version>0.2.10</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -16,43 +16,38 @@ public class SQLUpdateBatchActionImpl
extends AbstractSQLAction<List<Integer>> extends AbstractSQLAction<List<Integer>>
implements SQLUpdateBatchAction { implements SQLUpdateBatchAction {
List<String> sqlContents = new ArrayList<>(); List<String> sqlContents = new ArrayList<>();
public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) { public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
super(manager, sql); super(manager, sql);
this.sqlContents.add(sql); this.sqlContents.add(sql);
} }
@Override @Override
public @NotNull String getSQLContent() { public @NotNull List<String> getSQLContents() {
return this.sqlContents.get(0); return this.sqlContents;
} }
@Override @Override
public @NotNull List<String> getSQLContents() { public SQLUpdateBatchAction addBatch(@NotNull String sql) {
return this.sqlContents; this.sqlContents.add(sql);
} return this;
}
@Override @Override
public SQLUpdateBatchAction addBatch(@NotNull String sql) { public @NotNull List<Integer> execute() throws SQLException {
this.sqlContents.add(sql); Connection connection = getManager().getConnection();
return this; 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 statement.close();
public @NotNull List<Integer> execute() throws SQLException { connection.close();
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(); return returnedValues;
connection.close(); }
return returnedValues;
}
} }
+1 -1
View File
@@ -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.9</version> <version>0.2.10</version>
<modules> <modules>
<module>easysql-api</module> <module>easysql-api</module>