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.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 schemaName schema name
77       * @param tableName table name
78       * @return estimated count SQL
79       */
80      public Optional<String> buildEstimatedCountSQL(final String schemaName, final String tableName) {
81          return dialectSQLBuilder.buildEstimatedCountSQL(sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
82      }
83      
84      /**
85       * Build unique key minimum maximum values SQL.
86       *
87       * @param schemaName schema name
88       * @param tableName table name
89       * @param uniqueKey unique key
90       * @return min max unique key SQL
91       */
92      public String buildUniqueKeyMinMaxValuesSQL(final String schemaName, final String tableName, final String uniqueKey) {
93          String escapedUniqueKey = sqlSegmentBuilder.getEscapedIdentifier(uniqueKey);
94          return String.format("SELECT MIN(%s), MAX(%s) FROM %s", escapedUniqueKey, escapedUniqueKey, sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
95      }
96      
97      /**
98       * Build check empty table SQL.
99       *
100      * @param schemaName schema name
101      * @param tableName table name
102      * @return check SQL
103      */
104     public String buildCheckEmptyTableSQL(final String schemaName, final String tableName) {
105         return dialectSQLBuilder.buildCheckEmptyTableSQL(sqlSegmentBuilder.getQualifiedTableName(schemaName, tableName));
106     }
107 }