mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
commit
27e5f58692
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,16 @@
|
||||
package cn.hutool.db.dialect.impl;
|
||||
|
||||
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.SqlBuilder;
|
||||
import cn.hutool.db.sql.Wrapper;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* MySQL方言
|
||||
* @author loolly
|
||||
@ -21,9 +27,24 @@ public class MysqlDialect extends AnsiSqlDialect{
|
||||
protected SqlBuilder wrapPageSql(SqlBuilder find, Page page) {
|
||||
return find.append(" LIMIT ").append(page.getStartPosition()).append(", ").append(page.getPageSize());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String dialectName() {
|
||||
return DialectName.MYSQL.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建用于upsert的PreparedStatement
|
||||
*
|
||||
* @param conn 数据库连接对象
|
||||
* @param entity 数据实体类(包含表名)
|
||||
* @param 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);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,15 @@
|
||||
package cn.hutool.db.dialect.impl;
|
||||
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.StatementUtil;
|
||||
import cn.hutool.db.dialect.DialectName;
|
||||
import cn.hutool.db.sql.SqlBuilder;
|
||||
import cn.hutool.db.sql.Wrapper;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
/**
|
||||
* Postgree方言
|
||||
@ -20,4 +27,22 @@ public class PostgresqlDialect extends AnsiSqlDialect{
|
||||
public String dialectName() {
|
||||
return DialectName.POSTGREESQL.name();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建用于upsert的PreparedStatement
|
||||
*
|
||||
* @param conn 数据库连接对象
|
||||
* @param entity 数据实体类(包含表名)
|
||||
* @param keys 查找字段 必须是有唯一索引的列且不能为空
|
||||
* @return PreparedStatement
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
@Override
|
||||
public PreparedStatement psForUpsert(Connection conn, Entity entity, String... keys) throws SQLException {
|
||||
if (null==keys || keys.length==0){
|
||||
throw new SQLException("keys不能为空");
|
||||
}
|
||||
final SqlBuilder upsert = SqlBuilder.create(wrapper).upsert(entity, this.dialectName(),keys);
|
||||
return StatementUtil.prepareStatement(conn, upsert);
|
||||
}
|
||||
}
|
||||
|
@ -196,6 +196,87 @@ 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.MYSQL.match(dialectName)) {
|
||||
sql.append("INSERT INTO ");
|
||||
sql.append(entity.getTableName())
|
||||
.append(" (").append(fieldsPart).append(") VALUES (")
|
||||
.append(placeHolder).append(") on duplicate key update ")
|
||||
.append(ArrayUtil.join(ArrayUtil.map(entity.keySet().toArray(), String.class, (k) -> k + "=values(" + k + ")"), ","));
|
||||
} 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 if (DialectName.POSTGREESQL.match(dialectName)) {
|
||||
sql.append("INSERT INTO ");
|
||||
sql.append(entity.getTableName())
|
||||
.append(" (").append(fieldsPart).append(") VALUES (")
|
||||
.append(placeHolder).append(") on conflict (")
|
||||
.append(ArrayUtil.join(keys,","))
|
||||
.append(") do update set ")
|
||||
.append(ArrayUtil.join(ArrayUtil.map(entity.keySet().toArray(), String.class, (k) -> k + "=excluded." + k ), ","));
|
||||
} 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"));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -9,11 +12,16 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* MySQL操作单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class MySQLTest {
|
||||
@BeforeClass
|
||||
@Ignore
|
||||
public static void createTable() throws SQLException {
|
||||
Db db = Db.use("mysql");
|
||||
db.executeBatch("drop table if exists testuser", "CREATE TABLE if not exists `testuser` ( `id` int(11) NOT NULL, `account` varchar(255) DEFAULT NULL, `pass` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@ -34,13 +42,13 @@ public class MySQLTest {
|
||||
*
|
||||
* @throws SQLException SQL异常
|
||||
*/
|
||||
@Test(expected=SQLException.class)
|
||||
@Test(expected = SQLException.class)
|
||||
@Ignore
|
||||
public void txTest() throws SQLException {
|
||||
Db.use("mysql").tx(db -> {
|
||||
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
|
||||
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
|
||||
if(1 == update) {
|
||||
if (1 == update) {
|
||||
// 手动指定异常,然后测试回滚触发
|
||||
throw new RuntimeException("Error");
|
||||
}
|
||||
@ -64,4 +72,14 @@ public class MySQLTest {
|
||||
Console.log(all);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void upsertTest() throws SQLException {
|
||||
Db db = Db.use("mysql");
|
||||
db.insert(Entity.create("testuser").set("id", 1).set("account", "ice").set("pass", "123456"));
|
||||
db.upsert(Entity.create("testuser").set("id", 1).set("account", "icefairy").set("pass", "a123456"));
|
||||
Entity user = db.get(Entity.create("testuser").set("id", 1));
|
||||
System.out.println("user======="+user.getStr("account")+"___"+user.getStr("pass"));
|
||||
Assert.assertEquals(user.getStr("account"), new String("icefairy"));
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -9,9 +10,8 @@ import cn.hutool.core.lang.Console;
|
||||
|
||||
/**
|
||||
* PostgreSQL 单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class PostgreTest {
|
||||
|
||||
@ -34,4 +34,16 @@ public class PostgreTest {
|
||||
Console.log(entity.get("id"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void upsertTest() throws SQLException {
|
||||
Db db = Db.use("postgre");
|
||||
db.executeBatch("drop table if exists ctest",
|
||||
"create table if not exists \"ctest\" ( \"id\" serial4, \"t1\" varchar(255) COLLATE \"pg_catalog\".\"default\", \"t2\" varchar(255) COLLATE \"pg_catalog\".\"default\", \"t3\" varchar(255) COLLATE \"pg_catalog\".\"default\", CONSTRAINT \"ctest_pkey\" PRIMARY KEY (\"id\") ) ");
|
||||
db.insert(Entity.create("ctest").set("id", 1).set("t1", "111").set("t2", "222").set("t3", "333"));
|
||||
db.upsert(Entity.create("ctest").set("id", 1).set("t1", "new111").set("t2", "new222").set("t3", "bew333"),"id");
|
||||
Entity et=db.get(Entity.create("ctest").set("id", 1));
|
||||
Assert.assertEquals("new111",et.getStr("t1"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user