1
mirror of https://github.com/CarmJos/EasySQL.git synced 2026-06-04 15:28:20 +08:00

[v0.2.2] 版本优化

- `[F]` 修复部分类的使用异常问题
- `[F]` 修复 SQLUpdateBatchAction 中 getSQLContent 方法返回内容不正确导致的其他方法一并出现异常的问题。
- `[U]` 修改 SQLUpdateBatchAction 的默认异常处理器。
- `[F]` 修复 PreparedSQLBatchUpdateActionImpl 异常继承导致的无法使用的问题。
This commit is contained in:
2021-12-19 23:47:48 +08:00
parent d30b6b9ab2
commit 84c35eb481
23 changed files with 67 additions and 61 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<version>0.2.1</version>
<version>0.2.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -138,6 +138,7 @@ public interface SQLAction<T> {
return (exception, action) -> {
getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
getManager().getLogger().severe(exception.getLocalizedMessage());
exception.printStackTrace();
};
}
@@ -2,6 +2,10 @@ package cc.carm.lib.easysql.api.action;
import cc.carm.lib.easysql.api.SQLAction;
import java.sql.SQLException;
import java.util.List;
import java.util.function.BiConsumer;
public interface SQLUpdateAction extends SQLAction<Integer> {
/**
@@ -23,4 +27,6 @@ public interface SQLUpdateAction extends SQLAction<Integer> {
return setKeyIndex(-1); // will return changed lines number
}
}
@@ -3,7 +3,9 @@ package cc.carm.lib.easysql.api.action;
import cc.carm.lib.easysql.api.SQLAction;
import org.jetbrains.annotations.NotNull;
import java.sql.SQLException;
import java.util.List;
import java.util.function.BiConsumer;
public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
@@ -15,4 +17,20 @@ public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
*/
SQLUpdateBatchAction addBatch(@NotNull String sql);
List<String> getSQLContents();
@Override
default BiConsumer<SQLException, SQLAction<List<Integer>>> defaultExceptionHandler() {
return (exception, action) -> {
getManager().getLogger().severe("Error when execute SQLs : ");
int i = 1;
for (String content : getSQLContents()) {
getManager().getLogger().severe("#" + i + " [" + content + "]");
i++;
}
getManager().getLogger().severe(exception.getLocalizedMessage());
exception.printStackTrace();
};
}
}
@@ -7,8 +7,6 @@ public interface InsertBuilder<T> {
String getTableName();
InsertBuilder<T> setTableName(String tableName);
T setColumnNames(List<String> columnNames);
default T setColumnNames(String... columnNames) {
@@ -7,8 +7,6 @@ public interface ReplaceBuilder<T> {
String getTableName();
ReplaceBuilder<T> setTableName(String tableName);
T setColumnNames(List<String> columnNames);
default T setColumnNames(String... columnNames) {
@@ -8,8 +8,6 @@ public interface TableCreateBuilder extends SQLBuilder {
@NotNull String getTableName();
TableCreateBuilder setTableName(@NotNull String tableName);
@NotNull String getTableSettings();
TableCreateBuilder setTableSettings(@NotNull String settings);
@@ -1,4 +1,9 @@
package cc.carm.lib.easysql.api.builder;
public interface UpsertBuilder {
String getTableName();
UpsertBuilder setColumnNames(String[] columnNames, String updateColumn);
}
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.1</version>
<version>0.2.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.1</version>
<version>0.2.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
+2 -2
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.1</version>
<version>0.2.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -65,7 +65,7 @@
<!--项目地址 https://github.com/brettwooldridge/HikariCP/ -->
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<version>5.0.0</version>
<scope>compile</scope>
</dependency>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.2.1</version>
<version>0.2.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -12,10 +12,9 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class PreparedSQLBatchUpdateActionImpl extends SQLUpdateBatchActionImpl implements PreparedSQLUpdateBatchAction {
public class PreparedSQLBatchUpdateActionImpl extends AbstractSQLAction<List<Integer>> implements PreparedSQLUpdateBatchAction {
int keyIndex = -1;
List<Object[]> allParams;
@@ -54,6 +53,7 @@ public class PreparedSQLBatchUpdateActionImpl extends SQLUpdateBatchActionImpl i
);
outputDebugMessage();
if (keyIndex > 0) {
statement.executeBatch();
List<Integer> generatedKeys = new ArrayList<>();
ResultSet resultSet = statement.getGeneratedKeys();
if (resultSet != null) {
@@ -59,13 +59,14 @@ public class PreparedSQLUpdateActionImpl extends SQLUpdateActionImpl implements
);
outputDebugMessage();
if (keyIndex > 0) {
statement.executeUpdate();
ResultSet resultSet = statement.getGeneratedKeys();
if (resultSet != null) {
if (resultSet.next()) value = resultSet.getInt(keyIndex);
resultSet.close();
}
} else {
value = statement.executeUpdate(getSQLContent());
value = statement.executeUpdate();
}
statement.close();
@@ -8,7 +8,6 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.function.Consumer;
public class SQLUpdateActionImpl extends AbstractSQLAction<Integer> implements SQLUpdateAction {
@@ -23,9 +23,12 @@ public class SQLUpdateBatchActionImpl extends AbstractSQLAction<List<Integer>> i
@Override
public @NotNull String getSQLContent() {
return this.sqlContents.stream()
.map(content -> "[" + content + "]" + " ")
.collect(Collectors.joining());
return this.sqlContents.get(0);
}
@Override
public @NotNull List<String> getSQLContents() {
return this.sqlContents;
}
@Override
@@ -50,5 +53,4 @@ public class SQLUpdateBatchActionImpl extends AbstractSQLAction<List<Integer>> i
return returnedValues;
}
}
@@ -43,9 +43,4 @@ public abstract class InsertBuilderImpl<T> extends AbstractSQLBuilder implements
public String getTableName() {
return tableName;
}
public InsertBuilderImpl<T> setTableName(String tableName) {
this.tableName = tableName;
return this;
}
}
@@ -43,9 +43,4 @@ public abstract class ReplaceBuilderImpl<T> extends AbstractSQLBuilder implement
public String getTableName() {
return tableName;
}
public ReplaceBuilderImpl<T> setTableName(String tableName) {
this.tableName = tableName;
return this;
}
}
@@ -50,12 +50,6 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
return new SQLUpdateActionImpl(getManager(), createSQL.toString());
}
@Override
public TableCreateBuilder setTableName(@NotNull String tableName) {
this.tableName = tableName;
return this;
}
@Override
public TableCreateBuilder addColumn(@NotNull String column) {
this.columns.add(column);
@@ -15,7 +15,6 @@ public class TableQueryBuilderImpl
@NotNull String tableName;
ArrayList<Object> params = new ArrayList<>();
String[] columns;
@Nullable String orderBy;
@@ -49,7 +48,7 @@ public class TableQueryBuilderImpl
if (orderBy != null) sqlBuilder.append(orderBy);
return new PreparedQueryActionImpl(getManager(), sqlBuilder.toString())
.setParams(hasConditionParams() ? params : null);
.setParams(hasConditionParams() ? getConditionParams() : null);
}
@Override
@@ -1,7 +1,5 @@
package cc.carm.lib.easysql.util;
import cc.carm.lib.easysql.api.SQLAction;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.*;
@@ -40,10 +38,9 @@ public class StatementUtil {
) throws SQLException {
sql = sql.trim();
PreparedStatement statement = connection.prepareStatement(sql, returnGeneratedKey ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS);
final Map<Integer, Integer> nullTypeMap = new HashMap<>();
if (params != null) {
fillParams(statement, Arrays.asList(params), nullTypeMap);
}
Map<Integer, Integer> nullTypeMap = new HashMap<>();
if (params != null) fillParams(statement, Arrays.asList(params), nullTypeMap);
statement.addBatch();
return statement;
}
@@ -76,7 +73,7 @@ public class StatementUtil {
sql = sql.trim();
PreparedStatement statement = connection.prepareStatement(sql, returnGeneratedKey ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS);
final Map<Integer, Integer> nullTypeMap = new HashMap<>();
Map<Integer, Integer> nullTypeMap = new HashMap<>();
for (Object[] params : paramsBatch) {
fillParams(statement, Arrays.asList(params), nullTypeMap);
statement.addBatch();
+15 -15
View File
@@ -7,7 +7,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<packaging>pom</packaging>
<version>0.2.1</version>
<version>0.2.2-SNAPSHOT</version>
<modules>
<module>easysql-api</module>
@@ -126,20 +126,20 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-gpg-plugin</artifactId>-->
<!-- <version>1.5</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>sign-artifacts</id>-->
<!-- <phase>verify</phase>-->
<!-- <goals>-->
<!-- <goal>sign</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
</plugins>
<pluginManagement>