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占位符
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);
}
@ -100,12 +99,12 @@ public class AnsiSqlDialect implements Dialect {
}
@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);
}
@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 !");
if (ArrayUtil.hasBlank(query.getTableNames())) {
throw new DbRuntimeException("Table name must be not empty !");

View File

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

View File

@ -21,15 +21,17 @@ import org.dromara.hutool.db.sql.QuoteWrapper;
/**
* SQLServer2012 方言
*
* @author loolly
*
* @author Looly
*/
public class SqlServer2012Dialect extends AnsiSqlDialect {
private static final long serialVersionUID = -37598166015777797L;
/**
* 构造
*/
public SqlServer2012Dialect() {
//双引号和中括号适用双引号更广泛
quoteWrapper = new QuoteWrapper('"');
quoteWrapper = new QuoteWrapper('"');
}
@Override
@ -39,10 +41,10 @@ public class SqlServer2012Dialect extends AnsiSqlDialect {
find.append(" order by current_timestamp");
}
return find.append(" offset ")
.append(page.getStartPosition())//
.append(" row fetch next ")//row和rows同义词
.append(page.getPageSize())//
.append(" row only");//
.append(page.getStartPosition())//
.append(" row fetch next ")//row和rows同义词
.append(page.getPageSize())//
.append(" row only");//
}
@Override

View File

@ -17,12 +17,15 @@ import org.dromara.hutool.db.sql.QuoteWrapper;
/**
* SqlLite3方言
* @author loolly
*
* @author Looly
*/
public class Sqlite3Dialect extends AnsiSqlDialect{
public class Sqlite3Dialect extends AnsiSqlDialect {
private static final long serialVersionUID = -3527642408849291634L;
/**
* 构造
*/
public Sqlite3Dialect() {
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.lang.Assert;
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.db.DbRuntimeException;
import org.dromara.hutool.db.Entity;
@ -27,6 +28,7 @@ import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* {@link PreparedStatement}构建器构建结果为{@link StatementWrapper}
@ -137,6 +139,7 @@ public class StatementBuilder implements Builder<StatementWrapper> {
try {
ps = StatementWrapper.of(connection.prepareStatement(sql));
final Map<Integer, Integer> nullTypeMap = new HashMap<>();
Set<String> keys = null;
for (final Object params : paramsBatch) {
if (null == params) {
continue;
@ -144,7 +147,14 @@ public class StatementBuilder implements Builder<StatementWrapper> {
if (ArrayUtil.isArray(params)) {
ps.fillParams(new ArrayIter<>(params), nullTypeMap);
} 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();
}