AssertEngine.java 27.6 KB
Newer Older
L
liu ze jian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed 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.
 * </p>
 */

18 19 20 21
package io.shardingjdbc.dbtest.asserts;

import java.io.File;
import java.io.IOException;
22
import java.lang.reflect.Field;
23 24 25 26 27 28 29 30 31 32 33 34
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

35
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
36
import io.shardingjdbc.dbtest.config.bean.AssertDDLDefinition;
37
import io.shardingjdbc.dbtest.config.bean.AssertDMLDefinition;
38
import io.shardingjdbc.dbtest.data.ColumnDefinition;
39
import io.shardingjdbc.dbtest.init.InItCreateSchema;
40
import io.shardingjdbc.test.sql.SQLCasesLoader;
41 42 43 44 45 46
import org.junit.Assert;
import org.xml.sax.SAXException;

import io.shardingjdbc.core.jdbc.core.ShardingContext;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.rule.ShardingRule;
47 48
import io.shardingjdbc.dbtest.common.DatabaseUtil;
import io.shardingjdbc.dbtest.common.PathUtil;
49
import io.shardingjdbc.dbtest.config.bean.AssertDQLDefinition;
50 51 52 53 54 55 56
import io.shardingjdbc.dbtest.config.bean.AssertsDefinition;
import io.shardingjdbc.dbtest.data.AnalyzeDataset;
import io.shardingjdbc.dbtest.data.DatasetDatabase;
import io.shardingjdbc.dbtest.data.DatasetDefinition;
import io.shardingjdbc.dbtest.exception.DbTestException;

public class AssertEngine {
57
    
L
liu ze jian 已提交
58
    public static final Map<String, AssertsDefinition> ASSERTDEFINITIONMAPS = new HashMap<>();
59
    
L
liu ze jian 已提交
60 61 62 63 64 65 66 67
    /**
     * add check use cases.
     *
     * @param assertPath        Check the use case storage path
     * @param assertsDefinition Check use case definitions
     */
    public static void addAssertDefinition(final String assertPath, final AssertsDefinition assertsDefinition) {
        ASSERTDEFINITIONMAPS.put(assertPath, assertsDefinition);
68
    }
69
    
L
liu ze jian 已提交
70 71 72 73 74 75 76 77
    /**
     * Execution use case.
     *
     * @param path Check the use case storage path
     * @param id   Unique primary key for a use case
     * @return Successful implementation
     */
    public static boolean runAssert(final String path, final String id) {
78
        
L
liu ze jian 已提交
79
        AssertsDefinition assertsDefinition = ASSERTDEFINITIONMAPS.get(path);
80
        
81 82
        String rootPath = path.substring(0, path.lastIndexOf(File.separator) + 1);
        assertsDefinition.setPath(rootPath);
83
        
L
liu ze jian 已提交
84
        try {
85
            
86
            DataSource dataSource = getDataSource(PathUtil.getPath(assertsDefinition.getShardingRuleConfig(), rootPath));
87
            
88
            ShardingContext shardingContext = getShardingContext((ShardingDataSource) dataSource);
89
            Map<String, DataSource> dataSourceMaps = shardingContext.getDataSourceMap();
90
            
L
liu ze jian 已提交
91 92 93
            List<String> dbs = new ArrayList<>();
            for (String s : dataSourceMaps.keySet()) {
                dbs.add(s);
94
            }
95 96 97
            
            // dql run
            for (AssertDQLDefinition each : assertsDefinition.getAssertDQL()) {
L
liu ze jian 已提交
98
                if (id.equals(each.getId())) {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                    AssertDQLDefinition anAssert = each;
                    
                    String rootsql = anAssert.getSql();
                    rootsql = SQLCasesLoader.getSql(rootsql);
                    String initDataFile = PathUtil.getPath(anAssert.getInitDataFile(), rootPath);
                    Map<String, DatasetDefinition> mapDatasetDefinition = new HashMap<>();
                    Map<String, String> sqls = new HashMap<>();
                    getInitDatas(dbs, initDataFile, mapDatasetDefinition, sqls);
                    
                    if (mapDatasetDefinition.isEmpty()) {
                        throw new DbTestException(path + "  Use cases cannot be parsed");
                    }
                    
                    if (sqls.isEmpty()) {
                        throw new DbTestException(path + "  The use case cannot initialize the data");
                    }
                    
L
liu ze jian 已提交
116
                    try {
117 118
                        initTableData(dataSourceMaps, sqls, mapDatasetDefinition);
                        
L
liu ze jian 已提交
119 120 121 122 123 124 125
                        doSelectUsePreparedStatement(rootPath, dataSource, anAssert, rootsql);
                        doSelectUsePreparedStatementToExecuteSelect(rootPath, dataSource, anAssert, rootsql);
                        
                        doSelectUseStatement(rootPath, dataSource, anAssert, rootsql);
                        doSelectUseStatementToExecuteSelect(rootPath, dataSource, anAssert, rootsql);
                    } finally {
                        clearTableData(dataSourceMaps, mapDatasetDefinition);
126
                    }
L
liu ze jian 已提交
127
                    
L
liu ze jian 已提交
128 129
                }
            }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            
            // dml run
            for (AssertDMLDefinition each : assertsDefinition.getAssertDML()) {
                if (id.equals(each.getId())) {
                    AssertDMLDefinition anAssert = each;
                    String rootsql = anAssert.getSql();
                    rootsql = SQLCasesLoader.getSql(rootsql);
                    String initDataFile = PathUtil.getPath(anAssert.getInitDataFile(), rootPath);
                    Map<String, DatasetDefinition> mapDatasetDefinition = new HashMap<>();
                    Map<String, String> sqls = new HashMap<>();
                    getInitDatas(dbs, initDataFile, mapDatasetDefinition, sqls);
                    
                    if (mapDatasetDefinition.isEmpty()) {
                        throw new DbTestException(path + "  Use cases cannot be parsed");
                    }
                    
                    if (sqls.isEmpty()) {
                        throw new DbTestException(path + "  The use case cannot initialize the data");
                    }
                    
                    doUpdateUseStatementToExecuteUpdate(rootPath, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls);
                    
                    doUpdateUseStatementToExecute(rootPath, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls);
                    
                    doUpdateUsePreparedStatementToExecuteUpdate(rootPath, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls);
                    
                    doUpdateUsePreparedStatementToExecute(rootPath, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls);
                    
158 159
                }
            }
160 161 162 163 164 165 166 167
            
            // ddl run
            for (AssertDDLDefinition each : assertsDefinition.getAssertDDL()) {
                if (id.equals(each.getId())) {
                    AssertDDLDefinition anAssert = each;
                    String rootsql = anAssert.getSql();
                    rootsql = SQLCasesLoader.getSql(rootsql);
                    
L
liu ze jian 已提交
168
                    doUpdateUseStatementToExecuteUpdateDDL(rootPath, dataSource, anAssert, rootsql);
169
                    
L
liu ze jian 已提交
170
                    doUpdateUseStatementToExecuteDDL(rootPath, dataSource, anAssert, rootsql);
171
                    
L
liu ze jian 已提交
172
                    doUpdateUsePreparedStatementToExecuteUpdateDDL(rootPath, dataSource, anAssert, rootsql);
173
                    
L
liu ze jian 已提交
174
                    doUpdateUsePreparedStatementToExecuteDDL(rootPath, dataSource, anAssert, rootsql);
175 176 177
                    
                }
            }
178
            
L
liu ze jian 已提交
179 180 181 182 183
        } catch (NoSuchFieldException | IllegalAccessException | ParseException | XPathExpressionException | SQLException | ParserConfigurationException | SAXException | IOException e) {
            throw new DbTestException(e);
        }
        return true;
    }
184 185
    
    private static void doUpdateUsePreparedStatementToExecute(final String rootPath, final DataSource dataSource, final Map<String, DataSource> dataSourceMaps, final AssertDMLDefinition anAssert, final String rootsql, final Map<String, DatasetDefinition> mapDatasetDefinition, final Map<String, String> sqls) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
186 187 188
        try {
            initTableData(dataSourceMaps, sqls, mapDatasetDefinition);
            try (Connection con = dataSource.getConnection();) {
189
                boolean actual = DatabaseUtil.updateUsePreparedStatementToExecute(con, rootsql,
L
liu ze jian 已提交
190
                        anAssert.getParameters());
191
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
192
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
193
                
L
liu ze jian 已提交
194
                Assert.assertEquals("Update error", false, actual);
195
                
L
liu ze jian 已提交
196
                String checksql = anAssert.getExpectedSql();
197
                checksql = SQLCasesLoader.getSql(checksql);
198
                DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checksql,
L
liu ze jian 已提交
199
                        anAssert.getExpectedParameters());
200
                DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement);
L
liu ze jian 已提交
201 202 203 204 205
            }
        } finally {
            clearTableData(dataSourceMaps, mapDatasetDefinition);
        }
    }
206
    
L
liu ze jian 已提交
207
    private static void doUpdateUsePreparedStatementToExecuteDDL(final String rootPath, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
208 209 210 211 212 213 214 215 216 217 218
        try {
            try (Connection con = dataSource.getConnection()) {
                InItCreateSchema.dropTable();
                InItCreateSchema.createTable();
                DatabaseUtil.updateUsePreparedStatementToExecute(con, rootsql,
                        anAssert.getParameters());
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
                
                String table = anAssert.getTable();
                List<ColumnDefinition> columnDefinitions = DatabaseUtil.getColumnDefinitions(con, table);
L
liu ze jian 已提交
219
                DatabaseUtil.assertConfigs(checkDataset, columnDefinitions, table);
220 221 222 223 224 225 226
            }
        } finally {
            InItCreateSchema.dropTable();
            InItCreateSchema.createTable();
        }
    }
    
227
    private static void doUpdateUsePreparedStatementToExecuteUpdate(final String rootPath, final DataSource dataSource, final Map<String, DataSource> dataSourceMaps, final AssertDMLDefinition anAssert, final String rootsql, final Map<String, DatasetDefinition> mapDatasetDefinition, final Map<String, String> sqls) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
228 229 230
        try {
            initTableData(dataSourceMaps, sqls, mapDatasetDefinition);
            try (Connection con = dataSource.getConnection();) {
231
                int actual = DatabaseUtil.updateUsePreparedStatementToExecuteUpdate(con, rootsql,
L
liu ze jian 已提交
232
                        anAssert.getParameters());
233
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
234
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
235
                
L
liu ze jian 已提交
236
                Assert.assertEquals("Update row number error", anAssert.getExpectedUpdate().intValue(), actual);
237
                
L
liu ze jian 已提交
238
                String checksql = anAssert.getExpectedSql();
239
                checksql = SQLCasesLoader.getSql(checksql);
240
                DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checksql,
L
liu ze jian 已提交
241
                        anAssert.getExpectedParameters());
242
                DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement);
L
liu ze jian 已提交
243 244 245 246 247
            }
        } finally {
            clearTableData(dataSourceMaps, mapDatasetDefinition);
        }
    }
248
    
L
liu ze jian 已提交
249
    private static void doUpdateUsePreparedStatementToExecuteUpdateDDL(final String rootPath, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
250 251 252 253 254 255 256 257 258 259 260 261
        try {
            try (Connection con = dataSource.getConnection()) {
                InItCreateSchema.dropTable();
                InItCreateSchema.createTable();
                DatabaseUtil.updateUsePreparedStatementToExecuteUpdate(con, rootsql,
                        anAssert.getParameters());
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
                
                
                String table = anAssert.getTable();
                List<ColumnDefinition> columnDefinitions = DatabaseUtil.getColumnDefinitions(con, table);
L
liu ze jian 已提交
262
                DatabaseUtil.assertConfigs(checkDataset, columnDefinitions, table);
263 264 265 266 267 268 269
            }
        } finally {
            InItCreateSchema.dropTable();
            InItCreateSchema.createTable();
        }
    }
    
270
    private static void doUpdateUseStatementToExecute(final String rootPath, final DataSource dataSource, final Map<String, DataSource> dataSourceMaps, final AssertDMLDefinition anAssert, final String rootsql, final Map<String, DatasetDefinition> mapDatasetDefinition, final Map<String, String> sqls) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
271 272 273
        try {
            initTableData(dataSourceMaps, sqls, mapDatasetDefinition);
            try (Connection con = dataSource.getConnection();) {
274
                boolean actual = DatabaseUtil.updateUseStatementToExecute(con, rootsql, anAssert.getParameters());
275
                
276
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
277
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
278
                
L
liu ze jian 已提交
279
                Assert.assertEquals("Update error", false, actual);
280
                
L
liu ze jian 已提交
281
                String checksql = anAssert.getExpectedSql();
282
                checksql = SQLCasesLoader.getSql(checksql);
283
                DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checksql,
L
liu ze jian 已提交
284
                        anAssert.getExpectedParameters());
285
                DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement);
286
            }
L
liu ze jian 已提交
287 288 289 290
        } finally {
            clearTableData(dataSourceMaps, mapDatasetDefinition);
        }
    }
291
    
L
liu ze jian 已提交
292
    private static void doUpdateUseStatementToExecuteDDL(final String rootPath, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
293 294 295 296 297 298 299 300 301 302 303
        try {
            try (Connection con = dataSource.getConnection()) {
                InItCreateSchema.dropTable();
                InItCreateSchema.createTable();
                DatabaseUtil.updateUseStatementToExecute(con, rootsql, anAssert.getParameters());
                
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
                
                String table = anAssert.getTable();
                List<ColumnDefinition> columnDefinitions = DatabaseUtil.getColumnDefinitions(con, table);
L
liu ze jian 已提交
304
                DatabaseUtil.assertConfigs(checkDataset, columnDefinitions, table);
305 306 307 308 309 310 311
            }
        } finally {
            InItCreateSchema.dropTable();
            InItCreateSchema.createTable();
        }
    }
    
312
    private static void doUpdateUseStatementToExecuteUpdate(final String rootPath, final DataSource dataSource, final Map<String, DataSource> dataSourceMaps, final AssertDMLDefinition anAssert, final String rootsql, final Map<String, DatasetDefinition> mapDatasetDefinition, final Map<String, String> sqls) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
313 314 315
        try {
            initTableData(dataSourceMaps, sqls, mapDatasetDefinition);
            try (Connection con = dataSource.getConnection();) {
316 317
                int actual = DatabaseUtil.updateUseStatementToExecuteUpdate(con, rootsql, anAssert.getParameters());
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
318
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
319
                
L
liu ze jian 已提交
320
                Assert.assertEquals("Update row number error", anAssert.getExpectedUpdate().intValue(), actual);
321
                
L
liu ze jian 已提交
322
                String checksql = anAssert.getExpectedSql();
323
                checksql = SQLCasesLoader.getSql(checksql);
324
                DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checksql,
L
liu ze jian 已提交
325
                        anAssert.getExpectedParameters());
326
                DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement);
327
                
328
            }
L
liu ze jian 已提交
329 330 331 332
        } finally {
            clearTableData(dataSourceMaps, mapDatasetDefinition);
        }
    }
333
    
L
liu ze jian 已提交
334
    private static void doUpdateUseStatementToExecuteUpdateDDL(final String rootPath, final DataSource dataSource,  final AssertDDLDefinition anAssert, final String rootsql) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
335 336 337 338 339 340 341 342 343 344
        try {
            try (Connection con = dataSource.getConnection()) {
                InItCreateSchema.dropTable();
                InItCreateSchema.createTable();
                DatabaseUtil.updateUseStatementToExecuteUpdate(con, rootsql, anAssert.getParameters());
                String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
                DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
                
                String table = anAssert.getTable();
                List<ColumnDefinition> columnDefinitions = DatabaseUtil.getColumnDefinitions(con, table);
L
liu ze jian 已提交
345
                DatabaseUtil.assertConfigs(checkDataset, columnDefinitions, table);
346 347 348 349 350 351 352
            }
        } finally {
            InItCreateSchema.dropTable();
            InItCreateSchema.createTable();
        }
    }
    
353
    private static void doSelectUseStatement(final String rootPath, final DataSource dataSource, final AssertDQLDefinition anAssert, final String rootsql) throws SQLException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
354
        try (Connection con = dataSource.getConnection();) {
355
            DatasetDatabase ddStatement = DatabaseUtil.selectUseStatement(con, rootsql,
L
liu ze jian 已提交
356
                    anAssert.getParameters());
357
            String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
358
            DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
359
            
360
            DatabaseUtil.assertDatas(checkDataset, ddStatement);
361
        }
L
liu ze jian 已提交
362
    }
363 364
    
    private static void doSelectUseStatementToExecuteSelect(final String rootPath, final DataSource dataSource, final AssertDQLDefinition anAssert, final String rootsql) throws SQLException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
365
        try (Connection con = dataSource.getConnection();) {
366
            DatasetDatabase ddStatement = DatabaseUtil.selectUseStatementToExecuteSelect(con, rootsql,
L
liu ze jian 已提交
367
                    anAssert.getParameters());
368
            String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
369
            DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
370
            
371
            DatabaseUtil.assertDatas(checkDataset, ddStatement);
L
liu ze jian 已提交
372 373
        }
    }
374 375
    
    private static void doSelectUsePreparedStatement(final String rootPath, final DataSource dataSource, final AssertDQLDefinition anAssert, final String rootsql) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
376
        try (Connection con = dataSource.getConnection();) {
377
            DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, rootsql,
L
liu ze jian 已提交
378
                    anAssert.getParameters());
379
            
380
            String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
381
            DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
382
            
383
            DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement);
L
liu ze jian 已提交
384 385
        }
    }
386 387
    
    private static void doSelectUsePreparedStatementToExecuteSelect(final String rootPath, final DataSource dataSource, final AssertDQLDefinition anAssert, final String rootsql) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
L
liu ze jian 已提交
388
        try (Connection con = dataSource.getConnection();) {
389
            DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatementToExecuteSelect(con, rootsql,
L
liu ze jian 已提交
390
                    anAssert.getParameters());
391
            
392
            String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
L
liu ze jian 已提交
393
            DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
394
            
395
            DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement);
L
liu ze jian 已提交
396
        }
397
    }
398
    
399 400 401 402 403 404 405 406 407
    private static void getInitDatas(final List<String> dbs, final String initDataFile,
                                     final Map<String, DatasetDefinition> mapDatasetDefinition, final Map<String, String> sqls)
            throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
        for (String each : dbs) {
            String tempPath = initDataFile + "/" + each + ".xml";
            File file = new File(tempPath);
            if (file.exists()) {
                DatasetDefinition datasetDefinition = AnalyzeDataset.analyze(file);
                mapDatasetDefinition.put(each, datasetDefinition);
408
                
409 410
                Map<String, List<Map<String, String>>> datas = datasetDefinition.getDatas();
                for (Map.Entry<String, List<Map<String, String>>> eachEntry : datas.entrySet()) {
411
                    String sql = DatabaseUtil.analyzeSql(eachEntry.getKey(), eachEntry.getValue().get(0));
412 413 414 415 416
                    sqls.put(eachEntry.getKey(), sql);
                }
            }
        }
    }
417 418
    
    
419
    private static void clearTableData(final Map<String, DataSource> dataSourceMaps,
L
liu ze jian 已提交
420
                                       final Map<String, DatasetDefinition> mapDatasetDefinition) throws SQLException {
421
        for (Map.Entry<String, DataSource> eachEntry : dataSourceMaps.entrySet()) {
422
            
423 424 425
            DataSource dataSource1 = eachEntry.getValue();
            DatasetDefinition datasetDefinition = mapDatasetDefinition.get(eachEntry.getKey());
            Map<String, List<Map<String, String>>> datas = datasetDefinition.getDatas();
426
            
427 428
            for (Map.Entry<String, List<Map<String, String>>> eachListEntry : datas.entrySet()) {
                try (Connection conn = dataSource1.getConnection()) {
429
                    DatabaseUtil.cleanAllUsePreparedStatement(conn, eachListEntry.getKey());
430 431
                }
            }
432
            
433 434
        }
    }
435
    
L
liu ze jian 已提交
436 437
    private static void initTableData(final Map<String, DataSource> dataSourceMaps, final Map<String, String> sqls,
                                      final Map<String, DatasetDefinition> mapDatasetDefinition) throws SQLException, ParseException {
438 439 440
        for (Map.Entry<String, DataSource> eachDataSourceEntry : dataSourceMaps.entrySet()) {
            DataSource dataSource1 = eachDataSourceEntry.getValue();
            DatasetDefinition datasetDefinition = mapDatasetDefinition.get(eachDataSourceEntry.getKey());
441
            Map<String, List<ColumnDefinition>> configs = datasetDefinition.getMetadatas();
442
            Map<String, List<Map<String, String>>> datas = datasetDefinition.getDatas();
443
            
444 445
            for (Map.Entry<String, List<Map<String, String>>> eachListEntry : datas.entrySet()) {
                try (Connection conn = dataSource1.getConnection()) {
446
                    DatabaseUtil.insertUsePreparedStatement(conn, sqls.get(eachListEntry.getKey()),
447 448 449 450 451
                            datas.get(eachListEntry.getKey()), configs.get(eachListEntry.getKey()));
                }
            }
        }
    }
452 453 454 455
    
    
    /**
     * Generating DataSource from yaml.
456
     *
457 458
     * @param path path
     * @return DataSource
459
     * @throws IOException  IOException
460 461 462 463 464 465 466 467
     * @throws SQLException SQLException
     */
    public static DataSource getDataSource(final String path) throws IOException, SQLException {
        return ShardingDataSourceFactory.createDataSource(new File(path));
    }
    
    /**
     * According to the sub DataSource set of shardingDataSource.
468
     *
469 470
     * @param shardingDataSource shardingDataSource
     * @return DataSource map
471 472
     * @throws NoSuchFieldException     NoSuchFieldException
     * @throws SecurityException        SecurityException
473
     * @throws IllegalArgumentException IllegalArgumentException
474
     * @throws IllegalAccessException   IllegalAccessException
475 476 477 478 479 480 481 482 483
     */
    public static Map<String, DataSource> getDataSourceMap(final ShardingDataSource shardingDataSource)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        ShardingContext shardingContext = getShardingContext(shardingDataSource);
        return shardingContext.getDataSourceMap();
    }
    
    /**
     * According to ShardingRule in shardingDataSource.
484
     *
485 486
     * @param shardingDataSource shardingDataSource
     * @return ShardingRule
487 488
     * @throws NoSuchFieldException     NoSuchFieldException
     * @throws SecurityException        SecurityException
489
     * @throws IllegalArgumentException IllegalArgumentException
490
     * @throws IllegalAccessException   IllegalAccessException
491 492 493 494 495 496 497 498 499
     */
    public static ShardingRule getShardingRule(final ShardingDataSource shardingDataSource)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        ShardingContext shardingContext = getShardingContext(shardingDataSource);
        return shardingContext.getShardingRule();
    }
    
    /**
     * According to ShardingContext in shardingDataSource.
500
     *
501 502
     * @param shardingDataSource shardingDataSource
     * @return ShardingContext
503 504
     * @throws NoSuchFieldException     NoSuchFieldException
     * @throws SecurityException        SecurityException
505
     * @throws IllegalArgumentException IllegalArgumentException
506
     * @throws IllegalAccessException   IllegalAccessException
507 508 509 510 511 512 513
     */
    public static ShardingContext getShardingContext(final ShardingDataSource shardingDataSource)
            throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        Field field = shardingDataSource.getClass().getDeclaredField("shardingContext");
        field.setAccessible(true);
        return (ShardingContext) field.get(shardingDataSource);
    }
514
}