/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.infra.binder.statement;

import lombok.Getter;
import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.SQL92Statement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;

import java.util.Collections;

/**
 * Common SQL statement context.
 * 
 * @param <T> type of SQL statement
 */
@Getter
public class CommonSQLStatementContext<T extends SQLStatement> implements SQLStatementContext<T> {
    
    private final T sqlStatement;
    
    private final TablesContext tablesContext;
    
    private final DatabaseType databaseType;
    
    public CommonSQLStatementContext(final T sqlStatement) {
        this.sqlStatement = sqlStatement;
        databaseType = initDatabaseType(sqlStatement);
        tablesContext = new TablesContext(Collections.emptyList());
    }
    
    private DatabaseType initDatabaseType(final SQLStatement sqlStatement) {
        DatabaseType databaseType = null;
        if (sqlStatement instanceof MySQLStatement) {
            databaseType = DatabaseTypeRegistry.getActualDatabaseType("MySQL");
        } else if (sqlStatement instanceof PostgreSQLStatement) {
            databaseType = DatabaseTypeRegistry.getActualDatabaseType("PostgreSQL");
        } else if (sqlStatement instanceof OracleStatement) {
            databaseType = DatabaseTypeRegistry.getActualDatabaseType("Oracle");
        } else if (sqlStatement instanceof SQLServerStatement) {
            databaseType = DatabaseTypeRegistry.getActualDatabaseType("SQLServer");
        } else if (sqlStatement instanceof SQL92Statement) {
            databaseType = DatabaseTypeRegistry.getActualDatabaseType("SQL92");
        }
        return databaseType;
    }
}

@@ -2106,63 +2106,8 @@
 nt;%0A
-        databaseType = initDatabaseType(sqlStatement);%0A
     
@@ -2178,28 +2178,13 @@
     
-%7D%0A
     
-%0A    private D
+d
 atab
@@ -2195,11 +2195,12 @@
 ype 
-ini
+= ge
 tDat
@@ -2213,27 +2213,8 @@
 ype(
-final SQLStatement 
 sqlS
@@ -2222,27 +2222,41 @@
 atement)
- %7B
+;%0A    %7D
 %0A    
+%0A
     
+private 
 Database
@@ -2252,33 +2252,36 @@
 te DatabaseType 
-d
+getD
 atabaseType = nu
@@ -2279,16 +2279,43 @@
 Type
- = null;
+(final SQLStatement sqlStatement) %7B
 %0A   
@@ -2373,38 +2373,30 @@
             
-databaseType =
+return
  DatabaseTyp
@@ -2438,37 +2438,40 @@
 SQL%22);%0A        %7D
- else
+%0A       
  if (sqlStatemen
@@ -2514,38 +2514,30 @@
             
-databaseType =
+return
  DatabaseTyp
@@ -2584,37 +2584,40 @@
 SQL%22);%0A        %7D
- else
+%0A       
  if (sqlStatemen
@@ -2656,38 +2656,30 @@
             
-databaseType =
+return
  DatabaseTyp
@@ -2722,37 +2722,40 @@
 cle%22);%0A        %7D
- else
+%0A       
  if (sqlStatemen
@@ -2797,38 +2797,30 @@
             
-databaseType =
+return
  DatabaseTyp
@@ -2878,13 +2878,16 @@
    %7D
- else
+%0A       
  if 
@@ -2941,30 +2941,22 @@
         
-databaseType =
+return
  Databas
@@ -3023,27 +3023,82 @@
     
-return databaseType
+throw new UnsupportedOperationException(sqlStatement.getClass().getName())
 ;%0A  

Refactor CommonSQLStatementContext (#9558)