添加 commitIfTrue。
This commit is contained in:
@@ -3,16 +3,18 @@ package xyz.zhouxy.jdbc;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static xyz.zhouxy.jdbc.SimpleJdbcTemplate.ParamBuilder.*;
|
||||
import static xyz.zhouxy.jdbc.ParamBuilder.*;
|
||||
import static xyz.zhouxy.plusone.commons.sql.JdbcSql.IN;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -20,13 +22,16 @@ import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import xyz.zhouxy.jdbc.SimpleJdbcTemplate.JdbcExecutor;
|
||||
import xyz.zhouxy.plusone.commons.sql.SQL;
|
||||
import xyz.zhouxy.plusone.commons.util.ArrayTools;
|
||||
import xyz.zhouxy.plusone.commons.util.IdGenerator;
|
||||
import xyz.zhouxy.plusone.commons.util.IdWorker;
|
||||
import xyz.zhouxy.plusone.commons.util.Numbers;
|
||||
|
||||
class SimpleJdbcTemplateTests {
|
||||
|
||||
@@ -48,26 +53,26 @@ class SimpleJdbcTemplateTests {
|
||||
config.setJdbcUrl("jdbc:postgresql://localhost:5432/plusone");
|
||||
config.setUsername("postgres");
|
||||
config.setPassword("zhouxy108");
|
||||
config.setMaximumPoolSize(800);
|
||||
config.setMaximumPoolSize(8);
|
||||
config.setConnectionTimeout(1000000);
|
||||
dataSource = new HikariDataSource(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testQuery() throws SQLException {
|
||||
Object[] ids = buildParams("501533", "501554", "544599");
|
||||
String sql = SQL.newJdbcSql()
|
||||
.SELECT("*")
|
||||
.FROM("test_table")
|
||||
.WHERE(IN("id", ids))
|
||||
.toString();
|
||||
log.info(sql);
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
Object[] ids = buildParams("501533", "501554", "544599");
|
||||
String sql = SQL.newJdbcSql()
|
||||
.SELECT("*")
|
||||
.FROM("test_table")
|
||||
.WHERE(IN("id", ids))
|
||||
.toString();
|
||||
log.info(sql);
|
||||
List<DbRecord> rs = SimpleJdbcTemplate.connect(conn)
|
||||
.queryToRecordList(sql, ids);
|
||||
assertNotNull(rs);
|
||||
for (DbRecord baseEntity : rs) {
|
||||
// log.info("id: {}", baseEntity.getValueAsString("id"));
|
||||
// log.info("id: {}", baseEntity.getValueAsString("id")); // NOSONAR
|
||||
log.info(baseEntity.toString());
|
||||
assertEquals(Optional.empty(), baseEntity.getValueAsString("updated_by"));
|
||||
}
|
||||
@@ -77,19 +82,26 @@ class SimpleJdbcTemplateTests {
|
||||
@Test
|
||||
void testInsert() throws SQLException {
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
List<Map<String, Object>> keys = new ArrayList<>();
|
||||
SimpleJdbcTemplate.connect(conn).update("INSERT INTO base_table(status, created_by) VALUES (?, ?)",
|
||||
buildParams(1, 886), keys);
|
||||
List<DbRecord> keys = SimpleJdbcTemplate.connect(conn).update(
|
||||
"INSERT INTO base_table(status, created_by) VALUES (?, ?)",
|
||||
buildParams(1, 886L),
|
||||
ResultMap.recordResultMap);
|
||||
log.info("keys: {}", keys);
|
||||
assertEquals(1, keys.size());
|
||||
DbRecord result = keys.get(0);
|
||||
assertEquals(1, result.getValueAsInt("status").getAsInt());
|
||||
assertEquals(886L, result.getValueAsLong("created_by").getAsLong());
|
||||
assertTrue(result.get("id").isPresent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdate() throws SQLException {
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
List<Map<String, Object>> keys = new ArrayList<>();
|
||||
SimpleJdbcTemplate.connect(conn).update("UPDATE base_table SET status = ?, version = version + 1, update_time = now(), updated_by = ? WHERE id = ?",
|
||||
buildParams(2, 886, 9), keys);
|
||||
List<DbRecord> keys = SimpleJdbcTemplate.connect(conn).update(
|
||||
"UPDATE base_table SET status = ?, version = version + 1, update_time = now(), updated_by = ? WHERE id = ? AND version = ?",
|
||||
buildParams(2, 886, 9, 0),
|
||||
ResultMap.recordResultMap);
|
||||
log.info("keys: {}", keys);
|
||||
}
|
||||
}
|
||||
@@ -101,21 +113,13 @@ class SimpleJdbcTemplateTests {
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
long id = this.idGenerator.nextId();
|
||||
JdbcExecutor jdbcExecutor = SimpleJdbcTemplate.connect(conn);
|
||||
try {
|
||||
jdbcExecutor.tx(jdbc -> {
|
||||
jdbc.update("INSERT INTO base_table (id, created_by, create_time, status) VALUES (?, ?, ?, ?)",
|
||||
buildParams(id, 585757, LocalDateTime.now(), 0));
|
||||
throw new NullPointerException();
|
||||
});
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// ignore
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
jdbcExecutor.executeTransaction(jdbc -> {
|
||||
jdbc.update("INSERT INTO base_table (id, created_by, create_time, status) VALUES (?, ?, ?, ?)",
|
||||
buildParams(id, 585757, LocalDateTime.now(), 0));
|
||||
throw new NullPointerException();
|
||||
});
|
||||
Optional<Map<String, Object>> first = jdbcExecutor
|
||||
.queryFirst("SELECT * FROM base_table WHERE id = ?", id);
|
||||
.queryFirst("SELECT * FROM base_table WHERE id = ?", buildParams(id));
|
||||
log.info("first: {}", first);
|
||||
assertTrue(!first.isPresent());
|
||||
}
|
||||
@@ -123,23 +127,95 @@ class SimpleJdbcTemplateTests {
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
long id = this.idGenerator.nextId();
|
||||
JdbcExecutor jdbcExecutor = SimpleJdbcTemplate.connect(conn);
|
||||
try {
|
||||
jdbcExecutor.tx(jdbc -> {
|
||||
jdbc.update("INSERT INTO base_table (id, created_by, create_time, status) VALUES (?, ?, ?, ?)",
|
||||
buildParams(id, 585757, LocalDateTime.now(), 0));
|
||||
// throw new NullPointerException();
|
||||
});
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// ignore
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
jdbcExecutor.executeTransaction(jdbc -> {
|
||||
jdbc.update("INSERT INTO base_table (id, created_by, create_time, status) VALUES (?, ?, ?, ?)",
|
||||
buildParams(id, 585757, LocalDateTime.now(), 0));
|
||||
// throw new NullPointerException(); // NOSONAR
|
||||
});
|
||||
Optional<Map<String, Object>> first = jdbcExecutor
|
||||
.queryFirst("SELECT * FROM base_table WHERE id = ?", id);
|
||||
.queryFirst("SELECT * FROM base_table WHERE id = ?", buildParams(id));
|
||||
log.info("first: {}", first);
|
||||
assertTrue(first.isPresent());
|
||||
}
|
||||
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
long id = this.idGenerator.nextId();
|
||||
JdbcExecutor jdbcExecutor = SimpleJdbcTemplate.connect(conn);
|
||||
jdbcExecutor.commitIfTrue(jdbc -> {
|
||||
jdbc.update("INSERT INTO base_table (id, created_by, create_time, status) VALUES (?, ?, ?, ?)",
|
||||
buildParams(id, 585757, LocalDateTime.now(), 0));
|
||||
throw new NullPointerException();
|
||||
});
|
||||
Optional<Map<String, Object>> first = jdbcExecutor
|
||||
.queryFirst("SELECT * FROM base_table WHERE id = ?", buildParams(id));
|
||||
log.info("first: {}", first);
|
||||
assertTrue(!first.isPresent());
|
||||
}
|
||||
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
long id = this.idGenerator.nextId();
|
||||
JdbcExecutor jdbcExecutor = SimpleJdbcTemplate.connect(conn);
|
||||
jdbcExecutor.commitIfTrue(jdbc -> {
|
||||
jdbc.update("INSERT INTO base_table (id, created_by, create_time, status) VALUES (?, ?, ?, ?)",
|
||||
buildParams(id, 585757, LocalDateTime.now(), 0));
|
||||
return false;
|
||||
});
|
||||
Optional<Map<String, Object>> first = jdbcExecutor
|
||||
.queryFirst("SELECT * FROM base_table WHERE id = ?", buildParams(id));
|
||||
log.info("first: {}", first);
|
||||
assertTrue(!first.isPresent());
|
||||
}
|
||||
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
long id = this.idGenerator.nextId();
|
||||
JdbcExecutor jdbcExecutor = SimpleJdbcTemplate.connect(conn);
|
||||
jdbcExecutor.commitIfTrue(jdbc -> {
|
||||
jdbc.update("INSERT INTO base_table (id, created_by, create_time, status) VALUES (?, ?, ?, ?)",
|
||||
buildParams(id, 585757, LocalDateTime.now(), 0));
|
||||
return true;
|
||||
});
|
||||
Optional<Map<String, Object>> first = jdbcExecutor
|
||||
.queryFirst("SELECT * FROM base_table WHERE id = ?", buildParams(id));
|
||||
log.info("first: {}", first);
|
||||
assertTrue(first.isPresent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBatch() throws Exception {
|
||||
|
||||
Random random = ThreadLocalRandom.current();
|
||||
|
||||
LocalDate handleDate = LocalDate.of(1949, 10, 1);
|
||||
List<DbRecord> datas = Lists.newArrayList();
|
||||
while (handleDate.isBefore(LocalDate.of(2008, 8, 8))) {
|
||||
DbRecord r1 = new DbRecord();
|
||||
r1.put("username", "张三2");
|
||||
r1.put("usage_date", handleDate);
|
||||
r1.put("usage_duration", random.nextInt(500));
|
||||
datas.add(r1);
|
||||
DbRecord r2 = new DbRecord();
|
||||
r2.put("username", "李四2");
|
||||
r2.put("usage_date", handleDate);
|
||||
r2.put("usage_duration", random.nextInt(500));
|
||||
datas.add(r2);
|
||||
|
||||
handleDate = handleDate.plusDays(1L);
|
||||
}
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
List<int[]> result = SimpleJdbcTemplate.connect(conn)
|
||||
.batchUpdate("insert into test_table (username, usage_date, usage_duration) values (?,?,?)",
|
||||
buildBatchParams(datas, item -> buildParams(
|
||||
item.getValueAsString("username"),
|
||||
item.getValueAsString("usage_date"),
|
||||
item.getValueAsString("usage_duration"))),
|
||||
400);
|
||||
long sum = Numbers.sum(ArrayTools.concatIntArray(result));
|
||||
assertEquals(datas.size(), sum);
|
||||
log.info("sum: {}", sum);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user