mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复PostgreSQL、H2使用upsert字段大小写问题
This commit is contained in:
@@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.db.dialect.impl;
|
||||
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.db.Entity;
|
||||
import org.dromara.hutool.db.Page;
|
||||
@@ -23,7 +23,6 @@ import org.dromara.hutool.db.sql.SqlBuilder;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* H2数据库方言
|
||||
@@ -33,6 +32,9 @@ import java.sql.SQLException;
|
||||
public class H2Dialect extends AnsiSqlDialect {
|
||||
private static final long serialVersionUID = 1490520247974768214L;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public H2Dialect() {
|
||||
// wrapper = new Wrapper('"');
|
||||
}
|
||||
@@ -49,7 +51,7 @@ public class H2Dialect extends AnsiSqlDialect {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement psForUpsert(final Connection conn, final Entity entity, final String... keys) throws SQLException {
|
||||
public PreparedStatement psForUpsert(final Connection conn, final Entity entity, String... keys) {
|
||||
Assert.notEmpty(keys, "Keys must be not empty for H2 MERGE SQL.");
|
||||
SqlBuilder.validateEntity(entity);
|
||||
final SqlBuilder builder = SqlBuilder.of(quoteWrapper);
|
||||
@@ -58,7 +60,7 @@ public class H2Dialect extends AnsiSqlDialect {
|
||||
final StringBuilder placeHolder = new StringBuilder();
|
||||
|
||||
// 构建字段部分和参数占位符部分
|
||||
entity.forEach((field, value)->{
|
||||
entity.forEach((field, value) -> {
|
||||
if (StrUtil.isNotBlank(field)) {
|
||||
if (fieldsPart.length() > 0) {
|
||||
// 非第一个参数,追加逗号
|
||||
@@ -75,14 +77,15 @@ public class H2Dialect extends AnsiSqlDialect {
|
||||
String tableName = entity.getTableName();
|
||||
if (null != this.quoteWrapper) {
|
||||
tableName = this.quoteWrapper.wrap(tableName);
|
||||
keys = quoteWrapper.wrap(keys);
|
||||
}
|
||||
builder.append("MERGE INTO ").append(tableName)
|
||||
// 字段列表
|
||||
.append(" (").append(fieldsPart)
|
||||
// 更新关键字列表
|
||||
.append(") KEY(").append(ArrayUtil.join(keys, ", "))
|
||||
// 更新值列表
|
||||
.append(") VALUES (").append(placeHolder).append(")");
|
||||
// 字段列表
|
||||
.append(" (").append(fieldsPart)
|
||||
// 更新关键字列表
|
||||
.append(") KEY(").append(ArrayUtil.join(keys, ", "))
|
||||
// 更新值列表
|
||||
.append(") VALUES (").append(placeHolder).append(")");
|
||||
|
||||
return StatementUtil.prepareStatement(conn, builder);
|
||||
}
|
||||
|
@@ -17,21 +17,23 @@ import org.dromara.hutool.db.Entity;
|
||||
import org.dromara.hutool.db.Page;
|
||||
import org.dromara.hutool.db.StatementUtil;
|
||||
import org.dromara.hutool.db.dialect.DialectName;
|
||||
import org.dromara.hutool.db.sql.SqlBuilder;
|
||||
import org.dromara.hutool.db.sql.QuoteWrapper;
|
||||
import org.dromara.hutool.db.sql.SqlBuilder;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* MySQL方言
|
||||
* @author loolly
|
||||
*
|
||||
* @author loolly
|
||||
*/
|
||||
public class MysqlDialect extends AnsiSqlDialect{
|
||||
public class MysqlDialect extends AnsiSqlDialect {
|
||||
private static final long serialVersionUID = -3734718212043823636L;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public MysqlDialect() {
|
||||
quoteWrapper = new QuoteWrapper('`');
|
||||
}
|
||||
@@ -57,11 +59,10 @@ public class MysqlDialect extends AnsiSqlDialect{
|
||||
* @param entity 数据实体类(包含表名)
|
||||
* @param keys 此参数无效
|
||||
* @return PreparedStatement
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.7.20
|
||||
*/
|
||||
@Override
|
||||
public PreparedStatement psForUpsert(final Connection conn, final Entity entity, final String... keys) throws SQLException {
|
||||
public PreparedStatement psForUpsert(final Connection conn, final Entity entity, final String... keys) {
|
||||
SqlBuilder.validateEntity(entity);
|
||||
final SqlBuilder builder = SqlBuilder.of(quoteWrapper);
|
||||
|
||||
@@ -70,7 +71,7 @@ public class MysqlDialect extends AnsiSqlDialect{
|
||||
final StringBuilder updateHolder = new StringBuilder();
|
||||
|
||||
// 构建字段部分和参数占位符部分
|
||||
entity.forEach((field, value)->{
|
||||
entity.forEach((field, value) -> {
|
||||
if (StrUtil.isNotBlank(field)) {
|
||||
if (fieldsPart.length() > 0) {
|
||||
// 非第一个参数,追加逗号
|
||||
@@ -92,12 +93,12 @@ public class MysqlDialect extends AnsiSqlDialect{
|
||||
tableName = this.quoteWrapper.wrap(tableName);
|
||||
}
|
||||
builder.append("INSERT INTO ").append(tableName)
|
||||
// 字段列表
|
||||
.append(" (").append(fieldsPart)
|
||||
// 更新值列表
|
||||
.append(") VALUES (").append(placeHolder)
|
||||
// 主键冲突后的更新操作
|
||||
.append(") ON DUPLICATE KEY UPDATE ").append(updateHolder);
|
||||
// 字段列表
|
||||
.append(" (").append(fieldsPart)
|
||||
// 更新值列表
|
||||
.append(") VALUES (").append(placeHolder)
|
||||
// 主键冲突后的更新操作
|
||||
.append(") ON DUPLICATE KEY UPDATE ").append(updateHolder);
|
||||
|
||||
return StatementUtil.prepareStatement(conn, builder);
|
||||
}
|
||||
|
@@ -12,19 +12,18 @@
|
||||
|
||||
package org.dromara.hutool.db.dialect.impl;
|
||||
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.db.Entity;
|
||||
import org.dromara.hutool.db.StatementUtil;
|
||||
import org.dromara.hutool.db.dialect.DialectName;
|
||||
import org.dromara.hutool.db.sql.SqlBuilder;
|
||||
import org.dromara.hutool.db.sql.QuoteWrapper;
|
||||
import org.dromara.hutool.db.sql.SqlBuilder;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
/**
|
||||
@@ -48,7 +47,7 @@ public class PostgresqlDialect extends AnsiSqlDialect {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatement psForUpsert(final Connection conn, final Entity entity, final String... keys) throws SQLException {
|
||||
public PreparedStatement psForUpsert(final Connection conn, final Entity entity, String... keys) {
|
||||
Assert.notEmpty(keys, "Keys must be not empty for Postgres.");
|
||||
SqlBuilder.validateEntity(entity);
|
||||
final SqlBuilder builder = SqlBuilder.of(quoteWrapper);
|
||||
@@ -78,6 +77,7 @@ public class PostgresqlDialect extends AnsiSqlDialect {
|
||||
String tableName = entity.getTableName();
|
||||
if (null != this.quoteWrapper) {
|
||||
tableName = this.quoteWrapper.wrap(tableName);
|
||||
keys = quoteWrapper.wrap(keys);
|
||||
}
|
||||
builder.append("INSERT INTO ").append(tableName)
|
||||
// 字段列表
|
||||
|
Reference in New Issue
Block a user