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:
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<version>0.2.9</version>
|
||||
<version>0.2.10</version>
|
||||
</parent>
|
||||
<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.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;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -22,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> 需要返回的类型
|
||||
@@ -97,8 +98,24 @@ public interface SQLAction<T> {
|
||||
@Nullable
|
||||
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
||||
@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 {
|
||||
return executeFunction(function);
|
||||
return executeFunction(function, defaultResult);
|
||||
} catch (SQLException exception) {
|
||||
handleException(exceptionHandler, exception);
|
||||
return null;
|
||||
@@ -115,9 +132,26 @@ public interface SQLAction<T> {
|
||||
*/
|
||||
@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();
|
||||
return function.apply(value);
|
||||
R result = function.apply(value);
|
||||
return result == null ? defaultResult : result;
|
||||
} catch (SQLException exception) {
|
||||
throw new SQLException(exception);
|
||||
}
|
||||
@@ -157,8 +191,7 @@ public interface SQLAction<T> {
|
||||
* @return 默认的异常处理器
|
||||
*/
|
||||
default SQLExceptionHandler defaultExceptionHandler() {
|
||||
return Optional.ofNullable(DefaultSQLExceptionHandler.getCustomHandler())
|
||||
.orElse(new DefaultSQLExceptionHandler(getManager()));
|
||||
return DefaultSQLExceptionHandler.get(getManager().getLogger());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
+15
-9
@@ -1,11 +1,12 @@
|
||||
package cc.carm.lib.easysql.api.function.impl;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLAction;
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
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 {
|
||||
|
||||
@@ -19,20 +20,25 @@ public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
|
||||
return customDefaultHandler;
|
||||
}
|
||||
|
||||
private final SQLManager sqlManager;
|
||||
|
||||
public DefaultSQLExceptionHandler(SQLManager manager) {
|
||||
this.sqlManager = manager;
|
||||
public static @NotNull SQLExceptionHandler get(Logger logger) {
|
||||
if (getCustomHandler() != null) return getCustomHandler();
|
||||
else return new DefaultSQLExceptionHandler(logger);
|
||||
}
|
||||
|
||||
protected SQLManager getManager() {
|
||||
return sqlManager;
|
||||
private final Logger logger;
|
||||
|
||||
public DefaultSQLExceptionHandler(Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
protected Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(SQLException exception, SQLAction<?> sqlAction) {
|
||||
getManager().getLogger().severe("Error when execute [" + sqlAction.getSQLContent() + "]");
|
||||
getManager().getLogger().severe(exception.getLocalizedMessage());
|
||||
getLogger().severe("Error when execute [" + sqlAction.getSQLContent() + "]");
|
||||
getLogger().severe(exception.getLocalizedMessage());
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.9</version>
|
||||
<version>0.2.10</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.9</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 /* 不处理错误 */);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.9</version>
|
||||
<version>0.2.10</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.9</version>
|
||||
<version>0.2.10</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
+28
-33
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user