| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.shardingsphere.example.core.jdbc.repository; |
| |
| import org.apache.shardingsphere.example.core.api.entity.Address; |
| import org.apache.shardingsphere.example.core.api.repository.AddressRepository; |
| |
| import javax.sql.DataSource; |
| import java.sql.Connection; |
| import java.sql.PreparedStatement; |
| import java.sql.ResultSet; |
| import java.sql.SQLException; |
| import java.sql.Statement; |
| import java.util.LinkedList; |
| import java.util.List; |
| |
| public final class AddressRepositoryImpl implements AddressRepository { |
| |
| private final DataSource dataSource; |
| |
| public AddressRepositoryImpl(final DataSource dataSource) { |
| this.dataSource = dataSource; |
| } |
| |
| @Override |
| 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 (Connection connection = dataSource.getConnection(); |
| Statement statement = connection.createStatement()) { |
| statement.executeUpdate(sql); |
| } |
| } |
| |
| @Override |
| public void dropTable() throws SQLException { |
| String sql = "DROP TABLE t_address"; |
| try (Connection connection = dataSource.getConnection(); |
| Statement statement = connection.createStatement()) { |
| statement.executeUpdate(sql); |
| } |
| } |
| |
| @Override |
| public void truncateTable() throws SQLException { |
| String sql = "TRUNCATE TABLE t_address"; |
| try (Connection connection = dataSource.getConnection(); |
| Statement statement = connection.createStatement()) { |
| statement.executeUpdate(sql); |
| } |
| } |
| |
| @Override |
| public Long insert(final Address entity) throws SQLException { |
| String sql = "INSERT INTO t_address (address_id, address_name) VALUES (?, ?)"; |
| try (Connection connection = dataSource.getConnection(); |
| PreparedStatement preparedStatement = connection.prepareStatement(sql)) { |
| preparedStatement.setLong(1, entity.getAddressId()); |
| preparedStatement.setString(2, entity.getAddressName()); |
| preparedStatement.executeUpdate(); |
| } |
| return entity.getAddressId(); |
| } |
| |
| @Override |
| public void delete(final Long primaryKey) throws SQLException { |
| String sql = "DELETE FROM t_address WHERE address_id=?"; |
| try (Connection connection = dataSource.getConnection(); |
| PreparedStatement preparedStatement = connection.prepareStatement(sql)) { |
| preparedStatement.setLong(1, primaryKey); |
| preparedStatement.executeUpdate(); |
| } |
| } |
| |
| @Override |
| public List<Address> selectAll() throws SQLException { |
| String sql = "SELECT * FROM t_address"; |
| return getAddress(sql); |
| } |
| |
| private List<Address> getAddress(final String sql) throws SQLException { |
| List<Address> result = new LinkedList<>(); |
| try (Connection connection = dataSource.getConnection(); |
| PreparedStatement preparedStatement = connection.prepareStatement(sql); |
| ResultSet resultSet = preparedStatement.executeQuery()) { |
| while (resultSet.next()) { |
| Address address = new Address(); |
| address.setAddressId(resultSet.getLong(1)); |
| address.setAddressName(resultSet.getString(2)); |
| result.add(address); |
| } |
| } |
| return result; |
| } |
| } |