Commit 1b8e8fd7 authored by liya.cookie's avatar liya.cookie
Browse files

add broadcast table 't_country'

parent 57565bc0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -29,12 +29,12 @@ public class DataSourceUtil {
    
    private static final String USER_NAME = "root";
    
    private static final String PASSWORD = "";
    private static final String PASSWORD = "liya76133951";
    
    public static DataSource createDataSource(final String dataSourceName) {
        HikariDataSource result = new HikariDataSource();
        result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
        result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false", HOST, PORT, dataSourceName));
        result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8", HOST, PORT, dataSourceName));
        result.setUsername(USER_NAME);
        result.setPassword(PASSWORD);
        return result;
+66 −0
Original line number Diff line number Diff line
/*
 * 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 io.shardingsphere.example.common.entity;

public class Country {

    private long id;

    private String name;

    private String code;

    private String language;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    @Override
    public String toString() {
        return String.format("id: %s, name: %s, code: %s, language: %s", id, name, code, language);
    }
}
+23 −0
Original line number Diff line number Diff line
/*
 * 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 io.shardingsphere.example.common.repository;

import io.shardingsphere.example.common.entity.Country;

public interface CountryRepository extends CommonRepository<Country> {
}
+122 −0
Original line number Diff line number Diff line
/*
 * 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 io.shardingsphere.example.common.jdbc.repository;

import io.shardingsphere.example.common.entity.Country;
import io.shardingsphere.example.common.repository.CountryRepository;

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 class CountryRepositroyImpl implements CountryRepository {

    private final DataSource dataSource;

    public CountryRepositroyImpl(final DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void createTableIfNotExists() {
        String sql = "CREATE TABLE IF NOT EXISTS t_country (id BIGINT NOT NULL AUTO_INCREMENT, name VARCHAR(50),code VARCHAR(50), language VARCHAR(50), PRIMARY KEY (id))";
        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement()) {
            statement.executeUpdate(sql);
        } catch (final SQLException ignored) {
        }
    }

    @Override
    public void dropTable() {
        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() {
        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(Country country) {
        String sql = "INSERT INTO t_country (name, code, language) VALUES (?, ?, ?)";
        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, country.getName());
            preparedStatement.setString(2, country.getCode());
            preparedStatement.setString(3, country.getLanguage());
            preparedStatement.executeUpdate();
            ResultSet rs = connection.createStatement().executeQuery("SELECT @@IDENTITY");
            while(rs.next()){
                country.setId(rs.getLong("@@IDENTITY"));
            }
        } catch (final SQLException ignored) {
        }
        return country.getId();
    }

    @Override
    public void delete(Long id) {
        String sql = "DELETE FROM t_country WHERE id =?";
        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setLong(1, id);
            preparedStatement.executeUpdate();
        } catch (final SQLException ignored) {
        }
    }

    @Override
    public List<Country> selectAll() {
        String sql = "SELECT * FROM t_country";
        return getCountries(sql);
    }

    private List<Country> getCountries(String sql) {
        List<Country> result = new LinkedList<>();
        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Country country = new Country();
                country.setId(resultSet.getLong(1));
                country.setName(resultSet.getString(2));
                country.setCode(resultSet.getString(3));
                country.setLanguage(resultSet.getString(4));
                result.add(country);
            }
        } catch (final SQLException ignored) {
        }
        return result;
    }
}
+83 −14
Original line number Diff line number Diff line
@@ -17,14 +17,17 @@

package io.shardingsphere.example.common.jdbc.service;

import io.shardingsphere.example.common.entity.Country;
import io.shardingsphere.example.common.entity.Order;
import io.shardingsphere.example.common.entity.OrderItem;
import io.shardingsphere.example.common.repository.CountryRepository;
import io.shardingsphere.example.common.repository.OrderItemRepository;
import io.shardingsphere.example.common.repository.OrderRepository;
import io.shardingsphere.example.common.service.CommonService;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public final class CommonServiceImpl implements CommonService {

@@ -32,6 +35,14 @@ public final class CommonServiceImpl implements CommonService {

    private OrderItemRepository orderItemRepository;

    private CountryRepository countryRepository;

    public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository, final CountryRepository countryRepository) {
        this.orderRepository = orderRepository;
        this.orderItemRepository = orderItemRepository;
        this.countryRepository = countryRepository;
    }

    public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository) {
        this.orderRepository = orderRepository;
        this.orderItemRepository = orderItemRepository;
@@ -41,22 +52,25 @@ public final class CommonServiceImpl implements CommonService {
    public void initEnvironment() {
        orderRepository.createTableIfNotExists();
        orderItemRepository.createTableIfNotExists();
        countryRepository.createTableIfNotExists();
        orderRepository.truncateTable();
        orderItemRepository.truncateTable();
        countryRepository.truncateTable();
    }

    @Override
    public void cleanEnvironment() {
        orderRepository.dropTable();
        orderItemRepository.dropTable();
        countryRepository.dropTable();
    }

    @Override
    public void processSuccess() {
        System.out.println("-------------- Process Success Begin ---------------");
        List<Long> orderIds = insertData();
        InsertResult insertResult = insertData();
        printData();
        deleteData(orderIds);
        deleteData(insertResult.getOrderIds(),insertResult.getCountryIds());
        printData();
        System.out.println("-------------- Process Success Finish --------------");
    }
@@ -69,8 +83,14 @@ public final class CommonServiceImpl implements CommonService {
        throw new RuntimeException("Exception occur for transaction test.");
    }

    private List<Long> insertData() {
    private InsertResult insertData(){
        System.out.println("---------------------------- Insert Data ----------------------------");
        List<Long> orderIds = insertOrderData();
        List<Long> countryIds = insertCountryData();
        return new InsertResult(orderIds,countryIds);
    }

    private List<Long> insertOrderData() {
        List<Long> result = new ArrayList<>(10);
        for (int i = 1; i <= 10; i++) {
            Order order = new Order();
@@ -87,12 +107,37 @@ public final class CommonServiceImpl implements CommonService {
        return result;
    }

    private void deleteData(final List<Long> orderIds) {
    private List<Long> insertCountryData(){
        List<Long> result = new ArrayList<>();
        Locale[] locales = Locale.getAvailableLocales();
        int i = 0;
        for(Locale l:locales){
            final String country = l.getCountry();
            if(country == null || "".equals(country)){
                continue;
            }
            Country currCountry = new Country();
            currCountry.setName(l.getDisplayCountry(l));
            currCountry.setLanguage(l.getLanguage());
            currCountry.setCode(l.getCountry());
            countryRepository.insert(currCountry);
            result.add(currCountry.getId());
            if(++i == 10){
                break;
            }
        }
        return result;
    }

    private void deleteData(final List<Long> orderIds,final List<Long> countryIds) {
        System.out.println("---------------------------- Delete Data ----------------------------");
        for (Long each : orderIds) {
            orderRepository.delete(each);
            orderItemRepository.delete(each);
        }
        for (Long each: countryIds){
            countryRepository.delete(each);
        }
    }

    @Override
@@ -105,5 +150,29 @@ public final class CommonServiceImpl implements CommonService {
        for (Object each : orderItemRepository.selectAll()) {
            System.out.println(each);
        }
        System.out.println("---------------------------- Print Country Data -------------------");
        for (Object each : countryRepository.selectAll()) {
            System.out.println(each);
        }
    }

    private static class InsertResult{

        private List<Long> orderIds;

        private List<Long> countryIds;

        public InsertResult(List<Long> orderIds, List<Long> countryIds) {
            this.orderIds = orderIds;
            this.countryIds = countryIds;
        }

        public List<Long> getOrderIds() {
            return orderIds;
        }

        public List<Long> getCountryIds() {
            return countryIds;
        }
    }
}
Loading