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.sqlbuilder.sql;
19  
20  import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.dialect.DialectPipelineSQLBuilder;
21  import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.segment.PipelineSQLSegmentBuilder;
22  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
23  import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
24  
25  import java.util.Collection;
26  import java.util.Optional;
27  import java.util.stream.Collectors;
28  
29  /**
30   * Pipeline data consistency calculate SQL builder.
31   */
32  public final class PipelineDataConsistencyCalculateSQLBuilder {
33      
34      private final DialectPipelineSQLBuilder dialectSQLBuilder;
35      
36      private final PipelineSQLSegmentBuilder sqlSegmentBuilder;
37      
38      public PipelineDataConsistencyCalculateSQLBuilder(final DatabaseType databaseType) {
39          dialectSQLBuilder = DatabaseTypedSPILoader.getService(DialectPipelineSQLBuilder.class, databaseType);
40          sqlSegmentBuilder = new PipelineSQLSegmentBuilder(databaseType);
41      }
42      
43      /**
44       * Build query all ordering SQL.
45       *
46       * @param schemaName schema name
47       * @param tableName table name
48       * @param columnNames column names
49       * @param uniqueKey unique key, it may be primary key, not null
50       * @param firstQuery first query
51       * @return built SQL
52       */
53      public String buildQueryAllOrderingSQL(final String schemaName, final String tableName, final Collection<String> columnNames, final String uniqueKey, final boolean firstQuery) {
54          String qualifiedTableName = sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName);
55          String escapedUniqueKey = sqlSegmentBuilder.getEscapedIdentifier(uniqueKey);
56          String queryColumns = columnNames.stream().map(sqlSegmentBuilder::getEscapedIdentifier).collect(Collectors.joining(","));
57          return firstQuery
58                  ? String.format("SELECT %s FROM %s ORDER BY %s ASC", queryColumns, qualifiedTableName, escapedUniqueKey)
59                  : String.format("SELECT %s FROM %s WHERE %s>? ORDER BY %s ASC", queryColumns, qualifiedTableName, escapedUniqueKey, escapedUniqueKey);
60      }
61      
62      /**
63       * Build CRC32 SQL.
64       *
65       * @param schemaName schema name
66       * @param tableName table name
67       * @param columnName column name
68       * @return built SQL
69       */
70      public Optional<String> buildCRC32SQL(final String schemaName, final String tableName, final String columnName) {
71          return dialectSQLBuilder.buildCRC32SQL(sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName), sqlSegmentBuilder.getEscapedIdentifier(columnName));
72      }
73  }