Loading example-common/repository-api/src/main/java/org/apache/shardingsphere/example/common/repository/CommonRepository.java +24 −11 Original line number Diff line number Diff line Loading @@ -17,41 +17,54 @@ package org.apache.shardingsphere.example.common.repository; import java.sql.SQLException; import java.util.List; public interface CommonRepository<T, P> { /** * Create table if not exist. * * @throws SQLException SQL exception */ void createTableIfNotExists(); void createTableIfNotExists() throws SQLException; /** * Drop table. * * @throws SQLException SQL exception */ void dropTable(); void dropTable() throws SQLException; /** * Truncate table. * * @throws SQLException SQL exception */ void truncateTable(); void truncateTable() throws SQLException; /** * insert one entity. * insert data. * * @param entity entity * @return count or primary key * @throws SQLException SQL exception */ Long insert(T entity); Long insert(T entity) throws SQLException; /** * Do delete. * @param key key * Delete data. * * @param primaryKey primaryKey * @throws SQLException SQL exception */ void delete(P key); void delete(P primaryKey) throws SQLException; /** * select all. * @return list of entity * Select all data. * * @return all data * @throws SQLException SQL exception */ List<T> selectAll(); List<T> selectAll() throws SQLException; } example-common/repository-api/src/main/java/org/apache/shardingsphere/example/common/service/CommonService.java +32 −6 Original line number Diff line number Diff line Loading @@ -17,16 +17,42 @@ package org.apache.shardingsphere.example.common.service; public interface CommonService { import java.sql.SQLException; void initEnvironment(); public interface CommonService { void cleanEnvironment(); /** * Initialize environment. * * @throws SQLException SQL exception */ void initEnvironment() throws SQLException; void processSuccess(); /** * Clean environment. * * @throws SQLException SQL exception */ void cleanEnvironment() throws SQLException; void processFailure(); /** * Process success. * * @throws SQLException SQL exception */ void processSuccess() throws SQLException; void printData(); /** * Process failure. * * @throws SQLException SQL exception */ void processFailure() throws SQLException; /** * Print data. * * @throws SQLException SQL exception */ void printData() throws SQLException; } example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/CountryRepositoryImpl.java +17 −25 Original line number Diff line number Diff line Loading @@ -38,68 +38,61 @@ public final class CountryRepositoryImpl implements CountryRepository { } @Override public void createTableIfNotExists() { public void createTableIfNotExists() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS t_country (code VARCHAR(50), name VARCHAR(50), language VARCHAR(50), PRIMARY KEY (code))"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void dropTable() { public void dropTable() throws SQLException { String sql = "DROP TABLE t_country"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void truncateTable() { public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_country"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public Long insert(final Country country) { public Long insert(final Country country) throws SQLException { String sql = "INSERT INTO t_country (code, name, language) VALUES (?, ?, ?)"; int result = 0; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setString(1, country.getCode()); preparedStatement.setString(2, country.getName()); preparedStatement.setString(3, country.getLanguage()); result = preparedStatement.executeUpdate(); } catch (final SQLException ignored) { return new Integer(preparedStatement.executeUpdate()).longValue(); } return (long) result; } @Override public void delete(final String code) { public void delete(final String code) throws SQLException { String sql = "DELETE FROM t_country WHERE code =?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setString(1, code); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } } @Override public List<Country> selectAll() { public List<Country> selectAll() throws SQLException { String sql = "SELECT * FROM t_country"; return getCountries(sql); } private List<Country> getCountries(final String sql) { private List<Country> getCountries(final String sql) throws SQLException { List<Country> result = new LinkedList<>(); try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); Loading @@ -111,7 +104,6 @@ public final class CountryRepositoryImpl implements CountryRepository { country.setLanguage(resultSet.getString(3)); result.add(country); } } catch (final SQLException ignored) { } return result; } Loading example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/OrderItemRepositoryImpl.java +7 −13 Original line number Diff line number Diff line Loading @@ -38,38 +38,35 @@ public class OrderItemRepositoryImpl implements OrderItemRepository { } @Override public void createTableIfNotExists() { 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, status VARCHAR(50), PRIMARY KEY (order_item_id))"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void dropTable() { public void dropTable() throws SQLException { String sql = "DROP TABLE t_order_item"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void truncateTable() { public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_order_item"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public Long insert(final OrderItem orderItem) { public Long insert(final OrderItem orderItem) throws SQLException { String sql = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (?, ?, ?)"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { Loading @@ -82,29 +79,27 @@ public class OrderItemRepositoryImpl implements OrderItemRepository { orderItem.setOrderItemId(resultSet.getLong(1)); } } } catch (final SQLException ignored) { } return orderItem.getOrderItemId(); } @Override public void delete(final Long orderItemId) { public void delete(final Long orderItemId) throws SQLException { String sql = "DELETE FROM t_order_item WHERE order_item_id=?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setLong(1, orderItemId); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } } @Override public List<OrderItem> selectAll() { public List<OrderItem> selectAll() throws SQLException { String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id"; return getOrderItems(sql); } protected List<OrderItem> getOrderItems(final String sql) { protected List<OrderItem> getOrderItems(final String sql) throws SQLException { List<OrderItem> result = new LinkedList<>(); try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); Loading @@ -117,7 +112,6 @@ public class OrderItemRepositoryImpl implements OrderItemRepository { orderItem.setStatus(resultSet.getString(4)); result.add(orderItem); } } catch (final SQLException ignored) { } return result; } Loading example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/OrderRepositoryImpl.java +7 −13 Original line number Diff line number Diff line Loading @@ -38,37 +38,34 @@ public class OrderRepositoryImpl implements OrderRepository { } @Override public void createTableIfNotExists() { public void createTableIfNotExists() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void dropTable() { public void dropTable() throws SQLException { String sql = "DROP TABLE t_order"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void truncateTable() { public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_order"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public Long insert(final Order order) { public Long insert(final Order order) throws SQLException { String sql = "INSERT INTO t_order (user_id, status) VALUES (?, ?)"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { Loading @@ -80,29 +77,27 @@ public class OrderRepositoryImpl implements OrderRepository { order.setOrderId(resultSet.getLong(1)); } } } catch (final SQLException ignored) { } return order.getOrderId(); } @Override public void delete(final Long orderId) { public void delete(final Long orderId) throws SQLException { String sql = "DELETE FROM t_order WHERE order_id=?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setLong(1, orderId); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } } @Override public List<Order> selectAll() { public List<Order> selectAll() throws SQLException { String sql = "SELECT * FROM t_order"; return getOrders(sql); } protected List<Order> getOrders(final String sql) { protected List<Order> getOrders(final String sql) throws SQLException { List<Order> result = new LinkedList<>(); try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); Loading @@ -114,7 +109,6 @@ public class OrderRepositoryImpl implements OrderRepository { order.setStatus(resultSet.getString(3)); result.add(order); } } catch (final SQLException ignored) { } return result; } Loading Loading
example-common/repository-api/src/main/java/org/apache/shardingsphere/example/common/repository/CommonRepository.java +24 −11 Original line number Diff line number Diff line Loading @@ -17,41 +17,54 @@ package org.apache.shardingsphere.example.common.repository; import java.sql.SQLException; import java.util.List; public interface CommonRepository<T, P> { /** * Create table if not exist. * * @throws SQLException SQL exception */ void createTableIfNotExists(); void createTableIfNotExists() throws SQLException; /** * Drop table. * * @throws SQLException SQL exception */ void dropTable(); void dropTable() throws SQLException; /** * Truncate table. * * @throws SQLException SQL exception */ void truncateTable(); void truncateTable() throws SQLException; /** * insert one entity. * insert data. * * @param entity entity * @return count or primary key * @throws SQLException SQL exception */ Long insert(T entity); Long insert(T entity) throws SQLException; /** * Do delete. * @param key key * Delete data. * * @param primaryKey primaryKey * @throws SQLException SQL exception */ void delete(P key); void delete(P primaryKey) throws SQLException; /** * select all. * @return list of entity * Select all data. * * @return all data * @throws SQLException SQL exception */ List<T> selectAll(); List<T> selectAll() throws SQLException; }
example-common/repository-api/src/main/java/org/apache/shardingsphere/example/common/service/CommonService.java +32 −6 Original line number Diff line number Diff line Loading @@ -17,16 +17,42 @@ package org.apache.shardingsphere.example.common.service; public interface CommonService { import java.sql.SQLException; void initEnvironment(); public interface CommonService { void cleanEnvironment(); /** * Initialize environment. * * @throws SQLException SQL exception */ void initEnvironment() throws SQLException; void processSuccess(); /** * Clean environment. * * @throws SQLException SQL exception */ void cleanEnvironment() throws SQLException; void processFailure(); /** * Process success. * * @throws SQLException SQL exception */ void processSuccess() throws SQLException; void printData(); /** * Process failure. * * @throws SQLException SQL exception */ void processFailure() throws SQLException; /** * Print data. * * @throws SQLException SQL exception */ void printData() throws SQLException; }
example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/CountryRepositoryImpl.java +17 −25 Original line number Diff line number Diff line Loading @@ -38,68 +38,61 @@ public final class CountryRepositoryImpl implements CountryRepository { } @Override public void createTableIfNotExists() { public void createTableIfNotExists() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS t_country (code VARCHAR(50), name VARCHAR(50), language VARCHAR(50), PRIMARY KEY (code))"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void dropTable() { public void dropTable() throws SQLException { String sql = "DROP TABLE t_country"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void truncateTable() { public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_country"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public Long insert(final Country country) { public Long insert(final Country country) throws SQLException { String sql = "INSERT INTO t_country (code, name, language) VALUES (?, ?, ?)"; int result = 0; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setString(1, country.getCode()); preparedStatement.setString(2, country.getName()); preparedStatement.setString(3, country.getLanguage()); result = preparedStatement.executeUpdate(); } catch (final SQLException ignored) { return new Integer(preparedStatement.executeUpdate()).longValue(); } return (long) result; } @Override public void delete(final String code) { public void delete(final String code) throws SQLException { String sql = "DELETE FROM t_country WHERE code =?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setString(1, code); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } } @Override public List<Country> selectAll() { public List<Country> selectAll() throws SQLException { String sql = "SELECT * FROM t_country"; return getCountries(sql); } private List<Country> getCountries(final String sql) { private List<Country> getCountries(final String sql) throws SQLException { List<Country> result = new LinkedList<>(); try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); Loading @@ -111,7 +104,6 @@ public final class CountryRepositoryImpl implements CountryRepository { country.setLanguage(resultSet.getString(3)); result.add(country); } } catch (final SQLException ignored) { } return result; } Loading
example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/OrderItemRepositoryImpl.java +7 −13 Original line number Diff line number Diff line Loading @@ -38,38 +38,35 @@ public class OrderItemRepositoryImpl implements OrderItemRepository { } @Override public void createTableIfNotExists() { 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, status VARCHAR(50), PRIMARY KEY (order_item_id))"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void dropTable() { public void dropTable() throws SQLException { String sql = "DROP TABLE t_order_item"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void truncateTable() { public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_order_item"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public Long insert(final OrderItem orderItem) { public Long insert(final OrderItem orderItem) throws SQLException { String sql = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (?, ?, ?)"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { Loading @@ -82,29 +79,27 @@ public class OrderItemRepositoryImpl implements OrderItemRepository { orderItem.setOrderItemId(resultSet.getLong(1)); } } } catch (final SQLException ignored) { } return orderItem.getOrderItemId(); } @Override public void delete(final Long orderItemId) { public void delete(final Long orderItemId) throws SQLException { String sql = "DELETE FROM t_order_item WHERE order_item_id=?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setLong(1, orderItemId); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } } @Override public List<OrderItem> selectAll() { public List<OrderItem> selectAll() throws SQLException { String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id"; return getOrderItems(sql); } protected List<OrderItem> getOrderItems(final String sql) { protected List<OrderItem> getOrderItems(final String sql) throws SQLException { List<OrderItem> result = new LinkedList<>(); try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); Loading @@ -117,7 +112,6 @@ public class OrderItemRepositoryImpl implements OrderItemRepository { orderItem.setStatus(resultSet.getString(4)); result.add(orderItem); } } catch (final SQLException ignored) { } return result; } Loading
example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/OrderRepositoryImpl.java +7 −13 Original line number Diff line number Diff line Loading @@ -38,37 +38,34 @@ public class OrderRepositoryImpl implements OrderRepository { } @Override public void createTableIfNotExists() { public void createTableIfNotExists() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void dropTable() { public void dropTable() throws SQLException { String sql = "DROP TABLE t_order"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public void truncateTable() { public void truncateTable() throws SQLException { String sql = "TRUNCATE TABLE t_order"; try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.executeUpdate(sql); } catch (final SQLException ignored) { } } @Override public Long insert(final Order order) { public Long insert(final Order order) throws SQLException { String sql = "INSERT INTO t_order (user_id, status) VALUES (?, ?)"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { Loading @@ -80,29 +77,27 @@ public class OrderRepositoryImpl implements OrderRepository { order.setOrderId(resultSet.getLong(1)); } } } catch (final SQLException ignored) { } return order.getOrderId(); } @Override public void delete(final Long orderId) { public void delete(final Long orderId) throws SQLException { String sql = "DELETE FROM t_order WHERE order_id=?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setLong(1, orderId); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } } @Override public List<Order> selectAll() { public List<Order> selectAll() throws SQLException { String sql = "SELECT * FROM t_order"; return getOrders(sql); } protected List<Order> getOrders(final String sql) { protected List<Order> getOrders(final String sql) throws SQLException { List<Order> result = new LinkedList<>(); try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); Loading @@ -114,7 +109,6 @@ public class OrderRepositoryImpl implements OrderRepository { order.setStatus(resultSet.getString(3)); result.add(order); } } catch (final SQLException ignored) { } return result; } Loading