mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix pr#1023
This commit is contained in:
@@ -36,7 +36,6 @@ import java.util.Set;
|
||||
* ANSI SQL 方言
|
||||
*
|
||||
* @author loolly
|
||||
*
|
||||
*/
|
||||
public class AnsiSqlDialect implements Dialect {
|
||||
private static final long serialVersionUID = 2088101129774974580L;
|
||||
@@ -67,7 +66,7 @@ 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);
|
||||
final Set<String> fields = CollUtil.remove(entities[0].keySet(), StrUtil::isBlank);
|
||||
return StatementUtil.prepareStatementForBatch(conn, insert.build(), fields, entities);
|
||||
}
|
||||
|
||||
@@ -119,7 +118,7 @@ public class AnsiSqlDialect implements Dialect {
|
||||
@Override
|
||||
public PreparedStatement psForPage(final Connection conn, SqlBuilder sqlBuilder, final Page page) throws SQLException {
|
||||
// 根据不同数据库在查询SQL语句基础上包装其分页的语句
|
||||
if(null != page){
|
||||
if (null != page) {
|
||||
sqlBuilder = wrapPageSql(sqlBuilder.orderBy(page.getOrders()), page);
|
||||
}
|
||||
return StatementUtil.prepareStatement(conn, sqlBuilder);
|
||||
@@ -137,10 +136,10 @@ public class AnsiSqlDialect implements Dialect {
|
||||
protected SqlBuilder wrapPageSql(final SqlBuilder find, final Page page) {
|
||||
// limit A offset B 表示:A就是你需要多少行,B就是查询的起点位置。
|
||||
return find
|
||||
.append(" limit ")
|
||||
.append(page.getPageSize())
|
||||
.append(" offset ")
|
||||
.append(page.getStartPosition());
|
||||
.append(" limit ")
|
||||
.append(page.getPageSize())
|
||||
.append(" offset ")
|
||||
.append(page.getStartPosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -21,180 +21,182 @@ import java.util.List;
|
||||
*/
|
||||
public class DbTest {
|
||||
|
||||
@Test
|
||||
public void queryTest() {
|
||||
final List<Entity> find = Db.of().query("select * from user where age = ?", 18);
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
@Test
|
||||
public void queryTest() {
|
||||
final List<Entity> find = Db.of().query("select * from user where age = ?", 18);
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTest() {
|
||||
final List<Entity> find = Db.of().find(Entity.of("user").set("age", 18));
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
@Test
|
||||
public void findTest() {
|
||||
final List<Entity> find = Db.of().find(Entity.of("user").set("age", 18));
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageTest() {
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(Entity.of("user"),
|
||||
Page.of(0, 3));
|
||||
Assertions.assertEquals(3, page0.size());
|
||||
@Test
|
||||
public void pageTest() {
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(Entity.of("user"),
|
||||
Page.of(0, 3));
|
||||
Assertions.assertEquals(3, page0.size());
|
||||
|
||||
final List<Entity> page1 = Db.of().page(Entity.of("user"), Page.of(1, 3));
|
||||
Assertions.assertEquals(1, page1.size());
|
||||
}
|
||||
final List<Entity> page1 = Db.of().page(Entity.of("user"), Page.of(1, 3));
|
||||
Assertions.assertEquals(1, page1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageBySqlTest() {
|
||||
final String sql = "select * from user order by name";
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(
|
||||
sql, Page.of(0, 3));
|
||||
Assertions.assertEquals(3, page0.size());
|
||||
@Test
|
||||
public void pageBySqlTest() {
|
||||
final String sql = "select * from user order by name";
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(
|
||||
sql, Page.of(0, 3));
|
||||
Assertions.assertEquals(3, page0.size());
|
||||
|
||||
final List<Entity> page1 = Db.of().page(
|
||||
sql, Page.of(1, 3));
|
||||
Assertions.assertEquals(1, page1.size());
|
||||
}
|
||||
final List<Entity> page1 = Db.of().page(
|
||||
sql, Page.of(1, 3));
|
||||
Assertions.assertEquals(1, page1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageBySqlWithInTest() {
|
||||
// in和其他条件混用
|
||||
final String sql = "select * from user where age > :age and name in (:names) order by name";
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(
|
||||
sql, Page.of(0, 3),
|
||||
Entity.of().set("age", 12)
|
||||
.set("names", new String[]{"张三", "王五"})
|
||||
);
|
||||
Assertions.assertEquals(1, page0.size());
|
||||
}
|
||||
@Test
|
||||
public void pageBySqlWithInTest() {
|
||||
// in和其他条件混用
|
||||
final String sql = "select * from user where age > :age and name in (:names) order by name";
|
||||
// 测试数据库中一共4条数据,第0页有3条,第1页有1条
|
||||
final List<Entity> page0 = Db.of().page(
|
||||
sql, Page.of(0, 3),
|
||||
Entity.of().set("age", 12)
|
||||
.set("names", new String[]{"张三", "王五"})
|
||||
);
|
||||
Assertions.assertEquals(1, page0.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageWithParamsTest() {
|
||||
final String sql = "select * from user where name = ?";
|
||||
final PageResult<Entity> result = Db.of().page(
|
||||
sql, Page.of(0, 3), "张三");
|
||||
@Test
|
||||
public void pageWithParamsTest() {
|
||||
final String sql = "select * from user where name = ?";
|
||||
final PageResult<Entity> result = Db.of().page(
|
||||
sql, Page.of(0, 3), "张三");
|
||||
|
||||
Assertions.assertEquals(2, result.getTotal());
|
||||
Assertions.assertEquals(1, result.getTotalPage());
|
||||
Assertions.assertEquals(2, result.size());
|
||||
}
|
||||
Assertions.assertEquals(2, result.getTotal());
|
||||
Assertions.assertEquals(1, result.getTotalPage());
|
||||
Assertions.assertEquals(2, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void countTest() {
|
||||
final long count = Db.of().count("select * from user");
|
||||
Assertions.assertEquals(4, count);
|
||||
}
|
||||
@Test
|
||||
public void countTest() {
|
||||
final long count = Db.of().count("select * from user");
|
||||
Assertions.assertEquals(4, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void countByQueryTest() {
|
||||
final long count = Db.of().count(Entity.of("user"));
|
||||
Assertions.assertEquals(4, count);
|
||||
}
|
||||
@Test
|
||||
public void countByQueryTest() {
|
||||
final long count = Db.of().count(Entity.of("user"));
|
||||
Assertions.assertEquals(4, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void countTest2() {
|
||||
final long count = Db.of().count("select * from user order by name DESC");
|
||||
Assertions.assertEquals(4, count);
|
||||
}
|
||||
@Test
|
||||
public void countTest2() {
|
||||
final long count = Db.of().count("select * from user order by name DESC");
|
||||
Assertions.assertEquals(4, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLikeTest() {
|
||||
// 方式1
|
||||
List<Entity> find = Db.of().find(Entity.of("user").set("name", "like 王%"));
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
@Test
|
||||
public void findLikeTest() {
|
||||
// 方式1
|
||||
List<Entity> find = Db.of().find(Entity.of("user").set("name", "like 王%"));
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
|
||||
// 方式2
|
||||
find = Db.of().findLike("user", "name", "王", Condition.LikeType.StartWith);
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
// 方式2
|
||||
find = Db.of().findLike("user", "name", "王", Condition.LikeType.StartWith);
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
|
||||
// 方式3
|
||||
find = Db.of().query("select * from user where name like ?", "王%");
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
// 方式3
|
||||
find = Db.of().query("select * from user where name like ?", "王%");
|
||||
Assertions.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByTest() {
|
||||
final List<Entity> find = Db.of().findBy("user",
|
||||
Condition.parse("age", "> 18"),
|
||||
Condition.parse("age", "< 100")
|
||||
);
|
||||
for (final Entity entity : find) {
|
||||
LogUtil.debug("{}", entity);
|
||||
}
|
||||
Assertions.assertEquals("unitTestUser", find.get(0).get("name"));
|
||||
}
|
||||
@Test
|
||||
public void findByTest() {
|
||||
final List<Entity> find = Db.of().findBy("user",
|
||||
Condition.parse("age", "> 18"),
|
||||
Condition.parse("age", "< 100")
|
||||
);
|
||||
for (final Entity entity : find) {
|
||||
LogUtil.debug("{}", entity);
|
||||
}
|
||||
Assertions.assertEquals("unitTestUser", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void txTest() throws SQLException {
|
||||
Db.of().tx(db -> {
|
||||
db.insert(Entity.of("user").set("name", "unitTestuser2"));
|
||||
db.update(Entity.of().set("age", 79), Entity.of("user").set("name", "unitTestuser2"));
|
||||
db.del("user", "name", "unitTestuser2");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
@Disabled
|
||||
public void txTest() throws SQLException {
|
||||
Db.of().tx(db -> {
|
||||
db.insert(Entity.of("user").set("name", "unitTestuser2"));
|
||||
db.update(Entity.of().set("age", 79), Entity.of("user").set("name", "unitTestuser2"));
|
||||
db.del("user", "name", "unitTestuser2");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchInsert() throws SQLException {
|
||||
List<Entity> es = new ArrayList<>();
|
||||
String tableName = "act_ge_property";
|
||||
Entity e = buildEntity(tableName);
|
||||
es.add(e);
|
||||
Db.of().tx(db -> {
|
||||
db.insert(es);
|
||||
List<Entity> ens = Db.of().find(e);
|
||||
Assertions.assertEquals(1, ens.size());
|
||||
db.del(tableName, "NAME_", "!= null");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
@Disabled
|
||||
public void batchInsertTest() throws SQLException {
|
||||
final List<Entity> es = new ArrayList<>();
|
||||
final String tableName = "act_ge_property";
|
||||
final Entity e = buildEntity();
|
||||
es.add(e);
|
||||
Db.of().tx(db -> {
|
||||
db.insert(es);
|
||||
final List<Entity> ens = Db.of().find(e);
|
||||
Assertions.assertEquals(1, ens.size());
|
||||
db.del(tableName, "NAME_", "!= null");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchInsertBatch() throws SQLException {
|
||||
List<Entity> es = new ArrayList<>();
|
||||
String tableName = "act_ge_property";
|
||||
Entity e = buildEntity(tableName);
|
||||
es.add(e);
|
||||
es.add(buildEntity(tableName));
|
||||
es.add(buildEntity(tableName));
|
||||
es.add(buildEntity(tableName));
|
||||
Db.of().tx(db -> {
|
||||
db.insert(es);
|
||||
List<Entity> ens = Db.of().find(e);
|
||||
Assertions.assertEquals(1, ens.size());
|
||||
db.del(tableName, "NAME_", "!= null");
|
||||
});
|
||||
@Test
|
||||
@Disabled
|
||||
public void batchInsertBatch() throws SQLException {
|
||||
final List<Entity> es = new ArrayList<>();
|
||||
final String tableName = "act_ge_property";
|
||||
final Entity e = buildEntity();
|
||||
es.add(e);
|
||||
es.add(buildEntity());
|
||||
es.add(buildEntity());
|
||||
es.add(buildEntity());
|
||||
Db.of().tx(db -> {
|
||||
db.insert(es);
|
||||
final List<Entity> ens = Db.of().find(e);
|
||||
Assertions.assertEquals(1, ens.size());
|
||||
db.del(tableName, "NAME_", "!= null");
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Entity buildEntity(String tableName) {
|
||||
Entity entity = Entity.of("act_ge_property");
|
||||
entity.put("NAME_", RandomUtil.randomString(15));
|
||||
entity.put("VALUE_", RandomUtil.randomString(50));
|
||||
entity.put("REV_", RandomUtil.randomInt());
|
||||
return entity;
|
||||
}
|
||||
private Entity buildEntity() {
|
||||
final Entity entity = Entity.of("act_ge_property");
|
||||
entity.put("NAME_", RandomUtil.randomString(15));
|
||||
entity.put("VALUE_", RandomUtil.randomString(50));
|
||||
entity.put("REV_", RandomUtil.randomInt());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void queryFetchTest() {
|
||||
// https://gitee.com/dromara/hutool/issues/I4JXWN
|
||||
Db.of().query((conn -> {
|
||||
final PreparedStatement ps = conn.prepareStatement("select * from table",
|
||||
ResultSet.TYPE_FORWARD_ONLY,
|
||||
ResultSet.CONCUR_READ_ONLY);
|
||||
ps.setFetchSize(Integer.MIN_VALUE);
|
||||
ps.setFetchDirection(ResultSet.FETCH_FORWARD);
|
||||
return ps;
|
||||
}), new EntityListHandler());
|
||||
}
|
||||
@Test
|
||||
@Disabled
|
||||
public void queryFetchTest() {
|
||||
// https://gitee.com/dromara/hutool/issues/I4JXWN
|
||||
Db.of().query((conn -> {
|
||||
final PreparedStatement ps = conn.prepareStatement("select * from table",
|
||||
ResultSet.TYPE_FORWARD_ONLY,
|
||||
ResultSet.CONCUR_READ_ONLY);
|
||||
ps.setFetchSize(Integer.MIN_VALUE);
|
||||
ps.setFetchDirection(ResultSet.FETCH_FORWARD);
|
||||
return ps;
|
||||
}), new EntityListHandler());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void findWithDotTest() {
|
||||
// 当字段带有点时,导致被拆分分别wrap了,因此此时关闭自动包装即可
|
||||
Db.of().disableWrapper().find(Entity.of("user").set("a.b", "1"));
|
||||
}
|
||||
@Test
|
||||
@Disabled
|
||||
public void findWithDotTest() {
|
||||
// 当字段带有点时,导致被拆分分别wrap了,因此此时关闭自动包装即可
|
||||
Db.of().disableWrapper().find(Entity.of("user").set("a.b", "1"));
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user