mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-04 15:28:20 +08:00
修改协议
This commit is contained in:
+4
-4
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
@@ -30,8 +30,8 @@
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>GNU General Public License v3.0</name>
|
||||
<url>https://opensource.org/licenses/GPL-3.0</url>
|
||||
<name>The MIT License</name>
|
||||
<url>https://opensource.org/licenses/MIT</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
|
||||
@@ -29,138 +29,138 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public interface SQLAction<T> {
|
||||
|
||||
/**
|
||||
* 得到该Action的UUID
|
||||
*
|
||||
* @return UUID
|
||||
*/
|
||||
@NotNull UUID getActionUUID();
|
||||
/**
|
||||
* 得到该Action的UUID
|
||||
*
|
||||
* @return UUID
|
||||
*/
|
||||
@NotNull UUID getActionUUID();
|
||||
|
||||
/**
|
||||
* 得到短八位格式的UUID
|
||||
*
|
||||
* @return UUID(8)
|
||||
*/
|
||||
@NotNull String getShortID();
|
||||
/**
|
||||
* 得到短八位格式的UUID
|
||||
*
|
||||
* @return UUID(8)
|
||||
*/
|
||||
@NotNull String getShortID();
|
||||
|
||||
/**
|
||||
* 得到该Action的创建时间
|
||||
*
|
||||
* @return 创建时间
|
||||
*/
|
||||
long getCreateTime();
|
||||
/**
|
||||
* 得到该Action的创建时间
|
||||
*
|
||||
* @return 创建时间
|
||||
*/
|
||||
long getCreateTime();
|
||||
|
||||
/**
|
||||
* 得到该Action所要执行的源SQL语句
|
||||
*
|
||||
* @return 源SQL语句
|
||||
*/
|
||||
@NotNull String getSQLContent();
|
||||
/**
|
||||
* 得到该Action所要执行的源SQL语句
|
||||
*
|
||||
* @return 源SQL语句
|
||||
*/
|
||||
@NotNull String getSQLContent();
|
||||
|
||||
/**
|
||||
* 得到承载该Action的对应{@link SQLManager}
|
||||
*
|
||||
* @return {@link SQLManager}
|
||||
*/
|
||||
@NotNull SQLManager getManager();
|
||||
/**
|
||||
* 得到承载该Action的对应{@link SQLManager}
|
||||
*
|
||||
* @return {@link SQLManager}
|
||||
*/
|
||||
@NotNull SQLManager getManager();
|
||||
|
||||
/**
|
||||
* 执行该Action对应的SQL语句
|
||||
*
|
||||
* @return 指定数据类型
|
||||
* @throws SQLException 当SQL操作出现问题时抛出
|
||||
*/
|
||||
@NotNull T execute() throws SQLException;
|
||||
/**
|
||||
* 执行该Action对应的SQL语句
|
||||
*
|
||||
* @return 指定数据类型
|
||||
* @throws SQLException 当SQL操作出现问题时抛出
|
||||
*/
|
||||
@NotNull T execute() throws SQLException;
|
||||
|
||||
|
||||
/**
|
||||
* 执行语句并返回值
|
||||
*
|
||||
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
|
||||
* @return 指定类型数据
|
||||
*/
|
||||
@Nullable
|
||||
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||
return execute(t -> t, exceptionHandler);
|
||||
}
|
||||
/**
|
||||
* 执行语句并返回值
|
||||
*
|
||||
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
|
||||
* @return 指定类型数据
|
||||
*/
|
||||
@Nullable
|
||||
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||
return execute(t -> t, exceptionHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @param function 处理方法
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @param function 处理方法
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步执行SQL语句,采用默认异常处理,无需返回值。
|
||||
*/
|
||||
default void executeAsync() {
|
||||
executeAsync(null);
|
||||
}
|
||||
/**
|
||||
* 异步执行SQL语句,采用默认异常处理,无需返回值。
|
||||
*/
|
||||
default void executeAsync() {
|
||||
executeAsync(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步执行SQL语句
|
||||
*
|
||||
* @param success 成功时的操作
|
||||
*/
|
||||
default void executeAsync(@Nullable Consumer<T> success) {
|
||||
executeAsync(success, null);
|
||||
}
|
||||
/**
|
||||
* 异步执行SQL语句
|
||||
*
|
||||
* @param success 成功时的操作
|
||||
*/
|
||||
default void executeAsync(@Nullable Consumer<T> success) {
|
||||
executeAsync(success, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步执行SQL语句
|
||||
*
|
||||
* @param success 成功时的操作
|
||||
* @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()}
|
||||
*/
|
||||
void executeAsync(@Nullable Consumer<T> success, @Nullable BiConsumer<SQLException, SQLAction<T>> failure);
|
||||
/**
|
||||
* 异步执行SQL语句
|
||||
*
|
||||
* @param success 成功时的操作
|
||||
* @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()}
|
||||
*/
|
||||
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);
|
||||
}
|
||||
default void handleException(@Nullable BiConsumer<SQLException, SQLAction<T>> handler, SQLException exception) {
|
||||
if (handler == null) handler = defaultExceptionHandler();
|
||||
handler.accept(exception, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 默认的异常处理器
|
||||
*/
|
||||
default BiConsumer<SQLException, SQLAction<T>> defaultExceptionHandler() {
|
||||
return (exception, action) -> {
|
||||
getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
|
||||
getManager().getLogger().severe(exception.getLocalizedMessage());
|
||||
exception.printStackTrace();
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @return 默认的异常处理器
|
||||
*/
|
||||
default BiConsumer<SQLException, SQLAction<T>> defaultExceptionHandler() {
|
||||
return (exception, action) -> {
|
||||
getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
|
||||
getManager().getLogger().severe(exception.getLocalizedMessage());
|
||||
exception.printStackTrace();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public interface SQLBuilder {
|
||||
|
||||
/**
|
||||
* 得到承载该Builder的对应{@link SQLManager}
|
||||
*
|
||||
* @return {@link SQLManager}
|
||||
*/
|
||||
@NotNull SQLManager getManager();
|
||||
/**
|
||||
* 得到承载该Builder的对应{@link SQLManager}
|
||||
*
|
||||
* @return {@link SQLManager}
|
||||
*/
|
||||
@NotNull SQLManager getManager();
|
||||
|
||||
}
|
||||
|
||||
@@ -19,145 +19,145 @@ import java.util.logging.Logger;
|
||||
|
||||
public interface SQLManager {
|
||||
|
||||
Logger getLogger();
|
||||
Logger getLogger();
|
||||
|
||||
boolean isDebugMode();
|
||||
boolean isDebugMode();
|
||||
|
||||
void setDebugMode(boolean enable);
|
||||
void setDebugMode(boolean enable);
|
||||
|
||||
/**
|
||||
* 得到连接池源
|
||||
*
|
||||
* @return DataSource
|
||||
*/
|
||||
@NotNull DataSource getDataSource();
|
||||
/**
|
||||
* 得到连接池源
|
||||
*
|
||||
* @return DataSource
|
||||
*/
|
||||
@NotNull DataSource getDataSource();
|
||||
|
||||
/**
|
||||
* 得到一个数据库连接实例
|
||||
*
|
||||
* @return Connection
|
||||
* @throws SQLException 见 {@link DataSource#getConnection()}
|
||||
*/
|
||||
@NotNull Connection getConnection() throws SQLException;
|
||||
/**
|
||||
* 得到一个数据库连接实例
|
||||
*
|
||||
* @return Connection
|
||||
* @throws SQLException 见 {@link DataSource#getConnection()}
|
||||
*/
|
||||
@NotNull Connection getConnection() throws SQLException;
|
||||
|
||||
/**
|
||||
* 得到正使用的查询。
|
||||
*
|
||||
* @return 查询列表
|
||||
*/
|
||||
@NotNull Map<UUID, SQLQuery> getActiveQuery();
|
||||
/**
|
||||
* 得到正使用的查询。
|
||||
*
|
||||
* @return 查询列表
|
||||
*/
|
||||
@NotNull Map<UUID, SQLQuery> getActiveQuery();
|
||||
|
||||
/**
|
||||
* 执行一条不需要返回结果的SQL语句(多用于UPDATE、REPLACE、DELETE方法)
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @return 更新的行数
|
||||
* @see SQLUpdateAction
|
||||
*/
|
||||
@Nullable Integer executeSQL(String sql);
|
||||
/**
|
||||
* 执行一条不需要返回结果的SQL语句(多用于UPDATE、REPLACE、DELETE方法)
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @return 更新的行数
|
||||
* @see SQLUpdateAction
|
||||
*/
|
||||
@Nullable Integer executeSQL(String sql);
|
||||
|
||||
/**
|
||||
* 执行一条不需要返回结果的预处理SQL更改(UPDATE、REPLACE、DELETE)
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @param params SQL语句中 ? 的对应参数
|
||||
* @return 更新的行数
|
||||
* @see PreparedSQLUpdateAction
|
||||
*/
|
||||
@Nullable Integer executeSQL(String sql, Object[] params);
|
||||
/**
|
||||
* 执行一条不需要返回结果的预处理SQL更改(UPDATE、REPLACE、DELETE)
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @param params SQL语句中 ? 的对应参数
|
||||
* @return 更新的行数
|
||||
* @see PreparedSQLUpdateAction
|
||||
*/
|
||||
@Nullable Integer executeSQL(String sql, Object[] params);
|
||||
|
||||
/**
|
||||
* 执行多条不需要返回结果的SQL更改(UPDATE、REPLACE、DELETE)
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @param paramsBatch SQL语句中对应?的参数组
|
||||
* @return 对应参数返回的行数
|
||||
* @see PreparedSQLUpdateBatchAction
|
||||
*/
|
||||
@Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
|
||||
/**
|
||||
* 执行多条不需要返回结果的SQL更改(UPDATE、REPLACE、DELETE)
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @param paramsBatch SQL语句中对应?的参数组
|
||||
* @return 对应参数返回的行数
|
||||
* @see PreparedSQLUpdateBatchAction
|
||||
*/
|
||||
@Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
|
||||
|
||||
|
||||
/**
|
||||
* 执行多条不需要返回结果的SQL。
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @param moreSQL 更多SQL语句内容
|
||||
* @return 对应参数返回的行数
|
||||
* @see SQLUpdateBatchAction
|
||||
*/
|
||||
@Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL);
|
||||
/**
|
||||
* 执行多条不需要返回结果的SQL。
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @param moreSQL 更多SQL语句内容
|
||||
* @return 对应参数返回的行数
|
||||
* @see SQLUpdateBatchAction
|
||||
*/
|
||||
@Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL);
|
||||
|
||||
/**
|
||||
* 执行多条不需要返回结果的SQL。
|
||||
*
|
||||
* @param sqlBatch SQL语句内容
|
||||
* @return 对应参数返回的行数
|
||||
*/
|
||||
@Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch);
|
||||
/**
|
||||
* 执行多条不需要返回结果的SQL。
|
||||
*
|
||||
* @param sqlBatch SQL语句内容
|
||||
* @return 对应参数返回的行数
|
||||
*/
|
||||
@Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch);
|
||||
|
||||
/**
|
||||
* 在库中创建一个表
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return {@link TableCreateBuilder}
|
||||
*/
|
||||
TableCreateBuilder createTable(@NotNull String tableName);
|
||||
/**
|
||||
* 在库中创建一个表
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return {@link TableCreateBuilder}
|
||||
*/
|
||||
TableCreateBuilder createTable(@NotNull String tableName);
|
||||
|
||||
/**
|
||||
* 新建一个查询
|
||||
*
|
||||
* @return {@link QueryBuilder}
|
||||
*/
|
||||
QueryBuilder createQuery();
|
||||
/**
|
||||
* 新建一个查询
|
||||
*
|
||||
* @return {@link QueryBuilder}
|
||||
*/
|
||||
QueryBuilder createQuery();
|
||||
|
||||
/**
|
||||
* 创建一条插入操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link InsertBuilder}
|
||||
*/
|
||||
InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName);
|
||||
/**
|
||||
* 创建一条插入操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link InsertBuilder}
|
||||
*/
|
||||
InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName);
|
||||
|
||||
/**
|
||||
* 创建支持多组数据的插入操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link InsertBuilder}
|
||||
*/
|
||||
InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName);
|
||||
/**
|
||||
* 创建支持多组数据的插入操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link InsertBuilder}
|
||||
*/
|
||||
InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName);
|
||||
|
||||
/**
|
||||
* 创建一条替换操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link ReplaceBuilder}
|
||||
*/
|
||||
ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName);
|
||||
/**
|
||||
* 创建一条替换操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link ReplaceBuilder}
|
||||
*/
|
||||
ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName);
|
||||
|
||||
/**
|
||||
* 创建支持多组数据的替换操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link ReplaceBuilder}
|
||||
*/
|
||||
ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName);
|
||||
/**
|
||||
* 创建支持多组数据的替换操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link ReplaceBuilder}
|
||||
*/
|
||||
ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName);
|
||||
|
||||
/**
|
||||
* 创建更新操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link UpdateBuilder}
|
||||
*/
|
||||
UpdateBuilder createUpdate(@NotNull String tableName);
|
||||
/**
|
||||
* 创建更新操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link UpdateBuilder}
|
||||
*/
|
||||
UpdateBuilder createUpdate(@NotNull String tableName);
|
||||
|
||||
/**
|
||||
* 创建删除操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link DeleteBuilder}
|
||||
*/
|
||||
DeleteBuilder createDelete(@NotNull String tableName);
|
||||
/**
|
||||
* 创建删除操作
|
||||
*
|
||||
* @param tableName 目标表名
|
||||
* @return {@link DeleteBuilder}
|
||||
*/
|
||||
DeleteBuilder createDelete(@NotNull String tableName);
|
||||
|
||||
}
|
||||
|
||||
+14
-14
@@ -4,20 +4,20 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface PreparedSQLUpdateAction extends SQLUpdateAction {
|
||||
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedSQLUpdateAction}
|
||||
*/
|
||||
PreparedSQLUpdateAction setParams(Object... params);
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedSQLUpdateAction}
|
||||
*/
|
||||
PreparedSQLUpdateAction setParams(Object... params);
|
||||
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedSQLUpdateAction}
|
||||
*/
|
||||
PreparedSQLUpdateAction setParams(@Nullable Iterable<Object> params);
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedSQLUpdateAction}
|
||||
*/
|
||||
PreparedSQLUpdateAction setParams(@Nullable Iterable<Object> params);
|
||||
|
||||
}
|
||||
|
||||
+31
-31
@@ -6,39 +6,39 @@ import java.util.List;
|
||||
|
||||
public interface PreparedSQLUpdateBatchAction extends SQLAction<List<Integer>> {
|
||||
|
||||
/**
|
||||
* 设定多组SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param allParams 所有参数内容
|
||||
* @return {@link PreparedSQLUpdateBatchAction}
|
||||
*/
|
||||
PreparedSQLUpdateBatchAction setAllParams(Iterable<Object[]> allParams);
|
||||
/**
|
||||
* 设定多组SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param allParams 所有参数内容
|
||||
* @return {@link PreparedSQLUpdateBatchAction}
|
||||
*/
|
||||
PreparedSQLUpdateBatchAction setAllParams(Iterable<Object[]> allParams);
|
||||
|
||||
/**
|
||||
* 添加一组SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedSQLUpdateBatchAction}
|
||||
*/
|
||||
PreparedSQLUpdateBatchAction addParamsBatch(Object... params);
|
||||
/**
|
||||
* 添加一组SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedSQLUpdateBatchAction}
|
||||
*/
|
||||
PreparedSQLUpdateBatchAction addParamsBatch(Object... params);
|
||||
|
||||
/**
|
||||
* 设定自增主键的序列
|
||||
*
|
||||
* @param keyColumnIndex 自增主键的序列
|
||||
* <br>若该值 > 0,则 {@link #execute()} 返回自增主键数值
|
||||
* <br>若该值 ≤ 0,则 {@link #execute()} 返回变更的行数
|
||||
* @return {@link PreparedSQLUpdateBatchAction}
|
||||
*/
|
||||
PreparedSQLUpdateBatchAction setKeyIndex(int keyColumnIndex);
|
||||
/**
|
||||
* 设定自增主键的序列
|
||||
*
|
||||
* @param keyColumnIndex 自增主键的序列
|
||||
* <br>若该值 > 0,则 {@link #execute()} 返回自增主键数值
|
||||
* <br>若该值 ≤ 0,则 {@link #execute()} 返回变更的行数
|
||||
* @return {@link PreparedSQLUpdateBatchAction}
|
||||
*/
|
||||
PreparedSQLUpdateBatchAction setKeyIndex(int keyColumnIndex);
|
||||
|
||||
/**
|
||||
* 默认主键序列的数值为 -1 (≤0) ,即默认返回发生变更的行数。
|
||||
*
|
||||
* @return 默认主键序列
|
||||
*/
|
||||
default PreparedSQLUpdateBatchAction defaultKeyIndex() {
|
||||
return setKeyIndex(-1); // will return changed lines number
|
||||
}
|
||||
/**
|
||||
* 默认主键序列的数值为 -1 (≤0) ,即默认返回发生变更的行数。
|
||||
*
|
||||
* @return 默认主键序列
|
||||
*/
|
||||
default PreparedSQLUpdateBatchAction defaultKeyIndex() {
|
||||
return setKeyIndex(-1); // will return changed lines number
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,31 +2,26 @@ package cc.carm.lib.easysql.api.action;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLAction;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface SQLUpdateAction extends SQLAction<Integer> {
|
||||
|
||||
/**
|
||||
* 设定自增主键的序列
|
||||
*
|
||||
* @param keyColumnIndex 自增主键的序列
|
||||
* <br>若该值 > 0,则 {@link #execute()} 返回自增主键数值
|
||||
* <br>若该值 ≤ 0,则 {@link #execute()} 返回变更的行数
|
||||
* @return {@link SQLUpdateAction}
|
||||
*/
|
||||
SQLUpdateAction setKeyIndex(int keyColumnIndex);
|
||||
|
||||
/**
|
||||
* 默认主键序列的数值为 -1 (≤0) ,即默认返回发生变更的行数。
|
||||
*
|
||||
* @return 默认主键序列
|
||||
*/
|
||||
default SQLUpdateAction defaultKeyIndex() {
|
||||
return setKeyIndex(-1); // will return changed lines number
|
||||
}
|
||||
/**
|
||||
* 设定自增主键的序列
|
||||
*
|
||||
* @param keyColumnIndex 自增主键的序列
|
||||
* <br>若该值 > 0,则 {@link #execute()} 返回自增主键数值
|
||||
* <br>若该值 ≤ 0,则 {@link #execute()} 返回变更的行数
|
||||
* @return {@link SQLUpdateAction}
|
||||
*/
|
||||
SQLUpdateAction setKeyIndex(int keyColumnIndex);
|
||||
|
||||
/**
|
||||
* 默认主键序列的数值为 -1 (≤0) ,即默认返回发生变更的行数。
|
||||
*
|
||||
* @return 默认主键序列
|
||||
*/
|
||||
default SQLUpdateAction defaultKeyIndex() {
|
||||
return setKeyIndex(-1); // will return changed lines number
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+21
-21
@@ -9,28 +9,28 @@ import java.util.function.BiConsumer;
|
||||
|
||||
public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
|
||||
|
||||
/**
|
||||
* 添加一条批量执行的SQL语句
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @return {@link SQLUpdateBatchAction}
|
||||
*/
|
||||
SQLUpdateBatchAction addBatch(@NotNull String sql);
|
||||
/**
|
||||
* 添加一条批量执行的SQL语句
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @return {@link SQLUpdateBatchAction}
|
||||
*/
|
||||
SQLUpdateBatchAction addBatch(@NotNull String sql);
|
||||
|
||||
List<String> getSQLContents();
|
||||
List<String> getSQLContents();
|
||||
|
||||
@Override
|
||||
default BiConsumer<SQLException, SQLAction<List<Integer>>> defaultExceptionHandler() {
|
||||
return (exception, action) -> {
|
||||
getManager().getLogger().severe("Error when execute SQLs : ");
|
||||
int i = 1;
|
||||
for (String content : getSQLContents()) {
|
||||
getManager().getLogger().severe("#" + i + " [" + content + "]");
|
||||
i++;
|
||||
}
|
||||
getManager().getLogger().severe(exception.getLocalizedMessage());
|
||||
exception.printStackTrace();
|
||||
};
|
||||
}
|
||||
@Override
|
||||
default BiConsumer<SQLException, SQLAction<List<Integer>>> defaultExceptionHandler() {
|
||||
return (exception, action) -> {
|
||||
getManager().getLogger().severe("Error when execute SQLs : ");
|
||||
int i = 1;
|
||||
for (String content : getSQLContents()) {
|
||||
getManager().getLogger().severe("#" + i + " [" + content + "]");
|
||||
i++;
|
||||
}
|
||||
getManager().getLogger().severe(exception.getLocalizedMessage());
|
||||
exception.printStackTrace();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-22
@@ -7,29 +7,29 @@ import java.util.function.Consumer;
|
||||
|
||||
public interface PreparedQueryAction extends QueryAction {
|
||||
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction setParams(@Nullable Object... params);
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction setParams(@Nullable Object... params);
|
||||
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction setParams(@Nullable Iterable<Object> params);
|
||||
/**
|
||||
* 设定SQL语句中所有 ? 对应的参数
|
||||
*
|
||||
* @param params 参数内容
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction setParams(@Nullable Iterable<Object> params);
|
||||
|
||||
/**
|
||||
* 直接对 {@link PreparedStatement} 进行处理
|
||||
*
|
||||
* @param statement {@link Consumer} 处理操作
|
||||
* 若为空则不进行处理
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction handleStatement(@Nullable Consumer<PreparedStatement> statement);
|
||||
/**
|
||||
* 直接对 {@link PreparedStatement} 进行处理
|
||||
*
|
||||
* @param statement {@link Consumer} 处理操作
|
||||
* 若为空则不进行处理
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction handleStatement(@Nullable Consumer<PreparedStatement> statement);
|
||||
|
||||
}
|
||||
|
||||
@@ -30,22 +30,22 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,43 +8,43 @@ import java.sql.Statement;
|
||||
|
||||
public interface SQLQuery extends AutoCloseable {
|
||||
|
||||
/**
|
||||
* 获取该查询创建的时间
|
||||
*
|
||||
* @return 创建时间
|
||||
*/
|
||||
long getExecuteTime();
|
||||
/**
|
||||
* 获取该查询创建的时间
|
||||
*
|
||||
* @return 创建时间
|
||||
*/
|
||||
long getExecuteTime();
|
||||
|
||||
/**
|
||||
* 得到承载该SQLQuery的对应{@link SQLManager}
|
||||
*
|
||||
* @return {@link SQLManager}
|
||||
*/
|
||||
SQLManager getManager();
|
||||
/**
|
||||
* 得到承载该SQLQuery的对应{@link SQLManager}
|
||||
*
|
||||
* @return {@link SQLManager}
|
||||
*/
|
||||
SQLManager getManager();
|
||||
|
||||
/**
|
||||
* 得到承载该SQLQuery的对应{@link QueryAction}
|
||||
*
|
||||
* @return {@link QueryAction} 或 {@link PreparedQueryAction}
|
||||
*/
|
||||
QueryAction getAction();
|
||||
/**
|
||||
* 得到承载该SQLQuery的对应{@link QueryAction}
|
||||
*
|
||||
* @return {@link QueryAction} 或 {@link PreparedQueryAction}
|
||||
*/
|
||||
QueryAction getAction();
|
||||
|
||||
ResultSet getResultSet();
|
||||
ResultSet getResultSet();
|
||||
|
||||
/**
|
||||
* 得到设定的SQL语句
|
||||
*
|
||||
* @return SQL语句
|
||||
*/
|
||||
String getSQLContent();
|
||||
/**
|
||||
* 得到设定的SQL语句
|
||||
*
|
||||
* @return SQL语句
|
||||
*/
|
||||
String getSQLContent();
|
||||
|
||||
/**
|
||||
* 关闭所有内容
|
||||
*/
|
||||
void close();
|
||||
/**
|
||||
* 关闭所有内容
|
||||
*/
|
||||
void close();
|
||||
|
||||
Statement getStatement();
|
||||
Statement getStatement();
|
||||
|
||||
Connection getConnection();
|
||||
Connection getConnection();
|
||||
|
||||
}
|
||||
|
||||
@@ -9,34 +9,34 @@ import java.util.LinkedHashMap;
|
||||
|
||||
public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T> extends SQLBuilder {
|
||||
|
||||
T build();
|
||||
T build();
|
||||
|
||||
B setLimit(int limit);
|
||||
B setLimit(int limit);
|
||||
|
||||
B setConditions(@Nullable String condition);
|
||||
B setConditions(@Nullable String condition);
|
||||
|
||||
B setConditions(LinkedHashMap<@NotNull String, @Nullable Object> conditionSQLs);
|
||||
B setConditions(LinkedHashMap<@NotNull String, @Nullable Object> conditionSQLs);
|
||||
|
||||
B addCondition(@Nullable String condition);
|
||||
B addCondition(@Nullable String condition);
|
||||
|
||||
B addCondition(@NotNull String queryName, @NotNull String operator, @Nullable Object queryValue);
|
||||
B addCondition(@NotNull String queryName, @NotNull String operator, @Nullable Object queryValue);
|
||||
|
||||
default B addCondition(@NotNull String queryName, @Nullable Object queryValue) {
|
||||
return addCondition(queryName, "=", queryValue);
|
||||
}
|
||||
default B addCondition(@NotNull String queryName, @Nullable Object queryValue) {
|
||||
return addCondition(queryName, "=", queryValue);
|
||||
}
|
||||
|
||||
B addCondition(@NotNull String[] queryNames, @Nullable Object[] queryValues);
|
||||
B addCondition(@NotNull String[] queryNames, @Nullable Object[] queryValues);
|
||||
|
||||
B addNotNullCondition(@NotNull String queryName);
|
||||
B addNotNullCondition(@NotNull String queryName);
|
||||
|
||||
default B addTimeCondition(@NotNull String queryName, long startMillis, long endMillis) {
|
||||
return addTimeCondition(queryName,
|
||||
startMillis > 0 ? new Date(startMillis) : null,
|
||||
endMillis > 0 ? new Date(endMillis) : null
|
||||
);
|
||||
}
|
||||
default B addTimeCondition(@NotNull String queryName, long startMillis, long endMillis) {
|
||||
return addTimeCondition(queryName,
|
||||
startMillis > 0 ? new Date(startMillis) : null,
|
||||
endMillis > 0 ? new Date(endMillis) : null
|
||||
);
|
||||
}
|
||||
|
||||
B addTimeCondition(@NotNull String queryName, @Nullable java.util.Date startDate, @Nullable java.util.Date endDate);
|
||||
B addTimeCondition(@NotNull String queryName, @Nullable java.util.Date startDate, @Nullable java.util.Date endDate);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
||||
|
||||
public interface DeleteBuilder extends ConditionalBuilder<DeleteBuilder, PreparedSQLUpdateAction> {
|
||||
|
||||
String getTableName();
|
||||
String getTableName();
|
||||
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ import java.util.List;
|
||||
|
||||
public interface InsertBuilder<T> {
|
||||
|
||||
String getTableName();
|
||||
String getTableName();
|
||||
|
||||
T setColumnNames(List<String> columnNames);
|
||||
T setColumnNames(List<String> columnNames);
|
||||
|
||||
default T setColumnNames(String... columnNames) {
|
||||
return setColumnNames(columnNames == null ? null : Arrays.asList(columnNames));
|
||||
}
|
||||
default T setColumnNames(String... columnNames) {
|
||||
return setColumnNames(columnNames == null ? null : Arrays.asList(columnNames));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,31 +7,31 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface QueryBuilder extends SQLBuilder {
|
||||
|
||||
/**
|
||||
* 通过一条 SQL语句创建查询。
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @return {@link QueryAction}
|
||||
* @deprecated 存在SQL注入风险,建议使用 {@link QueryBuilder#withPreparedSQL(String)}
|
||||
*/
|
||||
@Deprecated
|
||||
QueryAction withSQL(@NotNull String sql);
|
||||
/**
|
||||
* 通过一条 SQL语句创建查询。
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @return {@link QueryAction}
|
||||
* @deprecated 存在SQL注入风险,建议使用 {@link QueryBuilder#withPreparedSQL(String)}
|
||||
*/
|
||||
@Deprecated
|
||||
QueryAction withSQL(@NotNull String sql);
|
||||
|
||||
/**
|
||||
* 通过一条 SQL语句创建预查询
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction withPreparedSQL(@NotNull String sql);
|
||||
/**
|
||||
* 通过一条 SQL语句创建预查询
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @return {@link PreparedQueryAction}
|
||||
*/
|
||||
PreparedQueryAction withPreparedSQL(@NotNull String sql);
|
||||
|
||||
/**
|
||||
* 创建表查询
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return {@link TableQueryBuilder}
|
||||
*/
|
||||
TableQueryBuilder inTable(@NotNull String tableName);
|
||||
/**
|
||||
* 创建表查询
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return {@link TableQueryBuilder}
|
||||
*/
|
||||
TableQueryBuilder inTable(@NotNull String tableName);
|
||||
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ import java.util.List;
|
||||
|
||||
public interface ReplaceBuilder<T> {
|
||||
|
||||
String getTableName();
|
||||
String getTableName();
|
||||
|
||||
T setColumnNames(List<String> columnNames);
|
||||
T setColumnNames(List<String> columnNames);
|
||||
|
||||
default T setColumnNames(String... columnNames) {
|
||||
return setColumnNames(columnNames == null ? null : Arrays.asList(columnNames));
|
||||
}
|
||||
default T setColumnNames(String... columnNames) {
|
||||
return setColumnNames(columnNames == null ? null : Arrays.asList(columnNames));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,25 +6,25 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface TableCreateBuilder extends SQLBuilder {
|
||||
|
||||
@NotNull String getTableName();
|
||||
@NotNull String getTableName();
|
||||
|
||||
@NotNull String getTableSettings();
|
||||
@NotNull String getTableSettings();
|
||||
|
||||
TableCreateBuilder setTableSettings(@NotNull String settings);
|
||||
TableCreateBuilder setTableSettings(@NotNull String settings);
|
||||
|
||||
SQLUpdateAction build();
|
||||
SQLUpdateAction build();
|
||||
|
||||
default TableCreateBuilder addColumn(@NotNull String columnName, @NotNull String settings) {
|
||||
return addColumn("`" + columnName + "` " + settings);
|
||||
}
|
||||
default TableCreateBuilder addColumn(@NotNull String columnName, @NotNull String settings) {
|
||||
return addColumn("`" + columnName + "` " + settings);
|
||||
}
|
||||
|
||||
TableCreateBuilder addColumn(@NotNull String column);
|
||||
TableCreateBuilder addColumn(@NotNull String column);
|
||||
|
||||
TableCreateBuilder setColumns(@NotNull String... columns);
|
||||
TableCreateBuilder setColumns(@NotNull String... columns);
|
||||
|
||||
default TableCreateBuilder defaultTablesSettings() {
|
||||
return setTableSettings("ENGINE=InnoDB DEFAULT CHARSET=utf8");
|
||||
}
|
||||
default TableCreateBuilder defaultTablesSettings() {
|
||||
return setTableSettings("ENGINE=InnoDB DEFAULT CHARSET=utf8");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,33 +5,33 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface TableQueryBuilder extends ConditionalBuilder<TableQueryBuilder, PreparedQueryAction> {
|
||||
|
||||
@NotNull String getTableName();
|
||||
@NotNull String getTableName();
|
||||
|
||||
/**
|
||||
* 选定用于查询的列名
|
||||
*
|
||||
* @param columnNames 列名
|
||||
* @return {@link TableQueryBuilder}
|
||||
*/
|
||||
TableQueryBuilder selectColumns(@NotNull String... columnNames);
|
||||
/**
|
||||
* 选定用于查询的列名
|
||||
*
|
||||
* @param columnNames 列名
|
||||
* @return {@link TableQueryBuilder}
|
||||
*/
|
||||
TableQueryBuilder selectColumns(@NotNull String... columnNames);
|
||||
|
||||
/**
|
||||
* 对结果进行排序
|
||||
*
|
||||
* @param columnName 排序使用的列名
|
||||
* @param asc 是否为正序排序 (为false则倒序排序)
|
||||
* @return {@link TableQueryBuilder}
|
||||
*/
|
||||
TableQueryBuilder orderBy(@NotNull String columnName, boolean asc);
|
||||
/**
|
||||
* 对结果进行排序
|
||||
*
|
||||
* @param columnName 排序使用的列名
|
||||
* @param asc 是否为正序排序 (为false则倒序排序)
|
||||
* @return {@link TableQueryBuilder}
|
||||
*/
|
||||
TableQueryBuilder orderBy(@NotNull String columnName, boolean asc);
|
||||
|
||||
/**
|
||||
* 限制查询条数,用于分页查询。
|
||||
*
|
||||
* @param start 开始数
|
||||
* @param end 结束条数
|
||||
* @return {@link TableQueryBuilder}
|
||||
* @since 0.2.6
|
||||
*/
|
||||
TableQueryBuilder setPageLimit(int start, int end);
|
||||
/**
|
||||
* 限制查询条数,用于分页查询。
|
||||
*
|
||||
* @param start 开始数
|
||||
* @param end 结束条数
|
||||
* @return {@link TableQueryBuilder}
|
||||
* @since 0.2.6
|
||||
*/
|
||||
TableQueryBuilder setPageLimit(int start, int end);
|
||||
|
||||
}
|
||||
|
||||
@@ -6,15 +6,15 @@ import java.util.LinkedHashMap;
|
||||
|
||||
public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, PreparedSQLUpdateAction> {
|
||||
|
||||
String getTableName();
|
||||
String getTableName();
|
||||
|
||||
UpdateBuilder setColumnValues(LinkedHashMap<String, Object> columnData);
|
||||
UpdateBuilder setColumnValues(LinkedHashMap<String, Object> columnData);
|
||||
|
||||
UpdateBuilder setColumnValues(String[] columnNames, Object[] columnValues);
|
||||
UpdateBuilder setColumnValues(String[] columnNames, Object[] columnValues);
|
||||
|
||||
default UpdateBuilder setColumnValues(String columnName, Object columnValue) {
|
||||
return setColumnValues(new String[]{columnName}, new Object[]{columnValue});
|
||||
}
|
||||
default UpdateBuilder setColumnValues(String columnName, Object columnValue) {
|
||||
return setColumnValues(new String[]{columnName}, new Object[]{columnValue});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ package cc.carm.lib.easysql.api.builder;
|
||||
|
||||
public interface UpsertBuilder {
|
||||
|
||||
String getTableName();
|
||||
String getTableName();
|
||||
|
||||
UpsertBuilder setColumnNames(String[] columnNames, String updateColumn);
|
||||
UpsertBuilder setColumnNames(String[] columnNames, String updateColumn);
|
||||
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.sql.SQLException;
|
||||
|
||||
public interface SQLFunction<T, R> {
|
||||
|
||||
@Nullable
|
||||
R apply(T t) throws SQLException;
|
||||
@Nullable
|
||||
R apply(T t) throws SQLException;
|
||||
|
||||
}
|
||||
|
||||
@@ -6,103 +6,103 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeDateUtils {
|
||||
public static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
public static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
public TimeDateUtils() {
|
||||
}
|
||||
public TimeDateUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到当前时间文本。
|
||||
*
|
||||
* @return 时间文本 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static String getCurrentTime() {
|
||||
return getTimeString(System.currentTimeMillis());
|
||||
}
|
||||
/**
|
||||
* 得到当前时间文本。
|
||||
*
|
||||
* @return 时间文本 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static String getCurrentTime() {
|
||||
return getTimeString(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个时间戳的文本
|
||||
*
|
||||
* @param timeMillis 时间戳
|
||||
* @return 时间文本 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static String getTimeString(long timeMillis) {
|
||||
return getFormat().format(new Date(timeMillis));
|
||||
}
|
||||
/**
|
||||
* 得到一个时间戳的文本
|
||||
*
|
||||
* @param timeMillis 时间戳
|
||||
* @return 时间文本 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static String getTimeString(long timeMillis) {
|
||||
return getFormat().format(new Date(timeMillis));
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个日期时间的文本
|
||||
*
|
||||
* @param time 日期时间
|
||||
* @return 时间文本 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static String getTimeString(Date time) {
|
||||
return getFormat().format(time);
|
||||
}
|
||||
/**
|
||||
* 得到一个日期时间的文本
|
||||
*
|
||||
* @param time 日期时间
|
||||
* @return 时间文本 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static String getTimeString(Date time) {
|
||||
return getFormat().format(time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个时间文本的时间戳
|
||||
*
|
||||
* @param timeString 时间文本
|
||||
* @return 时间戳 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static long parseTimeMillis(String timeString) {
|
||||
if (timeString == null) {
|
||||
return -1L;
|
||||
} else {
|
||||
try {
|
||||
return format.parse(timeString).getTime();
|
||||
} catch (ParseException var2) {
|
||||
return -1L;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 得到一个时间文本的时间戳
|
||||
*
|
||||
* @param timeString 时间文本
|
||||
* @return 时间戳 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static long parseTimeMillis(String timeString) {
|
||||
if (timeString == null) {
|
||||
return -1L;
|
||||
} else {
|
||||
try {
|
||||
return format.parse(timeString).getTime();
|
||||
} catch (ParseException var2) {
|
||||
return -1L;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到一个时间文本的对应日期实例
|
||||
*
|
||||
* @param timeString 时间文本
|
||||
* @return 日期实例 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static Date getTimeDate(String timeString) {
|
||||
if (timeString == null) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return format.parse(timeString);
|
||||
} catch (ParseException var2) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 得到一个时间文本的对应日期实例
|
||||
*
|
||||
* @param timeString 时间文本
|
||||
* @return 日期实例 格式{@link TimeDateUtils#getFormat()}
|
||||
*/
|
||||
public static Date getTimeDate(String timeString) {
|
||||
if (timeString == null) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return format.parse(timeString);
|
||||
} catch (ParseException var2) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将秒数转化为 DD:hh:mm:ss 格式
|
||||
*
|
||||
* @param allSeconds 秒数
|
||||
* @return DD:hh:mm:ss格式文本
|
||||
*/
|
||||
public static String toDHMSStyle(long allSeconds) {
|
||||
long days = allSeconds / 86400L;
|
||||
long hours = allSeconds % 86400L / 3600L;
|
||||
long minutes = allSeconds % 3600L / 60L;
|
||||
long seconds = allSeconds % 60L;
|
||||
String DateTimes;
|
||||
if (days > 0L) {
|
||||
DateTimes = days + "天" + (hours > 0L ? hours + "小时" : "") + (minutes > 0L ? minutes + "分钟" : "") + (seconds > 0L ? seconds + "秒" : "");
|
||||
} else if (hours > 0L) {
|
||||
DateTimes = hours + "小时" + (minutes > 0L ? minutes + "分钟" : "") + (seconds > 0L ? seconds + "秒" : "");
|
||||
} else if (minutes > 0L) {
|
||||
DateTimes = minutes + "分钟" + (seconds > 0L ? seconds + "秒" : "");
|
||||
} else {
|
||||
DateTimes = seconds + "秒";
|
||||
}
|
||||
/**
|
||||
* 将秒数转化为 DD:hh:mm:ss 格式
|
||||
*
|
||||
* @param allSeconds 秒数
|
||||
* @return DD:hh:mm:ss格式文本
|
||||
*/
|
||||
public static String toDHMSStyle(long allSeconds) {
|
||||
long days = allSeconds / 86400L;
|
||||
long hours = allSeconds % 86400L / 3600L;
|
||||
long minutes = allSeconds % 3600L / 60L;
|
||||
long seconds = allSeconds % 60L;
|
||||
String DateTimes;
|
||||
if (days > 0L) {
|
||||
DateTimes = days + "天" + (hours > 0L ? hours + "小时" : "") + (minutes > 0L ? minutes + "分钟" : "") + (seconds > 0L ? seconds + "秒" : "");
|
||||
} else if (hours > 0L) {
|
||||
DateTimes = hours + "小时" + (minutes > 0L ? minutes + "分钟" : "") + (seconds > 0L ? seconds + "秒" : "");
|
||||
} else if (minutes > 0L) {
|
||||
DateTimes = minutes + "分钟" + (seconds > 0L ? seconds + "秒" : "");
|
||||
} else {
|
||||
DateTimes = seconds + "秒";
|
||||
}
|
||||
|
||||
return DateTimes;
|
||||
}
|
||||
return DateTimes;
|
||||
}
|
||||
|
||||
public static DateFormat getFormat() {
|
||||
return format;
|
||||
}
|
||||
public static DateFormat getFormat() {
|
||||
return format;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ import java.util.UUID;
|
||||
|
||||
public class UUIDUtil {
|
||||
|
||||
public static UUID toUUID(String s) {
|
||||
if (s.length() == 36) {
|
||||
return UUID.fromString(s);
|
||||
} else {
|
||||
return UUID.fromString(s.substring(0, 8) + '-' + s.substring(8, 12) + '-' + s.substring(12, 16) + '-' + s.substring(16, 20) + '-' + s.substring(20));
|
||||
}
|
||||
}
|
||||
public static UUID toUUID(String s) {
|
||||
if (s.length() == 36) {
|
||||
return UUID.fromString(s);
|
||||
} else {
|
||||
return UUID.fromString(s.substring(0, 8) + '-' + s.substring(8, 12) + '-' + s.substring(12, 16) + '-' + s.substring(16, 20) + '-' + s.substring(20));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user