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.database.connector.core.spi.DatabaseTypedSPILoader;
23  import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
24  
25  import java.util.Optional;
26  
27  /**
28   * Pipeline prepare SQL builder.
29   */
30  public final class PipelinePrepareSQLBuilder {
31      
32      private final DialectPipelineSQLBuilder dialectSQLBuilder;
33      
34      private final PipelineSQLSegmentBuilder sqlSegmentBuilder;
35      
36      public PipelinePrepareSQLBuilder(final DatabaseType databaseType) {
37          dialectSQLBuilder = DatabaseTypedSPILoader.getService(DialectPipelineSQLBuilder.class, databaseType);
38          sqlSegmentBuilder = new PipelineSQLSegmentBuilder(databaseType);
39      }
40      
41      /**
42       * Build create schema SQL.
43       *
44       * @param schemaName schema name
45       * @return create schema SQL
46       */
47      public Optional<String> buildCreateSchemaSQL(final String schemaName) {
48          return dialectSQLBuilder.buildCreateSchemaSQL(sqlSegmentBuilder.getEscapedIdentifier(schemaName));
49      }
50      
51      /**
52       * Build drop SQL.
53       *
54       * @param schemaName schema name
55       * @param tableName table name
56       * @return drop SQL
57       */
58      public String buildDropSQL(final String schemaName, final String tableName) {
59          return String.format("DROP TABLE IF EXISTS %s", sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
60      }
61      
62      /**
63       * Build count SQL.
64       *
65       * @param schemaName schema name
66       * @param tableName table name
67       * @return count SQL
68       */
69      public String buildCountSQL(final String schemaName, final String tableName) {
70          return String.format("SELECT COUNT(*) FROM %s", sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
71      }
72      
73      /**
74       * Build estimated count SQL.
75       *
76       * @param catalogName catalog name
77       * @param schemaName schema name
78       * @param tableName table name
79       * @return estimated count SQL
80       */
81      public Optional<String> buildEstimatedCountSQL(final String catalogName, final String schemaName, final String tableName) {
82          return dialectSQLBuilder.buildEstimatedCountSQL(catalogName, sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
83      }
84      
85      /**
86       * Build unique key minimum maximum values SQL.
87       *
88       * @param schemaName schema name
89       * @param tableName table name
90       * @param uniqueKey unique key
91       * @return min max unique key SQL
92       */
93      public String buildUniqueKeyMinMaxValuesSQL(final String schemaName, final String tableName, final String uniqueKey) {
94          String escapedUniqueKey = sqlSegmentBuilder.getEscapedIdentifier(uniqueKey);
95          return String.format("SELECT MIN(%s), MAX(%s) FROM %s", escapedUniqueKey, escapedUniqueKey, sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
96      }
97      
98      /**
99       * Build check empty table SQL.
100      *
101      * @param schemaName schema name
102      * @param tableName table name
103      * @return check SQL
104      */
105     public String buildCheckEmptyTableSQL(final String schemaName, final String tableName) {
106         return dialectSQLBuilder.buildCheckEmptyTableSQL(sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
107     }
108     
109     /**
110      * Build split by unique key ranged SQL.
111      *
112      * @param schemaName schema name
113      * @param tableName table name
114      * @param uniqueKey unique key
115      * @param hasLowerBound has lower bound
116      * @return split SQL
117      */
118     public String buildSplitByUniqueKeyRangedSQL(final String schemaName, final String tableName, final String uniqueKey, final boolean hasLowerBound) {
119         String escapedUniqueKey = sqlSegmentBuilder.getEscapedIdentifier(uniqueKey);
120         String subQueryClause = dialectSQLBuilder.buildSplitByUniqueKeyRangedSubqueryClause(sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName), escapedUniqueKey, hasLowerBound);
121         return String.format("SELECT MAX(%s), COUNT(1), MIN(%s) FROM (%s) t", escapedUniqueKey, escapedUniqueKey, subQueryClause);
122     }
123 }