mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
upsert 接口和h2方言的upsert完成并测试通过
This commit is contained in:
parent
c5673bb06b
commit
64d21372ec
@ -200,9 +200,9 @@ public abstract class AbstractDb implements Serializable {
|
||||
* 执行自定义的{@link PreparedStatement},结果使用{@link RsHandler}处理<br>
|
||||
* 此方法主要用于自定义场景,如游标查询等
|
||||
*
|
||||
* @param <T> 结果集需要处理的对象类型
|
||||
* @param <T> 结果集需要处理的对象类型
|
||||
* @param statementFunc 自定义{@link PreparedStatement}创建函数
|
||||
* @param rsh 结果集处理对象
|
||||
* @param rsh 结果集处理对象
|
||||
* @return 结果对象
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.7.17
|
||||
@ -369,6 +369,26 @@ public abstract class AbstractDb implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用upsert语义插入或更新数据<br>
|
||||
* 根据给定的字段名查询数据,如果存在则更新这些数据,否则执行插入
|
||||
* 如果方言未实现本方法,内部会自动调用insertOrUpdate来实现功能,由于upsert和insert使用有区别,为了兼容性保留原有insertOrUpdate不做变动
|
||||
* @param record 记录
|
||||
* @param keys 需要检查唯一性的字段
|
||||
* @return 插入行数
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public int upsert(Entity record, String... keys) throws SQLException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
return runner.upsert(conn, record, keys);
|
||||
} finally {
|
||||
this.closeConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入数据<br>
|
||||
* 需要注意的是,批量插入每一条数据结构必须一致。批量插入数据时会获取第一条数据的字段结构,之后的数据会按照这个格式插入。<br>
|
||||
@ -864,7 +884,7 @@ public abstract class AbstractDb implements Serializable {
|
||||
/**
|
||||
* 分页查询
|
||||
*
|
||||
* @param <T> 处理结果类型,可以将ResultSet转换为给定类型
|
||||
* @param <T> 处理结果类型,可以将ResultSet转换为给定类型
|
||||
* @param sql SQL构建器
|
||||
* @param page 分页对象
|
||||
* @param rsh 结果集处理对象
|
||||
@ -884,8 +904,8 @@ public abstract class AbstractDb implements Serializable {
|
||||
/**
|
||||
* 分页查询
|
||||
*
|
||||
* @param sql SQL语句字符串
|
||||
* @param page 分页对象
|
||||
* @param sql SQL语句字符串
|
||||
* @param page 分页对象
|
||||
* @param params 参数列表
|
||||
* @return 结果对象
|
||||
* @throws SQLException SQL执行异常
|
||||
|
@ -86,6 +86,35 @@ public class DialectRunner implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新或插入数据<br>
|
||||
* 此方法不会关闭Connection
|
||||
* 如果方言未实现此方法则内部自动使用insertOrUpdate来替代功能
|
||||
*
|
||||
* @param conn 数据库连接
|
||||
* @param record 记录
|
||||
* @param keys 需要检查唯一性的字段
|
||||
* @return 插入行数
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public int upsert(Connection conn, Entity record, String... keys) throws SQLException {
|
||||
PreparedStatement ps = getDialect().psForUpsert(conn, record, keys);
|
||||
if (null != ps) {
|
||||
try {
|
||||
return ps.executeUpdate();
|
||||
} finally {
|
||||
DbUtil.close(ps);
|
||||
}
|
||||
} else {
|
||||
final Entity where = record.filter(keys);
|
||||
if (MapUtil.isNotEmpty(where) && count(conn, where) > 0) {
|
||||
return update(conn, record, where);
|
||||
} else {
|
||||
return insert(conn, record).length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据<br>
|
||||
* 此方法不会关闭Connection
|
||||
@ -212,7 +241,7 @@ public class DialectRunner implements Serializable {
|
||||
* 获取查询结果总数,生成类似于 SELECT count(1) from (sql) hutool_alias_count_<br>
|
||||
* 此方法会重新构建{@link SqlBuilder},并去除末尾的order by子句
|
||||
*
|
||||
* @param conn 数据库连接对象
|
||||
* @param conn 数据库连接对象
|
||||
* @param sqlBuilder 查询语句
|
||||
* @return 复合条件的结果数
|
||||
* @throws SQLException SQL执行异常
|
||||
|
@ -15,6 +15,7 @@ import cn.hutool.db.sql.SqlUtil;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
@ -121,7 +121,7 @@ public interface Dialect extends Serializable {
|
||||
* @return PreparedStatement
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
default PreparedStatement psForCount(Connection conn, Query query) throws SQLException{
|
||||
default PreparedStatement psForCount(Connection conn, Query query) throws SQLException {
|
||||
query.setFields(ListUtil.toList("count(1)"));
|
||||
return psForFind(conn, query);
|
||||
}
|
||||
@ -129,13 +129,13 @@ public interface Dialect extends Serializable {
|
||||
/**
|
||||
* 构建用于查询行数的PreparedStatement
|
||||
*
|
||||
* @param conn 数据库连接对象
|
||||
* @param conn 数据库连接对象
|
||||
* @param sqlBuilder 查询语句,应该包含分页等信息
|
||||
* @return PreparedStatement
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.7.2
|
||||
*/
|
||||
default PreparedStatement psForCount(Connection conn, SqlBuilder sqlBuilder) throws SQLException{
|
||||
default PreparedStatement psForCount(Connection conn, SqlBuilder sqlBuilder) throws SQLException {
|
||||
sqlBuilder = sqlBuilder
|
||||
.insertPreFragment("SELECT count(1) from(")
|
||||
// issue#I3IJ8X@Gitee,在子查询时需设置单独别名,此处为了防止和用户的表名冲突,使用自定义的较长别名
|
||||
@ -143,6 +143,20 @@ public interface Dialect extends Serializable {
|
||||
return psForPage(conn, sqlBuilder, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建用于upsert的PreparedStatement
|
||||
*
|
||||
* @param conn 数据库连接对象
|
||||
* @param entity 数据实体类(包含表名)
|
||||
* @param keys 查找字段
|
||||
* @return PreparedStatement
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
default PreparedStatement psForUpsert(Connection conn, Entity entity, String... keys) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 方言名
|
||||
*
|
||||
|
@ -1,9 +1,21 @@
|
||||
package cn.hutool.db.dialect.impl;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.Page;
|
||||
import cn.hutool.db.StatementUtil;
|
||||
import cn.hutool.db.dialect.DialectName;
|
||||
import cn.hutool.db.sql.Condition;
|
||||
import cn.hutool.db.sql.Query;
|
||||
import cn.hutool.db.sql.SqlBuilder;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* H2数据库方言
|
||||
*
|
||||
@ -26,4 +38,19 @@ public class H2Dialect extends AnsiSqlDialect {
|
||||
// limit A , B 表示:A就是查询的起点位置,B就是你需要多少行。
|
||||
return find.append(" limit ").append(page.getStartPosition()).append(" , ").append(page.getPageSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建用于upsert的PreparedStatement
|
||||
*
|
||||
* @param conn 数据库连接对象
|
||||
* @param entity 数据实体类(包含表名)
|
||||
* @param keys 查找字段 如果不提供keys将自动使用主键
|
||||
* @return PreparedStatement
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
@Override
|
||||
public PreparedStatement psForUpsert(Connection conn, Entity entity, String... keys) throws SQLException {
|
||||
final SqlBuilder upsert = SqlBuilder.create(wrapper).upsert(entity, this.dialectName(),keys);
|
||||
return StatementUtil.prepareStatement(conn, upsert);
|
||||
}
|
||||
}
|
||||
|
@ -196,6 +196,73 @@ public class SqlBuilder implements Builder<String> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入<br>
|
||||
* 插入会忽略空的字段名及其对应值,但是对于有字段名对应值为{@code null}的情况不忽略
|
||||
*
|
||||
* @param entity 实体
|
||||
* @param dialectName 方言名,用于对特殊数据库特殊处理
|
||||
* @param keys 根据何字段来确认唯一性,不传则用主键
|
||||
* @return 自己
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public SqlBuilder upsert(Entity entity, String dialectName, String... keys) {
|
||||
// 验证
|
||||
validateEntity(entity);
|
||||
|
||||
if (null != wrapper) {
|
||||
// 包装表名 entity = wrapper.wrap(entity);
|
||||
entity.setTableName(wrapper.wrap(entity.getTableName()));
|
||||
}
|
||||
|
||||
final boolean isOracle = DialectName.ORACLE.match(dialectName);// 对Oracle的特殊处理
|
||||
final StringBuilder fieldsPart = new StringBuilder();
|
||||
final StringBuilder placeHolder = new StringBuilder();
|
||||
|
||||
boolean isFirst = true;
|
||||
String field;
|
||||
Object value;
|
||||
for (Entry<String, Object> entry : entity.entrySet()) {
|
||||
field = entry.getKey();
|
||||
value = entry.getValue();
|
||||
if (StrUtil.isNotBlank(field) /* && null != value */) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
// 非第一个参数,追加逗号
|
||||
fieldsPart.append(", ");
|
||||
placeHolder.append(", ");
|
||||
}
|
||||
|
||||
this.fields.add(field);
|
||||
fieldsPart.append((null != wrapper) ? wrapper.wrap(field) : field);
|
||||
if (isOracle && value instanceof String && StrUtil.endWithIgnoreCase((String) value, ".nextval")) {
|
||||
// Oracle的特殊自增键,通过字段名.nextval获得下一个值
|
||||
placeHolder.append(value);
|
||||
} else {
|
||||
placeHolder.append("?");
|
||||
this.paramValues.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// issue#1656@Github Phoenix兼容
|
||||
if (DialectName.PHOENIX.match(dialectName)) {
|
||||
sql.append("UPSERT INTO ").append(entity.getTableName());
|
||||
} else if (DialectName.H2.match(dialectName)) {
|
||||
sql.append("MERGE INTO ").append(entity.getTableName());
|
||||
if (null != keys && keys.length > 0) {
|
||||
sql.append(" KEY(").append(ArrayUtil.join(keys, ","))
|
||||
.append(") VALUES (")
|
||||
.append(placeHolder)
|
||||
.append(")");
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException(dialectName + " not support yet");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import com.alibaba.druid.support.json.JSONUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
@ -9,14 +10,14 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* H2数据库单元测试
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class H2Test {
|
||||
|
||||
|
||||
private static final String DS_GROUP_NAME = "h2";
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws SQLException {
|
||||
Db db = Db.use(DS_GROUP_NAME);
|
||||
@ -27,7 +28,7 @@ public class H2Test {
|
||||
db.insert(Entity.create("test").set("a", 3).set("b", 31));
|
||||
db.insert(Entity.create("test").set("a", 4).set("b", 41));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void queryTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
@ -39,4 +40,11 @@ public class H2Test {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
@Test
|
||||
public void upsertTest() throws SQLException {
|
||||
Db db=Db.use(DS_GROUP_NAME);
|
||||
db.upsert(Entity.create("test").set("a",1).set("b",111),"a");
|
||||
Entity a1=db.get("test","a",1);
|
||||
Assert.assertEquals(Long.valueOf(111),a1.getLong("b"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user