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.infra.binder.context.statement;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
22  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
23  import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
24  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
25  import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
26  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.clickhouse.ClickHouseStatement;
27  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.doris.DorisStatement;
28  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.hive.HiveStatement;
29  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
30  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.OpenGaussStatement;
31  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;
32  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
33  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.presto.PrestoStatement;
34  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.SQL92Statement;
35  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
36  
37  import java.util.Collections;
38  
39  /**
40   * Common SQL statement context.
41   */
42  @Getter
43  public abstract class CommonSQLStatementContext implements SQLStatementContext {
44      
45      private final SQLStatement sqlStatement;
46      
47      private final TablesContext tablesContext;
48      
49      private final DatabaseType databaseType;
50      
51      protected CommonSQLStatementContext(final SQLStatement sqlStatement) {
52          this.sqlStatement = sqlStatement;
53          databaseType = getDatabaseType(sqlStatement);
54          tablesContext = new TablesContext(Collections.emptyList(), databaseType);
55      }
56      
57      private DatabaseType getDatabaseType(final SQLStatement sqlStatement) {
58          if (sqlStatement instanceof MySQLStatement) {
59              return TypedSPILoader.getService(DatabaseType.class, "MySQL");
60          }
61          if (sqlStatement instanceof PostgreSQLStatement) {
62              return TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
63          }
64          if (sqlStatement instanceof OracleStatement) {
65              return TypedSPILoader.getService(DatabaseType.class, "Oracle");
66          }
67          if (sqlStatement instanceof SQLServerStatement) {
68              return TypedSPILoader.getService(DatabaseType.class, "SQLServer");
69          }
70          if (sqlStatement instanceof OpenGaussStatement) {
71              return TypedSPILoader.getService(DatabaseType.class, "openGauss");
72          }
73          if (sqlStatement instanceof ClickHouseStatement) {
74              return TypedSPILoader.getService(DatabaseType.class, "ClickHouse");
75          }
76          if (sqlStatement instanceof DorisStatement) {
77              return TypedSPILoader.getService(DatabaseType.class, "Doris");
78          }
79          if (sqlStatement instanceof HiveStatement) {
80              return TypedSPILoader.getService(DatabaseType.class, "Hive");
81          }
82          if (sqlStatement instanceof PrestoStatement) {
83              return TypedSPILoader.getService(DatabaseType.class, "Presto");
84          }
85          if (sqlStatement instanceof SQL92Statement) {
86              return TypedSPILoader.getService(DatabaseType.class, "SQL92");
87          }
88          throw new UnsupportedSQLOperationException(sqlStatement.getClass().getName());
89      }
90  }