mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-05 09:01:26 +08:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5f13b9319d | |||
| 5601d1288c | |||
| 50b36d8430 | |||
| cd60bf256e | |||
| ebe68befee | |||
| 594413e13b | |||
| df94272c73 | |||
| ab986e9526 | |||
| 3f3b7bf4a5 | |||
| c69695c16d | |||
| 02aad28715 | |||
| b423000fe5 | |||
| cf356f2492 |
@@ -0,0 +1,36 @@
|
||||
name: Build
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
|
||||
- name: Set up JDK 11
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 11
|
||||
- name: Cache SonarCloud packages
|
||||
uses: actions/cache@v1
|
||||
with:
|
||||
path: ~/.sonar/cache
|
||||
key: ${{ runner.os }}-sonar
|
||||
restore-keys: ${{ runner.os }}-sonar
|
||||
- name: Cache Maven packages
|
||||
uses: actions/cache@v1
|
||||
with:
|
||||
path: ~/.m2
|
||||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: ${{ runner.os }}-m2
|
||||
- name: Build and analyze
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=CarmJos_EasySQL
|
||||
@@ -26,10 +26,6 @@
|
||||
- 基于JDBC开发,可自选连接池、JDBC驱动。
|
||||
- 简单便捷的增删改查接口,无需手写SQL语句。
|
||||
- 额外提供部分常用情况的SQL操作
|
||||
- 存在则更新,不存在则插入
|
||||
- 创建表
|
||||
- 修改表
|
||||
- ...
|
||||
- 自动关闭数据流
|
||||
- 支持同步操作与异步操作
|
||||
|
||||
@@ -41,18 +37,43 @@
|
||||
|
||||
您可以 [点击这里](easysql-demo/src/main/java/EasySQLDemo.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
||||
|
||||
### 依赖方式 (Maven)
|
||||
### 依赖方式
|
||||
|
||||
#### Maven 依赖
|
||||
|
||||
<details>
|
||||
<summary>远程库配置</summary>
|
||||
|
||||
```xml
|
||||
|
||||
<project>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>github</id>
|
||||
<!--采用github依赖库,安全稳定,但需要配置 (推荐)-->
|
||||
<id>EasySQL</id>
|
||||
<name>GitHub Packages</name>
|
||||
<url>https://maven.pkg.github.com/CarmJos/EasySQL</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<!--采用我的私人依赖库,简单方便,但可能因为变故而无法使用-->
|
||||
<id>carm-repo</id>
|
||||
<name>Carm's Repo</name>
|
||||
<url>https://repo.carm.cc/repository/maven-public/</url>
|
||||
</repository>
|
||||
|
||||
</repositories>
|
||||
</project>
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>原生依赖</summary>
|
||||
|
||||
```xml
|
||||
|
||||
<project>
|
||||
<dependencies>
|
||||
<!--对于需要提供公共接口的项目,可以仅打包API部分,方便他人调用-->
|
||||
<dependency>
|
||||
@@ -70,14 +91,19 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!--如需自定义连接池,则可以仅打包实现部分,自行创建SQLManager-->
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-beecp</artifactId>
|
||||
<version>[LATEST RELEASE]</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>含连接池版本</summary>
|
||||
|
||||
```xml
|
||||
|
||||
<project>
|
||||
<dependencies>
|
||||
<!--也可直接选择打包了连接池的版本-->
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
@@ -91,12 +117,64 @@
|
||||
<version>[LATEST VERSION]</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
#### Gradle 依赖
|
||||
|
||||
<details>
|
||||
<summary>远程库配置</summary>
|
||||
|
||||
```groovy
|
||||
repositories {
|
||||
// 采用github依赖库,安全稳定,但需要配置 (推荐)
|
||||
maven { url 'https://maven.pkg.github.com/CarmJos/EasySQL' }
|
||||
|
||||
// 采用我的私人依赖库,简单方便,但可能因为变故而无法使用
|
||||
maven { url 'https://repo.carm.cc/repository/maven-public/' }
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>原生依赖</summary>
|
||||
|
||||
```groovy
|
||||
|
||||
dependencies {
|
||||
|
||||
//对于需要提供公共接口的项目,可以仅打包API部分,方便他人调用
|
||||
api "cc.carm.lib:easysql-api:[LATEST RELEASE]"
|
||||
|
||||
//如需自定义连接池,则可以仅打包实现部分,自行创建SQLManager
|
||||
api "cc.carm.lib:easysql-impl:[LATEST RELEASE]"
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>含连接池版本</summary>
|
||||
|
||||
```groovy
|
||||
|
||||
dependencies {
|
||||
|
||||
//也可直接选择打包了连接池的版本
|
||||
|
||||
api "cc.carm.lib:easysql-beecp:[LATEST RELEASE]"
|
||||
|
||||
api "cc.carm.lib:easysql-hikaricp:[LATEST RELEASE]"
|
||||
|
||||
}
|
||||
```
|
||||
</details>
|
||||
|
||||
## 支持与捐赠
|
||||
|
||||
若您觉得本插件做的不错,您可以通过捐赠支持我!
|
||||
@@ -108,7 +186,9 @@
|
||||
## 开源协议
|
||||
|
||||
本项目源码采用 [GNU General Public License v3.0](https://opensource.org/licenses/GPL-3.0) 开源协议。
|
||||
> ### 关于 GPL 协议
|
||||
<details>
|
||||
<summary>关于 GPL 协议</summary>
|
||||
|
||||
> GNU General Public Licence (GPL) 有可能是开源界最常用的许可模式。GPL 保证了所有开发者的权利,同时为使用者提供了足够的复制,分发,修改的权利:
|
||||
>
|
||||
> #### 可自由复制
|
||||
@@ -123,3 +203,4 @@
|
||||
> 需要注意的是,分发的时候,需要明确提供源代码和二进制文件,另外,用于某些程序的某些协议有一些问题和限制,你可以看一下 @PierreJoye 写的 Practical Guide to GPL Compliance 一文。使用 GPL 协议,你必须在源代码代码中包含相应信息,以及协议本身。
|
||||
>
|
||||
> *以上文字来自 [五种开源协议GPL,LGPL,BSD,MIT,Apache](https://www.oschina.net/question/54100_9455) 。*
|
||||
</details>
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<version>0.2.2</version>
|
||||
<version>0.2.6</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cc.carm.lib.easysql.api;
|
||||
|
||||
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -7,13 +8,12 @@ import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* SQLAction 是用于承载SQL语句并进行处理、返回的基本类。
|
||||
*
|
||||
* <ul>
|
||||
* <li>同步执行 {@link #execute()}, {@link #execute(Function, BiConsumer)}
|
||||
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, BiConsumer)}
|
||||
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
||||
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
||||
*
|
||||
@@ -23,8 +23,6 @@ import java.util.function.Function;
|
||||
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <b>注意: 无论是否异步,都不需要自行关闭ResultSet,本API已自动关闭</b>
|
||||
*
|
||||
* @param <T> 需要返回的类型
|
||||
* @author CarmJos
|
||||
* @since 0.0.1
|
||||
@@ -74,20 +72,6 @@ public interface SQLAction<T> {
|
||||
*/
|
||||
@NotNull T execute() throws SQLException;
|
||||
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @param function 处理方法
|
||||
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
|
||||
* @param <R> 需要返回的内容
|
||||
* @return 指定类型数据
|
||||
*/
|
||||
@Nullable
|
||||
default <R> R execute(@NotNull Function<T, R> function, @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||
T value = execute(exceptionHandler);
|
||||
if (value == null) return null;
|
||||
else return function.apply(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行语句并返回值
|
||||
@@ -97,14 +81,44 @@ public interface SQLAction<T> {
|
||||
*/
|
||||
@Nullable
|
||||
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||
if (exceptionHandler == null) exceptionHandler = defaultExceptionHandler();
|
||||
T value = null;
|
||||
return execute(t -> t, exceptionHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @param function 处理方法
|
||||
* @param <R> 需要返回的内容
|
||||
* @return 指定类型数据
|
||||
* @throws SQLException 当SQL操作出现问题时抛出
|
||||
*/
|
||||
@Nullable
|
||||
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException {
|
||||
try {
|
||||
value = execute();
|
||||
T value = execute();
|
||||
return function.apply(value);
|
||||
} catch (SQLException exception) {
|
||||
exceptionHandler.accept(exception, this);
|
||||
throw new SQLException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @param function 处理方法
|
||||
* @param exceptionHandler 异常处理器 默认为 {@link #defaultExceptionHandler()}
|
||||
* @param <R> 需要返回的内容
|
||||
* @return 指定类型数据
|
||||
*/
|
||||
@Nullable
|
||||
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
||||
@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||
try {
|
||||
return executeFunction(function);
|
||||
} catch (SQLException exception) {
|
||||
handleException(exceptionHandler, exception);
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,6 +145,12 @@ public interface SQLAction<T> {
|
||||
*/
|
||||
void executeAsync(@Nullable Consumer<T> success, @Nullable BiConsumer<SQLException, SQLAction<T>> failure);
|
||||
|
||||
|
||||
default void handleException(@Nullable BiConsumer<SQLException, SQLAction<T>> handler, SQLException exception) {
|
||||
if (handler == null) handler = defaultExceptionHandler();
|
||||
handler.accept(exception, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 默认的异常处理器
|
||||
*/
|
||||
|
||||
@@ -49,6 +49,7 @@ public interface SQLManager {
|
||||
|
||||
/**
|
||||
* 执行一条不需要返回结果的SQL语句(多用于UPDATE、REPLACE、DELETE方法)
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @return 更新的行数
|
||||
@@ -79,6 +80,7 @@ public interface SQLManager {
|
||||
|
||||
/**
|
||||
* 执行多条不需要返回结果的SQL。
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句内容
|
||||
* @param moreSQL 更多SQL语句内容
|
||||
|
||||
@@ -1,7 +1,51 @@
|
||||
package cc.carm.lib.easysql.api.action.query;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLAction;
|
||||
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* SQLQueryAction 是用于承载SQL查询语句并进行处理、返回并自动关闭连接的基本类。
|
||||
*
|
||||
* <ul>
|
||||
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, BiConsumer)}
|
||||
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
||||
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
||||
*
|
||||
* <li>异步执行 {@link #executeAsync(Consumer, BiConsumer)}
|
||||
* <br>异步执行时将提供成功与异常两种处理方式
|
||||
* <br>可自行选择是否对数据或异常进行处理
|
||||
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <b>注意: 无论是否异步,都不需要自行关闭ResultSet,本API已自动关闭</b>
|
||||
*
|
||||
* @author CarmJos
|
||||
* @since 0.2.6
|
||||
*/
|
||||
public interface QueryAction extends SQLAction<SQLQuery> {
|
||||
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @param function 处理方法
|
||||
* @param <R> 需要返回的内容
|
||||
* @return 指定类型数据
|
||||
* @throws SQLException 当SQL操作出现问题时抛出
|
||||
*/
|
||||
@Nullable
|
||||
default <R> R executeFunction(@NotNull SQLFunction<SQLQuery, R> function)
|
||||
throws SQLException {
|
||||
try (SQLQuery value = execute()) {
|
||||
return function.apply(value);
|
||||
} catch (SQLException exception) {
|
||||
throw new SQLException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,36 +7,36 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public interface ConditionalBuilder<T> extends SQLBuilder {
|
||||
public interface ConditionalBuilder<B extends ConditionalBuilder<B, T>, T> extends SQLBuilder {
|
||||
|
||||
T build();
|
||||
|
||||
ConditionalBuilder<T> setLimit(int limit);
|
||||
B setLimit(int limit);
|
||||
|
||||
ConditionalBuilder<T> setConditions(@Nullable String condition);
|
||||
B setConditions(@Nullable String condition);
|
||||
|
||||
ConditionalBuilder<T> setConditions(LinkedHashMap<@NotNull String, @Nullable Object> conditionSQLs);
|
||||
B setConditions(LinkedHashMap<@NotNull String, @Nullable Object> conditionSQLs);
|
||||
|
||||
ConditionalBuilder<T> addCondition(@Nullable String condition);
|
||||
B addCondition(@Nullable String condition);
|
||||
|
||||
ConditionalBuilder<T> addCondition(@NotNull String queryName, @NotNull String operator, @Nullable Object queryValue);
|
||||
B addCondition(@NotNull String queryName, @NotNull String operator, @Nullable Object queryValue);
|
||||
|
||||
default ConditionalBuilder<T> addCondition(@NotNull String queryName, @Nullable Object queryValue) {
|
||||
default B addCondition(@NotNull String queryName, @Nullable Object queryValue) {
|
||||
return addCondition(queryName, "=", queryValue);
|
||||
}
|
||||
|
||||
ConditionalBuilder<T> addCondition(@NotNull String[] queryNames, @Nullable Object[] queryValues);
|
||||
B addCondition(@NotNull String[] queryNames, @Nullable Object[] queryValues);
|
||||
|
||||
ConditionalBuilder<T> addNotNullCondition(@NotNull String queryName);
|
||||
B addNotNullCondition(@NotNull String queryName);
|
||||
|
||||
default ConditionalBuilder<T> addTimeCondition(@NotNull String queryName, long startMillis, long endMillis) {
|
||||
default B addTimeCondition(@NotNull String queryName, long startMillis, long endMillis) {
|
||||
return addTimeCondition(queryName,
|
||||
startMillis > 0 ? new Date(startMillis) : null,
|
||||
endMillis > 0 ? new Date(endMillis) : null
|
||||
);
|
||||
}
|
||||
|
||||
ConditionalBuilder<T> addTimeCondition(@NotNull String queryName, @Nullable java.util.Date startDate, @Nullable java.util.Date endDate);
|
||||
B addTimeCondition(@NotNull String queryName, @Nullable java.util.Date startDate, @Nullable java.util.Date endDate);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package cc.carm.lib.easysql.api.builder;
|
||||
|
||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
||||
|
||||
public interface DeleteBuilder extends ConditionalBuilder<PreparedSQLUpdateAction> {
|
||||
public interface DeleteBuilder extends ConditionalBuilder<DeleteBuilder, PreparedSQLUpdateAction> {
|
||||
|
||||
String getTableName();
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ import org.jetbrains.annotations.NotNull;
|
||||
public interface QueryBuilder extends SQLBuilder {
|
||||
|
||||
/**
|
||||
* 通过一条 SQL语句创建查询
|
||||
* 通过一条 SQL语句创建查询。
|
||||
* 该方法使用 Statement 实现,请注意SQL注入风险!
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @return {@link QueryAction}
|
||||
* @deprecated 存在SQL注入风险,请使用 {@link QueryBuilder#withPreparedSQL(String)}
|
||||
* @deprecated 存在SQL注入风险,建议使用 {@link QueryBuilder#withPreparedSQL(String)}
|
||||
*/
|
||||
@Deprecated
|
||||
QueryAction withSQL(@NotNull String sql);
|
||||
|
||||
@@ -3,10 +3,16 @@ package cc.carm.lib.easysql.api.builder;
|
||||
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface TableQueryBuilder extends ConditionalBuilder<PreparedQueryAction> {
|
||||
public interface TableQueryBuilder extends ConditionalBuilder<TableQueryBuilder, PreparedQueryAction> {
|
||||
|
||||
@NotNull String getTableName();
|
||||
|
||||
/**
|
||||
* 选定用于查询的列名
|
||||
*
|
||||
* @param columnNames 列名
|
||||
* @return {@link TableQueryBuilder}
|
||||
*/
|
||||
TableQueryBuilder selectColumns(@NotNull String... columnNames);
|
||||
|
||||
/**
|
||||
@@ -18,4 +24,14 @@ public interface TableQueryBuilder extends ConditionalBuilder<PreparedQueryActio
|
||||
*/
|
||||
TableQueryBuilder orderBy(@NotNull String columnName, boolean asc);
|
||||
|
||||
/**
|
||||
* 限制查询条数,用于分页查询。
|
||||
*
|
||||
* @param start 开始数
|
||||
* @param end 结束条数
|
||||
* @return {@link TableQueryBuilder}
|
||||
* @since 0.2.6
|
||||
*/
|
||||
TableQueryBuilder setPageLimit(int start, int end);
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public interface UpdateBuilder extends ConditionalBuilder<PreparedSQLUpdateAction> {
|
||||
public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, PreparedSQLUpdateAction> {
|
||||
|
||||
String getTableName();
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package cc.carm.lib.easysql.api.function;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public interface SQLFunction<T, R> {
|
||||
|
||||
@Nullable
|
||||
R apply(T t) throws SQLException;
|
||||
|
||||
}
|
||||
+29
-2
@@ -5,14 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.2</version>
|
||||
<version>0.2.6</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>easysql-beecp</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>10-EasySQL-BeeCP</name>
|
||||
<name>12-EasySQL-BeeCP</name>
|
||||
<description>EasySQL的应用部分。此为BeeCP版本。</description>
|
||||
<url>https://github.com/CarmJos/EasySQL</url>
|
||||
|
||||
@@ -67,6 +67,7 @@
|
||||
<groupId>com.github.chris2018998</groupId>
|
||||
<artifactId>beecp</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<optional>true</optional>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -89,6 +90,32 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>cn.beecp</pattern>
|
||||
<shadedPattern>cc.carm.lib.easysql.beecp</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/MANIFEST.MF</exclude>
|
||||
<exclude>META-INF/*.txt</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package cc.carm.lib.easysql;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.action.query.SQLQuery;
|
||||
import cc.carm.lib.easysql.api.util.TimeDateUtils;
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import cn.beecp.BeeDataSource;
|
||||
import cn.beecp.BeeDataSourceConfig;
|
||||
@@ -18,4 +21,26 @@ public class EasySQL {
|
||||
return new SQLManagerImpl(new BeeDataSource(config));
|
||||
}
|
||||
|
||||
|
||||
public static void shutdownManager(SQLManager manager, boolean forceClose, boolean outputActiveQuery) {
|
||||
if (!manager.getActiveQuery().isEmpty()) {
|
||||
manager.getLogger().severe("There are " + manager.getActiveQuery().size() + " connections still running");
|
||||
for (SQLQuery value : manager.getActiveQuery().values()) {
|
||||
if (outputActiveQuery) {
|
||||
manager.getLogger().severe("#" + value.getAction().getShortID() + " -> " + value.getSQLContent());
|
||||
manager.getLogger().severe("- execute at " + TimeDateUtils.getTimeString(value.getExecuteTime()));
|
||||
}
|
||||
if (forceClose) value.close();
|
||||
}
|
||||
}
|
||||
if (manager.getDataSource() instanceof BeeDataSource) {
|
||||
//Close bee connection pool
|
||||
((BeeDataSource) manager.getDataSource()).close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void shutdownManager(SQLManager manager) {
|
||||
shutdownManager(manager, true, manager.isDebugMode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.2</version>
|
||||
<version>0.2.6</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -49,16 +49,11 @@ public class EasySQLDemo {
|
||||
.build().execute(query -> {
|
||||
//可以直接进行数据处理
|
||||
ResultSet result = query.getResultSet();
|
||||
try {
|
||||
if (result != null && result.next()) {
|
||||
return UUIDUtil.toUUID(result.getString("uuid"));
|
||||
}
|
||||
} catch (SQLException ignored) {
|
||||
}
|
||||
return null;
|
||||
return result.next() ? UUIDUtil.toUUID(result.getString("uuid")) : null;
|
||||
}, (exception, action) -> {
|
||||
// 处理异常,不想处理直接填null
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void sqlQueryAsync(SQLManager sqlManager) {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.2</version>
|
||||
<version>0.2.6</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -66,6 +66,7 @@
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>4.0.3</version>
|
||||
<optional>true</optional>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -88,6 +89,32 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>com.zaxxer.hikari</pattern>
|
||||
<shadedPattern>cc.carm.lib.easysql.hikari</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/MANIFEST.MF</exclude>
|
||||
<exclude>META-INF/*.txt</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package cc.carm.lib.easysql;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.action.query.SQLQuery;
|
||||
import cc.carm.lib.easysql.api.util.TimeDateUtils;
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class EasySQL {
|
||||
|
||||
public static SQLManagerImpl createManager(
|
||||
@NotNull String driver, @NotNull String url,
|
||||
@NotNull String username, @Nullable String password) {
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setDriverClassName(driver);
|
||||
config.setJdbcUrl(url);
|
||||
config.setUsername(username);
|
||||
config.setPassword(password);
|
||||
return createManager(config);
|
||||
}
|
||||
|
||||
public static SQLManagerImpl createManager(@NotNull Properties properties) {
|
||||
return createManager(new HikariConfig(properties));
|
||||
}
|
||||
|
||||
public static SQLManagerImpl createManager(@NotNull HikariConfig config) {
|
||||
return new SQLManagerImpl(new HikariDataSource(config));
|
||||
}
|
||||
|
||||
public static void shutdownManager(SQLManager manager, boolean forceClose, boolean outputActiveQuery) {
|
||||
if (!manager.getActiveQuery().isEmpty()) {
|
||||
manager.getLogger().severe("There are " + manager.getActiveQuery().size() + " connections still running");
|
||||
for (SQLQuery value : manager.getActiveQuery().values()) {
|
||||
if (outputActiveQuery) {
|
||||
manager.getLogger().severe("#" + value.getAction().getShortID() + " -> " + value.getSQLContent());
|
||||
manager.getLogger().severe("- execute at " + TimeDateUtils.getTimeString(value.getExecuteTime()));
|
||||
}
|
||||
if (forceClose) value.close();
|
||||
}
|
||||
}
|
||||
if (manager.getDataSource() instanceof HikariDataSource) {
|
||||
//Close hikari pool
|
||||
((HikariDataSource) manager.getDataSource()).close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void shutdownManager(SQLManager manager) {
|
||||
shutdownManager(manager, true, manager.isDebugMode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package easysql;
|
||||
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class EasySQL {
|
||||
|
||||
public static SQLManagerImpl createManager(
|
||||
@NotNull String driver, @NotNull String url,
|
||||
@NotNull String username, @Nullable String password) {
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setDriverClassName(driver);
|
||||
config.setJdbcUrl(url);
|
||||
config.setUsername(username);
|
||||
config.setPassword(password);
|
||||
return createManager(config);
|
||||
}
|
||||
|
||||
public static SQLManagerImpl createManager(@NotNull Properties properties) {
|
||||
return createManager(new HikariConfig(properties));
|
||||
}
|
||||
|
||||
public static SQLManagerImpl createManager(@NotNull HikariConfig config) {
|
||||
return new SQLManagerImpl(new HikariDataSource(config));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.2</version>
|
||||
<version>0.2.6</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
+20
-17
@@ -13,7 +13,8 @@ import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public abstract class AbstractConditionalBuilder<T> extends AbstractSQLBuilder implements ConditionalBuilder<T> {
|
||||
public abstract class AbstractConditionalBuilder<B extends ConditionalBuilder<B, T>, T>
|
||||
extends AbstractSQLBuilder implements ConditionalBuilder<B, T> {
|
||||
|
||||
ArrayList<String> conditionSQLs = new ArrayList<>();
|
||||
ArrayList<Object> conditionParams = new ArrayList<>();
|
||||
@@ -23,39 +24,41 @@ public abstract class AbstractConditionalBuilder<T> extends AbstractSQLBuilder i
|
||||
super(manager);
|
||||
}
|
||||
|
||||
protected abstract B getThis();
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> setConditions(@Nullable String condition) {
|
||||
public B setConditions(@Nullable String condition) {
|
||||
this.conditionSQLs = new ArrayList<>();
|
||||
this.conditionParams = new ArrayList<>();
|
||||
if (condition != null) this.conditionSQLs.add(condition);
|
||||
return this;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> setConditions(
|
||||
public B setConditions(
|
||||
LinkedHashMap<@NotNull String, @Nullable Object> conditions
|
||||
) {
|
||||
conditions.forEach(this::addCondition);
|
||||
return this;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> addCondition(@Nullable String condition) {
|
||||
public B addCondition(@Nullable String condition) {
|
||||
this.conditionSQLs.add(condition);
|
||||
return this;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> addCondition(
|
||||
public B addCondition(
|
||||
@NotNull String queryName, @NotNull String operator, @Nullable Object queryValue
|
||||
) {
|
||||
addCondition("`" + queryName + "` " + operator + " ?");
|
||||
this.conditionParams.add(queryValue);
|
||||
return this;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> addCondition(
|
||||
public B addCondition(
|
||||
@NotNull String[] queryNames, @Nullable Object[] queryValues
|
||||
) {
|
||||
if (queryNames.length != queryValues.length) {
|
||||
@@ -64,21 +67,21 @@ public abstract class AbstractConditionalBuilder<T> extends AbstractSQLBuilder i
|
||||
for (int i = 0; i < queryNames.length; i++) {
|
||||
addCondition(queryNames[i], queryValues[i]);
|
||||
}
|
||||
return this;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> addNotNullCondition(@NotNull String queryName) {
|
||||
public B addNotNullCondition(@NotNull String queryName) {
|
||||
return addCondition("`" + queryName + "` IS NOT NULL");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> addTimeCondition(
|
||||
public B addTimeCondition(
|
||||
@NotNull String queryName, @Nullable Date startDate, @Nullable Date endDate
|
||||
) {
|
||||
if (startDate == null && endDate == null) return this; // 都不限定时间,不用判断了
|
||||
if (startDate == null && endDate == null) return getThis(); // 都不限定时间,不用判断了
|
||||
if (startDate != null) {
|
||||
addCondition("`" + queryName + "` BETWEEN ? AND ?");
|
||||
this.conditionParams.add(startDate);
|
||||
@@ -96,14 +99,14 @@ public abstract class AbstractConditionalBuilder<T> extends AbstractSQLBuilder i
|
||||
} else {
|
||||
addCondition(queryName, "<=", endDate);
|
||||
}
|
||||
return this;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public AbstractConditionalBuilder<T> setLimit(int limit) {
|
||||
public B setLimit(int limit) {
|
||||
this.limit = limit;
|
||||
return this;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
protected String buildConditionSQL() {
|
||||
|
||||
@@ -7,7 +7,7 @@ import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class DeleteBuilderImpl
|
||||
extends AbstractConditionalBuilder<PreparedSQLUpdateAction>
|
||||
extends AbstractConditionalBuilder<DeleteBuilder, PreparedSQLUpdateAction>
|
||||
implements DeleteBuilder {
|
||||
|
||||
String tableName;
|
||||
@@ -39,4 +39,9 @@ public class DeleteBuilderImpl
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected DeleteBuilderImpl getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-4
@@ -7,10 +7,8 @@ import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class TableQueryBuilderImpl
|
||||
extends AbstractConditionalBuilder<PreparedQueryAction>
|
||||
extends AbstractConditionalBuilder<TableQueryBuilder, PreparedQueryAction>
|
||||
implements TableQueryBuilder {
|
||||
|
||||
@NotNull String tableName;
|
||||
@@ -19,6 +17,8 @@ public class TableQueryBuilderImpl
|
||||
|
||||
@Nullable String orderBy;
|
||||
|
||||
int[] pageLimit;
|
||||
|
||||
public TableQueryBuilderImpl(@NotNull SQLManagerImpl manager, @NotNull String tableName) {
|
||||
super(manager);
|
||||
this.tableName = tableName;
|
||||
@@ -44,7 +44,13 @@ public class TableQueryBuilderImpl
|
||||
sqlBuilder.append("`").append(tableName).append("`");
|
||||
|
||||
if (hasConditions()) sqlBuilder.append(" ").append(buildConditionSQL());
|
||||
if (limit > 0) sqlBuilder.append(" ").append(buildLimitSQL());
|
||||
|
||||
if (pageLimit != null && pageLimit.length == 2) {
|
||||
sqlBuilder.append(" LIMIT ").append(pageLimit[0]).append(",").append(pageLimit[1]);
|
||||
} else if (limit > 0) {
|
||||
sqlBuilder.append(" ").append(buildLimitSQL());
|
||||
}
|
||||
|
||||
if (orderBy != null) sqlBuilder.append(orderBy);
|
||||
|
||||
return new PreparedQueryActionImpl(getManager(), sqlBuilder.toString())
|
||||
@@ -68,4 +74,14 @@ public class TableQueryBuilderImpl
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableQueryBuilder setPageLimit(int start, int end) {
|
||||
this.pageLimit = new int[]{start, end};
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TableQueryBuilderImpl getThis() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.util.*;
|
||||
|
||||
public class UpdateBuilderImpl
|
||||
extends AbstractConditionalBuilder<PreparedSQLUpdateAction>
|
||||
extends AbstractConditionalBuilder<UpdateBuilder, PreparedSQLUpdateAction>
|
||||
implements UpdateBuilder {
|
||||
|
||||
String tableName;
|
||||
@@ -73,4 +73,8 @@ public class UpdateBuilderImpl
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected UpdateBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
|
||||
import cc.carm.lib.easysql.action.SQLUpdateActionImpl;
|
||||
import cc.carm.lib.easysql.action.SQLUpdateBatchActionImpl;
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.action.query.SQLQuery;
|
||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
||||
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
|
||||
import cc.carm.lib.easysql.api.action.query.SQLQuery;
|
||||
import cc.carm.lib.easysql.api.builder.*;
|
||||
import cc.carm.lib.easysql.builder.impl.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -4,10 +4,19 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
<sonar.organization>carmjos</sonar.organization>
|
||||
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
|
||||
</properties>
|
||||
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>0.2.2</version>
|
||||
<version>0.2.6</version>
|
||||
|
||||
<modules>
|
||||
<module>easysql-api</module>
|
||||
@@ -55,13 +64,6 @@
|
||||
<url>${project.url}/actions/workflows/maven.yml</url>
|
||||
</ciManagement>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
|
||||
<repository>
|
||||
@@ -252,4 +254,4 @@
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user