mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
mysql + postgres upsert完成并测试通过
This commit is contained in:
@@ -1,10 +1,16 @@
|
|||||||
package cn.hutool.db.dialect.impl;
|
package cn.hutool.db.dialect.impl;
|
||||||
|
|
||||||
|
import cn.hutool.db.Entity;
|
||||||
import cn.hutool.db.Page;
|
import cn.hutool.db.Page;
|
||||||
|
import cn.hutool.db.StatementUtil;
|
||||||
import cn.hutool.db.dialect.DialectName;
|
import cn.hutool.db.dialect.DialectName;
|
||||||
import cn.hutool.db.sql.SqlBuilder;
|
import cn.hutool.db.sql.SqlBuilder;
|
||||||
import cn.hutool.db.sql.Wrapper;
|
import cn.hutool.db.sql.Wrapper;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MySQL方言
|
* MySQL方言
|
||||||
* @author loolly
|
* @author loolly
|
||||||
@@ -21,9 +27,24 @@ public class MysqlDialect extends AnsiSqlDialect{
|
|||||||
protected SqlBuilder wrapPageSql(SqlBuilder find, Page page) {
|
protected SqlBuilder wrapPageSql(SqlBuilder find, Page page) {
|
||||||
return find.append(" LIMIT ").append(page.getStartPosition()).append(", ").append(page.getPageSize());
|
return find.append(" LIMIT ").append(page.getStartPosition()).append(", ").append(page.getPageSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String dialectName() {
|
public String dialectName() {
|
||||||
return DialectName.MYSQL.toString();
|
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;
|
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.dialect.DialectName;
|
||||||
|
import cn.hutool.db.sql.SqlBuilder;
|
||||||
import cn.hutool.db.sql.Wrapper;
|
import cn.hutool.db.sql.Wrapper;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Postgree方言
|
* Postgree方言
|
||||||
@@ -20,4 +27,22 @@ public class PostgresqlDialect extends AnsiSqlDialect{
|
|||||||
public String dialectName() {
|
public String dialectName() {
|
||||||
return DialectName.POSTGREESQL.name();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -202,7 +202,7 @@ public class SqlBuilder implements Builder<String> {
|
|||||||
*
|
*
|
||||||
* @param entity 实体
|
* @param entity 实体
|
||||||
* @param dialectName 方言名,用于对特殊数据库特殊处理
|
* @param dialectName 方言名,用于对特殊数据库特殊处理
|
||||||
* @param keys 根据何字段来确认唯一性,不传则用主键
|
* @param keys 根据何字段来确认唯一性,不传则用主键
|
||||||
* @return 自己
|
* @return 自己
|
||||||
* @since 5.7.21
|
* @since 5.7.21
|
||||||
*/
|
*/
|
||||||
@@ -249,6 +249,12 @@ public class SqlBuilder implements Builder<String> {
|
|||||||
// issue#1656@Github Phoenix兼容
|
// issue#1656@Github Phoenix兼容
|
||||||
if (DialectName.PHOENIX.match(dialectName)) {
|
if (DialectName.PHOENIX.match(dialectName)) {
|
||||||
sql.append("UPSERT INTO ").append(entity.getTableName());
|
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)) {
|
} else if (DialectName.H2.match(dialectName)) {
|
||||||
sql.append("MERGE INTO ").append(entity.getTableName());
|
sql.append("MERGE INTO ").append(entity.getTableName());
|
||||||
if (null != keys && keys.length > 0) {
|
if (null != keys && keys.length > 0) {
|
||||||
@@ -257,6 +263,14 @@ public class SqlBuilder implements Builder<String> {
|
|||||||
.append(placeHolder)
|
.append(placeHolder)
|
||||||
.append(")");
|
.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 {
|
} else {
|
||||||
throw new RuntimeException(dialectName + " not support yet");
|
throw new RuntimeException(dialectName + " not support yet");
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
package cn.hutool.db;
|
package cn.hutool.db;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Console;
|
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.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -9,11 +12,16 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* MySQL操作单元测试
|
* MySQL操作单元测试
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*
|
*
|
||||||
|
* @author looly
|
||||||
*/
|
*/
|
||||||
public class MySQLTest {
|
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
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
@@ -34,13 +42,13 @@ public class MySQLTest {
|
|||||||
*
|
*
|
||||||
* @throws SQLException SQL异常
|
* @throws SQLException SQL异常
|
||||||
*/
|
*/
|
||||||
@Test(expected=SQLException.class)
|
@Test(expected = SQLException.class)
|
||||||
@Ignore
|
@Ignore
|
||||||
public void txTest() throws SQLException {
|
public void txTest() throws SQLException {
|
||||||
Db.use("mysql").tx(db -> {
|
Db.use("mysql").tx(db -> {
|
||||||
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
|
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));
|
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
|
||||||
if(1 == update) {
|
if (1 == update) {
|
||||||
// 手动指定异常,然后测试回滚触发
|
// 手动指定异常,然后测试回滚触发
|
||||||
throw new RuntimeException("Error");
|
throw new RuntimeException("Error");
|
||||||
}
|
}
|
||||||
@@ -64,4 +72,14 @@ public class MySQLTest {
|
|||||||
Console.log(all);
|
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 java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -9,9 +10,8 @@ import cn.hutool.core.lang.Console;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* PostgreSQL 单元测试
|
* PostgreSQL 单元测试
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*
|
*
|
||||||
|
* @author looly
|
||||||
*/
|
*/
|
||||||
public class PostgreTest {
|
public class PostgreTest {
|
||||||
|
|
||||||
@@ -34,4 +34,16 @@ public class PostgreTest {
|
|||||||
Console.log(entity.get("id"));
|
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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user