Files
plusone-commons/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java

132 lines
4.9 KiB
Java
Raw Normal View History

2023-05-27 04:07:50 +08:00
package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
2023-06-05 18:31:51 +08:00
import static org.junit.jupiter.api.Assertions.assertNotNull;
2023-05-27 04:07:50 +08:00
import static org.junit.jupiter.api.Assertions.assertTrue;
2023-06-05 18:31:51 +08:00
import static xyz.zhouxy.plusone.commons.jdbc.SQL.*;
2023-05-27 04:07:50 +08:00
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Splitter;
import com.google.common.io.Files;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import xyz.zhouxy.plusone.commons.jdbc.DbRecord;
import xyz.zhouxy.plusone.commons.jdbc.SimpleJdbcTemplate;
class SimpleJdbcTemplateTests {
private static final Logger log = LoggerFactory.getLogger(SimpleJdbcTemplateTests.class);
DataSource dataSource;
String[] cStruct = {
"id",
"created_by",
"create_time",
"updated_by",
"update_time",
"status"
};
SimpleJdbcTemplateTests() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/plusone");
config.setUsername("postgres");
config.setPassword("zhouxy108");
this.dataSource = new HikariDataSource(config);
}
@Test
void testQuery() throws SQLException {
try (Connection conn = this.dataSource.getConnection()) {
2023-06-05 18:31:51 +08:00
Object[] params = MoreArrays.asObjectArray("501533", "501554", "544599");
String sql = newSql()
.SELECT("*")
.FROM("test_table")
.WHERE(NOT_IN("id", params))
.toString();
log.info(sql);
2023-05-27 04:07:50 +08:00
List<DbRecord> rs = SimpleJdbcTemplate.connect(conn)
2023-06-05 18:31:51 +08:00
.queryToRecordList(sql, params);
assertNotNull(rs);
2023-05-27 04:07:50 +08:00
for (DbRecord baseEntity : rs) {
2023-06-05 18:31:51 +08:00
// log.info("id: {}", baseEntity.getValueAsString("id"));
log.info(baseEntity.toString());
2023-05-27 04:07:50 +08:00
assertEquals(Optional.empty(), baseEntity.getValueAsString("updated_by"));
}
}
}
@Test
void testSaveTxt() throws IOException, SQLException {
File file = new File("C:\\Users\\zhouxy\\Desktop", "Untitled-1.txt");
assertTrue(file.exists());
List<String> recordStrList = Files.readLines(file, StandardCharsets.UTF_8);
if (MoreCollections.isEmpty(recordStrList)) {
log.info("file is empty.");
return;
}
List<DbRecord> recordList = new ArrayList<>(recordStrList.size());
for (String rStr : recordStrList) {
List<String> props = Splitter.on("|").splitToList(rStr)
.stream()
.map(s -> {
if (s == null) {
return null;
}
s = s.trim();
return ("".equals(s) || "null".equals(s) || "NULL".equals(s)) ? null : s;
})
.collect(Collectors.toList());
DbRecord r = new DbRecord();
for (int i = 0; i < cStruct.length; i++) {
r.put(cStruct[i], props.get(i));
}
recordList.add(r);
}
final String sql = "INSERT INTO test_table(id, created_by, create_time, updated_by, update_time, status) VALUES(?, ?, ?, ?, ?, ?)";
final List<Object[]> params = recordList.stream()
.map(r -> new Object[] {
OptionalUtil.orElseNull(r.getValueAsString("id")),
OptionalUtil.orElseNull(r.getValueAsString("created_by")),
OptionalUtil.orElseNull(r.getValueAsString("create_time")),
OptionalUtil.orElseNull(r.getValueAsString("updated_by")),
OptionalUtil.orElseNull(r.getValueAsString("update_time")),
OptionalUtil.orElseNull(r.getValueAsString("status"))
2023-05-27 04:07:50 +08:00
})
.collect(Collectors.toList());
try (Connection conn = this.dataSource.getConnection()) {
SimpleJdbcTemplate.connect(conn).tx(() -> {
SimpleJdbcTemplate.connect(conn).batchUpdate(sql, params, 20);
File targetFile = new File("C:\\Users\\zhouxy\\Desktop\\done", file.getName());
boolean moveSucceeded = file.renameTo(targetFile);
if (!moveSucceeded) {
throw new IOException("文件移动失败:" + file.getName());
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}