From 0d233a93f3affd626b2f7c626d641c983ade111a Mon Sep 17 00:00:00 2001 From: linghengqian Date: Fri, 10 Nov 2023 17:58:03 +0800 Subject: [PATCH] Fixes checkstyle --- .../infra/nativetest/FileTestUtils.java | 7 +- .../nativetest/jdbc/features/EncryptTest.java | 22 +++---- .../nativetest/jdbc/features/MaskTest.java | 22 +++---- .../jdbc/features/ReadWriteSplittingTest.java | 20 +++--- .../nativetest/jdbc/features/ShadowTest.java | 22 +++---- .../jdbc/features/ShardingTest.java | 22 +++---- ...QueryAssistedShardingEncryptAlgorithm.java | 2 +- .../jdbc/features/entity/Address.java | 4 +- .../jdbc/features/entity/Order.java | 4 +- .../jdbc/features/entity/OrderItem.java | 1 + .../repository/AddressRepository.java | 29 +++++++++ .../repository/OrderItemRepository.java | 48 +++++++++++++- .../features/repository/OrderRepository.java | 65 ++++++++++++++++++- 13 files changed, 194 insertions(+), 74 deletions(-) diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/FileTestUtils.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/FileTestUtils.java index 6e80670771e578..30768fb598a657 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/FileTestUtils.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/FileTestUtils.java @@ -37,11 +37,16 @@ */ public class FileTestUtils { + /** + * read file From file URL string. + * @param fileUrl fileUrl + * @return byte array + */ public static byte[] readFromFileURLString(final String fileUrl) { return readInputStream(ClassLoader.getSystemResourceAsStream(fileUrl)).getBytes(StandardCharsets.UTF_8); } - private static String readInputStream(InputStream is) { + private static String readInputStream(final InputStream is) { StringBuilder out = new StringBuilder(); try ( InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8); diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/EncryptTest.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/EncryptTest.java index a5a0dec01e3f10..4fd0146d782453 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/EncryptTest.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/EncryptTest.java @@ -49,18 +49,14 @@ public final class EncryptTest { private AddressRepository addressRepository; @Test - public void testEncryptInLocalTransactions() throws SQLException, IOException { - try { - DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/encrypt.yaml")); - orderRepository = new OrderRepository(dataSource); - orderItemRepository = new OrderItemRepository(dataSource); - addressRepository = new AddressRepository(dataSource); - this.initEnvironment(); - this.processSuccess(); - this.cleanEnvironment(); - } finally { - // this.cleanEnvironment(); - } + void testEncryptInLocalTransactions() throws SQLException, IOException { + DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/encrypt.yaml")); + orderRepository = new OrderRepository(dataSource); + orderItemRepository = new OrderItemRepository(dataSource); + addressRepository = new AddressRepository(dataSource); + this.initEnvironment(); + this.processSuccess(); + this.cleanEnvironment(); } private void initEnvironment() throws SQLException { @@ -73,7 +69,7 @@ private void initEnvironment() throws SQLException { } private void processSuccess() throws SQLException { - List orderIds = insertData(); + final List orderIds = insertData(); assertThat(orderRepository.selectAll(), equalTo(IntStream.range(1, 11).mapToObj(i -> new Order(i, i % 2, i, i, "INSERT_TEST")).collect(Collectors.toList()))); assertThat(orderItemRepository.selectAll(), diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/MaskTest.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/MaskTest.java index 5023952eb87328..18b11bf6af87d4 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/MaskTest.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/MaskTest.java @@ -48,18 +48,14 @@ public final class MaskTest { private AddressRepository addressRepository; @Test - public void testMaskInLocalTransactions() throws SQLException, IOException { - try { - DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/mask.yaml")); - orderRepository = new OrderRepository(dataSource); - orderItemRepository = new OrderItemRepository(dataSource); - addressRepository = new AddressRepository(dataSource); - this.initEnvironment(); - this.processSuccess(); - this.cleanEnvironment(); - } finally { - // this.cleanEnvironment(); - } + void testMaskInLocalTransactions() throws SQLException, IOException { + DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/mask.yaml")); + orderRepository = new OrderRepository(dataSource); + orderItemRepository = new OrderItemRepository(dataSource); + addressRepository = new AddressRepository(dataSource); + this.initEnvironment(); + this.processSuccess(); + this.cleanEnvironment(); } private void initEnvironment() throws SQLException { @@ -72,7 +68,7 @@ private void initEnvironment() throws SQLException { } private void processSuccess() throws SQLException { - List orderIds = insertData(); + final List orderIds = insertData(); assertThat(orderRepository.selectAll(), equalTo(IntStream.range(1, 11).mapToObj(i -> new Order(i, i % 2, i, i, "INSERT_TEST")).collect(Collectors.toList()))); assertThat(orderItemRepository.selectAll(), diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ReadWriteSplittingTest.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ReadWriteSplittingTest.java index 53100daa681882..566adbc091572a 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ReadWriteSplittingTest.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ReadWriteSplittingTest.java @@ -49,18 +49,14 @@ public final class ReadWriteSplittingTest { private AddressRepository addressRepository; @Test - public void testReadWriteSplittingInLocalTransactions() throws SQLException, IOException { - try { - DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/readwrite-splitting.yaml")); - orderRepository = new OrderRepository(dataSource); - orderItemRepository = new OrderItemRepository(dataSource); - addressRepository = new AddressRepository(dataSource); - this.initEnvironment(); - this.processSuccess(); - this.cleanEnvironment(); - } finally { - // this.cleanEnvironment(); - } + void testReadWriteSplittingInLocalTransactions() throws SQLException, IOException { + DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/readwrite-splitting.yaml")); + orderRepository = new OrderRepository(dataSource); + orderItemRepository = new OrderItemRepository(dataSource); + addressRepository = new AddressRepository(dataSource); + this.initEnvironment(); + this.processSuccess(); + this.cleanEnvironment(); } private void initEnvironment() throws SQLException { diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShadowTest.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShadowTest.java index ec672f899f2d80..34eef534fee1df 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShadowTest.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShadowTest.java @@ -49,18 +49,14 @@ public final class ShadowTest { private AddressRepository addressRepository; @Test - public void testShadowInLocalTransactions() throws SQLException, IOException { - try { - DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/shadow.yaml")); - orderRepository = new OrderRepository(dataSource); - orderItemRepository = new OrderItemRepository(dataSource); - addressRepository = new AddressRepository(dataSource); - this.initEnvironment(); - this.processSuccess(); - this.cleanEnvironment(); - } finally { - // this.cleanEnvironment(); - } + void testShadowInLocalTransactions() throws SQLException, IOException { + DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/shadow.yaml")); + orderRepository = new OrderRepository(dataSource); + orderItemRepository = new OrderItemRepository(dataSource); + addressRepository = new AddressRepository(dataSource); + this.initEnvironment(); + this.processSuccess(); + this.cleanEnvironment(); } private void initEnvironment() throws SQLException { @@ -75,7 +71,7 @@ private void initEnvironment() throws SQLException { } private void processSuccess() throws SQLException { - List orderIds = insertData(); + final List orderIds = insertData(); assertThat(this.selectAll(), equalTo(Arrays.asList( new Order(1, 0, 2, 2, "INSERT_TEST"), new Order(2, 0, 4, 4, "INSERT_TEST"), diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShardingTest.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShardingTest.java index 376b88aeef12ac..8e07d05a4b58f4 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShardingTest.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/ShardingTest.java @@ -49,18 +49,14 @@ public final class ShardingTest { private AddressRepository addressRepository; @Test - public void testShardingInLocalTransactions() throws SQLException, IOException { - try { - DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/sharding.yaml")); - orderRepository = new OrderRepository(dataSource); - orderItemRepository = new OrderItemRepository(dataSource); - addressRepository = new AddressRepository(dataSource); - this.initEnvironment(); - this.processSuccess(); - this.cleanEnvironment(); - } finally { - // this.cleanEnvironment(); - } + void testShardingInLocalTransactions() throws SQLException, IOException { + DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/sharding.yaml")); + orderRepository = new OrderRepository(dataSource); + orderItemRepository = new OrderItemRepository(dataSource); + addressRepository = new AddressRepository(dataSource); + this.initEnvironment(); + this.processSuccess(); + this.cleanEnvironment(); } private void initEnvironment() throws SQLException { @@ -73,7 +69,7 @@ private void initEnvironment() throws SQLException { } private void processSuccess() throws SQLException { - List orderIds = insertData(); + final List orderIds = insertData(); List orders = orderRepository.selectAll(); assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()), diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/TestQueryAssistedShardingEncryptAlgorithm.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/TestQueryAssistedShardingEncryptAlgorithm.java index 529a82d02e54cd..c985b8fd19ada3 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/TestQueryAssistedShardingEncryptAlgorithm.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/TestQueryAssistedShardingEncryptAlgorithm.java @@ -39,7 +39,7 @@ public String getType() { } @Override - public void init(Properties props) { + public void init(final Properties props) { this.props = props; } } diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Address.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Address.java index c82d7867cb197f..530e1eb6d86c69 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Address.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Address.java @@ -17,7 +17,9 @@ package org.apache.shardingsphere.infra.nativetest.jdbc.features.entity; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; import java.io.Serializable; diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Order.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Order.java index 5d1962aaa47e74..751889802f8bc7 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Order.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/Order.java @@ -17,7 +17,9 @@ package org.apache.shardingsphere.infra.nativetest.jdbc.features.entity; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; import java.io.Serializable; diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/OrderItem.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/OrderItem.java index 23ed77db9a319d..3c4a00c691f466 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/OrderItem.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/entity/OrderItem.java @@ -22,6 +22,7 @@ import lombok.NoArgsConstructor; import java.io.Serializable; + @Data @AllArgsConstructor @NoArgsConstructor diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/AddressRepository.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/AddressRepository.java index f6833ca5833321..65806f92e12cfe 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/AddressRepository.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/AddressRepository.java @@ -28,6 +28,7 @@ import java.util.LinkedList; import java.util.List; +@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"}) public final class AddressRepository { private final DataSource dataSource; @@ -36,6 +37,10 @@ public AddressRepository(final DataSource dataSource) { this.dataSource = dataSource; } + /** + * create table t_address if not exists. + * @throws SQLException SQL exception + */ public void createTableIfNotExists() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS t_address (address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT NULL, PRIMARY KEY (address_id))"; try ( @@ -45,6 +50,10 @@ public void createTableIfNotExists() throws SQLException { } } + /** + * drop table t_address. + * @throws SQLException SQL exception + */ public void dropTable() throws SQLException { String sql = "DROP TABLE IF EXISTS t_address"; try ( @@ -54,6 +63,10 @@ public void dropTable() throws SQLException { } } + /** + * truncate table t_address. + * @throws SQLException SQL exception + */ public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_address"; try ( @@ -63,6 +76,12 @@ public void truncateTable() throws SQLException { } } + /** + * insert something to table t_address. + * @param address address + * @return addressId of the insert statement + * @throws SQLException SQL exception + */ public Long insert(final Address address) throws SQLException { String sql = "INSERT INTO t_address (address_id, address_name) VALUES (?, ?)"; try ( @@ -75,6 +94,11 @@ public Long insert(final Address address) throws SQLException { return address.getAddressId(); } + /** + * delete by id. + * @param id id + * @throws SQLException SQL exception + */ public void delete(final Long id) throws SQLException { String sql = "DELETE FROM t_address WHERE address_id=?"; try ( @@ -85,6 +109,11 @@ public void delete(final Long id) throws SQLException { } } + /** + * select all. + * @return list of address + * @throws SQLException SQL exception + */ public List
selectAll() throws SQLException { String sql = "SELECT * FROM t_address"; List
result = new LinkedList<>(); diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderItemRepository.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderItemRepository.java index cfb02f2b693f97..320a0786935df8 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderItemRepository.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderItemRepository.java @@ -28,6 +28,7 @@ import java.util.LinkedList; import java.util.List; +@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"}) public final class OrderItemRepository { private final DataSource dataSource; @@ -36,9 +37,13 @@ public OrderItemRepository(final DataSource dataSource) { this.dataSource = dataSource; } + /** + * create table if not exists. + * @throws SQLException SQL exception + */ public void createTableIfNotExists() throws SQLException { - String sql = "CREATE TABLE IF NOT EXISTS t_order_item " + - "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, phone VARCHAR(50), status VARCHAR(50), PRIMARY KEY (order_item_id))"; + String sql = "CREATE TABLE IF NOT EXISTS t_order_item " + + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, phone VARCHAR(50), status VARCHAR(50), PRIMARY KEY (order_item_id))"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -46,6 +51,10 @@ public void createTableIfNotExists() throws SQLException { } } + /** + * drop table. + * @throws SQLException SQL exception + */ public void dropTable() throws SQLException { String sql = "DROP TABLE IF EXISTS t_order_item"; try ( @@ -55,6 +64,10 @@ public void dropTable() throws SQLException { } } + /** + * truncate table. + * @throws SQLException SQL exception + */ public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_order_item"; try ( @@ -64,9 +77,14 @@ public void truncateTable() throws SQLException { } } + /** + * create shadow table if not exists. + * @throws SQLException SQL exception + */ public void createTableIfNotExistsShadow() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS t_order_item " - + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, phone VARCHAR(50), status VARCHAR(50), PRIMARY KEY (order_item_id)) /* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; + + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, phone " + + "VARCHAR(50), status VARCHAR(50), PRIMARY KEY (order_item_id)) /* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -74,6 +92,10 @@ public void createTableIfNotExistsShadow() throws SQLException { } } + /** + * drop shadow table. + * @throws SQLException SQL exception + */ public void dropTableShadow() throws SQLException { String sql = "DROP TABLE t_order_item /* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; try ( @@ -83,6 +105,10 @@ public void dropTableShadow() throws SQLException { } } + /** + * truncate shadow table. + * @throws SQLException SQL exception + */ public void truncateTableShadow() throws SQLException { String sql = "TRUNCATE TABLE t_order_item /* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; try ( @@ -92,6 +118,12 @@ public void truncateTableShadow() throws SQLException { } } + /** + * insert something to table. + * @param orderItem orderItem + * @return orderItemId of the insert statement + * @throws SQLException SQL exception + */ public Long insert(final OrderItem orderItem) throws SQLException { String sql = "INSERT INTO t_order_item (order_id, user_id, phone, status) VALUES (?, ?, ?, ?)"; try ( @@ -111,6 +143,11 @@ public Long insert(final OrderItem orderItem) throws SQLException { return orderItem.getOrderItemId(); } + /** + * delete by orderItemId. + * @param orderItemId orderItemId + * @throws SQLException SQL exception + */ public void delete(final Long orderItemId) throws SQLException { String sql = "DELETE FROM t_order_item WHERE order_id=?"; try ( @@ -121,6 +158,11 @@ public void delete(final Long orderItemId) throws SQLException { } } + /** + * select all. + * @return list of OrderItem + * @throws SQLException SQL exception + */ public List selectAll() throws SQLException { String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id"; List result = new LinkedList<>(); diff --git a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderRepository.java b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderRepository.java index 22d597ebf434c1..564db2f9cfc267 100644 --- a/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderRepository.java +++ b/infra/nativetest/src/test/java/org/apache/shardingsphere/infra/nativetest/jdbc/features/repository/OrderRepository.java @@ -28,6 +28,7 @@ import java.util.LinkedList; import java.util.List; +@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"}) public final class OrderRepository { private final DataSource dataSource; @@ -36,9 +37,13 @@ public OrderRepository(final DataSource dataSource) { this.dataSource = dataSource; } + /** + * create table if not exists. + * @throws SQLException SQL exception + */ public void createTableIfNotExists() throws SQLException { - String sql = "CREATE TABLE IF NOT EXISTS t_order " + - "(order_id BIGINT NOT NULL AUTO_INCREMENT, order_type INT(11), user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))"; + String sql = "CREATE TABLE IF NOT EXISTS t_order " + + "(order_id BIGINT NOT NULL AUTO_INCREMENT, order_type INT(11), user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -46,6 +51,10 @@ public void createTableIfNotExists() throws SQLException { } } + /** + * drop table. + * @throws SQLException SQL exception + */ public void dropTable() throws SQLException { // todo lingh fix in shadow String sql = "DROP TABLE IF EXISTS t_order"; @@ -56,6 +65,10 @@ public void dropTable() throws SQLException { } } + /** + * truncate table. + * @throws SQLException SQL exception + */ public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_order"; try ( @@ -65,9 +78,15 @@ public void truncateTable() throws SQLException { } } + /** + * create shadow table if not exists. + * @throws SQLException SQL exception + */ public void createTableIfNotExistsShadow() throws SQLException { String sql = - "CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, order_type INT(11), user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)) /* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; + "CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, order_type INT(11), " + + "user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)) " + + "/* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -75,6 +94,10 @@ public void createTableIfNotExistsShadow() throws SQLException { } } + /** + * drop shadow table. + * @throws SQLException SQL exception + */ public void dropTableShadow() throws SQLException { String sql = "DROP TABLE IF EXISTS t_order /* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; try ( @@ -84,6 +107,10 @@ public void dropTableShadow() throws SQLException { } } + /** + * truncate shadow table. + * @throws SQLException SQL exception + */ public void truncateTableShadow() throws SQLException { String sql = "TRUNCATE TABLE t_order /* SHARDINGSPHERE_HINT:shadow=true,foo=bar*/"; try ( @@ -93,11 +120,21 @@ public void truncateTableShadow() throws SQLException { } } + /** + * select Order from shadow table. + * @return list of Order + * @throws SQLException SQL exception + */ public List selectShadowOrder() throws SQLException { String sql = "SELECT * FROM t_order WHERE order_type=1"; return getOrders(sql); } + /** + * delete Order from shadow table. + * @param orderId orderId + * @throws SQLException SQL Exception + */ public void deleteShadow(final Long orderId) throws SQLException { String sql = "DELETE FROM t_order WHERE order_id=? AND order_type=1"; try ( @@ -108,6 +145,12 @@ public void deleteShadow(final Long orderId) throws SQLException { } } + /** + * insert Order to table. + * @param order order + * @return orderId of the insert statement + * @throws SQLException SQL Exception + */ public Long insert(final Order order) throws SQLException { String sql = "INSERT INTO t_order (user_id, order_type, address_id, status) VALUES (?, ?, ?, ?)"; try ( @@ -127,6 +170,11 @@ public Long insert(final Order order) throws SQLException { return order.getOrderId(); } + /** + * delete by orderId. + * @param orderId orderId + * @throws SQLException SQL exception + */ public void delete(final Long orderId) throws SQLException { String sql = "DELETE FROM t_order WHERE order_id=?"; try ( @@ -137,10 +185,21 @@ public void delete(final Long orderId) throws SQLException { } } + /** + * select all. + * @return list of Order + * @throws SQLException SQL exception + */ public List selectAll() throws SQLException { return getOrders("SELECT * FROM t_order"); } + /** + * get Orders by SQL. + * @param sql SQL + * @return list of Order + * @throws SQLException SQL exception + */ private List getOrders(final String sql) throws SQLException { List result = new LinkedList<>(); try (