From e35e93b55a1ae31141646572f9e695d5b7e6d701 Mon Sep 17 00:00:00 2001 From: Fantasy Date: Thu, 21 Nov 2024 16:10:47 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E5=AE=8C=E6=88=90#4"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 090ac59fc11a649e26d14571086244010c7d4435. --- .../fantasy0v0/swift/jdbc/InsertBuilder.java | 48 +++++++++++---- .../github/fantasy0v0/swift/jdbc/JDBC.java | 5 +- .../github/fantasy0v0/swift/jdbc/Utils.java | 3 +- jdbc/src/test/java/test/InsertTest.java | 61 +++---------------- jdbc/src/test/java/test/SelectTest.java | 2 + jdbc/src/test/java/test/TransactionTest.java | 12 +++- .../test/container/ContainerExecutable.java | 10 +++ .../java/test/container/ContainerUtil.java | 32 ++++++++++ .../java/test/container/JdbcExecutable.java | 11 ++++ .../test/java/test/container/JdbcTest.java | 10 +++ .../test/java/test/jdbc/TransactionTest.java | 5 ++ 11 files changed, 131 insertions(+), 68 deletions(-) create mode 100644 jdbc/src/test/java/test/container/ContainerExecutable.java create mode 100644 jdbc/src/test/java/test/container/JdbcExecutable.java create mode 100644 jdbc/src/test/java/test/container/JdbcTest.java diff --git a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/InsertBuilder.java b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/InsertBuilder.java index 9953a18..8cf06a6 100644 --- a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/InsertBuilder.java +++ b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/InsertBuilder.java @@ -6,8 +6,10 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.text.NumberFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -34,8 +36,8 @@ public List batch(List> batchParams, FetchMapper keyMappe } } - private T _fetchKey(FetchMapper mapper, - List params) { + private List _fetchKey(FetchMapper mapper, + List params, boolean firstOnly) { try (ConnectionReference ref = ConnectionPoolUtil.getReference(dataSource)) { Connection conn = ref.unwrap(); LogUtil.performance().info("fetchKey begin"); @@ -46,8 +48,7 @@ private T _fetchKey(FetchMapper mapper, fillStatementParams(conn, statement, params, parameterHandler); int updated = statement.executeUpdate(); LogUtil.sql().debug("executeUpdate: {}", updated); - List list = fetchByResultSet(statement.getGeneratedKeys(), mapper, true); - return list.isEmpty() ? null : list.getFirst(); + return fetchByResultSet(statement.getGeneratedKeys(), mapper, firstOnly); } finally { long cost = System.nanoTime() / 1000 - startTime; NumberFormat format = NumberFormat.getNumberInstance(); @@ -58,27 +59,52 @@ private T _fetchKey(FetchMapper mapper, } } - public T fetchKey(FetchMapper mapper, List params) { - return _fetchKey(mapper, params); + public List fetchKey(FetchMapper mapper, List params) { + return _fetchKey(mapper, params, false); } - public T fetchKey(FetchMapper mapper, Object... params) { + public List fetchKey(FetchMapper mapper, Object... params) { return fetchKey(mapper, Arrays.stream(params).toList()); } - public T fetchKey(FetchMapper mapper) { + public List fetchKey(FetchMapper mapper) { return fetchKey(mapper, (List) null); } - public Object[] fetchKey(List params) { + public List fetchKey(List params) { return fetchKey(Utils::fetchByRow, params); } - public Object[] fetchKey(Object... params) { + public List fetchKey(Object... params) { return fetchKey(Arrays.stream(params).toList()); } - public Object[] fetchKey() { + public List fetchKey() { return fetchKey(Utils::fetchByRow, (List) null); } + + public T fetchKeyOne(FetchMapper mapper, List params) { + List list = _fetchKey(mapper, params, true); + return list.isEmpty() ? null : list.getFirst(); + } + + public T fetchKeyOne(FetchMapper mapper, Object... params) { + return fetchKeyOne(mapper, Arrays.stream(params).toList()); + } + + public T fetchKeyOne(FetchMapper mapper) { + return fetchKeyOne(mapper, (List) null); + } + + public Object[] fetchKeyOne(List params) { + return fetchKeyOne(Utils::fetchByRow, params); + } + + public Object[] fetchKeyOne(Object... params) { + return fetchKeyOne(Arrays.stream(params).toList()); + } + + public Object[] fetchKeyOne() { + return fetchKeyOne((List) null); + } } diff --git a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/JDBC.java b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/JDBC.java index b7ecff9..1746086 100644 --- a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/JDBC.java +++ b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/JDBC.java @@ -108,8 +108,9 @@ public static SelectBuilder select(String sql, Object... params) { return select(sql, Arrays.stream(params).toList()); } - public static InsertBuilder insert(String sql) { - return new InsertBuilder(requireNonNull(dataSource), statementConfiguration, sql.trim()); + @Deprecated + public static ModifyBuilder modify(String sql) { + return new ModifyBuilder(requireNonNull(dataSource), sql.trim()); } public static UpdateBuilder update(String sql) { diff --git a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java index a82d062..bf60847 100644 --- a/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java +++ b/jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Utils.java @@ -148,8 +148,7 @@ static List executeBatch(Connection conn, StatementConfiguration statemen fillStatementParams(conn, statement, params, parameterHandler); statement.addBatch(); } - int[] result = statement.executeBatch(); - LogUtil.sql().debug("executeBatch: {}", result.length); + statement.executeBatch(); return fetchByResultSet(statement.getGeneratedKeys(), mapper, false); } finally { long cost = System.nanoTime() / 1000 - startTime; diff --git a/jdbc/src/test/java/test/InsertTest.java b/jdbc/src/test/java/test/InsertTest.java index d34da87..23c779b 100644 --- a/jdbc/src/test/java/test/InsertTest.java +++ b/jdbc/src/test/java/test/InsertTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import test.container.ContainerUtil; +import test.container.JdbcContainer; import test.container.SwiftJdbcExtension; import test.exception.WorkException; @@ -28,18 +30,18 @@ public class InsertTest { void test() { Assertions.assertThrowsExactly(WorkException.class, () -> { transaction(() -> { - int executed = JDBC.insert(""" + int executed = JDBC.modify(""" insert into student(id, name, status) values(1000, '测试学生', 0)""").execute(); Assertions.assertEquals(1, executed); - executed = JDBC.insert(""" + executed = JDBC.modify(""" insert into student(id, name, status) values(?, ?, ?)""").execute(1001, "测试学生", 0); Assertions.assertEquals(1, executed); // test null - executed = JDBC.insert(""" + executed = JDBC.modify(""" insert into student(id, name, status, ext) values(?, ?, ?, ?)""").execute(1002, "测试学生", 0, null); Assertions.assertEquals(1, executed); @@ -63,35 +65,20 @@ void testBatch() { batchParams.add(List.of(1004, "测试用户5", 4)); batchParams.add(List.of(1005, "测试用户6", 5)); - int[] executed = JDBC.insert(""" + int[] executed = JDBC.modify(""" insert into student(id, name, status) - values(?, ?, ?)""").batch(batchParams); + values(?, ?, ?)""").executeBatch(batchParams); Assertions.assertEquals(6, executed.length); for (int i : executed) { Assertions.assertEquals(1, i); } - - batchParams = new ArrayList<>(); - batchParams.add(List.of("测试用户1", 0)); - batchParams.add(List.of("测试用户2", 1)); - batchParams.add(List.of("测试用户3", 2)); - batchParams.add(List.of("测试用户4", 3)); - batchParams.add(List.of("测试用户5", 4)); - batchParams.add(List.of("测试用户6", 5)); - List keys = JDBC.insert(""" - insert into swift_user(name, status) values(?, ?) - """).batch(batchParams, row -> row.getLong(1)); - Assertions.assertEquals(6, executed.length); - for (long key : keys) { - Assertions.assertTrue(key > 0); - } } @TestTemplate void fetch(DataSource dataSource) throws SQLException { String driverClassName = dataSource.unwrap(HikariDataSource.class).getDriverClassName(); if (driverClassName.contains("postgresql")) { - List result = JDBC.insert(""" + List result = JDBC.modify(""" insert into student(id, name, status) values(?, ?, ?) returning id""").fetch(1000L, "测试学生", 0); @@ -106,45 +93,17 @@ void testDateTime(DataSource dataSource) throws SQLException { if (driverClassName.contains("postgresql")) { OffsetDateTime offsetDateTime = OffsetDateTime.of(1500, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); LocalDateTime localDateTime = LocalDateTime.of(1600, 1, 1, 0, 0, 0, 0); - List objects = JDBC.insert(""" + List objects = JDBC.modify(""" insert into datetime_test(id, date) values(?, ?) returning date """).fetch(1, offsetDateTime); log.debug("value: {}", objects.getFirst()[0]); - objects = JDBC.insert(""" + objects = JDBC.modify(""" insert into datetime_test(id, date) values(?, ?) returning date """).fetch(1, localDateTime); log.debug("value: {}", objects.getFirst()[0]); } } - - @TestTemplate - void testFetchKey(DataSource dataSource) { - long key = JDBC.insert(""" - insert into swift_user(name, status) values('测试学生', 0) - """).fetchKey(row -> row.getLong(1)); - log.debug("key: {}", key); - Assertions.assertTrue(key > 0); - - key = JDBC.insert(""" - insert into swift_user(name, status) values(?, ?) - """).fetchKey(row -> row.getLong(1), "测试学生1", 1); - Assertions.assertTrue(key > 0); - - Object[] row = JDBC.insert(""" - insert into swift_user(name, status) values(?, ?) - """).fetchKey( "测试学生2", 2); - // pg会返回整行数据 - Assertions.assertTrue(row.length > 0); - Assertions.assertTrue(((Number)row[0]).longValue() > 0); - - row = JDBC.insert(""" - insert into swift_user(name, status) values('测试学生3', 2) - """).fetchKey(); - // pg会返回整行数据 - Assertions.assertTrue(row.length > 0); - Assertions.assertTrue(((Number)row[0]).longValue() > 0); - } } diff --git a/jdbc/src/test/java/test/SelectTest.java b/jdbc/src/test/java/test/SelectTest.java index 3da43c7..a46cb3e 100644 --- a/jdbc/src/test/java/test/SelectTest.java +++ b/jdbc/src/test/java/test/SelectTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import test.container.ContainerUtil; +import test.container.JdbcTest; import test.container.SwiftJdbcExtension; import test.vo.Student; diff --git a/jdbc/src/test/java/test/TransactionTest.java b/jdbc/src/test/java/test/TransactionTest.java index 2a32810..4b43e9e 100644 --- a/jdbc/src/test/java/test/TransactionTest.java +++ b/jdbc/src/test/java/test/TransactionTest.java @@ -1,11 +1,19 @@ package test; +import com.github.fantasy0v0.swift.jdbc.JDBC; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import test.container.ContainerUtil; +import test.container.JdbcContainer; import test.container.SwiftJdbcExtension; +import javax.sql.DataSource; + import static com.github.fantasy0v0.swift.jdbc.JDBC.*; @ExtendWith(SwiftJdbcExtension.class) @@ -32,7 +40,7 @@ void test() { transaction(() -> { select("select * from student").fetch(); transaction(() -> { - update("update student set name = ? where id = ?") + modify("update student set name = ? where id = ?") .execute("修改", 1L); }); }); @@ -42,7 +50,7 @@ void test() { @TestTemplate void rollback() { transaction(() -> { - update("update student set name = ? where id = ?") + modify("update student set name = ? where id = ?") .execute("修改", 1L); }); } diff --git a/jdbc/src/test/java/test/container/ContainerExecutable.java b/jdbc/src/test/java/test/container/ContainerExecutable.java new file mode 100644 index 0000000..12328a1 --- /dev/null +++ b/jdbc/src/test/java/test/container/ContainerExecutable.java @@ -0,0 +1,10 @@ +package test.container; + +import java.util.List; + +@FunctionalInterface +public interface ContainerExecutable { + + List execute(); + +} diff --git a/jdbc/src/test/java/test/container/ContainerUtil.java b/jdbc/src/test/java/test/container/ContainerUtil.java index 50a5508..fa39ecc 100644 --- a/jdbc/src/test/java/test/container/ContainerUtil.java +++ b/jdbc/src/test/java/test/container/ContainerUtil.java @@ -1,12 +1,22 @@ package test.container; +import com.github.fantasy0v0.swift.jdbc.JDBC; +import org.junit.jupiter.api.DynamicTest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testcontainers.containers.MySQLContainer; import org.testcontainers.containers.PostgreSQLContainer; +import javax.sql.DataSource; +import java.util.ArrayList; import java.util.List; +import static org.junit.jupiter.api.DynamicTest.dynamicTest; + public final class ContainerUtil { + private static final Logger log = LoggerFactory.getLogger(ContainerUtil.class); + public static final PostgreSQLContainer PG = new PostgreSQLContainer<>("postgres:16-alpine"); public static final String PG_LOCATIONS = "classpath:db/pg"; @@ -20,4 +30,26 @@ public final class ContainerUtil { JdbcContainer.create(MYSQL, MYSQL_LOCATIONS) ); + public static List testAllContainers(ContainerExecutable executable) { + List tests = new ArrayList<>(); + for (JdbcContainer container : ContainerUtil.containers) { + String name = container.getDriverClassName(); + tests.addAll( + executable.execute() + .stream() + .map(test -> dynamicTest(name + " " + test.name(), () -> { + DataSource dataSource = container.start(); + JDBC.configuration(dataSource); + try { + test.executable().execute(dataSource); + } finally { + container.stop(); + } + })) + .toList() + ); + } + return tests; + } + } diff --git a/jdbc/src/test/java/test/container/JdbcExecutable.java b/jdbc/src/test/java/test/container/JdbcExecutable.java new file mode 100644 index 0000000..9db67a4 --- /dev/null +++ b/jdbc/src/test/java/test/container/JdbcExecutable.java @@ -0,0 +1,11 @@ +package test.container; + +import javax.sql.DataSource; +import java.sql.SQLException; + +@FunctionalInterface +public interface JdbcExecutable { + + void execute(DataSource dataSource) throws SQLException; + +} diff --git a/jdbc/src/test/java/test/container/JdbcTest.java b/jdbc/src/test/java/test/container/JdbcTest.java new file mode 100644 index 0000000..4577a7b --- /dev/null +++ b/jdbc/src/test/java/test/container/JdbcTest.java @@ -0,0 +1,10 @@ +package test.container; + +public record JdbcTest(String name, + JdbcExecutable executable) { + + public static JdbcTest of(String name, JdbcExecutable executable) { + return new JdbcTest(name, executable); + } + +} diff --git a/jdbc/src/test/java/test/jdbc/TransactionTest.java b/jdbc/src/test/java/test/jdbc/TransactionTest.java index a6373fe..c4168ea 100644 --- a/jdbc/src/test/java/test/jdbc/TransactionTest.java +++ b/jdbc/src/test/java/test/jdbc/TransactionTest.java @@ -3,15 +3,20 @@ import com.github.fantasy0v0.swift.jdbc.exception.SwiftSQLException; import com.zaxxer.hikari.HikariDataSource; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import test.container.ContainerUtil; +import test.container.JdbcTest; import test.container.SwiftJdbcExtension; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; +import java.util.List; import static com.github.fantasy0v0.swift.jdbc.JDBC.*;