|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package examples.sharding; |
| 17 | + |
| 18 | +import static examples.sharding.TableCodesDynamicSqlSupport.tableCodes; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mybatis.dynamic.sql.SqlBuilder.countFrom; |
| 21 | +import static org.mybatis.dynamic.sql.SqlBuilder.insertInto; |
| 22 | +import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo; |
| 23 | +import static org.mybatis.dynamic.sql.SqlBuilder.select; |
| 24 | + |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.sql.Connection; |
| 28 | +import java.sql.DriverManager; |
| 29 | + |
| 30 | +import examples.sharding.TableCodesDynamicSqlSupport.TableCodes; |
| 31 | +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; |
| 32 | +import org.apache.ibatis.jdbc.ScriptRunner; |
| 33 | +import org.apache.ibatis.mapping.Environment; |
| 34 | +import org.apache.ibatis.session.Configuration; |
| 35 | +import org.apache.ibatis.session.SqlSession; |
| 36 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 37 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 38 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 39 | +import org.junit.jupiter.api.BeforeEach; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; |
| 42 | +import org.mybatis.dynamic.sql.render.RenderingStrategies; |
| 43 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 44 | + |
| 45 | +class ShardingTest { |
| 46 | + private static final String JDBC_URL = "jdbc:hsqldb:mem:aname"; |
| 47 | + private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver"; |
| 48 | + |
| 49 | + private SqlSessionFactory sqlSessionFactory; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setup() throws Exception { |
| 53 | + Class.forName(JDBC_DRIVER); |
| 54 | + InputStream is = getClass().getResourceAsStream("/examples/sharding/ShardingDB.sql"); |
| 55 | + assert is != null; |
| 56 | + try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) { |
| 57 | + ScriptRunner sr = new ScriptRunner(connection); |
| 58 | + sr.setLogWriter(null); |
| 59 | + sr.runScript(new InputStreamReader(is)); |
| 60 | + } |
| 61 | + |
| 62 | + UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); |
| 63 | + Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); |
| 64 | + Configuration config = new Configuration(environment); |
| 65 | + config.addMapper(ShardedMapper.class); |
| 66 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testShardedSelect() { |
| 71 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 72 | + ShardedMapper mapper = sqlSession.getMapper(ShardedMapper.class); |
| 73 | + TableCodes table = calculateTable(1); |
| 74 | + |
| 75 | + SelectStatementProvider selectStatement = select(table.description) |
| 76 | + .from(table) |
| 77 | + .where(table.id, isEqualTo(1)) |
| 78 | + .build() |
| 79 | + .render(RenderingStrategies.MYBATIS3); |
| 80 | + |
| 81 | + assertThat(selectStatement.getSelectStatement()) |
| 82 | + .isEqualTo("select description from tableCodes_odd where id = #{parameters.p1,jdbcType=INTEGER}"); |
| 83 | + |
| 84 | + String description = mapper.selectOneString(selectStatement); |
| 85 | + assertThat(description).isNull(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testShardedInserts() { |
| 91 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 92 | + ShardedMapper mapper = sqlSession.getMapper(ShardedMapper.class); |
| 93 | + |
| 94 | + mapper.generalInsert(buildInsert(1, "Description 1")); |
| 95 | + mapper.generalInsert(buildInsert(2, "Description 2")); |
| 96 | + mapper.generalInsert(buildInsert(3, "Description 3")); |
| 97 | + mapper.generalInsert(buildInsert(4, "Description 4")); |
| 98 | + mapper.generalInsert(buildInsert(5, "Description 5")); |
| 99 | + mapper.generalInsert(buildInsert(6, "Description 6")); |
| 100 | + mapper.generalInsert(buildInsert(7, "Description 7")); |
| 101 | + |
| 102 | + TableCodes oddTable = calculateTable(1); |
| 103 | + SelectStatementProvider oddCountStatement = countFrom(oddTable) |
| 104 | + .build() |
| 105 | + .render(RenderingStrategies.MYBATIS3); |
| 106 | + assertThat(oddCountStatement.getSelectStatement()).isEqualTo("select count(*) from tableCodes_odd"); |
| 107 | + long oddRows = mapper.count(oddCountStatement); |
| 108 | + assertThat(oddRows).isEqualTo(4L); |
| 109 | + |
| 110 | + TableCodes evenTable = calculateTable(2); |
| 111 | + SelectStatementProvider evenCountStatement = countFrom(evenTable) |
| 112 | + .build() |
| 113 | + .render(RenderingStrategies.MYBATIS3); |
| 114 | + assertThat(evenCountStatement.getSelectStatement()).isEqualTo("select count(*) from tableCodes_even"); |
| 115 | + long evenRows = mapper.count(evenCountStatement); |
| 116 | + assertThat(evenRows).isEqualTo(3L); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void testShardedSelects() { |
| 122 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 123 | + ShardedMapper mapper = sqlSession.getMapper(ShardedMapper.class); |
| 124 | + |
| 125 | + mapper.generalInsert(buildInsert(1, "Description 1")); |
| 126 | + mapper.generalInsert(buildInsert(2, "Description 2")); |
| 127 | + |
| 128 | + assertThat(mapper.selectOneString(buildSelect(1))).isEqualTo("Description 1"); |
| 129 | + assertThat(mapper.selectOneString(buildSelect(2))).isEqualTo("Description 2"); |
| 130 | + assertThat(mapper.selectOneString(buildSelect(3))).isNull(); |
| 131 | + assertThat(mapper.selectOneString(buildSelect(4))).isNull(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private GeneralInsertStatementProvider buildInsert(int id, String description) { |
| 136 | + TableCodesDynamicSqlSupport.TableCodes table = calculateTable(id); |
| 137 | + return insertInto(table) |
| 138 | + .set(table.id).toValue(id) |
| 139 | + .set(table.description).toValue(description) |
| 140 | + .build() |
| 141 | + .render(RenderingStrategies.MYBATIS3); |
| 142 | + } |
| 143 | + |
| 144 | + private SelectStatementProvider buildSelect(int id) { |
| 145 | + TableCodesDynamicSqlSupport.TableCodes table = calculateTable(id); |
| 146 | + return select(table.description) |
| 147 | + .from(table) |
| 148 | + .where(table.id, isEqualTo(id)) |
| 149 | + .build() |
| 150 | + .render(RenderingStrategies.MYBATIS3); |
| 151 | + } |
| 152 | + |
| 153 | + private TableCodes calculateTable(int id) { |
| 154 | + // it might be better to lookup instances in a Map rather than creating a new instance |
| 155 | + // every time |
| 156 | + return tableCodes.withName(calculateTableName(id)); |
| 157 | + } |
| 158 | + |
| 159 | + private String calculateTableName(int id) { |
| 160 | + if (id % 2 == 0) { |
| 161 | + return "tableCodes_even"; |
| 162 | + } else { |
| 163 | + return "tableCodes_odd"; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments