fix pr#1023

This commit is contained in:
Looly
2023-06-19 16:03:10 +08:00
parent 7026184fa3
commit e1b6ebd1ac
3 changed files with 161 additions and 160 deletions

View File

@@ -36,7 +36,6 @@ import java.util.Set;
* ANSI SQL 方言 * ANSI SQL 方言
* *
* @author loolly * @author loolly
*
*/ */
public class AnsiSqlDialect implements Dialect { public class AnsiSqlDialect implements Dialect {
private static final long serialVersionUID = 2088101129774974580L; private static final long serialVersionUID = 2088101129774974580L;
@@ -67,7 +66,7 @@ public class AnsiSqlDialect implements Dialect {
} }
// 批量根据第一行数据结构生成SQL占位符 // 批量根据第一行数据结构生成SQL占位符
final SqlBuilder insert = SqlBuilder.of(quoteWrapper).insert(entities[0], this.dialectName()); 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); return StatementUtil.prepareStatementForBatch(conn, insert.build(), fields, entities);
} }
@@ -119,7 +118,7 @@ public class AnsiSqlDialect implements Dialect {
@Override @Override
public PreparedStatement psForPage(final Connection conn, SqlBuilder sqlBuilder, final Page page) throws SQLException { public PreparedStatement psForPage(final Connection conn, SqlBuilder sqlBuilder, final Page page) throws SQLException {
// 根据不同数据库在查询SQL语句基础上包装其分页的语句 // 根据不同数据库在查询SQL语句基础上包装其分页的语句
if(null != page){ if (null != page) {
sqlBuilder = wrapPageSql(sqlBuilder.orderBy(page.getOrders()), page); sqlBuilder = wrapPageSql(sqlBuilder.orderBy(page.getOrders()), page);
} }
return StatementUtil.prepareStatement(conn, sqlBuilder); return StatementUtil.prepareStatement(conn, sqlBuilder);
@@ -137,10 +136,10 @@ public class AnsiSqlDialect implements Dialect {
protected SqlBuilder wrapPageSql(final SqlBuilder find, final Page page) { protected SqlBuilder wrapPageSql(final SqlBuilder find, final Page page) {
// limit A offset B 表示A就是你需要多少行B就是查询的起点位置。 // limit A offset B 表示A就是你需要多少行B就是查询的起点位置。
return find return find
.append(" limit ") .append(" limit ")
.append(page.getPageSize()) .append(page.getPageSize())
.append(" offset ") .append(" offset ")
.append(page.getStartPosition()); .append(page.getStartPosition());
} }
@Override @Override

View File

@@ -21,180 +21,182 @@ import java.util.List;
*/ */
public class DbTest { public class DbTest {
@Test @Test
public void queryTest() { public void queryTest() {
final List<Entity> find = Db.of().query("select * from user where age = ?", 18); final List<Entity> find = Db.of().query("select * from user where age = ?", 18);
Assertions.assertEquals("王五", find.get(0).get("name")); Assertions.assertEquals("王五", find.get(0).get("name"));
} }
@Test @Test
public void findTest() { public void findTest() {
final List<Entity> find = Db.of().find(Entity.of("user").set("age", 18)); final List<Entity> find = Db.of().find(Entity.of("user").set("age", 18));
Assertions.assertEquals("王五", find.get(0).get("name")); Assertions.assertEquals("王五", find.get(0).get("name"));
} }
@Test @Test
public void pageTest() { public void pageTest() {
// 测试数据库中一共4条数据第0页有3条第1页有1条 // 测试数据库中一共4条数据第0页有3条第1页有1条
final List<Entity> page0 = Db.of().page(Entity.of("user"), final List<Entity> page0 = Db.of().page(Entity.of("user"),
Page.of(0, 3)); Page.of(0, 3));
Assertions.assertEquals(3, page0.size()); Assertions.assertEquals(3, page0.size());
final List<Entity> page1 = Db.of().page(Entity.of("user"), Page.of(1, 3)); final List<Entity> page1 = Db.of().page(Entity.of("user"), Page.of(1, 3));
Assertions.assertEquals(1, page1.size()); Assertions.assertEquals(1, page1.size());
} }
@Test @Test
public void pageBySqlTest() { public void pageBySqlTest() {
final String sql = "select * from user order by name"; final String sql = "select * from user order by name";
// 测试数据库中一共4条数据第0页有3条第1页有1条 // 测试数据库中一共4条数据第0页有3条第1页有1条
final List<Entity> page0 = Db.of().page( final List<Entity> page0 = Db.of().page(
sql, Page.of(0, 3)); sql, Page.of(0, 3));
Assertions.assertEquals(3, page0.size()); Assertions.assertEquals(3, page0.size());
final List<Entity> page1 = Db.of().page( final List<Entity> page1 = Db.of().page(
sql, Page.of(1, 3)); sql, Page.of(1, 3));
Assertions.assertEquals(1, page1.size()); Assertions.assertEquals(1, page1.size());
} }
@Test @Test
public void pageBySqlWithInTest() { public void pageBySqlWithInTest() {
// in和其他条件混用 // in和其他条件混用
final String sql = "select * from user where age > :age and name in (:names) order by name"; final String sql = "select * from user where age > :age and name in (:names) order by name";
// 测试数据库中一共4条数据第0页有3条第1页有1条 // 测试数据库中一共4条数据第0页有3条第1页有1条
final List<Entity> page0 = Db.of().page( final List<Entity> page0 = Db.of().page(
sql, Page.of(0, 3), sql, Page.of(0, 3),
Entity.of().set("age", 12) Entity.of().set("age", 12)
.set("names", new String[]{"张三", "王五"}) .set("names", new String[]{"张三", "王五"})
); );
Assertions.assertEquals(1, page0.size()); Assertions.assertEquals(1, page0.size());
} }
@Test @Test
public void pageWithParamsTest() { public void pageWithParamsTest() {
final String sql = "select * from user where name = ?"; final String sql = "select * from user where name = ?";
final PageResult<Entity> result = Db.of().page( final PageResult<Entity> result = Db.of().page(
sql, Page.of(0, 3), "张三"); sql, Page.of(0, 3), "张三");
Assertions.assertEquals(2, result.getTotal()); Assertions.assertEquals(2, result.getTotal());
Assertions.assertEquals(1, result.getTotalPage()); Assertions.assertEquals(1, result.getTotalPage());
Assertions.assertEquals(2, result.size()); Assertions.assertEquals(2, result.size());
} }
@Test @Test
public void countTest() { public void countTest() {
final long count = Db.of().count("select * from user"); final long count = Db.of().count("select * from user");
Assertions.assertEquals(4, count); Assertions.assertEquals(4, count);
} }
@Test @Test
public void countByQueryTest() { public void countByQueryTest() {
final long count = Db.of().count(Entity.of("user")); final long count = Db.of().count(Entity.of("user"));
Assertions.assertEquals(4, count); Assertions.assertEquals(4, count);
} }
@Test @Test
public void countTest2() { public void countTest2() {
final long count = Db.of().count("select * from user order by name DESC"); final long count = Db.of().count("select * from user order by name DESC");
Assertions.assertEquals(4, count); Assertions.assertEquals(4, count);
} }
@Test @Test
public void findLikeTest() { public void findLikeTest() {
// 方式1 // 方式1
List<Entity> find = Db.of().find(Entity.of("user").set("name", "like 王%")); List<Entity> find = Db.of().find(Entity.of("user").set("name", "like 王%"));
Assertions.assertEquals("王五", find.get(0).get("name")); Assertions.assertEquals("王五", find.get(0).get("name"));
// 方式2 // 方式2
find = Db.of().findLike("user", "name", "", Condition.LikeType.StartWith); find = Db.of().findLike("user", "name", "", Condition.LikeType.StartWith);
Assertions.assertEquals("王五", find.get(0).get("name")); Assertions.assertEquals("王五", find.get(0).get("name"));
// 方式3 // 方式3
find = Db.of().query("select * from user where name like ?", "王%"); find = Db.of().query("select * from user where name like ?", "王%");
Assertions.assertEquals("王五", find.get(0).get("name")); Assertions.assertEquals("王五", find.get(0).get("name"));
} }
@Test @Test
public void findByTest() { public void findByTest() {
final List<Entity> find = Db.of().findBy("user", final List<Entity> find = Db.of().findBy("user",
Condition.parse("age", "> 18"), Condition.parse("age", "> 18"),
Condition.parse("age", "< 100") Condition.parse("age", "< 100")
); );
for (final Entity entity : find) { for (final Entity entity : find) {
LogUtil.debug("{}", entity); LogUtil.debug("{}", entity);
} }
Assertions.assertEquals("unitTestUser", find.get(0).get("name")); Assertions.assertEquals("unitTestUser", find.get(0).get("name"));
} }
@Test @Test
@Disabled @Disabled
public void txTest() throws SQLException { public void txTest() throws SQLException {
Db.of().tx(db -> { Db.of().tx(db -> {
db.insert(Entity.of("user").set("name", "unitTestuser2")); db.insert(Entity.of("user").set("name", "unitTestuser2"));
db.update(Entity.of().set("age", 79), Entity.of("user").set("name", "unitTestuser2")); db.update(Entity.of().set("age", 79), Entity.of("user").set("name", "unitTestuser2"));
db.del("user", "name", "unitTestuser2"); db.del("user", "name", "unitTestuser2");
}); });
} }
@Test @Test
public void batchInsert() throws SQLException { @Disabled
List<Entity> es = new ArrayList<>(); public void batchInsertTest() throws SQLException {
String tableName = "act_ge_property"; final List<Entity> es = new ArrayList<>();
Entity e = buildEntity(tableName); final String tableName = "act_ge_property";
es.add(e); final Entity e = buildEntity();
Db.of().tx(db -> { es.add(e);
db.insert(es); Db.of().tx(db -> {
List<Entity> ens = Db.of().find(e); db.insert(es);
Assertions.assertEquals(1, ens.size()); final List<Entity> ens = Db.of().find(e);
db.del(tableName, "NAME_", "!= null"); Assertions.assertEquals(1, ens.size());
}); db.del(tableName, "NAME_", "!= null");
} });
}
@Test @Test
public void batchInsertBatch() throws SQLException { @Disabled
List<Entity> es = new ArrayList<>(); public void batchInsertBatch() throws SQLException {
String tableName = "act_ge_property"; final List<Entity> es = new ArrayList<>();
Entity e = buildEntity(tableName); final String tableName = "act_ge_property";
es.add(e); final Entity e = buildEntity();
es.add(buildEntity(tableName)); es.add(e);
es.add(buildEntity(tableName)); es.add(buildEntity());
es.add(buildEntity(tableName)); es.add(buildEntity());
Db.of().tx(db -> { es.add(buildEntity());
db.insert(es); Db.of().tx(db -> {
List<Entity> ens = Db.of().find(e); db.insert(es);
Assertions.assertEquals(1, ens.size()); final List<Entity> ens = Db.of().find(e);
db.del(tableName, "NAME_", "!= null"); Assertions.assertEquals(1, ens.size());
}); db.del(tableName, "NAME_", "!= null");
});
} }
private Entity buildEntity(String tableName) { private Entity buildEntity() {
Entity entity = Entity.of("act_ge_property"); final Entity entity = Entity.of("act_ge_property");
entity.put("NAME_", RandomUtil.randomString(15)); entity.put("NAME_", RandomUtil.randomString(15));
entity.put("VALUE_", RandomUtil.randomString(50)); entity.put("VALUE_", RandomUtil.randomString(50));
entity.put("REV_", RandomUtil.randomInt()); entity.put("REV_", RandomUtil.randomInt());
return entity; return entity;
} }
@Test @Test
@Disabled @Disabled
public void queryFetchTest() { public void queryFetchTest() {
// https://gitee.com/dromara/hutool/issues/I4JXWN // https://gitee.com/dromara/hutool/issues/I4JXWN
Db.of().query((conn -> { Db.of().query((conn -> {
final PreparedStatement ps = conn.prepareStatement("select * from table", final PreparedStatement ps = conn.prepareStatement("select * from table",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY); ResultSet.CONCUR_READ_ONLY);
ps.setFetchSize(Integer.MIN_VALUE); ps.setFetchSize(Integer.MIN_VALUE);
ps.setFetchDirection(ResultSet.FETCH_FORWARD); ps.setFetchDirection(ResultSet.FETCH_FORWARD);
return ps; return ps;
}), new EntityListHandler()); }), new EntityListHandler());
} }
@Test @Test
@Disabled @Disabled
public void findWithDotTest() { public void findWithDotTest() {
// 当字段带有点时导致被拆分分别wrap了因此此时关闭自动包装即可 // 当字段带有点时导致被拆分分别wrap了因此此时关闭自动包装即可
Db.of().disableWrapper().find(Entity.of("user").set("a.b", "1")); Db.of().disableWrapper().find(Entity.of("user").set("a.b", "1"));
} }
} }

Binary file not shown.