View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.core.checker;
19  
20  import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithTargetTableNotEmptyException;
21  import org.apache.shardingsphere.data.pipeline.core.importer.ImporterConfiguration;
22  import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql.PipelinePrepareSQLBuilder;
23  import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
24  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
25  import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
26  import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException;
27  import org.apache.shardingsphere.infra.metadata.caseinsensitive.CaseInsensitiveQualifiedTable;
28  
29  import javax.sql.DataSource;
30  import java.sql.Connection;
31  import java.sql.PreparedStatement;
32  import java.sql.ResultSet;
33  import java.sql.SQLException;
34  import java.util.Collection;
35  
36  /**
37   * Data source check engine.
38   */
39  public final class DataSourceCheckEngine {
40      
41      private final DialectDataSourceChecker checker;
42      
43      private final PipelinePrepareSQLBuilder sqlBuilder;
44      
45      public DataSourceCheckEngine(final DatabaseType databaseType) {
46          checker = DatabaseTypedSPILoader.findService(DialectDataSourceChecker.class, databaseType).orElse(null);
47          sqlBuilder = new PipelinePrepareSQLBuilder(databaseType);
48      }
49      
50      /**
51       * Check data source connections.
52       *
53       * @param dataSources data sources
54       * @throws SQLWrapperException SQL wrapper exception
55       */
56      public void checkConnection(final Collection<DataSource> dataSources) {
57          try {
58              for (DataSource each : dataSources) {
59                  each.getConnection().close();
60              }
61          } catch (final SQLException ex) {
62              throw new SQLWrapperException(ex);
63          }
64      }
65      
66      /**
67       * Check source data source.
68       *
69       * @param dataSources to be checked source data source
70       */
71      public void checkSourceDataSources(final Collection<DataSource> dataSources) {
72          checkConnection(dataSources);
73          if (null == checker) {
74              return;
75          }
76          dataSources.forEach(checker::checkPrivilege);
77          dataSources.forEach(checker::checkVariable);
78      }
79      
80      /**
81       * Check target data sources.
82       *
83       * @param dataSources to be checked target data sources
84       * @param importerConfig importer configuration
85       */
86      public void checkTargetDataSources(final Collection<DataSource> dataSources, final ImporterConfiguration importerConfig) {
87          checkConnection(dataSources);
88          checkEmptyTable(dataSources, importerConfig);
89      }
90      
91      private void checkEmptyTable(final Collection<DataSource> dataSources, final ImporterConfiguration importerConfig) {
92          try {
93              for (DataSource each : dataSources) {
94                  for (CaseInsensitiveQualifiedTable qualifiedTable : importerConfig.getQualifiedTables()) {
95                      ShardingSpherePreconditions.checkState(checkEmptyTable(each, qualifiedTable), () -> new PrepareJobWithTargetTableNotEmptyException(qualifiedTable.getTableName().toString()));
96                  }
97              }
98          } catch (final SQLException ex) {
99              throw new SQLWrapperException(ex);
100         }
101     }
102     
103     /**
104      * Check whether empty table.
105      *
106      * @param dataSource data source
107      * @param qualifiedTable qualified table
108      * @return empty or not
109      * @throws SQLException if there's database operation failure
110      */
111     public boolean checkEmptyTable(final DataSource dataSource, final CaseInsensitiveQualifiedTable qualifiedTable) throws SQLException {
112         String sql = sqlBuilder.buildCheckEmptyTableSQL(qualifiedTable.getSchemaName().toString(), qualifiedTable.getTableName().toString());
113         try (
114                 Connection connection = dataSource.getConnection();
115                 PreparedStatement preparedStatement = connection.prepareStatement(sql);
116                 ResultSet resultSet = preparedStatement.executeQuery()) {
117             return !resultSet.next();
118         }
119     }
120 }