mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-14 03:15:55 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 479c23f985 | |||
| f606fd4efc | |||
| 81f065a85a | |||
| b6871b1000 | |||
| 934f9d841e | |||
| a88843f768 |
@@ -24,9 +24,9 @@ _SQLTable同时提供了有SQLManager参数与无参的操作方法,其中无
|
|||||||
首先,我们需要创建一个枚举类,[示例代码](../demo/src/main/java/DataTables1.java)如下所示:
|
首先,我们需要创建一个枚举类,[示例代码](../demo/src/main/java/DataTables1.java)如下所示:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import enums.cc.carm.lib.easysql.api.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import enums.cc.carm.lib.easysql.api.NumberType;
|
||||||
import cc.carm.lib.easysql.api.table.NamedSQLTable;
|
import table.cc.carm.lib.easysql.api.NamedSQLTable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -91,9 +91,9 @@ _该方法为本人最常用,也是最推荐的方法。_
|
|||||||
[示例代码](../demo/src/main/java/DataTables2.java)如下:
|
[示例代码](../demo/src/main/java/DataTables2.java)如下:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
import builder.cc.carm.lib.easysql.api.TableCreateBuilder;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import enums.cc.carm.lib.easysql.api.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import enums.cc.carm.lib.easysql.api.NumberType;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -29,11 +29,11 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
# Checkout the repository to the GitHub Actions runner
|
# Checkout the repository to the GitHub Actions runner
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
# Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis
|
# Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis
|
||||||
- name: Run Codacy Analysis CLI
|
- name: Run Codacy Analysis CLI
|
||||||
uses: codacy/codacy-analysis-cli-action@30783d03e758713bb5ed7b79292cfb14b9dd9a4a
|
uses: codacy/codacy-analysis-cli-action@d840f886c4bd4edc059706d09c6a1586111c540b
|
||||||
with:
|
with:
|
||||||
# Check https://github.com/codacy/codacy-analysis-cli#project-token to get your project token from your Codacy repository
|
# Check https://github.com/codacy/codacy-analysis-cli#project-token to get your project token from your Codacy repository
|
||||||
# You can also omit the token and run the tools that support default configurations
|
# You can also omit the token and run the tools that support default configurations
|
||||||
@@ -49,6 +49,6 @@ jobs:
|
|||||||
|
|
||||||
# Upload the SARIF file generated in the previous step
|
# Upload the SARIF file generated in the previous step
|
||||||
- name: Upload SARIF results file
|
- name: Upload SARIF results file
|
||||||
uses: github/codeql-action/upload-sarif@v4
|
uses: github/codeql-action/upload-sarif@v1
|
||||||
with:
|
with:
|
||||||
sarif_file: results.sarif
|
sarif_file: results.sarif
|
||||||
|
|||||||
@@ -38,11 +38,11 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@v4
|
uses: github/codeql-action/init@v1
|
||||||
with:
|
with:
|
||||||
languages: ${{ matrix.language }}
|
languages: ${{ matrix.language }}
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
@@ -53,7 +53,7 @@ jobs:
|
|||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
- name: Autobuild
|
- name: Autobuild
|
||||||
uses: github/codeql-action/autobuild@v4
|
uses: github/codeql-action/autobuild@v1
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
# 📚 https://git.io/JvXDl
|
# 📚 https://git.io/JvXDl
|
||||||
@@ -67,4 +67,4 @@ jobs:
|
|||||||
# make release
|
# make release
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@v4
|
uses: github/codeql-action/analyze@v1
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ jobs:
|
|||||||
name: "Publish Project (GitHub Packages)"
|
name: "Publish Project (GitHub Packages)"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v2
|
||||||
- name: "Set up JDK"
|
- name: "Set up JDK"
|
||||||
uses: actions/setup-java@v5
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
java-version: '11'
|
java-version: '11'
|
||||||
distribution: 'adopt'
|
distribution: 'adopt'
|
||||||
@@ -39,9 +39,9 @@ jobs:
|
|||||||
name: "Deploy Project (Central Repository)"
|
name: "Deploy Project (Central Repository)"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v2
|
||||||
- name: "Set up JDK"
|
- name: "Set up JDK"
|
||||||
uses: actions/setup-java@v5
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
java-version: '11'
|
java-version: '11'
|
||||||
distribution: 'adopt'
|
distribution: 'adopt'
|
||||||
@@ -63,9 +63,9 @@ jobs:
|
|||||||
name: "Deploy Project (GitHub Repository)"
|
name: "Deploy Project (GitHub Repository)"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v2
|
||||||
- name: "Set up JDK"
|
- name: "Set up JDK"
|
||||||
uses: actions/setup-java@v5
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
java-version: '11'
|
java-version: '11'
|
||||||
distribution: 'adopt'
|
distribution: 'adopt'
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v2
|
||||||
- name: "Set up JDK"
|
- name: "Set up JDK"
|
||||||
uses: actions/setup-java@v5
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
java-version: '11'
|
java-version: '11'
|
||||||
distribution: 'adopt'
|
distribution: 'adopt'
|
||||||
@@ -26,7 +26,7 @@ jobs:
|
|||||||
- name: "Target Stage"
|
- name: "Target Stage"
|
||||||
run: mkdir staging && cp */target/*.jar staging
|
run: mkdir staging && cp */target/*.jar staging
|
||||||
- name: "Upload artifact"
|
- name: "Upload artifact"
|
||||||
uses: actions/upload-artifact@v7
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
name: Artifact
|
name: Artifact
|
||||||
path: staging
|
path: staging
|
||||||
|
|||||||
+1
-2
@@ -1,4 +1,3 @@
|
|||||||
/.idea/
|
/.idea/
|
||||||
**/target/
|
**/target/
|
||||||
**.iml
|
**.iml
|
||||||
demo/logs
|
|
||||||
@@ -200,9 +200,11 @@ dependencies {
|
|||||||
|
|
||||||
## 支持与捐赠
|
## 支持与捐赠
|
||||||
|
|
||||||
若您觉得本插件做的不错,您可以通过捐赠支持我!
|
若您觉得本插件做的不错,您可以通过捐赠支持我! 感谢您对开源项目的支持!
|
||||||
|
|
||||||
|
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
|
||||||
|
[](https://www.jetbrains.com/?from=https://github.com/CarmJos/EasySQL)
|
||||||
|
|
||||||
感谢您对开源项目的支持!
|
|
||||||
|
|
||||||
## 开源协议
|
## 开源协议
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -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.4.7</version>
|
<version>0.4.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
<version>2.0.17</version>
|
<version>1.7.36</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package cc.carm.lib.easysql.api;
|
package cc.carm.lib.easysql.api;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SQLBuilder 是用于构建SQL语句以生成SQLAction执行操作的中间类。
|
* SQLBuilder 是用于构建SQL语句以生成SQLAction执行操作的中间类。
|
||||||
* <br>其连接了{@link SQLManager} 与 {@link SQLAction} ,避免大量的代码堆积
|
* <br>其连接了{@link SQLManager} 与 {@link SQLBaseAction} ,避免大量的代码堆积
|
||||||
* <br>也是本接口的核心功能所在
|
* <br>也是本接口的核心功能所在
|
||||||
*
|
*
|
||||||
* @author CarmJos
|
* @author CarmJos
|
||||||
|
|||||||
@@ -1,140 +1,26 @@
|
|||||||
package cc.carm.lib.easysql.api;
|
package cc.carm.lib.easysql.api;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.query.PreparedSQLQueryAction;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.query.SQLQueryAction;
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLBatchUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.SQLBatchUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.SQLUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.builder.*;
|
import cc.carm.lib.easysql.api.builder.*;
|
||||||
|
import cc.carm.lib.easysql.api.enums.IsolationLevel;
|
||||||
import cc.carm.lib.easysql.api.function.SQLBiFunction;
|
import cc.carm.lib.easysql.api.function.SQLBiFunction;
|
||||||
import cc.carm.lib.easysql.api.function.SQLDebugHandler;
|
|
||||||
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.migrate.SQLMigrate;
|
import cc.carm.lib.easysql.api.transaction.SQLTransaction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
/**
|
public interface SQLManager extends SQLSource {
|
||||||
* SQLManager 是EasySQL的核心类,用于管理数据库连接,提供数据库操作的方法。
|
|
||||||
*
|
|
||||||
* @author CarmJos
|
|
||||||
*/
|
|
||||||
public interface SQLManager {
|
|
||||||
|
|
||||||
Logger getLogger();
|
|
||||||
|
|
||||||
boolean isDebugMode();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取用于执行 {@link SQLAction#executeAsync()} 的线程池。
|
|
||||||
* <br> 默认线程池为 {@link #defaultExecutorPool(String)} 。
|
|
||||||
*
|
|
||||||
* @return {@link ExecutorService}
|
|
||||||
*/
|
|
||||||
@NotNull ExecutorService getExecutorPool();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定用于执行 {@link SQLAction#executeAsync()} 的线程池.
|
|
||||||
* <br> 默认线程池为 {@link #defaultExecutorPool(String)} 。
|
|
||||||
*
|
|
||||||
* @param executorPool {@link ExecutorService}
|
|
||||||
*/
|
|
||||||
void setExecutorPool(@NotNull ExecutorService executorPool);
|
|
||||||
|
|
||||||
static ExecutorService defaultExecutorPool(String threadName) {
|
|
||||||
return Executors.newFixedThreadPool(4, r -> {
|
|
||||||
Thread thread = new Thread(r, threadName);
|
|
||||||
thread.setDaemon(true);
|
|
||||||
return thread;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定是否启用调试模式。
|
|
||||||
* 启用调试模式后,会在每次执行SQL语句时,调用 {@link #getDebugHandler()} 来输出调试信息。
|
|
||||||
*
|
|
||||||
* @param debugMode 是否启用调试模式
|
|
||||||
*/
|
|
||||||
void setDebugMode(@NotNull Supplier<@NotNull Boolean> debugMode);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定是否启用调试模式。
|
|
||||||
* 启用调试模式后,会在每次执行SQL语句时,调用 {@link #getDebugHandler()} 来输出调试信息。
|
|
||||||
*
|
|
||||||
* @param enable 是否启用调试模式
|
|
||||||
*/
|
|
||||||
default void setDebugMode(boolean enable) {
|
|
||||||
setDebugMode(() -> enable);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取调试处理器,用于处理调试信息。
|
|
||||||
*
|
|
||||||
* @return {@link SQLDebugHandler}
|
|
||||||
*/
|
|
||||||
@NotNull SQLDebugHandler getDebugHandler();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定调试处理器,默认为 {@link SQLDebugHandler#defaultHandler(Logger)} 。
|
|
||||||
*
|
|
||||||
* @param debugHandler {@link SQLDebugHandler}
|
|
||||||
*/
|
|
||||||
void setDebugHandler(@NotNull SQLDebugHandler debugHandler);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到连接池源
|
|
||||||
*
|
|
||||||
* @return DataSource
|
|
||||||
*/
|
|
||||||
@NotNull DataSource getDataSource();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到一个数据库连接实例
|
|
||||||
*
|
|
||||||
* @return Connection
|
|
||||||
* @throws SQLException 见 {@link DataSource#getConnection()}
|
|
||||||
*/
|
|
||||||
@NotNull Connection getConnection() throws SQLException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到正使用的查询。
|
|
||||||
*
|
|
||||||
* @return 查询列表
|
|
||||||
*/
|
|
||||||
@NotNull Map<UUID, SQLQuery> getActiveQuery();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取改管理器提供的默认异常处理器。
|
|
||||||
* 若未使用过 {@link #setExceptionHandler(SQLExceptionHandler)} 方法,
|
|
||||||
* 则默认返回 {@link SQLExceptionHandler#detailed(Logger)} 。
|
|
||||||
*
|
|
||||||
* @return {@link SQLExceptionHandler}
|
|
||||||
*/
|
|
||||||
@NotNull SQLExceptionHandler getExceptionHandler();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定通用的异常处理器。
|
|
||||||
* <br> 在使用 {@link SQLAction#execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。
|
|
||||||
* <br> 若该方法传入参数为 null,则会使用 {@link SQLExceptionHandler#detailed(Logger)} 。
|
|
||||||
*
|
|
||||||
* @param handler 异常处理器
|
|
||||||
*/
|
|
||||||
void setExceptionHandler(@Nullable SQLExceptionHandler handler);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行一条不需要返回结果的SQL语句(多用于UPDATE、REPLACE、DELETE方法)
|
* 执行一条不需要返回结果的SQL语句(多用于UPDATE、REPLACE、DELETE方法)
|
||||||
@@ -162,11 +48,10 @@ public interface SQLManager {
|
|||||||
* @param sql SQL语句内容
|
* @param sql SQL语句内容
|
||||||
* @param paramsBatch SQL语句中对应?的参数组
|
* @param paramsBatch SQL语句中对应?的参数组
|
||||||
* @return 对应参数返回的行数
|
* @return 对应参数返回的行数
|
||||||
* @see PreparedSQLUpdateBatchAction
|
* @see PreparedSQLBatchUpdateAction
|
||||||
*/
|
*/
|
||||||
@Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
|
@Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行多条不需要返回结果的SQL。
|
* 执行多条不需要返回结果的SQL。
|
||||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||||
@@ -174,7 +59,7 @@ public interface SQLManager {
|
|||||||
* @param sql SQL语句内容
|
* @param sql SQL语句内容
|
||||||
* @param moreSQL 更多SQL语句内容
|
* @param moreSQL 更多SQL语句内容
|
||||||
* @return 对应参数返回的行数
|
* @return 对应参数返回的行数
|
||||||
* @see SQLUpdateBatchAction
|
* @see SQLBatchUpdateAction
|
||||||
*/
|
*/
|
||||||
@Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL);
|
@Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL);
|
||||||
|
|
||||||
@@ -186,6 +71,92 @@ public interface SQLManager {
|
|||||||
*/
|
*/
|
||||||
@Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch);
|
@Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch);
|
||||||
|
|
||||||
|
default @NotNull SQLTransaction createTransaction() {
|
||||||
|
return createTransaction(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull SQLTransaction createTransaction(@Nullable IsolationLevel level);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新建一个查询。
|
||||||
|
*
|
||||||
|
* @return {@link QueryBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull QueryBuilder<SQLQueryAction.Advanced, PreparedSQLQueryAction.Advanced> createQuery();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一条插入操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link InsertBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull InsertBuilder<PreparedSQLUpdateAction.Advanced<Integer>> insertInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建支持多组数据的插入操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link InsertBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull InsertBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> insertBatchInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一条替换操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link ReplaceBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull ReplaceBuilder<PreparedSQLUpdateAction.Advanced<Integer>> replaceInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建支持多组数据的替换操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link ReplaceBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull ReplaceBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> replaceBatchInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建更新操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link UpdateBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull UpdateBuilder<PreparedSQLUpdateAction.Advanced<Integer>> updateInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建删除操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link DeleteBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull DeleteBuilder<PreparedSQLUpdateAction.Advanced<Integer>> deleteFrom(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在库中创建一个表。
|
||||||
|
*
|
||||||
|
* @param tableName 表名
|
||||||
|
* @return {@link TableCreateBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull TableCreateBuilder createTable(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对库中的某个表执行更改。
|
||||||
|
*
|
||||||
|
* @param tableName 表名
|
||||||
|
* @return {@link TableAlterBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull TableAlterBuilder alterTable(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快速获取表的部分元数据。
|
||||||
|
* <br> 当需要获取其他元数据时,请使用 {@link #fetchMetadata(SQLFunction, SQLFunction)} 方法。
|
||||||
|
*
|
||||||
|
* @param tablePattern 表名通配符
|
||||||
|
* @return {@link TableMetadataBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull TableMetadataBuilder fetchTableMetadata(@NotNull String tablePattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取并操作 {@link DatabaseMetaData} 以得到需要的数据库消息。
|
* 获取并操作 {@link DatabaseMetaData} 以得到需要的数据库消息。
|
||||||
*
|
*
|
||||||
@@ -234,91 +205,4 @@ public interface SQLManager {
|
|||||||
<R> CompletableFuture<R> fetchMetadata(@NotNull SQLBiFunction<DatabaseMetaData, Connection, ResultSet> supplier,
|
<R> CompletableFuture<R> fetchMetadata(@NotNull SQLBiFunction<DatabaseMetaData, Connection, ResultSet> supplier,
|
||||||
@NotNull SQLFunction<@NotNull ResultSet, R> reader);
|
@NotNull SQLFunction<@NotNull ResultSet, R> reader);
|
||||||
|
|
||||||
/**
|
|
||||||
* 在库中创建一个表。
|
|
||||||
*
|
|
||||||
* @param tableName 表名
|
|
||||||
* @return {@link TableCreateBuilder}
|
|
||||||
*/
|
|
||||||
TableCreateBuilder createTable(@NotNull String tableName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 对库中的某个表执行更改。
|
|
||||||
*
|
|
||||||
* @param tableName 表名
|
|
||||||
* @return {@link TableAlterBuilder}
|
|
||||||
*/
|
|
||||||
TableAlterBuilder alterTable(@NotNull String tableName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 快速获取表的部分元数据。
|
|
||||||
* <br> 当需要获取其他元数据时,请使用 {@link #fetchMetadata(SQLFunction, SQLFunction)} 方法。
|
|
||||||
*
|
|
||||||
* @param tablePattern 表名通配符
|
|
||||||
* @return {@link TableMetadataBuilder}
|
|
||||||
*/
|
|
||||||
TableMetadataBuilder fetchTableMetadata(@NotNull String tablePattern);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 新建一个查询。
|
|
||||||
*
|
|
||||||
* @return {@link QueryBuilder}
|
|
||||||
*/
|
|
||||||
QueryBuilder createQuery();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 新建一个迁移操作。
|
|
||||||
*
|
|
||||||
* @return {@link SQLMigrate}
|
|
||||||
*/
|
|
||||||
SQLMigrate createMigrate();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建一条插入操作。
|
|
||||||
*
|
|
||||||
* @param tableName 目标表名
|
|
||||||
* @return {@link InsertBuilder}
|
|
||||||
*/
|
|
||||||
InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull String tableName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建支持多组数据的插入操作。
|
|
||||||
*
|
|
||||||
* @param tableName 目标表名
|
|
||||||
* @return {@link InsertBuilder}
|
|
||||||
*/
|
|
||||||
InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull String tableName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建一条替换操作。
|
|
||||||
*
|
|
||||||
* @param tableName 目标表名
|
|
||||||
* @return {@link ReplaceBuilder}
|
|
||||||
*/
|
|
||||||
ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull String tableName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建支持多组数据的替换操作。
|
|
||||||
*
|
|
||||||
* @param tableName 目标表名
|
|
||||||
* @return {@link ReplaceBuilder}
|
|
||||||
*/
|
|
||||||
ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull String tableName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建更新操作。
|
|
||||||
*
|
|
||||||
* @param tableName 目标表名
|
|
||||||
* @return {@link UpdateBuilder}
|
|
||||||
*/
|
|
||||||
UpdateBuilder createUpdate(@NotNull String tableName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建删除操作。
|
|
||||||
*
|
|
||||||
* @param tableName 目标表名
|
|
||||||
* @return {@link DeleteBuilder}
|
|
||||||
*/
|
|
||||||
DeleteBuilder createDelete(@NotNull String tableName);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.api;
|
package cc.carm.lib.easysql.api;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
import cc.carm.lib.easysql.api.action.query.PreparedSQLQueryAction;
|
||||||
import cc.carm.lib.easysql.api.action.query.QueryAction;
|
import cc.carm.lib.easysql.api.action.query.SQLQueryAction;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -43,18 +43,14 @@ public interface SQLQuery extends AutoCloseable {
|
|||||||
SQLManager getManager();
|
SQLManager getManager();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 得到承载该SQLQuery的对应{@link QueryAction}
|
* 得到承载该SQLQuery的对应{@link SQLQueryAction}
|
||||||
*
|
*
|
||||||
* @return {@link QueryAction} 或 {@link PreparedQueryAction}
|
* @return {@link SQLQueryAction} 或 {@link PreparedSQLQueryAction}
|
||||||
*/
|
*/
|
||||||
QueryAction getAction();
|
SQLQueryAction<?> getAction();
|
||||||
|
|
||||||
ResultSet getResultSet();
|
ResultSet getResultSet();
|
||||||
|
|
||||||
default boolean containsResult(String columnName) throws SQLException {
|
|
||||||
return getResultSet() != null && getResultSet().getObject(columnName) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 得到设定的SQL语句
|
* 得到设定的SQL语句
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
package cc.carm.lib.easysql.api;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLDebugHandler;
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public interface SQLSource {
|
||||||
|
|
||||||
|
@NotNull Logger getLogger();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用于执行 {@link SQLAdvancedAction#executeAsync()} 的线程池。
|
||||||
|
* <br> 默认线程池为 {@link #defaultExecutorPool(String)} 。
|
||||||
|
*
|
||||||
|
* @return {@link ExecutorService}
|
||||||
|
*/
|
||||||
|
@NotNull ExecutorService getExecutorPool();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定用于执行 {@link SQLAdvancedAction#executeAsync()} 的线程池.
|
||||||
|
* <br> 默认线程池为 {@link #defaultExecutorPool(String)} 。
|
||||||
|
*
|
||||||
|
* @param executorPool {@link ExecutorService}
|
||||||
|
*/
|
||||||
|
void setExecutorPool(@NotNull ExecutorService executorPool);
|
||||||
|
|
||||||
|
static @NotNull ExecutorService defaultExecutorPool(@NotNull String threadName) {
|
||||||
|
return Executors.newFixedThreadPool(4, r -> {
|
||||||
|
Thread thread = new Thread(r, threadName);
|
||||||
|
thread.setDaemon(true);
|
||||||
|
return thread;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isDebugMode();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定是否启用调试模式。
|
||||||
|
* 启用调试模式后,会在每次执行SQL语句时,调用 {@link #getDebugHandler()} 来输出调试信息。
|
||||||
|
*
|
||||||
|
* @param debugMode 是否启用调试模式
|
||||||
|
*/
|
||||||
|
void setDebugMode(@NotNull Supplier<@NotNull Boolean> debugMode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定是否启用调试模式。
|
||||||
|
* 启用调试模式后,会在每次执行SQL语句时,调用 {@link #getDebugHandler()} 来输出调试信息。
|
||||||
|
*
|
||||||
|
* @param enable 是否启用调试模式
|
||||||
|
*/
|
||||||
|
default void setDebugMode(boolean enable) {
|
||||||
|
setDebugMode(() -> enable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取调试处理器,用于处理调试信息。
|
||||||
|
*
|
||||||
|
* @return {@link SQLDebugHandler}
|
||||||
|
*/
|
||||||
|
@NotNull SQLDebugHandler getDebugHandler();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定调试处理器,默认为 {@link SQLDebugHandler#defaultHandler(Logger)} 。
|
||||||
|
*
|
||||||
|
* @param debugHandler {@link SQLDebugHandler}
|
||||||
|
*/
|
||||||
|
void setDebugHandler(@NotNull SQLDebugHandler debugHandler);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到连接池源
|
||||||
|
*
|
||||||
|
* @return DataSource
|
||||||
|
*/
|
||||||
|
@NotNull DataSource getDataSource();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到一个数据库连接实例
|
||||||
|
*
|
||||||
|
* @return Connection
|
||||||
|
* @throws SQLException 见 {@link DataSource#getConnection()}
|
||||||
|
*/
|
||||||
|
@NotNull Connection getConnection() throws SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到正使用的查询。
|
||||||
|
*
|
||||||
|
* @return 查询列表
|
||||||
|
*/
|
||||||
|
@NotNull Map<UUID, SQLQuery> getActiveQueries();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取改管理器提供的默认异常处理器。
|
||||||
|
* 若未使用过 {@link #setExceptionHandler(SQLExceptionHandler)} 方法,
|
||||||
|
* 则默认返回 {@link SQLExceptionHandler#detailed(Logger)} 。
|
||||||
|
*
|
||||||
|
* @return {@link SQLExceptionHandler}
|
||||||
|
*/
|
||||||
|
@NotNull SQLExceptionHandler getExceptionHandler();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定通用的异常处理器。
|
||||||
|
* <br> 在使用 {@link SQLAdvancedAction#execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。
|
||||||
|
* <br> 若该方法传入参数为 null,则会使用 {@link SQLExceptionHandler#detailed(Logger)} 。
|
||||||
|
*
|
||||||
|
* @param handler 异常处理器
|
||||||
|
*/
|
||||||
|
void setExceptionHandler(@Nullable SQLExceptionHandler handler);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.action;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public interface PreparedSQLUpdateAction<T extends Number> extends SQLUpdateAction<T> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定SQL语句中所有 ? 对应的参数
|
|
||||||
*
|
|
||||||
* @param params 参数内容
|
|
||||||
* @return {@link PreparedSQLUpdateAction}
|
|
||||||
*/
|
|
||||||
PreparedSQLUpdateAction<T> setParams(Object... params);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定SQL语句中所有 ? 对应的参数
|
|
||||||
*
|
|
||||||
* @param params 参数内容
|
|
||||||
* @return {@link PreparedSQLUpdateAction}
|
|
||||||
* @since 0.4.0
|
|
||||||
*/
|
|
||||||
PreparedSQLUpdateAction<T> setParams(@Nullable Iterable<Object> params);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.action;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface PreparedSQLUpdateBatchAction<T extends Number> extends SQLAction<List<T>> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定多组SQL语句中所有 ? 对应的参数
|
|
||||||
*
|
|
||||||
* @param allParams 所有参数内容
|
|
||||||
* @return {@link PreparedSQLUpdateBatchAction}
|
|
||||||
*/
|
|
||||||
PreparedSQLUpdateBatchAction<T> setAllParams(Iterable<Object[]> allParams);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加一组SQL语句中所有 ? 对应的参数
|
|
||||||
*
|
|
||||||
* @param params 参数内容
|
|
||||||
* @return {@link PreparedSQLUpdateBatchAction}
|
|
||||||
*/
|
|
||||||
PreparedSQLUpdateBatchAction<T> addParamsBatch(Object... params);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定该操作返回自增键序列。
|
|
||||||
*
|
|
||||||
* @return {@link SQLUpdateAction}
|
|
||||||
*/
|
|
||||||
PreparedSQLUpdateBatchAction<T> returnGeneratedKeys();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定该操作返回自增键序列。
|
|
||||||
*
|
|
||||||
* @param keyTypeClass 自增序列的数字类型
|
|
||||||
* @param <N> 自增键序列类型 {@link Number}
|
|
||||||
* @return {@link SQLUpdateAction}
|
|
||||||
* @since 0.4.0
|
|
||||||
*/
|
|
||||||
<N extends Number> PreparedSQLUpdateBatchAction<N> returnGeneratedKeys(Class<N> keyTypeClass);
|
|
||||||
|
|
||||||
}
|
|
||||||
+9
-110
@@ -1,4 +1,4 @@
|
|||||||
package cc.carm.lib.easysql.api;
|
package cc.carm.lib.easysql.api.action;
|
||||||
|
|
||||||
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;
|
||||||
@@ -9,15 +9,11 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SQLAction 是用于承载SQL语句并进行处理、返回的基本类。
|
* SQLAdvancedAction 是用于承载SQL语句并进行处理、返回的高级类。
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, SQLExceptionHandler)}
|
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, SQLExceptionHandler)}
|
||||||
@@ -33,74 +29,9 @@ import java.util.concurrent.TimeUnit;
|
|||||||
*
|
*
|
||||||
* @param <T> 需要返回的类型
|
* @param <T> 需要返回的类型
|
||||||
* @author CarmJos
|
* @author CarmJos
|
||||||
* @since 0.0.1
|
* @since 0.5.0
|
||||||
*/
|
*/
|
||||||
public interface SQLAction<T> {
|
public interface SQLAdvancedAction<T> extends SQLBaseAction<T> {
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到该Action的UUID
|
|
||||||
*
|
|
||||||
* @return UUID
|
|
||||||
*/
|
|
||||||
@NotNull UUID getActionUUID();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到短八位格式的UUID
|
|
||||||
*
|
|
||||||
* @return UUID(8)
|
|
||||||
*/
|
|
||||||
@NotNull String getShortID();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到该Action的创建时间。
|
|
||||||
* <br>注意,此处获得的时间非时间戳毫秒数,仅用于计算耗时。
|
|
||||||
*
|
|
||||||
* @return 创建时间 (毫秒)
|
|
||||||
*/
|
|
||||||
default long getCreateTime() {
|
|
||||||
return getCreateTime(TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到该Action的创建时间
|
|
||||||
* <br>注意,此处获得的时间非时间戳毫秒数,仅用于计算耗时。
|
|
||||||
*
|
|
||||||
* @param unit 时间单位
|
|
||||||
* @return 创建时间
|
|
||||||
*/
|
|
||||||
long getCreateTime(TimeUnit unit);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到该Action所要执行的源SQL语句
|
|
||||||
*
|
|
||||||
* @return 源SQL语句
|
|
||||||
*/
|
|
||||||
@NotNull String getSQLContent();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到该Action所要执行的源SQL语句列表。
|
|
||||||
*
|
|
||||||
* @return 源SQL语句列表
|
|
||||||
*/
|
|
||||||
default @NotNull List<String> getSQLContents() {
|
|
||||||
return Collections.singletonList(getSQLContent());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到承载该Action的对应{@link SQLManager}
|
|
||||||
*
|
|
||||||
* @return {@link SQLManager}
|
|
||||||
*/
|
|
||||||
@NotNull SQLManager getManager();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行该Action对应的SQL语句
|
|
||||||
*
|
|
||||||
* @return 指定数据类型
|
|
||||||
* @throws SQLException 当SQL操作出现问题时抛出
|
|
||||||
*/
|
|
||||||
@NotNull T execute() throws SQLException;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行语句并返回值
|
* 执行语句并返回值
|
||||||
@@ -149,40 +80,6 @@ public interface SQLAction<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行语句并处理返回值
|
|
||||||
*
|
|
||||||
* @param function 处理方法
|
|
||||||
* @param <R> 需要返回的内容
|
|
||||||
* @return 指定类型数据
|
|
||||||
* @throws SQLException 当SQL操作出现问题时抛出
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
default <R> R executeFunction(@NotNull SQLFunction<@NotNull T, R> function) throws SQLException {
|
|
||||||
return executeFunction(function, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行语句并处理返回值
|
|
||||||
*
|
|
||||||
* @param function 处理方法
|
|
||||||
* @param defaultResult 默认结果,若处理后的结果为null,则返回该值
|
|
||||||
* @param <R> 需要返回的内容
|
|
||||||
* @return 指定类型数据
|
|
||||||
* @throws SQLException 当SQL操作出现问题时抛出
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
@Contract("_,!null -> !null")
|
|
||||||
default <R> R executeFunction(@NotNull SQLFunction<@NotNull T, R> function,
|
|
||||||
@Nullable R defaultResult) throws SQLException {
|
|
||||||
try {
|
|
||||||
R result = function.apply(execute());
|
|
||||||
return result == null ? defaultResult : result;
|
|
||||||
} catch (SQLException exception) {
|
|
||||||
throw new SQLException(exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步执行SQL语句,采用默认异常处理,无需返回值。
|
* 异步执行SQL语句,采用默认异常处理,无需返回值。
|
||||||
*/
|
*/
|
||||||
@@ -203,7 +100,7 @@ public interface SQLAction<T> {
|
|||||||
* 异步执行SQL语句
|
* 异步执行SQL语句
|
||||||
*
|
*
|
||||||
* @param success 成功时的操作
|
* @param success 成功时的操作
|
||||||
* @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()}
|
* @param failure 异常处理器 默认为 {@link SQLAdvancedAction#defaultExceptionHandler()}
|
||||||
*/
|
*/
|
||||||
void executeAsync(@Nullable SQLHandler<T> success,
|
void executeAsync(@Nullable SQLHandler<T> success,
|
||||||
@Nullable SQLExceptionHandler failure);
|
@Nullable SQLExceptionHandler failure);
|
||||||
@@ -224,6 +121,7 @@ public interface SQLAction<T> {
|
|||||||
*/
|
*/
|
||||||
<R> @NotNull CompletableFuture<R> executeFuture(@NotNull SQLFunction<T, R> handler);
|
<R> @NotNull CompletableFuture<R> executeFuture(@NotNull SQLFunction<T, R> handler);
|
||||||
|
|
||||||
|
|
||||||
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
|
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
|
||||||
if (handler == null) handler = defaultExceptionHandler();
|
if (handler == null) handler = defaultExceptionHandler();
|
||||||
handler.accept(exception, this);
|
handler.accept(exception, this);
|
||||||
@@ -237,7 +135,7 @@ public interface SQLAction<T> {
|
|||||||
* @return {@link SQLExceptionHandler}
|
* @return {@link SQLExceptionHandler}
|
||||||
*/
|
*/
|
||||||
default SQLExceptionHandler defaultExceptionHandler() {
|
default SQLExceptionHandler defaultExceptionHandler() {
|
||||||
return getManager().getExceptionHandler();
|
return getSource().getExceptionHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -248,7 +146,8 @@ public interface SQLAction<T> {
|
|||||||
* @param handler 异常处理器
|
* @param handler 异常处理器
|
||||||
*/
|
*/
|
||||||
default void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
|
default void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
|
||||||
getManager().setExceptionHandler(handler);
|
getSource().setExceptionHandler(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package cc.carm.lib.easysql.api.action;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLSource;
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQLBaseAction 是用于承载SQL语句并进行处理、返回的基本类。
|
||||||
|
*
|
||||||
|
* @param <T> 需要返回的类型
|
||||||
|
* @author CarmJos
|
||||||
|
* @since 0.0.1
|
||||||
|
*/
|
||||||
|
public interface SQLBaseAction<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该Action的UUID
|
||||||
|
*
|
||||||
|
* @return UUID
|
||||||
|
*/
|
||||||
|
@NotNull UUID getActionUUID();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到短八位格式的UUID
|
||||||
|
*
|
||||||
|
* @return UUID(8)
|
||||||
|
*/
|
||||||
|
@NotNull String getShortID();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该Action的创建时间。
|
||||||
|
* <br>注意,此处获得的时间非时间戳毫秒数,仅用于计算耗时。
|
||||||
|
*
|
||||||
|
* @return 创建时间 (毫秒)
|
||||||
|
*/
|
||||||
|
default long getCreateTime() {
|
||||||
|
return getCreateTime(TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该Action的创建时间
|
||||||
|
* <br>注意,此处获得的时间非时间戳毫秒数,仅用于计算耗时。
|
||||||
|
*
|
||||||
|
* @param unit 时间单位
|
||||||
|
* @return 创建时间
|
||||||
|
*/
|
||||||
|
long getCreateTime(TimeUnit unit);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该Action所要执行的源SQL语句
|
||||||
|
*
|
||||||
|
* @return 源SQL语句
|
||||||
|
*/
|
||||||
|
@NotNull String getSQLContent();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到该Action所要执行的源SQL语句列表。
|
||||||
|
*
|
||||||
|
* @return 源SQL语句列表
|
||||||
|
*/
|
||||||
|
default @NotNull List<String> getSQLContents() {
|
||||||
|
return Collections.singletonList(getSQLContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到承载该Action的对应{@link SQLSource}
|
||||||
|
*
|
||||||
|
* @return {@link SQLSource}
|
||||||
|
*/
|
||||||
|
@NotNull SQLSource getSource();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行该Action对应的SQL语句
|
||||||
|
*
|
||||||
|
* @return 指定数据类型
|
||||||
|
* @throws SQLException 当SQL操作出现问题时抛出
|
||||||
|
*/
|
||||||
|
@NotNull T execute() throws SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行语句并处理返回值
|
||||||
|
*
|
||||||
|
* @param function 处理方法
|
||||||
|
* @param <R> 需要返回的内容
|
||||||
|
* @return 指定类型数据
|
||||||
|
* @throws SQLException 当SQL操作出现问题时抛出
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
default <R> R executeFunction(@NotNull SQLFunction<@NotNull T, R> function) throws SQLException {
|
||||||
|
return executeFunction(function, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行语句并处理返回值
|
||||||
|
*
|
||||||
|
* @param function 处理方法
|
||||||
|
* @param defaultResult 默认结果,若处理后的结果为null,则返回该值
|
||||||
|
* @param <R> 需要返回的内容
|
||||||
|
* @return 指定类型数据
|
||||||
|
* @throws SQLException 当SQL操作出现问题时抛出
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
@Contract("_,!null -> !null")
|
||||||
|
default <R> R executeFunction(@NotNull SQLFunction<@NotNull T, R> function,
|
||||||
|
@Nullable R defaultResult) throws SQLException {
|
||||||
|
try {
|
||||||
|
R result = function.apply(execute());
|
||||||
|
return result == null ? defaultResult : result;
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
throw new SQLException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.action;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
|
|
||||||
public interface SQLUpdateAction<T extends Number> extends SQLAction<T> {
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定该操作返回自增键序列。
|
|
||||||
*
|
|
||||||
* @return {@link SQLUpdateAction}
|
|
||||||
*/
|
|
||||||
SQLUpdateAction<T> returnGeneratedKey();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设定该操作返回自增键序列。
|
|
||||||
*
|
|
||||||
* @param keyTypeClass 自增序列的数字类型
|
|
||||||
* @param <N> 自增键序列类型 {@link Number}
|
|
||||||
* @return {@link SQLUpdateAction}
|
|
||||||
* @since 0.4.0
|
|
||||||
*/
|
|
||||||
<N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.action;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@SuppressWarnings("UnusedReturnValue")
|
|
||||||
public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加一条批量执行的SQL语句
|
|
||||||
*
|
|
||||||
* @param sql SQL语句
|
|
||||||
* @return {@link SQLUpdateBatchAction}
|
|
||||||
*/
|
|
||||||
SQLUpdateBatchAction addBatch(@NotNull String sql);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
default @NotNull String getSQLContent() {
|
|
||||||
return getSQLContents().get(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@NotNull List<String> getSQLContents();
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.action.query;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
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 Iterable<Object> params);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 直接对 {@link PreparedStatement} 进行处理
|
|
||||||
*
|
|
||||||
* @param statement {@link Consumer} 处理操作
|
|
||||||
* 若为空则不进行处理
|
|
||||||
* @return {@link PreparedQueryAction}
|
|
||||||
*/
|
|
||||||
PreparedQueryAction handleStatement(@Nullable Consumer<PreparedStatement> statement);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package cc.carm.lib.easysql.api.action.query;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
|
||||||
|
public interface PreparedSQLQueryAction<B extends PreparedSQLQueryAction<B>> extends SQLQueryAction<B> {
|
||||||
|
|
||||||
|
interface Base extends PreparedSQLQueryAction<Base> {
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Advanced extends PreparedSQLQueryAction<Advanced>, SQLAdvancedAction<SQLQuery> {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定SQL语句中所有 ? 对应的参数
|
||||||
|
*
|
||||||
|
* @param params 参数内容
|
||||||
|
* @return {@link PreparedSQLQueryAction}
|
||||||
|
*/
|
||||||
|
@NotNull B setParams(@Nullable Object... params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定SQL语句中所有 ? 对应的参数
|
||||||
|
*
|
||||||
|
* @param params 参数内容
|
||||||
|
* @return {@link PreparedSQLQueryAction}
|
||||||
|
*/
|
||||||
|
@NotNull B setParams(@Nullable Iterable<Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 直接对 {@link PreparedStatement} 进行处理
|
||||||
|
*
|
||||||
|
* @param statement 通过 {@link SQLHandler} 处理操作
|
||||||
|
* 若为空则不进行处理
|
||||||
|
* @return {@link PreparedSQLQueryAction}
|
||||||
|
*/
|
||||||
|
@NotNull B handleStatement(@Nullable SQLHandler<PreparedStatement> statement);
|
||||||
|
|
||||||
|
}
|
||||||
+10
-3
@@ -1,7 +1,8 @@
|
|||||||
package cc.carm.lib.easysql.api.action.query;
|
package cc.carm.lib.easysql.api.action.query;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
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;
|
||||||
@@ -19,7 +20,7 @@ import java.sql.SQLException;
|
|||||||
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
||||||
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
||||||
*
|
*
|
||||||
* <li>异步执行 {@link #executeAsync(SQLHandler, SQLExceptionHandler)}
|
* <li>异步执行 {@link Advanced#executeAsync(SQLHandler, SQLExceptionHandler)}
|
||||||
* <br>异步执行时将提供成功与异常两种处理方式
|
* <br>异步执行时将提供成功与异常两种处理方式
|
||||||
* <br>可自行选择是否对数据或异常进行处理
|
* <br>可自行选择是否对数据或异常进行处理
|
||||||
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
||||||
@@ -30,7 +31,13 @@ import java.sql.SQLException;
|
|||||||
* @author CarmJos
|
* @author CarmJos
|
||||||
* @since 0.2.6
|
* @since 0.2.6
|
||||||
*/
|
*/
|
||||||
public interface QueryAction extends SQLAction<SQLQuery> {
|
public interface SQLQueryAction<B extends SQLQueryAction<B>> extends SQLBaseAction<SQLQuery> {
|
||||||
|
|
||||||
|
interface Base extends SQLQueryAction<Base> {
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Advanced extends SQLQueryAction<Advanced>, SQLAdvancedAction<SQLQuery> {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Contract("_,!null -> !null")
|
@Contract("_,!null -> !null")
|
||||||
+49
@@ -0,0 +1,49 @@
|
|||||||
|
package cc.carm.lib.easysql.api.action.update;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface PreparedSQLBatchUpdateAction<T extends Number, B extends PreparedSQLBatchUpdateAction<T, B>>
|
||||||
|
extends SQLBaseAction<List<T>> {
|
||||||
|
|
||||||
|
interface Base<T extends Number> extends PreparedSQLBatchUpdateAction<T, Base<T>> {
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Advanced<T extends Number> extends PreparedSQLBatchUpdateAction<T, Advanced<T>>, SQLAdvancedAction<List<T>> {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定多组SQL语句中所有 ? 对应的参数
|
||||||
|
*
|
||||||
|
* @param allValues 所有参数内容
|
||||||
|
* @return {@link PreparedSQLBatchUpdateAction}
|
||||||
|
*/
|
||||||
|
B allValues(Iterable<Object[]> allValues);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加一组SQL语句中所有 ? 对应的参数
|
||||||
|
*
|
||||||
|
* @param values 参数内容
|
||||||
|
* @return {@link PreparedSQLBatchUpdateAction}
|
||||||
|
*/
|
||||||
|
B addValues(Object... values);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定该操作返回自增键序列。
|
||||||
|
*
|
||||||
|
* @return {@link PreparedSQLBatchUpdateAction}
|
||||||
|
*/
|
||||||
|
B returnGeneratedKeys();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定该操作返回自增键序列。
|
||||||
|
*
|
||||||
|
* @param keyTypeClass 自增序列的数字类型
|
||||||
|
* @param <N> 自增键序列类型 {@link Number}
|
||||||
|
* @return {@link PreparedSQLBatchUpdateAction}
|
||||||
|
* @since 0.4.0
|
||||||
|
*/
|
||||||
|
<N extends Number, D extends PreparedSQLBatchUpdateAction<N, D>> D returnGeneratedKeys(Class<N> keyTypeClass);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package cc.carm.lib.easysql.api.action.update;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public interface PreparedSQLUpdateAction<T extends Number, B extends PreparedSQLUpdateAction<T, B>>
|
||||||
|
extends SQLUpdateAction<T, B> {
|
||||||
|
|
||||||
|
interface Base<T extends Number> extends PreparedSQLUpdateAction<T, Base<T>> {
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Advanced<T extends Number> extends PreparedSQLUpdateAction<T, Advanced<T>>, SQLAdvancedAction<T> {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定SQL语句中所有 ? 对应的参数
|
||||||
|
*
|
||||||
|
* @param params 参数内容
|
||||||
|
* @return {@link PreparedSQLUpdateAction}
|
||||||
|
*/
|
||||||
|
B values(Object... params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定SQL语句中所有 ? 对应的参数
|
||||||
|
*
|
||||||
|
* @param params 参数内容
|
||||||
|
* @return {@link PreparedSQLUpdateAction}
|
||||||
|
* @since 0.4.0
|
||||||
|
*/
|
||||||
|
B values(@Nullable Iterable<Object> params);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package cc.carm.lib.easysql.api.action.update;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface SQLBatchUpdateAction<T extends Number, B extends SQLBatchUpdateAction<T, B>> extends SQLBaseAction<T> {
|
||||||
|
|
||||||
|
interface Base<T extends Number> extends SQLBatchUpdateAction<T, Base<T>>, SQLBaseAction<T> {
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Advanced<T extends Number> extends SQLBatchUpdateAction<T, Advanced<T>>, SQLAdvancedAction<T> {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加一条批量执行的SQL语句
|
||||||
|
*
|
||||||
|
* @param sql SQL语句
|
||||||
|
* @return {@link SQLBatchUpdateAction}
|
||||||
|
*/
|
||||||
|
B addBatch(@NotNull String sql);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default @NotNull String getSQLContent() {
|
||||||
|
return getSQLContents().get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NotNull List<String> getSQLContents();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package cc.carm.lib.easysql.api.action.update;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
|
|
||||||
|
public interface SQLUpdateAction<T extends Number, B extends SQLUpdateAction<T, B>> extends SQLBaseAction<T> {
|
||||||
|
|
||||||
|
interface Base<T extends Number> extends SQLUpdateAction<T, Base<T>> {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Advanced<T extends Number> extends SQLUpdateAction<T, Advanced<T>>, SQLAdvancedAction<T> {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定该操作返回自增键序列。
|
||||||
|
*
|
||||||
|
* @return {@link SQLUpdateAction}
|
||||||
|
*/
|
||||||
|
B returnGeneratedKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定该操作返回自增键序列。
|
||||||
|
*
|
||||||
|
* @param keyTypeClass 自增序列的数字类型
|
||||||
|
* @param <N> 自增键序列类型 {@link Number}
|
||||||
|
* @return {@link SQLUpdateAction}
|
||||||
|
* @since 0.4.0
|
||||||
|
*/
|
||||||
|
<N extends Number, D extends SQLUpdateAction<N, D>> D returnGeneratedKey(Class<N> keyTypeClass);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.SQLBuilder;
|
import cc.carm.lib.easysql.api.SQLBuilder;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T extends SQLAction<?>> extends SQLBuilder {
|
public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T extends SQLBaseAction<?>> extends SQLBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将现有条件构建完整的SQL语句用于执行。
|
* 将现有条件构建完整的SQL语句用于执行。
|
||||||
*
|
*
|
||||||
* @return {@link SQLAction}
|
* @return {@link SQLBaseAction}
|
||||||
*/
|
*/
|
||||||
T build();
|
@NotNull T build();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设定限定的条目数
|
* 设定限定的条目数
|
||||||
@@ -23,7 +23,7 @@ public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T extend
|
|||||||
* @param limit 条数限制
|
* @param limit 条数限制
|
||||||
* @return {@link B}
|
* @return {@link B}
|
||||||
*/
|
*/
|
||||||
B setLimit(int limit);
|
@NotNull B limit(int limit);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 直接设定条件的源文本,不需要以WHERE开头。
|
* 直接设定条件的源文本,不需要以WHERE开头。
|
||||||
@@ -32,7 +32,7 @@ public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T extend
|
|||||||
* @param condition 条件文本,不需要以WHERE开头。
|
* @param condition 条件文本,不需要以WHERE开头。
|
||||||
* @return {@link B}
|
* @return {@link B}
|
||||||
*/
|
*/
|
||||||
B setConditions(@Nullable String condition);
|
@NotNull B where(@Nullable String condition);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 直接设定每个条件的文本与其对应数值,将以AND链接,且不需要以WHERE开头。
|
* 直接设定每个条件的文本与其对应数值,将以AND链接,且不需要以WHERE开头。
|
||||||
@@ -41,17 +41,17 @@ public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T extend
|
|||||||
* @param conditionSQLs 条件内容,将以AND链接,且不需要以WHERE开头。
|
* @param conditionSQLs 条件内容,将以AND链接,且不需要以WHERE开头。
|
||||||
* @return {@link B}
|
* @return {@link B}
|
||||||
*/
|
*/
|
||||||
B setConditions(LinkedHashMap<@NotNull String, @Nullable Object> conditionSQLs);
|
@NotNull B where(LinkedHashMap<@NotNull String, @Nullable Object> conditionSQLs);
|
||||||
|
|
||||||
B addCondition(@Nullable String condition);
|
@NotNull B addCondition(@Nullable String condition);
|
||||||
|
|
||||||
B addCondition(@NotNull String columnName, @NotNull String operator, @Nullable Object queryValue);
|
@NotNull B addCondition(@NotNull String columnName, @NotNull String operator, @Nullable Object queryValue);
|
||||||
|
|
||||||
B addCondition(@NotNull String columnName, @Nullable Object queryValue);
|
@NotNull B addCondition(@NotNull String columnName, @Nullable Object queryValue);
|
||||||
|
|
||||||
B addCondition(@NotNull String[] columnNames, @Nullable Object[] queryValues);
|
@NotNull B addCondition(@NotNull String[] columnNames, @Nullable Object[] queryValues);
|
||||||
|
|
||||||
B addNotNullCondition(@NotNull String columnName);
|
@NotNull B addNotNullCondition(@NotNull String columnName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加时间的限定条件。 若设定了开始时间,则限定条件为 {@code endMillis >= startMillis};
|
* 添加时间的限定条件。 若设定了开始时间,则限定条件为 {@code endMillis >= startMillis};
|
||||||
@@ -61,7 +61,7 @@ public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T extend
|
|||||||
* @param endMillis 结束时间戳,若{@code <0}则不作限定
|
* @param endMillis 结束时间戳,若{@code <0}则不作限定
|
||||||
* @return {@link B}
|
* @return {@link B}
|
||||||
*/
|
*/
|
||||||
default B addTimeCondition(@NotNull String columnName, long startMillis, long endMillis) {
|
default @NotNull B addTimeCondition(@NotNull String columnName, long startMillis, long endMillis) {
|
||||||
return addTimeCondition(columnName,
|
return addTimeCondition(columnName,
|
||||||
startMillis > 0 ? new Date(startMillis) : null,
|
startMillis > 0 ? new Date(startMillis) : null,
|
||||||
endMillis > 0 ? new Date(endMillis) : null
|
endMillis > 0 ? new Date(endMillis) : null
|
||||||
@@ -76,7 +76,7 @@ public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T extend
|
|||||||
* @param endDate 结束时间,若为null则不作限定
|
* @param endDate 结束时间,若为null则不作限定
|
||||||
* @return {@link B}
|
* @return {@link B}
|
||||||
*/
|
*/
|
||||||
B addTimeCondition(@NotNull String columnName, @Nullable java.util.Date startDate, @Nullable java.util.Date endDate);
|
@NotNull B addTimeCondition(@NotNull String columnName, @Nullable java.util.Date startDate, @Nullable java.util.Date endDate);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLUpdateAction;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public interface DeleteBuilder extends ConditionalBuilder<DeleteBuilder, SQLAction<Integer>> {
|
public interface DeleteBuilder<B extends PreparedSQLUpdateAction<Integer, B>>
|
||||||
|
extends ConditionalBuilder<DeleteBuilder<B>, B> {
|
||||||
|
|
||||||
String getTableName();
|
@NotNull String getTableName();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface InsertBuilder<T extends SQLAction<?>> {
|
public interface InsertBuilder<T> {
|
||||||
|
|
||||||
String getTableName();
|
@NotNull String getTableName();
|
||||||
|
|
||||||
T setColumnNames(List<String> columnNames);
|
@NotNull T columns(List<String> columnNames);
|
||||||
|
|
||||||
default T setColumnNames(String... columnNames) {
|
default @NotNull T columns(String... columnNames) {
|
||||||
return setColumnNames(columnNames == null ? null : Arrays.asList(columnNames));
|
return columns(columnNames == null ? null : Arrays.asList(columnNames));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,30 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLBuilder;
|
import cc.carm.lib.easysql.api.SQLBuilder;
|
||||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
import cc.carm.lib.easysql.api.action.query.PreparedSQLQueryAction;
|
||||||
import cc.carm.lib.easysql.api.action.query.QueryAction;
|
import cc.carm.lib.easysql.api.action.query.SQLQueryAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public interface QueryBuilder extends SQLBuilder {
|
public interface QueryBuilder<Q extends SQLQueryAction<Q>, P extends PreparedSQLQueryAction<P>> extends SQLBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过一条 SQL语句创建查询。
|
* 通过一条 SQL语句创建查询。
|
||||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||||
*
|
*
|
||||||
* @param sql SQL语句
|
* @param sql SQL语句
|
||||||
* @return {@link QueryAction}
|
* @return {@link SQLQueryAction}
|
||||||
* @deprecated 存在SQL注入风险,建议使用 {@link QueryBuilder#withPreparedSQL(String)}
|
* @deprecated 存在SQL注入风险,建议使用 {@link QueryBuilder#withPreparedSQL(String)}
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
QueryAction withSQL(@NotNull String sql);
|
@NotNull Q withSQL(@NotNull String sql);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过一条 SQL语句创建预查询
|
* 通过一条 SQL语句创建预查询
|
||||||
*
|
*
|
||||||
* @param sql SQL语句
|
* @param sql SQL语句
|
||||||
* @return {@link PreparedQueryAction}
|
* @return {@link PreparedSQLQueryAction}
|
||||||
*/
|
*/
|
||||||
PreparedQueryAction withPreparedSQL(@NotNull String sql);
|
@NotNull P withPreparedSQL(@NotNull String sql);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建表查询
|
* 创建表查询
|
||||||
@@ -32,6 +32,6 @@ public interface QueryBuilder extends SQLBuilder {
|
|||||||
* @param tableName 表名
|
* @param tableName 表名
|
||||||
* @return {@link TableQueryBuilder}
|
* @return {@link TableQueryBuilder}
|
||||||
*/
|
*/
|
||||||
TableQueryBuilder inTable(@NotNull String tableName);
|
@NotNull TableQueryBuilder<P> fromTable(@NotNull String tableName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -10,16 +11,16 @@ import java.util.List;
|
|||||||
* <br> 执行后,将通过表中键判断该数据是否存在,若存在则用新数据替换原来的值,若不存在则会插入该数据。
|
* <br> 执行后,将通过表中键判断该数据是否存在,若存在则用新数据替换原来的值,若不存在则会插入该数据。
|
||||||
* <br> 在使用REPLACE时,表与所给行列数据中必须包含唯一索引(或主键),且索引不得为空值,否则将等同于插入语句。
|
* <br> 在使用REPLACE时,表与所给行列数据中必须包含唯一索引(或主键),且索引不得为空值,否则将等同于插入语句。
|
||||||
*
|
*
|
||||||
* @param <T> 最终构建出的 {@link SQLAction} 类型
|
* @param <T> 最终构建出的 {@link SQLBaseAction} 类型
|
||||||
*/
|
*/
|
||||||
public interface ReplaceBuilder<T extends SQLAction<?>> {
|
public interface ReplaceBuilder<T extends SQLBaseAction<?>> {
|
||||||
|
|
||||||
String getTableName();
|
@NotNull String getTableName();
|
||||||
|
|
||||||
T setColumnNames(List<String> columnNames);
|
@NotNull T columns(List<String> columnNames);
|
||||||
|
|
||||||
default T setColumnNames(String... columnNames) {
|
default @NotNull T columns(String... columnNames) {
|
||||||
return setColumnNames(columnNames == null ? null : Arrays.asList(columnNames));
|
return columns(columnNames == null ? null : Arrays.asList(columnNames));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.SQLBuilder;
|
import cc.carm.lib.easysql.api.SQLBuilder;
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -10,46 +9,46 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
public interface TableAlterBuilder extends SQLBuilder {
|
public interface TableAlterBuilder extends SQLBuilder {
|
||||||
|
|
||||||
SQLAction<Integer> renameTo(@NotNull String newTableName);
|
@NotNull SQLAdvancedAction<Integer> renameTo(@NotNull String newTableName);
|
||||||
|
|
||||||
SQLAction<Integer> changeComment(@NotNull String newTableComment);
|
@NotNull SQLAdvancedAction<Integer> changeComment(@NotNull String newTableComment);
|
||||||
|
|
||||||
SQLAction<Integer> setAutoIncrementIndex(int index);
|
@NotNull SQLAdvancedAction<Integer> setAutoIncrementIndex(int index);
|
||||||
|
|
||||||
SQLAction<Integer> addIndex(@NotNull IndexType indexType, @Nullable String indexName,
|
@NotNull SQLAdvancedAction<Integer> addIndex(@NotNull IndexType indexType, @Nullable String indexName,
|
||||||
@NotNull String columnName, @NotNull String... moreColumns);
|
@NotNull String columnName, @NotNull String... moreColumns);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为该表移除一个索引
|
* 为该表移除一个索引
|
||||||
*
|
*
|
||||||
* @param indexName 索引名
|
* @param indexName 索引名
|
||||||
* @return {@link SQLUpdateAction}
|
* @return {@link SQLAdvancedAction}
|
||||||
*/
|
*/
|
||||||
SQLAction<Integer> dropIndex(@NotNull String indexName);
|
@NotNull SQLAdvancedAction<Integer> dropIndex(@NotNull String indexName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为该表移除一个外键
|
* 为该表移除一个外键
|
||||||
*
|
*
|
||||||
* @param keySymbol 外键名
|
* @param keySymbol 外键名
|
||||||
* @return {@link SQLUpdateAction}
|
* @return {@link SQLAdvancedAction}
|
||||||
*/
|
*/
|
||||||
SQLAction<Integer> dropForeignKey(@NotNull String keySymbol);
|
@NotNull SQLAdvancedAction<Integer> dropForeignKey(@NotNull String keySymbol);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为该表移除主键(须添加新主键)
|
* 为该表移除主键(须添加新主键)
|
||||||
*
|
*
|
||||||
* @return {@link SQLUpdateAction}
|
* @return {@link SQLAdvancedAction}
|
||||||
*/
|
*/
|
||||||
SQLAction<Integer> dropPrimaryKey();
|
@NotNull SQLAdvancedAction<Integer> dropPrimaryKey();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为表添加一列
|
* 为表添加一列
|
||||||
*
|
*
|
||||||
* @param columnName 列名
|
* @param columnName 列名
|
||||||
* @param settings 列的相关设定
|
* @param settings 列的相关设定
|
||||||
* @return {@link SQLUpdateAction}
|
* @return {@link SQLAdvancedAction}
|
||||||
*/
|
*/
|
||||||
default SQLAction<Integer> addColumn(@NotNull String columnName, @NotNull String settings) {
|
default @NotNull SQLAdvancedAction<Integer> addColumn(@NotNull String columnName, @NotNull String settings) {
|
||||||
return addColumn(columnName, settings, null);
|
return addColumn(columnName, settings, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,23 +60,27 @@ public interface TableAlterBuilder extends SQLBuilder {
|
|||||||
* @param afterColumn 该列增添到哪个列的后面,
|
* @param afterColumn 该列增添到哪个列的后面,
|
||||||
* <p> 该参数若省缺则放于最后一行
|
* <p> 该参数若省缺则放于最后一行
|
||||||
* <p> 若为 "" 则置于首行。
|
* <p> 若为 "" 则置于首行。
|
||||||
* @return {@link SQLUpdateAction}
|
* @return {@link SQLAdvancedAction}
|
||||||
*/
|
*/
|
||||||
SQLAction<Integer> addColumn(@NotNull String columnName, @NotNull String settings, @Nullable String afterColumn);
|
@NotNull SQLAdvancedAction<Integer> addColumn(@NotNull String columnName,
|
||||||
|
@NotNull String settings,
|
||||||
|
@Nullable String afterColumn);
|
||||||
|
|
||||||
SQLAction<Integer> renameColumn(@NotNull String columnName, @NotNull String newName);
|
@NotNull SQLAdvancedAction<Integer> renameColumn(@NotNull String columnName, @NotNull String newName);
|
||||||
|
|
||||||
SQLAction<Integer> modifyColumn(@NotNull String columnName, @NotNull String settings);
|
@NotNull SQLAdvancedAction<Integer> modifyColumn(@NotNull String columnName, @NotNull String settings);
|
||||||
|
|
||||||
default SQLAction<Integer> modifyColumn(@NotNull String columnName, @NotNull String columnSettings, @NotNull String afterColumn) {
|
default @NotNull SQLAdvancedAction<Integer> modifyColumn(@NotNull String columnName,
|
||||||
|
@NotNull String columnSettings,
|
||||||
|
@NotNull String afterColumn) {
|
||||||
return modifyColumn(columnName, columnSettings + " AFTER `" + afterColumn + "`");
|
return modifyColumn(columnName, columnSettings + " AFTER `" + afterColumn + "`");
|
||||||
}
|
}
|
||||||
|
|
||||||
SQLAction<Integer> removeColumn(@NotNull String columnName);
|
@NotNull SQLAdvancedAction<Integer> removeColumn(@NotNull String columnName);
|
||||||
|
|
||||||
SQLAction<Integer> setColumnDefault(@NotNull String columnName, @NotNull String defaultValue);
|
@NotNull SQLAdvancedAction<Integer> setColumnDefault(@NotNull String columnName, @NotNull String defaultValue);
|
||||||
|
|
||||||
SQLAction<Integer> removeColumnDefault(@NotNull String columnName);
|
@NotNull SQLAdvancedAction<Integer> removeColumnDefault(@NotNull String columnName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为该表添加一个自增列
|
* 为该表添加一个自增列
|
||||||
@@ -90,8 +93,9 @@ public interface TableAlterBuilder extends SQLBuilder {
|
|||||||
* @param unsigned 是否采用 UNSIGNED (即无负数,可以增加自增键的最高数,建议为true)
|
* @param unsigned 是否采用 UNSIGNED (即无负数,可以增加自增键的最高数,建议为true)
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default SQLAction<Integer> addAutoIncrementColumn(@NotNull String columnName, @Nullable NumberType numberType,
|
default @NotNull SQLAdvancedAction<Integer> addAutoIncrementColumn(@NotNull String columnName,
|
||||||
boolean primary, boolean unsigned) {
|
@Nullable NumberType numberType,
|
||||||
|
boolean primary, boolean unsigned) {
|
||||||
return addColumn(columnName,
|
return addColumn(columnName,
|
||||||
(numberType == null ? NumberType.INT : numberType).name()
|
(numberType == null ? NumberType.INT : numberType).name()
|
||||||
+ (unsigned ? " UNSIGNED " : " ")
|
+ (unsigned ? " UNSIGNED " : " ")
|
||||||
@@ -109,7 +113,8 @@ public interface TableAlterBuilder extends SQLBuilder {
|
|||||||
* @param numberType 数字类型,若省缺则为 {@link NumberType#INT}
|
* @param numberType 数字类型,若省缺则为 {@link NumberType#INT}
|
||||||
* @return {@link TableAlterBuilder}
|
* @return {@link TableAlterBuilder}
|
||||||
*/
|
*/
|
||||||
default SQLAction<Integer> addAutoIncrementColumn(@NotNull String columnName, @NotNull NumberType numberType) {
|
default @NotNull SQLAdvancedAction<Integer> addAutoIncrementColumn(@NotNull String columnName,
|
||||||
|
@NotNull NumberType numberType) {
|
||||||
return addAutoIncrementColumn(columnName, numberType, false, true);
|
return addAutoIncrementColumn(columnName, numberType, false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +127,7 @@ public interface TableAlterBuilder extends SQLBuilder {
|
|||||||
* @param columnName 列名
|
* @param columnName 列名
|
||||||
* @return {@link TableAlterBuilder}
|
* @return {@link TableAlterBuilder}
|
||||||
*/
|
*/
|
||||||
default SQLAction<Integer> addAutoIncrementColumn(@NotNull String columnName) {
|
default @NotNull SQLAdvancedAction<Integer> addAutoIncrementColumn(@NotNull String columnName) {
|
||||||
return addAutoIncrementColumn(columnName, NumberType.INT);
|
return addAutoIncrementColumn(columnName, NumberType.INT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLBuilder;
|
import cc.carm.lib.easysql.api.SQLBuilder;
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.update.SQLUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
@@ -19,9 +19,9 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
/**
|
/**
|
||||||
* 将现有条件构建完整的SQL语句用于执行。
|
* 将现有条件构建完整的SQL语句用于执行。
|
||||||
*
|
*
|
||||||
* @return {@link SQLUpdateAction}
|
* @return {@link SQLUpdateAction.Advanced}
|
||||||
*/
|
*/
|
||||||
SQLUpdateAction<Integer> build();
|
@NotNull SQLUpdateAction.Advanced<Integer> build();
|
||||||
|
|
||||||
@NotNull String getTableName();
|
@NotNull String getTableName();
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull String getTableSettings();
|
@NotNull String getTableSettings();
|
||||||
|
|
||||||
TableCreateBuilder setTableSettings(@NotNull String settings);
|
@NotNull TableCreateBuilder setTableSettings(@NotNull String settings);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设定表的标注,一般用于解释该表的作用。
|
* 设定表的标注,一般用于解释该表的作用。
|
||||||
@@ -41,7 +41,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param comment 表标注
|
* @param comment 表标注
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
TableCreateBuilder setTableComment(@Nullable String comment);
|
@NotNull TableCreateBuilder setTableComment(@Nullable String comment);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 直接设定表的所有列信息
|
* 直接设定表的所有列信息
|
||||||
@@ -49,7 +49,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param columns 列的相关信息 (包括列设定)
|
* @param columns 列的相关信息 (包括列设定)
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
TableCreateBuilder setColumns(@NotNull String... columns);
|
@NotNull TableCreateBuilder setColumns(@NotNull String... columns);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为该表添加一个列
|
* 为该表添加一个列
|
||||||
@@ -58,7 +58,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* <br>如 `uuid` VARCHAR(36) NOT NULL UNIQUE KEY
|
* <br>如 `uuid` VARCHAR(36) NOT NULL UNIQUE KEY
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
TableCreateBuilder addColumn(@NotNull String column);
|
@NotNull TableCreateBuilder addColumn(@NotNull String column);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为该表添加一个列
|
* 为该表添加一个列
|
||||||
@@ -68,7 +68,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* <br>如 VARCHAR(36) NOT NULL UNIQUE KEY
|
* <br>如 VARCHAR(36) NOT NULL UNIQUE KEY
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addColumn(@NotNull String columnName, @NotNull String settings) {
|
default @NotNull TableCreateBuilder addColumn(@NotNull String columnName, @NotNull String settings) {
|
||||||
Objects.requireNonNull(columnName, "columnName could not be null");
|
Objects.requireNonNull(columnName, "columnName could not be null");
|
||||||
return addColumn(withBackQuote(columnName) + " " + settings);
|
return addColumn(withBackQuote(columnName) + " " + settings);
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param comments 列的注解,用于解释该列数据的作用
|
* @param comments 列的注解,用于解释该列数据的作用
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addColumn(@NotNull String columnName, @NotNull String settings, @NotNull String comments) {
|
default @NotNull TableCreateBuilder addColumn(@NotNull String columnName, @NotNull String settings, @NotNull String comments) {
|
||||||
return addColumn(columnName, settings + " COMMENT " + withQuote(comments));
|
return addColumn(columnName, settings + " COMMENT " + withQuote(comments));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,8 +97,8 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param unsigned 是否采用 UNSIGNED (即无负数,可以增加自增键的最高数,建议为true)
|
* @param unsigned 是否采用 UNSIGNED (即无负数,可以增加自增键的最高数,建议为true)
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName, @Nullable NumberType numberType,
|
@NotNull TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName, @Nullable NumberType numberType,
|
||||||
boolean asPrimaryKey, boolean unsigned);
|
boolean asPrimaryKey, boolean unsigned);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为该表添加一个INT类型的自增主键列
|
* 为该表添加一个INT类型的自增主键列
|
||||||
@@ -110,8 +110,8 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param unsigned 是否采用 UNSIGNED (即无负数,可以增加自增键的最高数,建议为true)
|
* @param unsigned 是否采用 UNSIGNED (即无负数,可以增加自增键的最高数,建议为true)
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName,
|
default @NotNull TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName,
|
||||||
boolean asPrimaryKey, boolean unsigned) {
|
boolean asPrimaryKey, boolean unsigned) {
|
||||||
return addAutoIncrementColumn(columnName, NumberType.INT, asPrimaryKey, unsigned);
|
return addAutoIncrementColumn(columnName, NumberType.INT, asPrimaryKey, unsigned);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param asPrimaryKey 是否为主键,若为false则设定为唯一键
|
* @param asPrimaryKey 是否为主键,若为false则设定为唯一键
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName, boolean asPrimaryKey) {
|
default @NotNull TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName, boolean asPrimaryKey) {
|
||||||
return addAutoIncrementColumn(columnName, asPrimaryKey, true);
|
return addAutoIncrementColumn(columnName, asPrimaryKey, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +137,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param columnName 列名
|
* @param columnName 列名
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName) {
|
default @NotNull TableCreateBuilder addAutoIncrementColumn(@NotNull String columnName) {
|
||||||
return addAutoIncrementColumn(columnName, true);
|
return addAutoIncrementColumn(columnName, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,8 +152,8 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param columnName 索引包含的列
|
* @param columnName 索引包含的列
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder setIndex(@NotNull String columnName,
|
default @NotNull TableCreateBuilder setIndex(@NotNull String columnName,
|
||||||
@NotNull IndexType type) {
|
@NotNull IndexType type) {
|
||||||
return setIndex(type, null, columnName);
|
return setIndex(type, null, columnName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,8 +170,8 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param moreColumns 联合索引需要包含的列
|
* @param moreColumns 联合索引需要包含的列
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
TableCreateBuilder setIndex(@NotNull IndexType type, @Nullable String indexName,
|
@NotNull TableCreateBuilder setIndex(@NotNull IndexType type, @Nullable String indexName,
|
||||||
@NotNull String columnName, @NotNull String... moreColumns);
|
@NotNull String columnName, @NotNull String... moreColumns);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -185,7 +185,7 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param foreignColumn 外键关联表中对应的关联列,必须为目标表的主键,即 {@link IndexType#PRIMARY_KEY}
|
* @param foreignColumn 外键关联表中对应的关联列,必须为目标表的主键,即 {@link IndexType#PRIMARY_KEY}
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addForeignKey(@NotNull String tableColumn, @NotNull String foreignColumn) {
|
default @NotNull TableCreateBuilder addForeignKey(@NotNull String tableColumn, @NotNull String foreignColumn) {
|
||||||
return addForeignKey(tableColumn, getTableName(), foreignColumn);
|
return addForeignKey(tableColumn, getTableName(), foreignColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,8 +203,8 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* <p> 2. 数据类型必须和所要建立主键的列的数据类型相同。
|
* <p> 2. 数据类型必须和所要建立主键的列的数据类型相同。
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addForeignKey(@NotNull String tableColumn,
|
default @NotNull TableCreateBuilder addForeignKey(@NotNull String tableColumn,
|
||||||
@NotNull String foreignTable, @NotNull String foreignColumn) {
|
@NotNull String foreignTable, @NotNull String foreignColumn) {
|
||||||
return addForeignKey(tableColumn, null, foreignTable, foreignColumn);
|
return addForeignKey(tableColumn, null, foreignTable, foreignColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,8 +223,8 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* <p> 2. 数据类型必须和所要建立主键的列的数据类型相同。
|
* <p> 2. 数据类型必须和所要建立主键的列的数据类型相同。
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
default TableCreateBuilder addForeignKey(@NotNull String tableColumn, @Nullable String constraintName,
|
default @NotNull TableCreateBuilder addForeignKey(@NotNull String tableColumn, @Nullable String constraintName,
|
||||||
@NotNull String foreignTable, @NotNull String foreignColumn) {
|
@NotNull String foreignTable, @NotNull String foreignColumn) {
|
||||||
return addForeignKey(tableColumn, constraintName, foreignTable, foreignColumn, null, null);
|
return addForeignKey(tableColumn, constraintName, foreignTable, foreignColumn, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,12 +245,12 @@ public interface TableCreateBuilder extends SQLBuilder {
|
|||||||
* @param deleteRule 在外键被删除时采用的规则,缺省时默认为{@link ForeignKeyRule#RESTRICT}
|
* @param deleteRule 在外键被删除时采用的规则,缺省时默认为{@link ForeignKeyRule#RESTRICT}
|
||||||
* @return {@link TableCreateBuilder}
|
* @return {@link TableCreateBuilder}
|
||||||
*/
|
*/
|
||||||
TableCreateBuilder addForeignKey(@NotNull String tableColumn, @Nullable String constraintName,
|
@NotNull TableCreateBuilder addForeignKey(@NotNull String tableColumn, @Nullable String constraintName,
|
||||||
@NotNull String foreignTable, @NotNull String foreignColumn,
|
@NotNull String foreignTable, @NotNull String foreignColumn,
|
||||||
@Nullable ForeignKeyRule updateRule, @Nullable ForeignKeyRule deleteRule);
|
@Nullable ForeignKeyRule updateRule, @Nullable ForeignKeyRule deleteRule);
|
||||||
|
|
||||||
default String defaultTablesSettings() {
|
default String defaultTablesSettings() {
|
||||||
return "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
|
return "ENGINE=InnoDB DEFAULT CHARSET=utf8";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
import cc.carm.lib.easysql.api.action.query.PreparedSQLQueryAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public interface TableQueryBuilder extends ConditionalBuilder<TableQueryBuilder, PreparedQueryAction> {
|
public interface TableQueryBuilder<B extends PreparedSQLQueryAction<B>>
|
||||||
|
extends ConditionalBuilder<TableQueryBuilder<B>, B> {
|
||||||
|
|
||||||
@NotNull String getTableName();
|
@NotNull String getTableName();
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ public interface TableQueryBuilder extends ConditionalBuilder<TableQueryBuilder,
|
|||||||
* @param columnNames 列名
|
* @param columnNames 列名
|
||||||
* @return {@link TableQueryBuilder}
|
* @return {@link TableQueryBuilder}
|
||||||
*/
|
*/
|
||||||
TableQueryBuilder selectColumns(@NotNull String... columnNames);
|
TableQueryBuilder<B> select(@NotNull String... columnNames);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对结果进行排序
|
* 对结果进行排序
|
||||||
@@ -22,7 +23,7 @@ public interface TableQueryBuilder extends ConditionalBuilder<TableQueryBuilder,
|
|||||||
* @param asc 是否为正序排序 (为false则倒序排序)
|
* @param asc 是否为正序排序 (为false则倒序排序)
|
||||||
* @return {@link TableQueryBuilder}
|
* @return {@link TableQueryBuilder}
|
||||||
*/
|
*/
|
||||||
TableQueryBuilder orderBy(@NotNull String columnName, boolean asc);
|
TableQueryBuilder<B> orderBy(@NotNull String columnName, boolean asc);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 限制查询条数,用于分页查询。
|
* 限制查询条数,用于分页查询。
|
||||||
@@ -32,6 +33,6 @@ public interface TableQueryBuilder extends ConditionalBuilder<TableQueryBuilder,
|
|||||||
* @return {@link TableQueryBuilder}
|
* @return {@link TableQueryBuilder}
|
||||||
* @since 0.2.6
|
* @since 0.2.6
|
||||||
*/
|
*/
|
||||||
TableQueryBuilder setPageLimit(int start, int end);
|
TableQueryBuilder<B> limit(int start, int end);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
package cc.carm.lib.easysql.api.builder;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLUpdateAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, SQLAction<Integer>> {
|
public interface UpdateBuilder<B extends PreparedSQLUpdateAction<Integer, B>>
|
||||||
|
extends ConditionalBuilder<UpdateBuilder<B>, B> {
|
||||||
|
|
||||||
String getTableName();
|
String getTableName();
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, SQLActi
|
|||||||
* @return {@link UpdateBuilder}
|
* @return {@link UpdateBuilder}
|
||||||
* @since 0.3.7
|
* @since 0.3.7
|
||||||
*/
|
*/
|
||||||
UpdateBuilder addColumnValue(@NotNull String columnName, @Nullable Object columnValue);
|
UpdateBuilder<B> set(@NotNull String columnName, @Nullable Object columnValue);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设定更新的全部字段值 <b>(此操作会覆盖之前的设定)</b>
|
* 设定更新的全部字段值 <b>(此操作会覆盖之前的设定)</b>
|
||||||
@@ -27,7 +28,7 @@ public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, SQLActi
|
|||||||
* @param columnData 字段名和值的键值对
|
* @param columnData 字段名和值的键值对
|
||||||
* @return {@link UpdateBuilder}
|
* @return {@link UpdateBuilder}
|
||||||
*/
|
*/
|
||||||
UpdateBuilder setColumnValues(LinkedHashMap<@NotNull String, @Nullable Object> columnData);
|
UpdateBuilder<B> setAll(LinkedHashMap<@NotNull String, @Nullable Object> columnData);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设定更新的全部字段值 <b>(此操作会覆盖之前的设定)</b>
|
* 设定更新的全部字段值 <b>(此操作会覆盖之前的设定)</b>
|
||||||
@@ -37,20 +38,19 @@ public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, SQLActi
|
|||||||
* @param columnValues 字段名对应的值
|
* @param columnValues 字段名对应的值
|
||||||
* @return {@link UpdateBuilder}
|
* @return {@link UpdateBuilder}
|
||||||
*/
|
*/
|
||||||
UpdateBuilder setColumnValues(@NotNull String[] columnNames, @Nullable Object[] columnValues);
|
UpdateBuilder<B> setAll(@NotNull String[] columnNames, @Nullable Object[] columnValues);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设定更新的全部字段值 <b>(此操作会覆盖之前的设定)</b>
|
* 设定更新的全部字段值 <b>(此操作会覆盖之前的设定)</b>
|
||||||
* <p> 如需同时更新多条字段,请使用 {@link #setColumnValues(String[], Object[])} 或 {@link #setColumnValues(LinkedHashMap)}
|
* <p> 如需同时更新多条字段,请使用 {@link #setAll(String[], Object[])} 或 {@link #setAll(LinkedHashMap)}
|
||||||
* <br>也可以使用 {@link #addColumnValue(String, Object)} 一条条的添加字段
|
* <br>也可以使用 {@link #set(String, Object)} 一条条的添加字段
|
||||||
*
|
*
|
||||||
* @param columnName 字段名
|
* @param columnName 字段名
|
||||||
* @param columnValue 字段名对应的值
|
* @param columnValue 字段名对应的值
|
||||||
* @return {@link UpdateBuilder}
|
* @return {@link UpdateBuilder}
|
||||||
*/
|
*/
|
||||||
default UpdateBuilder setColumnValues(@NotNull String columnName, @Nullable Object columnValue) {
|
default UpdateBuilder<B> setAll(@NotNull String columnName, @Nullable Object columnValue) {
|
||||||
return setColumnValues(new String[]{columnName}, new Object[]{columnValue});
|
return setAll(new String[]{columnName}, new Object[]{columnValue});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.builder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存在则更新,不存在则插入。
|
|
||||||
*
|
|
||||||
* @see ReplaceBuilder
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public interface UpsertBuilder {
|
|
||||||
|
|
||||||
String getTableName();
|
|
||||||
|
|
||||||
default UpsertBuilder setColumnNames(String[] columnNames, String updateColumn) {
|
|
||||||
throw new UnsupportedOperationException("Please use REPLACE .");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package cc.carm.lib.easysql.api.enums;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 事务隔离级别。
|
||||||
|
* 部分Javadoc来自文章 <a href="https://cloud.tencent.com/developer/article/1865041">《理解事务的4种隔离级别》</a> 。
|
||||||
|
*/
|
||||||
|
public enum IsolationLevel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读未提交。即一个事务可以读取另一个未提交事务的数据。
|
||||||
|
*/
|
||||||
|
READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED),
|
||||||
|
/**
|
||||||
|
* 读提交。即一个事务要等另一个事务提交后才能读取数据。
|
||||||
|
*/
|
||||||
|
READ_COMMITTED(Connection.TRANSACTION_READ_COMMITTED),
|
||||||
|
/**
|
||||||
|
* 重复读。即在开始读取数据(事务开启)时,不再允许修改操作。
|
||||||
|
*/
|
||||||
|
REPEATED_READ(Connection.TRANSACTION_REPEATABLE_READ),
|
||||||
|
/**
|
||||||
|
* 序列化读取。此为最高隔离等级。在该级别下,事务串行化顺序执行,可以避免脏读、不可重复读与幻读。
|
||||||
|
* <br>注意: 这种事务隔离级别效率低下,比较耗数据库性能,一般不使用。
|
||||||
|
*/
|
||||||
|
SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE);
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
IsolationLevel(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getID() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.enums;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 2022/11/28<br>
|
|
||||||
* EasySQL<br>
|
|
||||||
*
|
|
||||||
* @author huanmeng_qwq
|
|
||||||
*/
|
|
||||||
public class MigrateResult {
|
|
||||||
/**
|
|
||||||
* 旧表不存在
|
|
||||||
*/
|
|
||||||
public static final MigrateResult OLD_TABLE_NOT_EXIST = new MigrateResult(true);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 新表已存在
|
|
||||||
*/
|
|
||||||
public static final MigrateResult NEW_TABLE_EXIST = new MigrateResult(false);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 新表字段为空
|
|
||||||
*/
|
|
||||||
public static final MigrateResult NEW_COLUMN_EMPTY = new MigrateResult(false);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 旧表字段为在新表这设置对应的字段名
|
|
||||||
*/
|
|
||||||
public static final MigrateResult COLUMN_NOT_SET = new MigrateResult(false);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 迁移成功
|
|
||||||
*/
|
|
||||||
public static final MigrateResult SUCCESS = new MigrateResult(true);
|
|
||||||
|
|
||||||
private final boolean success;
|
|
||||||
private Throwable error;
|
|
||||||
|
|
||||||
MigrateResult(boolean success) {
|
|
||||||
this.success = success;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MigrateResult(boolean success, Throwable error) {
|
|
||||||
this.success = success;
|
|
||||||
this.error = error;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MigrateResult from(MigrateResult migrateResult, Throwable error) {
|
|
||||||
return new MigrateResult(migrateResult.success, error);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean success() {
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Throwable error() {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package cc.carm.lib.easysql.api.function;
|
package cc.carm.lib.easysql.api.function;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedBatchUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -14,19 +14,19 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常处理器。
|
* 异常处理器。
|
||||||
* <br> 在使用 {@link SQLAction#execute(SQLExceptionHandler)} 等相关方法时,
|
* <br> 在使用 {@link SQLBaseAction#execute(SQLExceptionHandler)} 等相关方法时,
|
||||||
* 如果发生异常,则会调用错误处理器进行错误内容的输出提示。
|
* 如果发生异常,则会调用错误处理器进行错误内容的输出提示。
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface SQLDebugHandler {
|
public interface SQLDebugHandler {
|
||||||
/**
|
/**
|
||||||
* 该方法将在 {@link SQLAction#execute()} 执行前调用。
|
* 该方法将在 {@link SQLBaseAction#execute()} 执行前调用。
|
||||||
*
|
*
|
||||||
* @param action {@link SQLAction} 对象
|
* @param action {@link SQLBaseAction} 对象
|
||||||
* @param params 执行传入的参数列表。
|
* @param params 执行传入的参数列表。
|
||||||
* 实际上,仅有 {@link PreparedSQLUpdateAction} 和 {@link PreparedSQLUpdateBatchAction} 才会有传入参数。
|
* 实际上,仅有 {@link PreparedUpdateAction} 和 {@link PreparedBatchUpdateAction} 才会有传入参数。
|
||||||
*/
|
*/
|
||||||
void beforeExecute(@NotNull SQLAction<?> action, @NotNull List<@Nullable Object[]> params);
|
void beforeExecute(@NotNull SQLBaseAction<?> action, @NotNull List<@Nullable Object[]> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 该方法将在 {@link SQLQuery#close()} 执行后调用。
|
* 该方法将在 {@link SQLQuery#close()} 执行后调用。
|
||||||
@@ -54,14 +54,14 @@ public interface SQLDebugHandler {
|
|||||||
return new SQLDebugHandler() {
|
return new SQLDebugHandler() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeExecute(@NotNull SQLAction<?> action, @NotNull List<@Nullable Object[]> params) {
|
public void beforeExecute(@NotNull SQLBaseAction<?> action, @NotNull List<@Nullable Object[]> params) {
|
||||||
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
||||||
logger.info("┣# ActionUUID: {}", action.getActionUUID());
|
logger.info("┣# 操作编号: {}", action.getActionUUID());
|
||||||
logger.info("┣# ActionType: {}", action.getClass().getSimpleName());
|
logger.info("┣# 操作类型: {}", action.getClass().getSimpleName());
|
||||||
if (action.getSQLContents().size() == 1) {
|
if (action.getSQLContents().size() == 1) {
|
||||||
logger.info("┣# SQLContent: {}", action.getSQLContents().get(0));
|
logger.info("┣# SQL语句: {}", action.getSQLContents().get(0));
|
||||||
} else {
|
} else {
|
||||||
logger.info("┣# SQLContents: ");
|
logger.info("┣# SQL语句: ");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (String sqlContent : action.getSQLContents()) {
|
for (String sqlContent : action.getSQLContents()) {
|
||||||
logger.info("┃ - [{}] {}", ++i, sqlContent);
|
logger.info("┃ - [{}] {}", ++i, sqlContent);
|
||||||
@@ -70,25 +70,25 @@ public interface SQLDebugHandler {
|
|||||||
if (params.size() == 1) {
|
if (params.size() == 1) {
|
||||||
Object[] param = params.get(0);
|
Object[] param = params.get(0);
|
||||||
if (param != null) {
|
if (param != null) {
|
||||||
logger.info("┣# SQLParam: {}", parseParams(param));
|
logger.info("┣# SQL参数: {}", parseParams(param));
|
||||||
}
|
}
|
||||||
} else if (params.size() > 1) {
|
} else if (params.size() > 1) {
|
||||||
logger.info("┣# SQLParams: ");
|
logger.info("┣# SQL参数: ");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object[] param : params) {
|
for (Object[] param : params) {
|
||||||
logger.info("┃ - [{}] {}", ++i, parseParams(param));
|
logger.info("┃ - [{}] {}", ++i, parseParams(param));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.info("┣# CreateTime: {}", action.getCreateTime(TimeUnit.MILLISECONDS));
|
logger.info("┣# 创建时间: {}", action.getCreateTime(TimeUnit.MILLISECONDS));
|
||||||
logger.info("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
logger.info("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterQuery(@NotNull SQLQuery query, long executeNanoTime, long closeNanoTime) {
|
public void afterQuery(@NotNull SQLQuery query, long executeNanoTime, long closeNanoTime) {
|
||||||
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
||||||
logger.info("┣# ActionUUID: {}", query.getAction().getActionUUID());
|
logger.info("┣# 操作编号: {}", query.getAction().getActionUUID());
|
||||||
logger.info("┣# SQLContent: {}", query.getSQLContent());
|
logger.info("┣# SQL语句: {}", query.getSQLContent());
|
||||||
logger.info("┣# CloseTime: {} (cost {} ms)",
|
logger.info("┣# 关闭时间: {} (cost {} ms)",
|
||||||
TimeUnit.NANOSECONDS.toMillis(closeNanoTime),
|
TimeUnit.NANOSECONDS.toMillis(closeNanoTime),
|
||||||
((double) (closeNanoTime - executeNanoTime) / 1000000)
|
((double) (closeNanoTime - executeNanoTime) / 1000000)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.api.function;
|
package cc.carm.lib.easysql.api.function;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import cc.carm.lib.easysql.api.action.SQLAdvancedAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -8,11 +9,11 @@ import java.util.function.BiConsumer;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常处理器。
|
* 异常处理器。
|
||||||
* <br> 在使用 {@link SQLAction#execute(SQLExceptionHandler)} 等相关方法时,
|
* <br> 在使用 {@link SQLAdvancedAction#execute(SQLExceptionHandler)} 等相关方法时,
|
||||||
* 如果发生异常,则会调用错误处理器进行错误内容的输出提示。
|
* 如果发生异常,则会调用错误处理器进行错误内容的输出提示。
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface SQLExceptionHandler extends BiConsumer<SQLException, SQLAction<?>> {
|
public interface SQLExceptionHandler extends BiConsumer<SQLException, SQLBaseAction<?>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认的异常处理器,将详细的输出相关错误与错误来源。
|
* 默认的异常处理器,将详细的输出相关错误与错误来源。
|
||||||
@@ -38,6 +39,7 @@ public interface SQLExceptionHandler extends BiConsumer<SQLException, SQLAction<
|
|||||||
*
|
*
|
||||||
* @return 无输出的处理器。
|
* @return 无输出的处理器。
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
static SQLExceptionHandler silent() {
|
static SQLExceptionHandler silent() {
|
||||||
return (exception, sqlAction) -> {
|
return (exception, sqlAction) -> {
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.api.migrate;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
|
||||||
import cc.carm.lib.easysql.api.enums.MigrateResult;
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将现有的表结构与数据迁移之新的表中
|
|
||||||
*
|
|
||||||
* @author huanmeng_qwq
|
|
||||||
* @since 0.4.7
|
|
||||||
*/
|
|
||||||
public interface SQLMigrate {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为迁移工作定义前后的表名信息
|
|
||||||
*
|
|
||||||
* @param oldTableName 旧表名
|
|
||||||
* @param newTableName 新表名
|
|
||||||
* @param newTableSettings 新表的设置
|
|
||||||
* @return {@link SQLMigrate}
|
|
||||||
*/
|
|
||||||
@Contract("null,_,_ -> fail;!null,_,_ -> this")
|
|
||||||
SQLMigrate tableName(String oldTableName, String newTableName, String newTableSettings);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为迁移工作定义前后的列名信息
|
|
||||||
*
|
|
||||||
* @param oldColumnName 旧列名
|
|
||||||
* @param newColumnName 新列名
|
|
||||||
* @param newColumnSettings 新列的设置
|
|
||||||
* @return {@link SQLMigrate}
|
|
||||||
*/
|
|
||||||
@Contract("null,_,_ -> fail;!null,_,_ -> this")
|
|
||||||
default SQLMigrate column(String oldColumnName, String newColumnName, String newColumnSettings) {
|
|
||||||
return column(oldColumnName, newColumnName, newColumnSettings, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为迁移工作定义前后的列名信息
|
|
||||||
*
|
|
||||||
* @param oldColumnName 旧列名
|
|
||||||
* @param newColumnName 新列名
|
|
||||||
* @param newColumnSettings 新列的设置
|
|
||||||
* @param indexType 索引类型
|
|
||||||
* @return {@link SQLMigrate}
|
|
||||||
*/
|
|
||||||
@Contract("null,_,_,_ -> fail;!null,_,_,_ -> this")
|
|
||||||
SQLMigrate column(String oldColumnName, String newColumnName, String newColumnSettings, IndexType indexType);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为迁移工作定义前后的自增列名信息
|
|
||||||
*
|
|
||||||
* @param oldColumnName 旧列名
|
|
||||||
* @param newColumnName 新列名
|
|
||||||
* @return {@link SQLMigrate}
|
|
||||||
*/
|
|
||||||
@Contract("null,_->fail;!null,_ -> this")
|
|
||||||
SQLMigrate autoIncrementColumn(String oldColumnName, String newColumnName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 迁移数据
|
|
||||||
*
|
|
||||||
* @return {@link MigrateResult}
|
|
||||||
*/
|
|
||||||
MigrateResult migrate() throws SQLException;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package cc.carm.lib.easysql.api.model;
|
||||||
|
|
||||||
|
public class SQLCondition {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package cc.carm.lib.easysql.api.model;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public class TableColumn {
|
||||||
|
|
||||||
|
@NotNull String name;
|
||||||
|
@NotNull String type;
|
||||||
|
|
||||||
|
boolean notNull;
|
||||||
|
|
||||||
|
boolean primaryKey;
|
||||||
|
boolean uniqueKey;
|
||||||
|
|
||||||
|
boolean autoIncrement;
|
||||||
|
|
||||||
|
@Nullable String defaultValue;
|
||||||
|
@Nullable String onUpdate;
|
||||||
|
|
||||||
|
@Nullable String comment;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.api.table;
|
package cc.carm.lib.easysql.api.table;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
import cc.carm.lib.easysql.api.SQLTable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -16,7 +15,7 @@ public abstract class NamedSQLTable implements SQLTable {
|
|||||||
protected @Nullable SQLManager manager;
|
protected @Nullable SQLManager manager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请调用 {@link NamedSQLTable} 下的静态方法进行对象的初始化。
|
* 请调用 {@link SQLTable} 下的静态方法进行对象的初始化。
|
||||||
*
|
*
|
||||||
* @param tableName 该表的名称
|
* @param tableName 该表的名称
|
||||||
*/
|
*/
|
||||||
|
|||||||
+19
-19
@@ -1,10 +1,10 @@
|
|||||||
package cc.carm.lib.easysql.api;
|
package cc.carm.lib.easysql.api.table;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLBatchUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.builder.*;
|
import cc.carm.lib.easysql.api.builder.*;
|
||||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||||
import cc.carm.lib.easysql.api.table.NamedSQLTable;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ public interface SQLTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) {
|
default @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) {
|
||||||
return sqlManager.createQuery().inTable(getTableName());
|
return sqlManager.createQuery().fromTable(getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull DeleteBuilder createDelete() {
|
default @NotNull DeleteBuilder createDelete() {
|
||||||
@@ -88,7 +88,7 @@ public interface SQLTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) {
|
default @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) {
|
||||||
return sqlManager.createDelete(getTableName());
|
return sqlManager.deleteFrom(getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull UpdateBuilder createUpdate() {
|
default @NotNull UpdateBuilder createUpdate() {
|
||||||
@@ -97,44 +97,44 @@ public interface SQLTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) {
|
default @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) {
|
||||||
return sqlManager.createUpdate(getTableName());
|
return sqlManager.updateInto(getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert() {
|
default @NotNull InsertBuilder<PreparedSQLUpdateAction.Advanced<Integer>> createInsert() {
|
||||||
return Optional.ofNullable(getSQLManager()).map(this::createInsert)
|
return Optional.ofNullable(getSQLManager()).map(this::createInsert)
|
||||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull SQLManager sqlManager) {
|
default @NotNull InsertBuilder<PreparedSQLUpdateAction.Advanced<Integer>> createInsert(@NotNull SQLManager sqlManager) {
|
||||||
return sqlManager.createInsert(getTableName());
|
return sqlManager.insertInto(getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch() {
|
default @NotNull InsertBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> createInsertBatch() {
|
||||||
return Optional.ofNullable(getSQLManager()).map(this::createInsertBatch)
|
return Optional.ofNullable(getSQLManager()).map(this::createInsertBatch)
|
||||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
|
default @NotNull InsertBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
|
||||||
return sqlManager.createInsertBatch(getTableName());
|
return sqlManager.insertBatchInto(getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace() {
|
default @NotNull ReplaceBuilder<PreparedSQLUpdateAction.Advanced<Integer>> createReplace() {
|
||||||
return Optional.ofNullable(getSQLManager()).map(this::createReplace)
|
return Optional.ofNullable(getSQLManager()).map(this::createReplace)
|
||||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull SQLManager sqlManager) {
|
default @NotNull ReplaceBuilder<PreparedSQLUpdateAction.Advanced<Integer>> createReplace(@NotNull SQLManager sqlManager) {
|
||||||
return sqlManager.createReplace(getTableName());
|
return sqlManager.replaceInto(getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch() {
|
default @NotNull ReplaceBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> createReplaceBatch() {
|
||||||
return Optional.ofNullable(getSQLManager()).map(this::createReplaceBatch)
|
return Optional.ofNullable(getSQLManager()).map(this::createReplaceBatch)
|
||||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
|
default @NotNull ReplaceBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
|
||||||
return sqlManager.createReplaceBatch(getTableName());
|
return sqlManager.replaceBatchInto(getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull TableAlterBuilder alter() {
|
default @NotNull TableAlterBuilder alter() {
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package cc.carm.lib.easysql.api.transaction;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public interface SQLSavepoint {
|
||||||
|
|
||||||
|
int getID();
|
||||||
|
|
||||||
|
@NotNull String getName();
|
||||||
|
|
||||||
|
void release();
|
||||||
|
|
||||||
|
boolean isReleased();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
package cc.carm.lib.easysql.api.transaction;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.action.query.PreparedSQLQueryAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.query.SQLQueryAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLBatchUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.SQLBatchUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.SQLUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.builder.*;
|
||||||
|
import cc.carm.lib.easysql.api.enums.IsolationLevel;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface SQLTransaction extends AutoCloseable {
|
||||||
|
|
||||||
|
@NotNull SQLManager getManager();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到本次事务的隔离级别
|
||||||
|
*
|
||||||
|
* @return {@link IsolationLevel} 隔离级别
|
||||||
|
*/
|
||||||
|
@NotNull IsolationLevel getIsolationLevel();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交已有操作
|
||||||
|
*/
|
||||||
|
void commit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定一个记录点
|
||||||
|
*
|
||||||
|
* @param name 记录点名称
|
||||||
|
* @return {@link SQLSavepoint} 事务记录点
|
||||||
|
*/
|
||||||
|
@NotNull SQLSavepoint savepoint(@NotNull String name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回退全部操作
|
||||||
|
*/
|
||||||
|
default void rollback() {
|
||||||
|
rollback(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回退操作到某个记录点或回退整个事务操作。
|
||||||
|
*
|
||||||
|
* @param savepoint 记录点,若记录点为NULL则回退整个事务的操作。
|
||||||
|
*/
|
||||||
|
void rollback(@Nullable SQLSavepoint savepoint);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行一条不需要返回结果的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 paramsBatch SQL语句中对应?的参数组
|
||||||
|
* @return 对应参数返回的行数
|
||||||
|
* @see PreparedSQLBatchUpdateAction
|
||||||
|
*/
|
||||||
|
@Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行多条不需要返回结果的SQL。
|
||||||
|
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||||
|
*
|
||||||
|
* @param sql SQL语句内容
|
||||||
|
* @param moreSQL 更多SQL语句内容
|
||||||
|
* @return 对应参数返回的行数
|
||||||
|
* @see SQLBatchUpdateAction
|
||||||
|
*/
|
||||||
|
@Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行多条不需要返回结果的SQL。
|
||||||
|
*
|
||||||
|
* @param sqlBatch SQL语句内容
|
||||||
|
* @return 对应参数返回的行数
|
||||||
|
*/
|
||||||
|
@Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新建一个查询。
|
||||||
|
*
|
||||||
|
* @return {@link QueryBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull QueryBuilder<SQLQueryAction.Base, PreparedSQLQueryAction.Base> createQuery();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一条插入操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link InsertBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull InsertBuilder<PreparedSQLUpdateAction.Base<Integer>> insertInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建支持多组数据的插入操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link InsertBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull InsertBuilder<PreparedSQLBatchUpdateAction.Base<Integer>> insertBatchInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一条替换操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link ReplaceBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull ReplaceBuilder<PreparedSQLUpdateAction.Base<Integer>> replaceInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建支持多组数据的替换操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link ReplaceBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull ReplaceBuilder<PreparedSQLBatchUpdateAction.Base<Integer>> replaceBatchInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建更新操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link UpdateBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull UpdateBuilder<PreparedSQLUpdateAction.Base<Integer>> updateInto(@NotNull String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建删除操作。
|
||||||
|
*
|
||||||
|
* @param tableName 目标表名
|
||||||
|
* @return {@link DeleteBuilder}
|
||||||
|
*/
|
||||||
|
@NotNull DeleteBuilder<PreparedSQLUpdateAction.Base<Integer>> deleteFrom(@NotNull String tableName);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package cc.carm.lib.easysql.api.transaction;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLSource;
|
||||||
|
import cc.carm.lib.easysql.api.action.SQLBaseAction;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public interface TransactionAction<T> extends SQLBaseAction<T> {
|
||||||
|
|
||||||
|
@NotNull SQLTransaction getTransaction();
|
||||||
|
|
||||||
|
default @NotNull SQLSource getSource() {
|
||||||
|
return getTransaction().getManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+4
-4
@@ -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.4.7</version>
|
<version>0.4.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
<maven.javadoc.skip>true</maven.javadoc.skip>
|
<maven.javadoc.skip>true</maven.javadoc.skip>
|
||||||
<maven.deploy.skip>true</maven.deploy.skip>
|
<maven.deploy.skip>true</maven.deploy.skip>
|
||||||
|
|
||||||
<log4j.version>2.25.3</log4j.version>
|
<log4j.version>2.19.0</log4j.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<artifactId>easysql-demo</artifactId>
|
<artifactId>easysql-demo</artifactId>
|
||||||
@@ -69,14 +69,14 @@
|
|||||||
<!--项目地址 https://github.com/brettwooldridge/HikariCP/ -->
|
<!--项目地址 https://github.com/brettwooldridge/HikariCP/ -->
|
||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
<artifactId>HikariCP</artifactId>
|
<artifactId>HikariCP</artifactId>
|
||||||
<version>7.0.2</version>
|
<version>4.0.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
<version>2.4.240</version>
|
<version>2.1.214</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import cc.carm.lib.easysql.api.SQLManager;
|
import cc.carm.lib.easysql.api.table.SQLTable;
|
||||||
import cc.carm.lib.easysql.api.SQLTable;
|
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
import cc.carm.lib.easysql.api.table.NamedSQLTable;
|
import cc.carm.lib.easysql.api.table.NamedSQLTable;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import cc.carm.lib.easysql.api.SQLManager;
|
import cc.carm.lib.easysql.api.table.SQLTable;
|
||||||
import cc.carm.lib.easysql.api.SQLTable;
|
|
||||||
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import cc.carm.lib.easysql.api.SQLManager;
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.api.SQLTable;
|
import cc.carm.lib.easysql.api.table.SQLTable;
|
||||||
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
@@ -84,8 +84,8 @@ public class EasySQLDemo {
|
|||||||
public void sqlQuery(SQLManager sqlManager) {
|
public void sqlQuery(SQLManager sqlManager) {
|
||||||
// 同步SQL查询
|
// 同步SQL查询
|
||||||
try (SQLQuery query = sqlManager.createQuery()
|
try (SQLQuery query = sqlManager.createQuery()
|
||||||
.inTable("users") // 在users表中查询
|
.fromTable("users") // 在users表中查询
|
||||||
.selectColumns("id", "name") // 选中 id 与 name列
|
.select("id", "name") // 选中 id 与 name列
|
||||||
.addCondition("age", ">", 18) // 限定 age 要大于5
|
.addCondition("age", ">", 18) // 限定 age 要大于5
|
||||||
.addCondition("email", null) // 限定查询 email 字段为空
|
.addCondition("email", null) // 限定查询 email 字段为空
|
||||||
.addNotNullCondition("phone") // 限定 phone 字段不为空
|
.addNotNullCondition("phone") // 限定 phone 字段不为空
|
||||||
@@ -101,10 +101,10 @@ public class EasySQLDemo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
UUID userUUID = sqlManager.createQuery()
|
UUID userUUID = sqlManager.createQuery()
|
||||||
.inTable("users") // 在users表中查询
|
.fromTable("users") // 在users表中查询
|
||||||
.selectColumns("uuid")
|
.select("uuid")
|
||||||
.addCondition("id", 5) // 限定 id 为 5
|
.addCondition("id", 5) // 限定 id 为 5
|
||||||
.setLimit(1) // 只取出一个数据
|
.limit(1) // 只取出一个数据
|
||||||
.build().execute(query -> {
|
.build().execute(query -> {
|
||||||
//可以直接进行数据处理
|
//可以直接进行数据处理
|
||||||
ResultSet result = query.getResultSet();
|
ResultSet result = query.getResultSet();
|
||||||
@@ -118,9 +118,9 @@ public class EasySQLDemo {
|
|||||||
public void sqlQueryAsync(SQLManager sqlManager) {
|
public void sqlQueryAsync(SQLManager sqlManager) {
|
||||||
// 异步SQL查询
|
// 异步SQL查询
|
||||||
sqlManager.createQuery()
|
sqlManager.createQuery()
|
||||||
.inTable("users") // 在users表中查询
|
.fromTable("users") // 在users表中查询
|
||||||
.addCondition("id", 5) // 限定 id 为 5
|
.addCondition("id", 5) // 限定 id 为 5
|
||||||
.setLimit(1) // 只取出一个数据
|
.limit(1) // 只取出一个数据
|
||||||
.build().executeAsync(success -> {
|
.build().executeAsync(success -> {
|
||||||
ResultSet resultSet = success.getResultSet();
|
ResultSet resultSet = success.getResultSet();
|
||||||
if (resultSet != null && resultSet.next()) {
|
if (resultSet != null && resultSet.next()) {
|
||||||
@@ -136,9 +136,9 @@ public class EasySQLDemo {
|
|||||||
|
|
||||||
public void sqlInsert(SQLManager sqlManager) {
|
public void sqlInsert(SQLManager sqlManager) {
|
||||||
// 同步SQL插入 (不使用try-catch的情况下,返回的数值可能为空。)
|
// 同步SQL插入 (不使用try-catch的情况下,返回的数值可能为空。)
|
||||||
int id = sqlManager.createInsert("users")
|
int id = sqlManager.insertInto("users")
|
||||||
.setColumnNames("username", "phone", "email", "registerTime")
|
.columns("username", "phone", "email", "registerTime")
|
||||||
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
.values("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||||
.returnGeneratedKey() // 设定在后续返回自增主键
|
.returnGeneratedKey() // 设定在后续返回自增主键
|
||||||
.execute((exception, action) -> {
|
.execute((exception, action) -> {
|
||||||
// 处理异常
|
// 处理异常
|
||||||
@@ -147,10 +147,10 @@ public class EasySQLDemo {
|
|||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int userID = sqlManager.createInsert("users")
|
long userID = sqlManager.insertInto("users")
|
||||||
.setColumnNames("username", "phone", "email", "registerTime")
|
.columns("username", "phone", "email", "registerTime")
|
||||||
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
.values("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
|
||||||
.returnGeneratedKey().execute();
|
.returnGeneratedKey(Long.class).execute();
|
||||||
|
|
||||||
System.out.println("新用户的ID为 " + userID);
|
System.out.println("新用户的ID为 " + userID);
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package cc.carm.lib.easysql;
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
|
||||||
import cc.carm.lib.easysql.tests.*;
|
import cc.carm.lib.easysql.tests.*;
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
@@ -56,8 +54,6 @@ public class EasySQLTest {
|
|||||||
tests.add(new TableMetadataTest());
|
tests.add(new TableMetadataTest());
|
||||||
// tests.add(new DeleteTest());
|
// tests.add(new DeleteTest());
|
||||||
|
|
||||||
tests.add(new TableMigrateTest());
|
|
||||||
|
|
||||||
print("准备进行测试...");
|
print("准备进行测试...");
|
||||||
|
|
||||||
int index = 1;
|
int index = 1;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package cc.carm.lib.easysql;
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.enums.IsolationLevel;
|
||||||
|
import cc.carm.lib.easysql.api.transaction.SQLSavepoint;
|
||||||
|
import cc.carm.lib.easysql.api.transaction.SQLTransaction;
|
||||||
|
|
||||||
|
public class TransactionTest {
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
|
public void demo() {
|
||||||
|
SQLManager sqlManager = null;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try (SQLTransaction transaction = sqlManager.createTransaction(IsolationLevel.REPEATED_READ)) {
|
||||||
|
|
||||||
|
transaction.updateInto("A")
|
||||||
|
.set("name", "CARM")
|
||||||
|
.addCondition("name", "CUMR")
|
||||||
|
.build().execute();
|
||||||
|
|
||||||
|
SQLSavepoint pointA = transaction.savepoint("TEST");
|
||||||
|
|
||||||
|
transaction.insertInto("A")
|
||||||
|
.columns("name")
|
||||||
|
.values("TEST")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
try {
|
||||||
|
transaction.commit(); // 提交
|
||||||
|
} catch (Exception ex) {
|
||||||
|
transaction.rollback(pointA); // 出错回滚到pointA
|
||||||
|
transaction.commit(); // 提交快照前的代码
|
||||||
|
}
|
||||||
|
|
||||||
|
pointA.release(); // release savepoint (结束后也会被自动释放)
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
@@ -12,9 +11,9 @@ public class QueryAsyncTest extends TestHandler {
|
|||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
sqlManager.createQuery()
|
sqlManager.createQuery()
|
||||||
.inTable("test_user_table")
|
.fromTable("test_user_table")
|
||||||
.orderBy("id", false)
|
.orderBy("id", false)
|
||||||
.setLimit(1)
|
.limit(1)
|
||||||
.build().executeAsync(query -> {
|
.build().executeAsync(query -> {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000L);
|
Thread.sleep(1000L);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
@@ -15,9 +14,9 @@ public class QueryCloseTest extends TestHandler {
|
|||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
try (SQLQuery query = sqlManager.createQuery()
|
try (SQLQuery query = sqlManager.createQuery()
|
||||||
.inTable("test_user_table")
|
.fromTable("test_user_table")
|
||||||
.orderBy("id", false)
|
.orderBy("id", false)
|
||||||
.setLimit(5)
|
.limit(5)
|
||||||
.build().execute()) {
|
.build().execute()) {
|
||||||
ResultSet resultSet = query.getResultSet();
|
ResultSet resultSet = query.getResultSet();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
@@ -12,9 +11,9 @@ public class QueryFunctionTest extends TestHandler {
|
|||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
Integer id_1 = sqlManager.createQuery()
|
Integer id_1 = sqlManager.createQuery()
|
||||||
.inTable("test_user_table")
|
.fromTable("test_user_table")
|
||||||
.orderBy("id", false)
|
.orderBy("id", false)
|
||||||
.setLimit(1)
|
.limit(1)
|
||||||
.build().executeFunction(query -> {
|
.build().executeFunction(query -> {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000L);
|
Thread.sleep(1000L);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@@ -15,9 +14,9 @@ public class QueryFutureTest extends TestHandler {
|
|||||||
|
|
||||||
|
|
||||||
Future<Integer> future = sqlManager.createQuery()
|
Future<Integer> future = sqlManager.createQuery()
|
||||||
.inTable("test_user_table")
|
.fromTable("test_user_table")
|
||||||
.orderBy("id", false)
|
.orderBy("id", false)
|
||||||
.setLimit(1)
|
.limit(1)
|
||||||
.build().executeFuture((query) -> {
|
.build().executeFuture((query) -> {
|
||||||
if (!query.getResultSet().next()) {
|
if (!query.getResultSet().next()) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -13,9 +12,9 @@ public class QueryNotCloseTest extends TestHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
SQLQuery query = sqlManager.createQuery()
|
SQLQuery query = sqlManager.createQuery()
|
||||||
.inTable("test_user_table")
|
.fromTable("test_user_table")
|
||||||
.orderBy("id", false)
|
.orderBy("id", false)
|
||||||
.setLimit(5)
|
.limit(5)
|
||||||
.build().execute();
|
.build().execute();
|
||||||
|
|
||||||
ResultSet resultSet = query.getResultSet();
|
ResultSet resultSet = query.getResultSet();
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -26,8 +25,8 @@ public class SQLUpdateBatchTests extends TestHandler {
|
|||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
List<Long> updates = sqlManager.createInsertBatch("test_user_table")
|
List<Long> updates = sqlManager.createInsertBatch("test_user_table")
|
||||||
.setColumnNames("uuid", "username", "age")
|
.columns("uuid", "username", "age")
|
||||||
.setAllParams(generateParams())
|
.allValues(generateParams())
|
||||||
.returnGeneratedKeys(Long.class)
|
.returnGeneratedKeys(Long.class)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -12,8 +10,8 @@ public class SQLUpdateReturnKeysTest extends SQLUpdateBatchTests {
|
|||||||
@Override
|
@Override
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
List<Integer> generatedKeys = sqlManager.createInsertBatch("test_user_table")
|
List<Integer> generatedKeys = sqlManager.createInsertBatch("test_user_table")
|
||||||
.setColumnNames("uuid", "username", "age")
|
.columns("uuid", "username", "age")
|
||||||
.setAllParams(generateParams())
|
.allValues(generateParams())
|
||||||
.returnGeneratedKeys(Integer.class)
|
.returnGeneratedKeys(Integer.class)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
|
||||||
import cc.carm.lib.easysql.api.enums.MigrateResult;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 2022/11/28<br>
|
|
||||||
* EasySQL<br>
|
|
||||||
*
|
|
||||||
* @author huanmeng_qwq
|
|
||||||
*/
|
|
||||||
public class TableMigrateTest extends TestHandler {
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws Exception {
|
|
||||||
print(" 创建 test_migrate_table");
|
|
||||||
sqlManager.createTable("test_migrate_table")
|
|
||||||
.addAutoIncrementColumn("id")
|
|
||||||
.addColumn("uuid", "VARCHAR(36) NOT NULL", "用户UUID")
|
|
||||||
.addColumn("username", "VARCHAR(16) NOT NULL", "用户名")
|
|
||||||
.addColumn("age", "TINYINT DEFAULT 0 NOT NULL", "年龄")
|
|
||||||
|
|
||||||
.setIndex("uuid", IndexType.UNIQUE_KEY)
|
|
||||||
.build().execute();
|
|
||||||
|
|
||||||
print(" 向 test_migrate_table 写入数据");
|
|
||||||
sqlManager.createInsert("test_migrate_table")
|
|
||||||
.setColumnNames("uuid", "username", "age")
|
|
||||||
.setParams("1234567890", "qwq", 18)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
print(" 迁移 test_migrate_table 至 test_transfer_table");
|
|
||||||
MigrateResult migrate = sqlManager.createMigrate()
|
|
||||||
.tableName("test_migrate_table", "test_transfer_table", null)
|
|
||||||
.autoIncrementColumn("id", "dbId")
|
|
||||||
.column("uuid", "uniqueId", "VARCHAR(40) NOT NULL")
|
|
||||||
.column("username", "name", "VARCHAR(20) NOT NULL")
|
|
||||||
.column("age", "age", "TINYINT DEFAULT 0 NOT NULL")
|
|
||||||
.migrate();
|
|
||||||
|
|
||||||
if (migrate.error() != null) {
|
|
||||||
migrate.error().printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> set = sqlManager.fetchTableMetadata("test_transfer_table").listColumns().join()
|
|
||||||
.stream()
|
|
||||||
.map(String::toLowerCase)
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
print(" 迁移后的表结构: " + set);
|
|
||||||
boolean containsAll = set.containsAll(Stream.of
|
|
||||||
("dbId", "uniqueId", "name", "age").map(String::toLowerCase).collect(Collectors.toSet()));
|
|
||||||
print(" 迁移结果: " + (migrate.success() ? "true" : migrate.error()) + " , 是否包含所有字段: " + containsAll);
|
|
||||||
try (SQLQuery query = sqlManager.createQuery().inTable("test_transfer_table").build().execute()) {
|
|
||||||
while (query.getResultSet().next()) {
|
|
||||||
print(" 迁移后的数据: " +
|
|
||||||
query.getResultSet().getInt("dbId") + " , " +
|
|
||||||
query.getResultSet().getString("uniqueId") + " , " +
|
|
||||||
query.getResultSet().getString("name") + " , " +
|
|
||||||
query.getResultSet().getInt("age")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package cc.carm.lib.easysql.tests;
|
package cc.carm.lib.easysql.tests;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.TestHandler;
|
import cc.carm.lib.easysql.TestHandler;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -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.4.7</version>
|
<version>0.4.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
+71
-108
@@ -1,128 +1,66 @@
|
|||||||
package cc.carm.lib.easysql.manager;
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.PreparedSQLBatchUpdateActionImpl;
|
import cc.carm.lib.easysql.action.PreparedSQLBatchUpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
|
import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.action.SQLUpdateActionImpl;
|
import cc.carm.lib.easysql.action.SQLBatchUpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.action.SQLUpdateBatchActionImpl;
|
import cc.carm.lib.easysql.action.UpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.action.base.BatchUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedBatchUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLBatchUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.builder.*;
|
import cc.carm.lib.easysql.api.builder.*;
|
||||||
|
import cc.carm.lib.easysql.api.enums.IsolationLevel;
|
||||||
import cc.carm.lib.easysql.api.function.SQLBiFunction;
|
import cc.carm.lib.easysql.api.function.SQLBiFunction;
|
||||||
import cc.carm.lib.easysql.api.function.SQLDebugHandler;
|
import cc.carm.lib.easysql.api.function.SQLDebugHandler;
|
||||||
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.migrate.SQLMigrate;
|
import cc.carm.lib.easysql.api.transaction.SQLTransaction;
|
||||||
import cc.carm.lib.easysql.builder.impl.*;
|
import cc.carm.lib.easysql.builder.impl.*;
|
||||||
import cc.carm.lib.easysql.migrate.SQLMigrateImpl;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class SQLManagerImpl implements SQLManager {
|
public class SQLManagerImpl extends SQLSourceImpl implements SQLManager {
|
||||||
|
|
||||||
private final Logger LOGGER;
|
|
||||||
private final DataSource dataSource;
|
|
||||||
private final ConcurrentHashMap<UUID, SQLQuery> activeQuery = new ConcurrentHashMap<>();
|
|
||||||
protected ExecutorService executorPool;
|
|
||||||
@NotNull Supplier<Boolean> debugMode = () -> Boolean.FALSE;
|
|
||||||
|
|
||||||
@NotNull SQLExceptionHandler exceptionHandler;
|
|
||||||
@NotNull SQLDebugHandler debugHandler;
|
|
||||||
|
|
||||||
public SQLManagerImpl(@NotNull DataSource dataSource) {
|
public SQLManagerImpl(@NotNull DataSource dataSource) {
|
||||||
this(dataSource, null);
|
super(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SQLManagerImpl(@NotNull DataSource dataSource, @Nullable String name) {
|
public SQLManagerImpl(@NotNull DataSource dataSource, @Nullable String name) {
|
||||||
this(dataSource, LoggerFactory.getLogger(SQLManagerImpl.class), name);
|
super(dataSource, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SQLManagerImpl(@NotNull DataSource dataSource, @NotNull Logger logger) {
|
||||||
|
super(dataSource, logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SQLManagerImpl(@NotNull DataSource dataSource, @NotNull Logger logger, @Nullable String name) {
|
public SQLManagerImpl(@NotNull DataSource dataSource, @NotNull Logger logger, @Nullable String name) {
|
||||||
String managerName = "SQLManager" + (name != null ? "#" + name : "");
|
super(dataSource, logger, name);
|
||||||
this.LOGGER = logger;
|
|
||||||
this.dataSource = dataSource;
|
|
||||||
this.executorPool = SQLManager.defaultExecutorPool(managerName);
|
|
||||||
this.exceptionHandler = SQLExceptionHandler.detailed(getLogger());
|
|
||||||
this.debugHandler = SQLDebugHandler.defaultHandler(getLogger());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public SQLManagerImpl(@NotNull DataSource dataSource, @NotNull Logger logger, @NotNull ExecutorService executorPool,
|
||||||
public boolean isDebugMode() {
|
@NotNull Supplier<Boolean> debugMode, @NotNull SQLDebugHandler debugHandler,
|
||||||
return this.debugMode.get();
|
@NotNull SQLExceptionHandler exceptionHandler) {
|
||||||
}
|
super(dataSource, logger, executorPool, debugMode, debugHandler, exceptionHandler);
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setDebugMode(@NotNull Supplier<@NotNull Boolean> debugMode) {
|
|
||||||
this.debugMode = debugMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull SQLDebugHandler getDebugHandler() {
|
|
||||||
return this.debugHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setDebugHandler(@NotNull SQLDebugHandler debugHandler) {
|
|
||||||
this.debugHandler = debugHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Logger getLogger() {
|
|
||||||
return LOGGER;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ExecutorService getExecutorPool() {
|
|
||||||
return executorPool;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setExecutorPool(@NotNull ExecutorService executorPool) {
|
|
||||||
this.executorPool = executorPool;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull DataSource getDataSource() {
|
|
||||||
return this.dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Connection getConnection() throws SQLException {
|
|
||||||
return getDataSource().getConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Map<UUID, SQLQuery> getActiveQuery() {
|
|
||||||
return this.activeQuery;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull SQLExceptionHandler getExceptionHandler() {
|
|
||||||
return this.exceptionHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
|
|
||||||
if (handler == null) this.exceptionHandler = SQLExceptionHandler.detailed(getLogger());
|
|
||||||
else this.exceptionHandler = handler;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer executeSQL(String sql) {
|
public Integer executeSQL(String sql) {
|
||||||
return new SQLUpdateActionImpl<>(this, Integer.class, sql).execute(null);
|
return new UpdateActionImpl<>(this, Integer.class, sql).execute(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -132,12 +70,12 @@ public class SQLManagerImpl implements SQLManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch) {
|
public List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch) {
|
||||||
return new PreparedSQLBatchUpdateActionImpl<>(this, Integer.class, sql).setAllParams(paramsBatch).execute(null);
|
return new PreparedSQLBatchUpdateActionImpl<>(this, Integer.class, sql).allValues(paramsBatch).execute(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL) {
|
public List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL) {
|
||||||
SQLUpdateBatchAction action = new SQLUpdateBatchActionImpl(this, sql);
|
BatchUpdateAction action = new SQLBatchUpdateActionImpl(this, sql);
|
||||||
if (moreSQL != null && moreSQL.length > 0) {
|
if (moreSQL != null && moreSQL.length > 0) {
|
||||||
Arrays.stream(moreSQL).forEach(action::addBatch);
|
Arrays.stream(moreSQL).forEach(action::addBatch);
|
||||||
}
|
}
|
||||||
@@ -149,7 +87,7 @@ public class SQLManagerImpl implements SQLManager {
|
|||||||
Iterator<String> iterator = sqlBatch.iterator();
|
Iterator<String> iterator = sqlBatch.iterator();
|
||||||
if (!iterator.hasNext()) return null; // PLEASE GIVE IT SOMETHING
|
if (!iterator.hasNext()) return null; // PLEASE GIVE IT SOMETHING
|
||||||
|
|
||||||
SQLUpdateBatchAction action = new SQLUpdateBatchActionImpl(this, iterator.next());
|
BatchUpdateAction action = new SQLBatchUpdateActionImpl(this, iterator.next());
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
action.addBatch(iterator.next());
|
action.addBatch(iterator.next());
|
||||||
}
|
}
|
||||||
@@ -157,6 +95,11 @@ public class SQLManagerImpl implements SQLManager {
|
|||||||
return action.execute(null);
|
return action.execute(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull SQLTransaction createTransaction(@Nullable IsolationLevel level) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R> CompletableFuture<R> fetchMetadata(@NotNull SQLBiFunction<DatabaseMetaData, Connection, R> reader) {
|
public <R> CompletableFuture<R> fetchMetadata(@NotNull SQLBiFunction<DatabaseMetaData, Connection, R> reader) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
@@ -202,45 +145,65 @@ public class SQLManagerImpl implements SQLManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLMigrate createMigrate() {
|
public InsertBuilder<PreparedBatchUpdateAction<Integer>> createInsertBatch(@NotNull String tableName) {
|
||||||
return new SQLMigrateImpl(this);
|
return new InsertBuilderImpl<PreparedBatchUpdateAction<Integer>>(this, tableName) {
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull String tableName) {
|
|
||||||
return new InsertBuilderImpl<PreparedSQLUpdateBatchAction<Integer>>(this, tableName) {
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateBatchAction<Integer> setColumnNames(List<String> columnNames) {
|
public PreparedBatchUpdateAction<Integer> columns(List<String> columnNames) {
|
||||||
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull String tableName) {
|
public InsertBuilder<PreparedUpdateAction<Integer>> insertInto(@NotNull String tableName) {
|
||||||
return new InsertBuilderImpl<PreparedSQLUpdateAction<Integer>>(this, tableName) {
|
return new InsertBuilderImpl<PreparedUpdateAction<Integer>>(this, tableName) {
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateAction<Integer> setColumnNames(List<String> columnNames) {
|
public PreparedUpdateAction<Integer> columns(List<String> columnNames) {
|
||||||
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull String tableName) {
|
public @NotNull InsertBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> insertBatchInto(@NotNull String tableName) {
|
||||||
return new ReplaceBuilderImpl<PreparedSQLUpdateBatchAction<Integer>>(this, tableName) {
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction.Advanced<Integer>> replaceInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLBatchUpdateAction.Advanced<Integer>> replaceBatchInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull UpdateBuilder<PreparedSQLUpdateAction.Advanced<Integer>> updateInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull DeleteBuilder<PreparedSQLUpdateAction.Advanced<Integer>> deleteFrom(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReplaceBuilder<PreparedBatchUpdateAction<Integer>> createReplaceBatch(@NotNull String tableName) {
|
||||||
|
return new ReplaceBuilderImpl<PreparedBatchUpdateAction<Integer>>(this, tableName) {
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateBatchAction<Integer> setColumnNames(List<String> columnNames) {
|
public PreparedBatchUpdateAction<Integer> columns(List<String> columnNames) {
|
||||||
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull String tableName) {
|
public ReplaceBuilder<PreparedUpdateAction<Integer>> createReplace(@NotNull String tableName) {
|
||||||
return new ReplaceBuilderImpl<PreparedSQLUpdateAction<Integer>>(this, tableName) {
|
return new ReplaceBuilderImpl<PreparedUpdateAction<Integer>>(this, tableName) {
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateAction<Integer> setColumnNames(List<String> columnNames) {
|
public PreparedUpdateAction<Integer> columns(List<String> columnNames) {
|
||||||
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
import cc.carm.lib.easysql.api.SQLSource;
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLDebugHandler;
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class SQLSourceImpl implements SQLSource {
|
||||||
|
|
||||||
|
protected final @NotNull Logger logger;
|
||||||
|
|
||||||
|
protected final @NotNull DataSource dataSource;
|
||||||
|
protected @NotNull ExecutorService executorPool;
|
||||||
|
|
||||||
|
protected @NotNull Supplier<Boolean> debugMode = () -> Boolean.FALSE;
|
||||||
|
protected @NotNull SQLDebugHandler debugHandler;
|
||||||
|
protected @NotNull SQLExceptionHandler exceptionHandler;
|
||||||
|
|
||||||
|
protected final ConcurrentHashMap<UUID, SQLQuery> activeQueries = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public SQLSourceImpl(@NotNull DataSource dataSource) {
|
||||||
|
this(dataSource, (String) null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SQLSourceImpl(@NotNull DataSource dataSource, @Nullable String name) {
|
||||||
|
this(dataSource, LoggerFactory.getLogger(SQLManagerImpl.class), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SQLSourceImpl(@NotNull DataSource dataSource, @NotNull Logger logger) {
|
||||||
|
this(dataSource, logger, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SQLSourceImpl(@NotNull DataSource dataSource, @NotNull Logger logger, @Nullable String name) {
|
||||||
|
this(
|
||||||
|
dataSource, logger,
|
||||||
|
SQLSource.defaultExecutorPool("SQLSource" + (name != null ? "#" + name : "")),
|
||||||
|
() -> false, SQLDebugHandler.defaultHandler(logger), SQLExceptionHandler.detailed(logger)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SQLSourceImpl(@NotNull DataSource dataSource, @NotNull Logger logger, @NotNull ExecutorService executorPool,
|
||||||
|
@NotNull Supplier<Boolean> debugMode, @NotNull SQLDebugHandler debugHandler,
|
||||||
|
@NotNull SQLExceptionHandler exceptionHandler) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.dataSource = dataSource;
|
||||||
|
this.executorPool = executorPool;
|
||||||
|
this.exceptionHandler = exceptionHandler;
|
||||||
|
this.debugMode = debugMode;
|
||||||
|
this.debugHandler = debugHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Map<UUID, SQLQuery> getActiveQueries() {
|
||||||
|
return this.activeQueries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull DataSource getDataSource() {
|
||||||
|
return this.dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Connection getConnection() throws SQLException {
|
||||||
|
return getDataSource().getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Logger getLogger() {
|
||||||
|
return this.logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ExecutorService getExecutorPool() {
|
||||||
|
return this.executorPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setExecutorPool(@NotNull ExecutorService executorPool) {
|
||||||
|
this.executorPool = executorPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDebugMode() {
|
||||||
|
return this.debugMode.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDebugMode(@NotNull Supplier<@NotNull Boolean> debugMode) {
|
||||||
|
this.debugMode = debugMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull SQLDebugHandler getDebugHandler() {
|
||||||
|
return this.debugHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDebugHandler(@NotNull SQLDebugHandler debugHandler) {
|
||||||
|
this.debugHandler = debugHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull SQLExceptionHandler getExceptionHandler() {
|
||||||
|
return this.exceptionHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
|
||||||
|
if (handler == null) this.exceptionHandler = SQLExceptionHandler.detailed(getLogger());
|
||||||
|
else this.exceptionHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.action.query.PreparedSQLQueryAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.query.SQLQueryAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLBatchUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.action.update.PreparedSQLUpdateAction;
|
||||||
|
import cc.carm.lib.easysql.api.builder.*;
|
||||||
|
import cc.carm.lib.easysql.api.enums.IsolationLevel;
|
||||||
|
import cc.carm.lib.easysql.api.transaction.SQLSavepoint;
|
||||||
|
import cc.carm.lib.easysql.api.transaction.SQLTransaction;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SQLTransactionImpl implements SQLTransaction {
|
||||||
|
@Override
|
||||||
|
public @NotNull SQLManager getManager() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull IsolationLevel getIsolationLevel() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commit() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull SQLSavepoint savepoint(@NotNull String name) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rollback(@Nullable SQLSavepoint savepoint) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable Integer executeSQL(String sql) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable Integer executeSQL(String sql, Object[] params) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull QueryBuilder<SQLQueryAction.Base, PreparedSQLQueryAction.Base> createQuery() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull InsertBuilder<PreparedSQLUpdateAction.Base<Integer>> insertInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull InsertBuilder<PreparedSQLBatchUpdateAction.Base<Integer>> insertBatchInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction.Base<Integer>> replaceInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLBatchUpdateAction.Base<Integer>> replaceBatchInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull UpdateBuilder<PreparedSQLUpdateAction.Base<Integer>> updateInto(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull DeleteBuilder<PreparedSQLUpdateAction.Base<Integer>> deleteFrom(@NotNull String tableName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
package cc.carm.lib.easysql.action;
|
package cc.carm.lib.easysql.action;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
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 cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.action;
|
package cc.carm.lib.easysql.action;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedBatchUpdateAction;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import cc.carm.lib.easysql.util.StatementUtil;
|
import cc.carm.lib.easysql.util.StatementUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
public class PreparedSQLBatchUpdateActionImpl<T extends Number>
|
public class PreparedSQLBatchUpdateActionImpl<T extends Number>
|
||||||
extends AbstractSQLAction<List<T>>
|
extends AbstractSQLAction<List<T>>
|
||||||
implements PreparedSQLUpdateBatchAction<T> {
|
implements PreparedBatchUpdateAction<T> {
|
||||||
|
|
||||||
boolean returnKeys = false;
|
boolean returnKeys = false;
|
||||||
@NotNull List<Object[]> allParams = new ArrayList<>();
|
@NotNull List<Object[]> allParams = new ArrayList<>();
|
||||||
@@ -38,16 +38,16 @@ public class PreparedSQLBatchUpdateActionImpl<T extends Number>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLBatchUpdateActionImpl<T> setAllParams(Iterable<Object[]> allParams) {
|
public PreparedSQLBatchUpdateActionImpl<T> allValues(Iterable<Object[]> allValues) {
|
||||||
List<Object[]> paramsList = new ArrayList<>();
|
List<Object[]> paramsList = new ArrayList<>();
|
||||||
allParams.forEach(paramsList::add);
|
allValues.forEach(paramsList::add);
|
||||||
this.allParams = paramsList;
|
this.allParams = paramsList;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLBatchUpdateActionImpl<T> addParamsBatch(Object... params) {
|
public PreparedSQLBatchUpdateActionImpl<T> addValues(Object... values) {
|
||||||
this.allParams.add(params);
|
this.allParams.add(values);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ public class PreparedSQLBatchUpdateActionImpl<T extends Number>
|
|||||||
@Override
|
@Override
|
||||||
public <N extends Number> PreparedSQLBatchUpdateActionImpl<N> returnGeneratedKeys(Class<N> keyTypeClass) {
|
public <N extends Number> PreparedSQLBatchUpdateActionImpl<N> returnGeneratedKeys(Class<N> keyTypeClass) {
|
||||||
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent())
|
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent())
|
||||||
.setAllParams(allParams).returnGeneratedKeys();
|
.allValues(allParams).returnGeneratedKeys();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package cc.carm.lib.easysql.action;
|
package cc.carm.lib.easysql.action;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.base.UpdateAction;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import cc.carm.lib.easysql.util.StatementUtil;
|
import cc.carm.lib.easysql.util.StatementUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -17,8 +17,8 @@ import java.util.List;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class PreparedSQLUpdateActionImpl<T extends Number>
|
public class PreparedSQLUpdateActionImpl<T extends Number>
|
||||||
extends SQLUpdateActionImpl<T>
|
extends UpdateActionImpl<T>
|
||||||
implements PreparedSQLUpdateAction<T> {
|
implements PreparedUpdateAction<T> {
|
||||||
|
|
||||||
Object[] params;
|
Object[] params;
|
||||||
|
|
||||||
@@ -46,19 +46,19 @@ public class PreparedSQLUpdateActionImpl<T extends Number>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateActionImpl<T> setParams(Object... params) {
|
public PreparedSQLUpdateActionImpl<T> values(Object... params) {
|
||||||
this.params = params;
|
this.params = params;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateActionImpl<T> setParams(@Nullable Iterable<Object> params) {
|
public PreparedSQLUpdateActionImpl<T> values(@Nullable Iterable<Object> params) {
|
||||||
if (params == null) {
|
if (params == null) {
|
||||||
return setParams((Object[]) null);
|
return values((Object[]) null);
|
||||||
} else {
|
} else {
|
||||||
List<Object> paramsList = new ArrayList<>();
|
List<Object> paramsList = new ArrayList<>();
|
||||||
params.forEach(paramsList::add);
|
params.forEach(paramsList::add);
|
||||||
return setParams(paramsList.toArray());
|
return values(paramsList.toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ public class PreparedSQLUpdateActionImpl<T extends Number>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass) {
|
public <N extends Number> UpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass) {
|
||||||
PreparedSQLUpdateActionImpl<N> newAction = new PreparedSQLUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent(), params);
|
PreparedSQLUpdateActionImpl<N> newAction = new PreparedSQLUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent(), params);
|
||||||
newAction.returnGeneratedKey();
|
newAction.returnGeneratedKey();
|
||||||
return newAction;
|
return newAction;
|
||||||
|
|||||||
+6
-6
@@ -1,7 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.action;
|
package cc.carm.lib.easysql.action;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.base.BatchUpdateAction;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@@ -13,13 +13,13 @@ import java.util.List;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class SQLUpdateBatchActionImpl
|
public class SQLBatchUpdateActionImpl
|
||||||
extends AbstractSQLAction<List<Integer>>
|
extends AbstractSQLAction<List<Integer>>
|
||||||
implements SQLUpdateBatchAction {
|
implements BatchUpdateAction {
|
||||||
|
|
||||||
protected final List<String> sqlContents = new ArrayList<>();
|
protected final List<String> sqlContents = new ArrayList<>();
|
||||||
|
|
||||||
public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
public SQLBatchUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
||||||
super(manager, sql);
|
super(manager, sql);
|
||||||
this.sqlContents.add(sql);
|
this.sqlContents.add(sql);
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ public class SQLUpdateBatchActionImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLUpdateBatchAction addBatch(@NotNull String sql) {
|
public BatchUpdateAction addBatch(@NotNull String sql) {
|
||||||
Objects.requireNonNull(sql, "sql could not be null");
|
Objects.requireNonNull(sql, "sql could not be null");
|
||||||
this.sqlContents.add(sql);
|
this.sqlContents.add(sql);
|
||||||
return this;
|
return this;
|
||||||
+11
-11
@@ -1,7 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.action;
|
package cc.carm.lib.easysql.action;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.base.UpdateAction;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@@ -11,22 +11,22 @@ import java.sql.Statement;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class SQLUpdateActionImpl<T extends Number>
|
public class UpdateActionImpl<T extends Number>
|
||||||
extends AbstractSQLAction<T>
|
extends AbstractSQLAction<T>
|
||||||
implements SQLUpdateAction<T> {
|
implements UpdateAction<T> {
|
||||||
|
|
||||||
protected final @NotNull Class<T> numberClass;
|
protected final @NotNull Class<T> numberClass;
|
||||||
|
|
||||||
protected boolean returnGeneratedKeys = false;
|
protected boolean returnGeneratedKeys = false;
|
||||||
|
|
||||||
public SQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
|
public UpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
|
||||||
@NotNull String sql) {
|
@NotNull String sql) {
|
||||||
super(manager, sql);
|
super(manager, sql);
|
||||||
this.numberClass = numberClass;
|
this.numberClass = numberClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
|
public UpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
|
||||||
@NotNull UUID uuid, @NotNull String sql) {
|
@NotNull UUID uuid, @NotNull String sql) {
|
||||||
super(manager, sql, uuid);
|
super(manager, sql, uuid);
|
||||||
this.numberClass = numberClass;
|
this.numberClass = numberClass;
|
||||||
}
|
}
|
||||||
@@ -52,14 +52,14 @@ public class SQLUpdateActionImpl<T extends Number>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLUpdateAction<T> returnGeneratedKey() {
|
public UpdateAction<T> returnGeneratedKey() {
|
||||||
this.returnGeneratedKeys = true;
|
this.returnGeneratedKeys = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass) {
|
public <N extends Number> UpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass) {
|
||||||
return new SQLUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent()).returnGeneratedKey();
|
return new UpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent()).returnGeneratedKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+8
-8
@@ -1,7 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.action.query;
|
package cc.carm.lib.easysql.action.query;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import cc.carm.lib.easysql.query.SQLQueryImpl;
|
import cc.carm.lib.easysql.query.SQLQueryImpl;
|
||||||
import cc.carm.lib.easysql.util.StatementUtil;
|
import cc.carm.lib.easysql.util.StatementUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -15,23 +15,23 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class PreparedQueryActionImpl extends QueryActionImpl implements PreparedQueryAction {
|
public class PreparedSQLQueryActionImpl extends SQLQueryActionImpl implements PreparedQueryAction {
|
||||||
|
|
||||||
Consumer<PreparedStatement> handler;
|
Consumer<PreparedStatement> handler;
|
||||||
Object[] params;
|
Object[] params;
|
||||||
|
|
||||||
public PreparedQueryActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
public PreparedSQLQueryActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
||||||
super(manager, sql);
|
super(manager, sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedQueryActionImpl setParams(@Nullable Object... params) {
|
public @NotNull PreparedSQLQueryActionImpl setParams(@Nullable Object... params) {
|
||||||
this.params = params;
|
this.params = params;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedQueryActionImpl setParams(@Nullable Iterable<Object> params) {
|
public PreparedSQLQueryActionImpl setParams(@Nullable Iterable<Object> params) {
|
||||||
if (params == null) {
|
if (params == null) {
|
||||||
return setParams((Object[]) null);
|
return setParams((Object[]) null);
|
||||||
} else {
|
} else {
|
||||||
@@ -42,7 +42,7 @@ public class PreparedQueryActionImpl extends QueryActionImpl implements Prepared
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedQueryActionImpl handleStatement(@Nullable Consumer<PreparedStatement> statement) {
|
public PreparedSQLQueryActionImpl handleStatement(@Nullable Consumer<PreparedStatement> statement) {
|
||||||
this.handler = statement;
|
this.handler = statement;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ public class PreparedQueryActionImpl extends QueryActionImpl implements Prepared
|
|||||||
connection, preparedStatement,
|
connection, preparedStatement,
|
||||||
preparedStatement.executeQuery()
|
preparedStatement.executeQuery()
|
||||||
);
|
);
|
||||||
getManager().getActiveQuery().put(getActionUUID(), query);
|
getManager().getActiveQueries().put(getActionUUID(), query);
|
||||||
return query;
|
return query;
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
preparedStatement.close();
|
preparedStatement.close();
|
||||||
+5
-5
@@ -2,10 +2,10 @@ package cc.carm.lib.easysql.action.query;
|
|||||||
|
|
||||||
import cc.carm.lib.easysql.action.AbstractSQLAction;
|
import cc.carm.lib.easysql.action.AbstractSQLAction;
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.api.action.query.QueryAction;
|
import cc.carm.lib.easysql.api.action.query.SQLQueryAction;
|
||||||
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
||||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import cc.carm.lib.easysql.query.SQLQueryImpl;
|
import cc.carm.lib.easysql.query.SQLQueryImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -14,9 +14,9 @@ import java.sql.SQLException;
|
|||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements QueryAction {
|
public class SQLQueryActionImpl extends AbstractSQLAction<SQLQuery> implements SQLQueryAction<SQLQueryActionImpl> {
|
||||||
|
|
||||||
public QueryActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
public SQLQueryActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
||||||
super(manager, sql);
|
super(manager, sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
|
|||||||
connection, statement,
|
connection, statement,
|
||||||
statement.executeQuery(getSQLContent())
|
statement.executeQuery(getSQLContent())
|
||||||
);
|
);
|
||||||
getManager().getActiveQuery().put(getActionUUID(), query);
|
getManager().getActiveQueries().put(getActionUUID(), query);
|
||||||
|
|
||||||
return query;
|
return query;
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package cc.carm.lib.easysql.builder;
|
package cc.carm.lib.easysql.builder;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import cc.carm.lib.easysql.api.SQLBuilder;
|
import cc.carm.lib.easysql.api.SQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -20,4 +20,5 @@ public abstract class AbstractSQLBuilder implements SQLBuilder {
|
|||||||
public @NotNull SQLManagerImpl getManager() {
|
public @NotNull SQLManagerImpl getManager() {
|
||||||
return this.sqlManager;
|
return this.sqlManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.ConditionalBuilder;
|
import cc.carm.lib.easysql.api.builder.ConditionalBuilder;
|
||||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -27,7 +26,7 @@ public abstract class AbstractConditionalBuilder<B extends ConditionalBuilder<B,
|
|||||||
protected abstract B getThis();
|
protected abstract B getThis();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public B setConditions(@Nullable String condition) {
|
public B where(@Nullable String condition) {
|
||||||
this.conditionSQLs = new ArrayList<>();
|
this.conditionSQLs = new ArrayList<>();
|
||||||
this.conditionParams = new ArrayList<>();
|
this.conditionParams = new ArrayList<>();
|
||||||
if (condition != null) this.conditionSQLs.add(condition);
|
if (condition != null) this.conditionSQLs.add(condition);
|
||||||
@@ -35,7 +34,7 @@ public abstract class AbstractConditionalBuilder<B extends ConditionalBuilder<B,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public B setConditions(
|
public B where(
|
||||||
LinkedHashMap<@NotNull String, @Nullable Object> conditions
|
LinkedHashMap<@NotNull String, @Nullable Object> conditions
|
||||||
) {
|
) {
|
||||||
conditions.forEach(this::addCondition);
|
conditions.forEach(this::addCondition);
|
||||||
@@ -119,7 +118,7 @@ public abstract class AbstractConditionalBuilder<B extends ConditionalBuilder<B,
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public B setLimit(int limit) {
|
public B limit(int limit) {
|
||||||
this.limit = limit;
|
this.limit = limit;
|
||||||
return getThis();
|
return getThis();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
|
import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.DeleteBuilder;
|
import cc.carm.lib.easysql.api.builder.DeleteBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -24,7 +23,7 @@ public class DeleteBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateAction<Integer> build() {
|
public PreparedUpdateAction<Integer> build() {
|
||||||
|
|
||||||
StringBuilder sqlBuilder = new StringBuilder();
|
StringBuilder sqlBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.InsertBuilder;
|
import cc.carm.lib.easysql.api.builder.InsertBuilder;
|
||||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.query.PreparedQueryActionImpl;
|
import cc.carm.lib.easysql.action.query.PreparedSQLQueryActionImpl;
|
||||||
import cc.carm.lib.easysql.action.query.QueryActionImpl;
|
import cc.carm.lib.easysql.action.query.SQLQueryActionImpl;
|
||||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
|
||||||
import cc.carm.lib.easysql.api.action.query.QueryAction;
|
import cc.carm.lib.easysql.api.action.base.QueryAction;
|
||||||
import cc.carm.lib.easysql.api.builder.QueryBuilder;
|
import cc.carm.lib.easysql.api.builder.QueryBuilder;
|
||||||
import cc.carm.lib.easysql.api.builder.TableQueryBuilder;
|
import cc.carm.lib.easysql.api.builder.TableQueryBuilder;
|
||||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class QueryBuilderImpl extends AbstractSQLBuilder implements QueryBuilder {
|
public class QueryBuilderImpl extends AbstractSQLBuilder implements QueryBuilder {
|
||||||
@@ -23,17 +21,17 @@ public class QueryBuilderImpl extends AbstractSQLBuilder implements QueryBuilder
|
|||||||
@Deprecated
|
@Deprecated
|
||||||
public QueryAction withSQL(@NotNull String sql) {
|
public QueryAction withSQL(@NotNull String sql) {
|
||||||
Objects.requireNonNull(sql, "sql could not be null");
|
Objects.requireNonNull(sql, "sql could not be null");
|
||||||
return new QueryActionImpl(getManager(), sql);
|
return new SQLQueryActionImpl(getManager(), sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedQueryAction withPreparedSQL(@NotNull String sql) {
|
public PreparedQueryAction withPreparedSQL(@NotNull String sql) {
|
||||||
Objects.requireNonNull(sql, "sql could not be null");
|
Objects.requireNonNull(sql, "sql could not be null");
|
||||||
return new PreparedQueryActionImpl(getManager(), sql);
|
return new PreparedSQLQueryActionImpl(getManager(), sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableQueryBuilder inTable(@NotNull String tableName) {
|
public TableQueryBuilder fromTable(@NotNull String tableName) {
|
||||||
Objects.requireNonNull(tableName, "tableName could not be null");
|
Objects.requireNonNull(tableName, "tableName could not be null");
|
||||||
return new TableQueryBuilderImpl(getManager(), tableName);
|
return new TableQueryBuilderImpl(getManager(), tableName);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.ReplaceBuilder;
|
import cc.carm.lib.easysql.api.builder.ReplaceBuilder;
|
||||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.SQLUpdateActionImpl;
|
import cc.carm.lib.easysql.action.UpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.TableAlterBuilder;
|
import cc.carm.lib.easysql.api.builder.TableAlterBuilder;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -141,7 +140,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SQLUpdateActionImpl<Integer> createAction(@NotNull String sql) {
|
private UpdateActionImpl<Integer> createAction(@NotNull String sql) {
|
||||||
return new SQLUpdateActionImpl<>(getManager(), Integer.class, sql);
|
return new UpdateActionImpl<>(getManager(), Integer.class, sql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.SQLUpdateActionImpl;
|
import cc.carm.lib.easysql.action.UpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.base.UpdateAction;
|
||||||
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
||||||
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLUpdateAction<Integer> build() {
|
public UpdateAction<Integer> build() {
|
||||||
StringBuilder createSQL = new StringBuilder();
|
StringBuilder createSQL = new StringBuilder();
|
||||||
createSQL.append("CREATE TABLE IF NOT EXISTS ").append(withBackQuote(tableName));
|
createSQL.append("CREATE TABLE IF NOT EXISTS ").append(withBackQuote(tableName));
|
||||||
createSQL.append("(");
|
createSQL.append("(");
|
||||||
@@ -94,7 +94,7 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
createSQL.append(" COMMENT ").append(withQuote(tableComment));
|
createSQL.append(" COMMENT ").append(withQuote(tableComment));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SQLUpdateActionImpl<>(getManager(), Integer.class, createSQL.toString());
|
return new UpdateActionImpl<>(getManager(), Integer.class, createSQL.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import cc.carm.lib.easysql.api.builder.TableMetadataBuilder;
|
|||||||
import cc.carm.lib.easysql.api.function.SQLBiFunction;
|
import cc.carm.lib.easysql.api.function.SQLBiFunction;
|
||||||
import cc.carm.lib.easysql.api.function.SQLFunction;
|
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.query.PreparedQueryActionImpl;
|
import cc.carm.lib.easysql.action.query.PreparedSQLQueryActionImpl;
|
||||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
|
||||||
import cc.carm.lib.easysql.api.builder.TableQueryBuilder;
|
import cc.carm.lib.easysql.api.builder.TableQueryBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ public class TableQueryBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedQueryActionImpl build() {
|
public PreparedSQLQueryActionImpl build() {
|
||||||
StringBuilder sqlBuilder = new StringBuilder();
|
StringBuilder sqlBuilder = new StringBuilder();
|
||||||
sqlBuilder.append("SELECT").append(" ");
|
sqlBuilder.append("SELECT").append(" ");
|
||||||
if (columns == null || columns.length < 1) {
|
if (columns == null || columns.length < 1) {
|
||||||
@@ -58,7 +58,7 @@ public class TableQueryBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new PreparedQueryActionImpl(getManager(), sqlBuilder.toString())
|
return new PreparedSQLQueryActionImpl(getManager(), sqlBuilder.toString())
|
||||||
.setParams(hasConditionParams() ? getConditionParams() : null);
|
.setParams(hasConditionParams() ? getConditionParams() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ public class TableQueryBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableQueryBuilderImpl selectColumns(@NotNull String... columnNames) {
|
public TableQueryBuilderImpl select(@NotNull String... columnNames) {
|
||||||
Objects.requireNonNull(columnNames, "columnNames could not be null");
|
Objects.requireNonNull(columnNames, "columnNames could not be null");
|
||||||
this.columns = columnNames;
|
this.columns = columnNames;
|
||||||
return this;
|
return this;
|
||||||
@@ -82,7 +82,7 @@ public class TableQueryBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableQueryBuilder setPageLimit(int start, int end) {
|
public TableQueryBuilder limit(int start, int end) {
|
||||||
this.pageLimit = new int[]{start, end};
|
this.pageLimit = new int[]{start, end};
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package cc.carm.lib.easysql.builder.impl;
|
package cc.carm.lib.easysql.builder.impl;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
|
import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
|
||||||
import cc.carm.lib.easysql.api.SQLAction;
|
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.UpdateBuilder;
|
import cc.carm.lib.easysql.api.builder.UpdateBuilder;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -27,7 +26,7 @@ public class UpdateBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreparedSQLUpdateAction<Integer> build() {
|
public PreparedUpdateAction<Integer> build() {
|
||||||
|
|
||||||
StringBuilder sqlBuilder = new StringBuilder();
|
StringBuilder sqlBuilder = new StringBuilder();
|
||||||
|
|
||||||
@@ -56,20 +55,20 @@ public class UpdateBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UpdateBuilder addColumnValue(@NotNull String columnName, Object columnValue) {
|
public UpdateBuilder set(@NotNull String columnName, Object columnValue) {
|
||||||
Objects.requireNonNull(columnName, "columnName could not be null");
|
Objects.requireNonNull(columnName, "columnName could not be null");
|
||||||
this.columnData.put(columnName, columnValue);
|
this.columnData.put(columnName, columnValue);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UpdateBuilder setColumnValues(LinkedHashMap<String, Object> columnData) {
|
public UpdateBuilder setAll(LinkedHashMap<String, Object> columnData) {
|
||||||
this.columnData = columnData;
|
this.columnData = columnData;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UpdateBuilder setColumnValues(@NotNull String[] columnNames, @Nullable Object[] columnValues) {
|
public UpdateBuilder setAll(@NotNull String[] columnNames, @Nullable Object[] columnValues) {
|
||||||
Objects.requireNonNull(columnNames, "columnName could not be null");
|
Objects.requireNonNull(columnNames, "columnName could not be null");
|
||||||
if (columnNames.length != columnValues.length) {
|
if (columnNames.length != columnValues.length) {
|
||||||
throw new RuntimeException("columnNames are not match with columnValues");
|
throw new RuntimeException("columnNames are not match with columnValues");
|
||||||
@@ -78,7 +77,7 @@ public class UpdateBuilderImpl
|
|||||||
for (int i = 0; i < columnNames.length; i++) {
|
for (int i = 0; i < columnNames.length; i++) {
|
||||||
columnData.put(columnNames[i], columnValues[i]);
|
columnData.put(columnNames[i], columnValues[i]);
|
||||||
}
|
}
|
||||||
return setColumnValues(columnData);
|
return setAll(columnData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.migrate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 2022/11/28<br>
|
|
||||||
* EasySQL<br>
|
|
||||||
*
|
|
||||||
* @author huanmeng_qwq
|
|
||||||
*/
|
|
||||||
public class AutoIncrementMigrateData extends MigrateData {
|
|
||||||
public AutoIncrementMigrateData(String name) {
|
|
||||||
super(name, null, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.migrate;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 2022/11/28<br>
|
|
||||||
* EasySQL<br>
|
|
||||||
*
|
|
||||||
* @author huanmeng_qwq
|
|
||||||
*/
|
|
||||||
public class MigrateData {
|
|
||||||
private final String name;
|
|
||||||
private final String settings;
|
|
||||||
private final IndexType indexType;
|
|
||||||
|
|
||||||
public MigrateData(String name, String settings, IndexType indexType) {
|
|
||||||
this.name = name;
|
|
||||||
this.settings = settings;
|
|
||||||
this.indexType = indexType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String name() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String settings() {
|
|
||||||
return settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IndexType indexType() {
|
|
||||||
return indexType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.migrate;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
|
||||||
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
|
||||||
import cc.carm.lib.easysql.api.enums.MigrateResult;
|
|
||||||
import cc.carm.lib.easysql.api.migrate.SQLMigrate;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 2022/11/28<br>
|
|
||||||
* EasySQL<br>
|
|
||||||
*
|
|
||||||
* @author huanmeng_qwq
|
|
||||||
*/
|
|
||||||
public class SQLMigrateImpl implements SQLMigrate {
|
|
||||||
private final SQLManager sqlManager;
|
|
||||||
|
|
||||||
private final Map<String, MigrateData> columnMap;
|
|
||||||
|
|
||||||
private String oldTableName;
|
|
||||||
private MigrateData newTableData;
|
|
||||||
|
|
||||||
public SQLMigrateImpl(SQLManager sqlManager) {
|
|
||||||
this.sqlManager = sqlManager;
|
|
||||||
this.columnMap = new LinkedHashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SQLMigrate tableName(String oldTableName, String newTableName, String newTableSettings) {
|
|
||||||
if (oldTableName == null) {
|
|
||||||
throw new IllegalArgumentException("oldTableName can not be null");
|
|
||||||
}
|
|
||||||
this.oldTableName = oldTableName;
|
|
||||||
this.newTableData = new MigrateData(newTableName, newTableSettings, null);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SQLMigrate column(String oldColumnName, String newColumnName, String newColumnSettings, IndexType indexType) {
|
|
||||||
if (oldColumnName == null) {
|
|
||||||
throw new IllegalArgumentException("oldColumnName can not be null");
|
|
||||||
}
|
|
||||||
columnMap.put(oldColumnName, new MigrateData(newColumnName, newColumnSettings, indexType));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SQLMigrate autoIncrementColumn(String oldColumnName, String newColumnName) {
|
|
||||||
if (oldColumnName == null) {
|
|
||||||
throw new IllegalArgumentException("oldColumnName can not be null");
|
|
||||||
}
|
|
||||||
columnMap.put(oldColumnName, new AutoIncrementMigrateData(newColumnName));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public MigrateResult migrate() throws SQLException {
|
|
||||||
if (oldTableName == null) {
|
|
||||||
return MigrateResult.from(MigrateResult.OLD_TABLE_NOT_EXIST, new IllegalArgumentException("oldTableName can not be null"));
|
|
||||||
}
|
|
||||||
if (newTableData == null) {
|
|
||||||
return MigrateResult.from(MigrateResult.NEW_TABLE_EXIST, new IllegalArgumentException("new table name can not be null"));
|
|
||||||
}
|
|
||||||
if (columnMap.isEmpty()) {
|
|
||||||
return MigrateResult.from(MigrateResult.NEW_COLUMN_EMPTY, new IllegalArgumentException("new column can not be empty"));
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// check table
|
|
||||||
Set<String> columns = sqlManager.fetchTableMetadata(oldTableName).listColumns().join();
|
|
||||||
for (String column : columns) {
|
|
||||||
if (columnMap.keySet().stream().noneMatch(k -> k.toLowerCase(Locale.ROOT).equals(column.toLowerCase(Locale.ROOT)))) {
|
|
||||||
return MigrateResult.from(MigrateResult.COLUMN_NOT_SET, new IllegalArgumentException("column " + column + " not set"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// create new table
|
|
||||||
TableCreateBuilder table = sqlManager.createTable(newTableData.name());
|
|
||||||
if (newTableData.settings() != null) {
|
|
||||||
table.setTableSettings(newTableData.settings());
|
|
||||||
}
|
|
||||||
// add columns
|
|
||||||
for (Map.Entry<String, MigrateData> entry : columnMap.entrySet()) {
|
|
||||||
MigrateData migrateData = entry.getValue();
|
|
||||||
if (migrateData.name() == null) {
|
|
||||||
// ignore
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (migrateData instanceof AutoIncrementMigrateData) {
|
|
||||||
table.addAutoIncrementColumn(migrateData.name());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
table.addColumn(migrateData.name(), migrateData.settings());
|
|
||||||
if (migrateData.indexType() != null) {
|
|
||||||
table.setIndex(migrateData.name(), migrateData.indexType());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
table.build().execute();
|
|
||||||
|
|
||||||
// insert data
|
|
||||||
try (SQLQuery query = sqlManager.createQuery().inTable(oldTableName).build().execute()) {
|
|
||||||
insert(query.getResultSet());
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
return new MigrateResult(false, e);
|
|
||||||
}
|
|
||||||
return MigrateResult.SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void insert(ResultSet resultSet) throws SQLException {
|
|
||||||
while (resultSet.next()) {
|
|
||||||
Map<String, String> columnNames = tableColumnNames();
|
|
||||||
PreparedSQLUpdateAction<Integer> updateAction = sqlManager.createInsert(newTableData.name()).setColumnNames(columnNames.values().toArray(new String[0]));
|
|
||||||
Object[] values = new Object[columnNames.size()];
|
|
||||||
int i = 0;
|
|
||||||
for (Map.Entry<String, String> entry : columnNames.entrySet()) {
|
|
||||||
values[i] = resultSet.getObject(entry.getKey());
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
updateAction.setParams(values);
|
|
||||||
updateAction.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, String> tableColumnNames() {
|
|
||||||
Map<String, String> map = new ConcurrentHashMap<>();
|
|
||||||
for (Map.Entry<String, MigrateData> entry : columnMap.entrySet()) {
|
|
||||||
MigrateData migrateData = entry.getValue();
|
|
||||||
if (migrateData.name() == null) {
|
|
||||||
// ignore
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (migrateData instanceof AutoIncrementMigrateData) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
map.put(entry.getKey(), migrateData.name());
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package cc.carm.lib.easysql.query;
|
package cc.carm.lib.easysql.query;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.action.query.QueryActionImpl;
|
import cc.carm.lib.easysql.action.query.SQLQueryActionImpl;
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.SQLManagerImpl;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -18,17 +18,17 @@ public class SQLQueryImpl implements SQLQuery {
|
|||||||
protected final Connection connection;
|
protected final Connection connection;
|
||||||
protected final Statement statement;
|
protected final Statement statement;
|
||||||
protected final ResultSet resultSet;
|
protected final ResultSet resultSet;
|
||||||
protected QueryActionImpl queryAction;
|
protected SQLQueryActionImpl queryAction;
|
||||||
|
|
||||||
public SQLQueryImpl(
|
public SQLQueryImpl(
|
||||||
SQLManagerImpl sqlManager, QueryActionImpl queryAction,
|
SQLManagerImpl sqlManager, SQLQueryActionImpl queryAction,
|
||||||
Connection connection, Statement statement, ResultSet resultSet
|
Connection connection, Statement statement, ResultSet resultSet
|
||||||
) {
|
) {
|
||||||
this(sqlManager, queryAction, connection, statement, resultSet, System.nanoTime());
|
this(sqlManager, queryAction, connection, statement, resultSet, System.nanoTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
public SQLQueryImpl(
|
public SQLQueryImpl(
|
||||||
SQLManagerImpl sqlManager, QueryActionImpl queryAction,
|
SQLManagerImpl sqlManager, SQLQueryActionImpl queryAction,
|
||||||
Connection connection, Statement statement, ResultSet resultSet,
|
Connection connection, Statement statement, ResultSet resultSet,
|
||||||
long executeTime
|
long executeTime
|
||||||
) {
|
) {
|
||||||
@@ -51,7 +51,7 @@ public class SQLQueryImpl implements SQLQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueryActionImpl getAction() {
|
public SQLQueryActionImpl getAction() {
|
||||||
return this.queryAction;
|
return this.queryAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ public class SQLQueryImpl implements SQLQuery {
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getManager().getActiveQuery().remove(getAction().getActionUUID());
|
getManager().getActiveQueries().remove(getAction().getActionUUID());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
getAction().handleException(getAction().defaultExceptionHandler(), e);
|
getAction().handleException(getAction().defaultExceptionHandler(), e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,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.4.7</version>
|
<version>0.4.6</version>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>api</module>
|
<module>api</module>
|
||||||
@@ -116,7 +116,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains</groupId>
|
<groupId>org.jetbrains</groupId>
|
||||||
<artifactId>annotations</artifactId>
|
<artifactId>annotations</artifactId>
|
||||||
<version>26.1.0</version>
|
<version>23.0.0</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-gpg-plugin</artifactId>
|
<artifactId>maven-gpg-plugin</artifactId>
|
||||||
<version>3.2.8</version>
|
<version>3.0.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>sign-artifacts</id>
|
<id>sign-artifacts</id>
|
||||||
@@ -153,7 +153,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-release-plugin</artifactId>
|
<artifactId>maven-release-plugin</artifactId>
|
||||||
<version>3.3.1</version>
|
<version>2.5.3</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<autoVersionSubmodules>true</autoVersionSubmodules>
|
<autoVersionSubmodules>true</autoVersionSubmodules>
|
||||||
<useReleaseProfile>false</useReleaseProfile>
|
<useReleaseProfile>false</useReleaseProfile>
|
||||||
@@ -169,7 +169,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
<version>3.12.0</version>
|
<version>3.4.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<classifier>javadoc</classifier>
|
<classifier>javadoc</classifier>
|
||||||
<detectJavaApiLink>false</detectJavaApiLink>
|
<detectJavaApiLink>false</detectJavaApiLink>
|
||||||
@@ -196,7 +196,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.15.0</version>
|
<version>3.10.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>${jdk.version}</source>
|
<source>${jdk.version}</source>
|
||||||
<target>${jdk.version}</target>
|
<target>${jdk.version}</target>
|
||||||
@@ -208,13 +208,13 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>3.5.0</version>
|
<version>3.3.0</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
<version>3.4.0</version>
|
<version>3.2.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
@@ -228,7 +228,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
<version>3.6.2</version>
|
<version>3.4.0</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
@@ -256,7 +256,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>3.5.5</version>
|
<version>2.22.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<useSystemClassLoader>false</useSystemClassLoader>
|
<useSystemClassLoader>false</useSystemClassLoader>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
|
||||||
"extends": [
|
|
||||||
"config:recommended"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -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.4.7</version>
|
<version>0.4.6</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
@@ -79,7 +79,7 @@
|
|||||||
<!--项目地址 https://github.com/Chris2018998/BeeCP -->
|
<!--项目地址 https://github.com/Chris2018998/BeeCP -->
|
||||||
<groupId>com.github.chris2018998</groupId>
|
<groupId>com.github.chris2018998</groupId>
|
||||||
<artifactId>beecp</artifactId>
|
<artifactId>beecp</artifactId>
|
||||||
<version>5.2.2</version>
|
<version>3.3.8</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@@ -107,7 +107,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
<version>3.6.2</version>
|
<version>3.4.0</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package cc.carm.lib.easysql;
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.api.util.TimeDateUtils;
|
import cc.carm.lib.easysql.api.util.TimeDateUtils;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
|
||||||
import cn.beecp.BeeDataSource;
|
import cn.beecp.BeeDataSource;
|
||||||
import cn.beecp.BeeDataSourceConfig;
|
import cn.beecp.BeeDataSourceConfig;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|||||||
@@ -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.4.7</version>
|
<version>0.4.6</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
@@ -74,7 +74,7 @@
|
|||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
<artifactId>HikariCP</artifactId>
|
<artifactId>HikariCP</artifactId>
|
||||||
<!--suppress MavenPackageUpdate -->
|
<!--suppress MavenPackageUpdate -->
|
||||||
<version>7.0.2</version>
|
<version>4.0.3</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@@ -102,7 +102,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
<version>3.6.2</version>
|
<version>3.4.0</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package cc.carm.lib.easysql;
|
package cc.carm.lib.easysql;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.lib.easysql.api.util.TimeDateUtils;
|
import cc.carm.lib.easysql.api.util.TimeDateUtils;
|
||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|||||||
Reference in New Issue
Block a user