Skip to content

Commit

Permalink
[v0.2.10] [A] 添加含默认值的SQL执行函数。
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Jan 24, 2022
1 parent 3e05e57 commit d29d060
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 65 deletions.
2 changes: 1 addition & 1 deletion easysql-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
45 changes: 39 additions & 6 deletions easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
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;

/**
Expand All @@ -22,7 +22,8 @@
* <li>异步执行 {@link #executeAsync(SQLHandler, SQLExceptionHandler)}
* <br>异步执行时将提供成功与异常两种处理方式
* <br>可自行选择是否对数据或异常进行处理
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}
* <br>若有特殊需要,可通过{@link #setExceptionHandler(SQLExceptionHandler)} 方法修改默认的处理器</li>
* </ul>
*
* @param <T> 需要返回的类型
Expand Down Expand Up @@ -97,8 +98,24 @@ default T execute(@Nullable SQLExceptionHandler exceptionHandler) {
@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;
Expand All @@ -115,9 +132,26 @@ default <R> R execute(@NotNull SQLFunction<T, R> function,
*/
@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);
}
Expand Down Expand Up @@ -157,8 +191,7 @@ default void handleException(@Nullable SQLExceptionHandler handler, SQLException
* @return 默认的异常处理器
*/
default SQLExceptionHandler defaultExceptionHandler() {
return Optional.ofNullable(DefaultSQLExceptionHandler.getCustomHandler())
.orElse(new DefaultSQLExceptionHandler(getManager()));
return DefaultSQLExceptionHandler.get(getManager().getLogger());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cc.carm.lib.easysql.api.builder;

import cc.carm.lib.easysql.api.SQLBuilder;

public interface TableAlertBuilder extends SQLBuilder {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
public interface SQLFunction<T, R> {

@Nullable
R apply(T t) throws SQLException;
R apply(@NotNull T t) throws SQLException;

}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -19,20 +20,25 @@ public static void setCustomHandler(@Nullable SQLExceptionHandler handler) {
return customDefaultHandler;
}

private final SQLManager sqlManager;
public static @NotNull SQLExceptionHandler get(Logger logger) {
if (getCustomHandler() != null) return getCustomHandler();
else return new DefaultSQLExceptionHandler(logger);
}

private final Logger logger;

public DefaultSQLExceptionHandler(SQLManager manager) {
this.sqlManager = manager;
public DefaultSQLExceptionHandler(Logger logger) {
this.logger = logger;
}

protected SQLManager getManager() {
return sqlManager;
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();
}

Expand Down
2 changes: 1 addition & 1 deletion easysql-beecp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion easysql-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
1 change: 1 addition & 0 deletions easysql-demo/src/main/java/EasySQLDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void createTable(SQLManager sqlManager) {
.addColumn("email", "VARCHAR(32)")
.addColumn("phone", "VARCHAR(16)")
.addColumn("registerTime", "DATETIME NOT NULL")
.addColumn("INDEX `phone`") // 添加索引
.build().execute(null /* 不处理错误 */);
}

Expand Down
2 changes: 1 addition & 1 deletion easysql-hikaricp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion easysql-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,38 @@ public class SQLUpdateBatchActionImpl
extends AbstractSQLAction<List<Integer>>
implements SQLUpdateBatchAction {

List<String> sqlContents = new ArrayList<>();

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 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());

statement.close();
connection.close();

return returnedValues;
}
List<String> sqlContents = new ArrayList<>();

public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
super(manager, sql);
this.sqlContents.add(sql);
}

@Override
public @NotNull List<String> getSQLContents() {
return this.sqlContents;
}

@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());

statement.close();
connection.close();

return returnedValues;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<packaging>pom</packaging>
<version>0.2.9</version>
<version>0.2.10</version>

<modules>
<module>easysql-api</module>
Expand Down

0 comments on commit d29d060

Please sign in to comment.