mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-14 03:15:55 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5f13b9319d | |||
| 5601d1288c | |||
| 50b36d8430 | |||
| cd60bf256e | |||
| ebe68befee | |||
| 594413e13b |
@@ -37,19 +37,31 @@
|
||||
|
||||
您可以 [点击这里](easysql-demo/src/main/java/EasySQLDemo.java) 查看部分代码演示,更多演示详见 [开发介绍](.documentation/README.md) 。
|
||||
|
||||
### 依赖方式
|
||||
### 依赖方式
|
||||
|
||||
#### Maven 依赖
|
||||
|
||||
<details>
|
||||
<summary>远程库配置(Maven)</summary>
|
||||
<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>
|
||||
```
|
||||
@@ -57,9 +69,10 @@
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>原生依赖(Maven)</summary>
|
||||
<summary>原生依赖</summary>
|
||||
|
||||
```xml
|
||||
|
||||
<project>
|
||||
<dependencies>
|
||||
<!--对于需要提供公共接口的项目,可以仅打包API部分,方便他人调用-->
|
||||
@@ -81,12 +94,14 @@
|
||||
</dependencies>
|
||||
</project>
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>含连接池版本(Maven)</summary>
|
||||
<summary>含连接池版本</summary>
|
||||
|
||||
```xml
|
||||
|
||||
<project>
|
||||
<dependencies>
|
||||
<!--也可直接选择打包了连接池的版本-->
|
||||
@@ -108,6 +123,58 @@
|
||||
|
||||
</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>
|
||||
|
||||
## 支持与捐赠
|
||||
|
||||
若您觉得本插件做的不错,您可以通过捐赠支持我!
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<version>0.2.4</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;
|
||||
|
||||
}
|
||||
@@ -5,14 +5,14 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.4</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>
|
||||
|
||||
@@ -101,10 +101,6 @@
|
||||
</executions>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>org.slf4j</pattern>
|
||||
<shadedPattern>cc.carm.lib.easysql.slf4j</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>cn.beecp</pattern>
|
||||
<shadedPattern>cc.carm.lib.easysql.beecp</shadedPattern>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.4</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.4</version>
|
||||
<version>0.2.6</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -100,10 +100,6 @@
|
||||
</executions>
|
||||
<configuration>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>org.slf4j</pattern>
|
||||
<shadedPattern>cc.carm.lib.easysql.slf4j</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>com.zaxxer.hikari</pattern>
|
||||
<shadedPattern>cc.carm.lib.easysql.hikari</shadedPattern>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.2.4</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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user