This commit is contained in:
Looly 2024-01-10 16:43:48 +08:00
parent 5345e4cc61
commit 037d53794d
5 changed files with 30 additions and 13 deletions

View File

@ -66,7 +66,6 @@ public class AnsiSqlDialect implements Dialect {
} }
// 批量根据第一行数据结构生成SQL占位符 // 批量根据第一行数据结构生成SQL占位符
final SqlBuilder insert = SqlBuilder.of(quoteWrapper).insert(entities[0], this.dialectName()); final SqlBuilder insert = SqlBuilder.of(quoteWrapper).insert(entities[0], this.dialectName());
final Set<String> fields = CollUtil.remove(entities[0].keySet(), StrUtil::isBlank);
return StatementUtil.prepareStatementForBatch(conn, insert.build(), entities); return StatementUtil.prepareStatementForBatch(conn, insert.build(), entities);
} }
@ -100,12 +99,12 @@ public class AnsiSqlDialect implements Dialect {
} }
@Override @Override
public PreparedStatement psForFind(final Connection conn, final Query query) throws SQLException { public PreparedStatement psForFind(final Connection conn, final Query query) {
return psForPage(conn, query); return psForPage(conn, query);
} }
@Override @Override
public PreparedStatement psForPage(final Connection conn, final Query query) throws SQLException { public PreparedStatement psForPage(final Connection conn, final Query query) {
Assert.notNull(query, "query must be not null !"); Assert.notNull(query, "query must be not null !");
if (ArrayUtil.hasBlank(query.getTableNames())) { if (ArrayUtil.hasBlank(query.getTableNames())) {
throw new DbRuntimeException("Table name must be not empty !"); throw new DbRuntimeException("Table name must be not empty !");

View File

@ -29,6 +29,9 @@ import java.sql.SQLException;
public class PhoenixDialect extends AnsiSqlDialect { public class PhoenixDialect extends AnsiSqlDialect {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/**
* 构造
*/
public PhoenixDialect() { public PhoenixDialect() {
// wrapper = new Wrapper('"'); // wrapper = new Wrapper('"');
} }

View File

@ -21,12 +21,14 @@ import org.dromara.hutool.db.sql.QuoteWrapper;
/** /**
* SQLServer2012 方言 * SQLServer2012 方言
* *
* @author loolly * @author Looly
*
*/ */
public class SqlServer2012Dialect extends AnsiSqlDialect { public class SqlServer2012Dialect extends AnsiSqlDialect {
private static final long serialVersionUID = -37598166015777797L; private static final long serialVersionUID = -37598166015777797L;
/**
* 构造
*/
public SqlServer2012Dialect() { public SqlServer2012Dialect() {
//双引号和中括号适用双引号更广泛 //双引号和中括号适用双引号更广泛
quoteWrapper = new QuoteWrapper('"'); quoteWrapper = new QuoteWrapper('"');

View File

@ -17,12 +17,15 @@ import org.dromara.hutool.db.sql.QuoteWrapper;
/** /**
* SqlLite3方言 * SqlLite3方言
* @author loolly
* *
* @author Looly
*/ */
public class Sqlite3Dialect extends AnsiSqlDialect{ public class Sqlite3Dialect extends AnsiSqlDialect {
private static final long serialVersionUID = -3527642408849291634L; private static final long serialVersionUID = -3527642408849291634L;
/**
* 构造
*/
public Sqlite3Dialect() { public Sqlite3Dialect() {
quoteWrapper = new QuoteWrapper('[', ']'); quoteWrapper = new QuoteWrapper('[', ']');
} }

View File

@ -18,6 +18,7 @@ import org.dromara.hutool.core.collection.iter.ArrayIter;
import org.dromara.hutool.core.convert.Convert; import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.builder.Builder; import org.dromara.hutool.core.lang.builder.Builder;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.DbRuntimeException; import org.dromara.hutool.db.DbRuntimeException;
import org.dromara.hutool.db.Entity; import org.dromara.hutool.db.Entity;
@ -27,6 +28,7 @@ import java.sql.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* {@link PreparedStatement}构建器构建结果为{@link StatementWrapper} * {@link PreparedStatement}构建器构建结果为{@link StatementWrapper}
@ -137,6 +139,7 @@ public class StatementBuilder implements Builder<StatementWrapper> {
try { try {
ps = StatementWrapper.of(connection.prepareStatement(sql)); ps = StatementWrapper.of(connection.prepareStatement(sql));
final Map<Integer, Integer> nullTypeMap = new HashMap<>(); final Map<Integer, Integer> nullTypeMap = new HashMap<>();
Set<String> keys = null;
for (final Object params : paramsBatch) { for (final Object params : paramsBatch) {
if (null == params) { if (null == params) {
continue; continue;
@ -144,7 +147,14 @@ public class StatementBuilder implements Builder<StatementWrapper> {
if (ArrayUtil.isArray(params)) { if (ArrayUtil.isArray(params)) {
ps.fillParams(new ArrayIter<>(params), nullTypeMap); ps.fillParams(new ArrayIter<>(params), nullTypeMap);
} else if (params instanceof Entity) { } else if (params instanceof Entity) {
ps.fillParams(((Entity) params).values(), nullTypeMap); final Entity entity = (Entity) params;
// 对于多Entity批量插入的情况为防止数据不对齐故按照首行提供键值对筛选
if(null == keys){
keys = entity.keySet();
ps.fillParams(entity.values(), nullTypeMap);
} else{
ps.fillParams(MapUtil.valuesOfKeys(entity, keys), nullTypeMap);
}
} }
ps.addBatch(); ps.addBatch();
} }