/*
 * 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.core.rewrite.rewriter.sql;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder;
import org.apache.shardingsphere.core.rewrite.builder.SQLBuilder;
import org.apache.shardingsphere.core.rewrite.placeholder.InsertColumnsPlaceholder;
import org.apache.shardingsphere.core.rewrite.placeholder.TablePlaceholder;
import org.apache.shardingsphere.core.rewrite.token.pojo.InsertColumnsToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.SQLToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.Substitutable;
import org.apache.shardingsphere.core.rewrite.token.pojo.TableToken;

import java.util.List;


/**
 * Base SQL rewriter.
 *
 * @author panjuan
 */
@RequiredArgsConstructor
public final class BaseSQLRewriter implements SQLRewriter {
    
    private final SQLStatement sqlStatement;
    
    private final List<SQLToken> sqlTokens;
    
    private int currentSQLTokenIndex;
    
    /**
     * Append whole SQL.
     *
     * @param sqlBuilder SQL builder
     */
    public void appendWholeSQL(final SQLBuilder sqlBuilder) {
        sqlBuilder.appendLiterals(sqlStatement.getLogicSQL());
    }
    
    @Override
    public void rewrite(final SQLBuilder sqlBuilder, final ParameterBuilder parameterBuilder, final SQLToken sqlToken) {
        if (sqlToken instanceof InsertColumnsToken) {
            appendInsertColumnsPlaceholder(sqlBuilder, (InsertColumnsToken) sqlToken);
        } else if (sqlToken instanceof TableToken) {
            appendTablePlaceholder(sqlBuilder, (TableToken) sqlToken);
        }
        appendLiteral(sqlBuilder, sqlToken);
    }
    
    private void appendInsertColumnsPlaceholder(final SQLBuilder sqlBuilder, final InsertColumnsToken insertColumnsToken) {
        InsertColumnsPlaceholder columnsPlaceholder = new InsertColumnsPlaceholder(insertColumnsToken.getColumns(), insertColumnsToken.isToAppendParenthesis());
        sqlBuilder.appendPlaceholder(columnsPlaceholder);
    }
    
    private void appendTablePlaceholder(final SQLBuilder sqlBuilder, final TableToken tableToken) {
        sqlBuilder.appendPlaceholder(new TablePlaceholder(tableToken.getTableName().toLowerCase(), tableToken.getQuoteCharacter()));
    }
    
    private void appendLiteral(final SQLBuilder sqlBuilder, final SQLToken sqlToken) {
        String logicSQL = sqlStatement.getLogicSQL();
        int stopIndex = sqlTokens.size() - 1 == currentSQLTokenIndex ? logicSQL.length() : sqlTokens.get(currentSQLTokenIndex + 1).getStartIndex();
        sqlBuilder.appendLiterals(logicSQL.substring(getStartIndex(sqlToken) > logicSQL.length() ? logicSQL.length() : getStartIndex(sqlToken), stopIndex));
        currentSQLTokenIndex++;
    }
    
    private int getStartIndex(final SQLToken sqlToken) {
        return sqlToken instanceof Substitutable ? ((Substitutable) sqlToken).getStopIndex() + 1 : sqlToken.getStartIndex();
    }
    
    /**
     * Append initial literal.
     *
     * @param sqlBuilder SQL builder
     */
    public void appendInitialLiteral(final SQLBuilder sqlBuilder) {
        sqlBuilder.appendLiterals(sqlStatement.getLogicSQL().substring(0, sqlTokens.get(0).getStartIndex()));
    }
}

@@ -901,80 +901,8 @@
 or;%0A
-import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;%0A
 impo
@@ -961,32 +961,32 @@
 rameterBuilder;%0A
+
 import org.apach
@@ -1343,80 +1343,8 @@
 en;%0A
-import org.apache.shardingsphere.core.rewrite.token.pojo.Substitutable;%0A
 impo
@@ -1413,32 +1413,8 @@
 n;%0A%0A
-import java.util.List;%0A%0A
 %0A/**
@@ -1556,371 +1556,8 @@
    %0A
-    private final SQLStatement sqlStatement;%0A    %0A    private final List%3CSQLToken%3E sqlTokens;%0A    %0A    private int currentSQLTokenIndex;%0A    %0A    /**%0A     * Append whole SQL.%0A     *%0A     * @param sqlBuilder SQL builder%0A     */%0A    public void appendWholeSQL(final SQLBuilder sqlBuilder) %7B%0A        sqlBuilder.appendLiterals(sqlStatement.getLogicSQL());%0A    %7D%0A    %0A
     
@@ -1966,53 +1966,8 @@
   %7D%0A
-        appendLiteral(sqlBuilder, sqlToken);%0A
     
@@ -2419,32 +2419,32 @@
 n tableToken) %7B%0A
+
         sqlBuild
@@ -2570,968 +2570,6 @@
   %7D%0A
-    %0A    private void appendLiteral(final SQLBuilder sqlBuilder, final SQLToken sqlToken) %7B%0A        String logicSQL = sqlStatement.getLogicSQL();%0A        int stopIndex = sqlTokens.size() - 1 == currentSQLTokenIndex ? logicSQL.length() : sqlTokens.get(currentSQLTokenIndex + 1).getStartIndex();%0A        sqlBuilder.appendLiterals(logicSQL.substring(getStartIndex(sqlToken) %3E logicSQL.length() ? logicSQL.length() : getStartIndex(sqlToken), stopIndex));%0A        currentSQLTokenIndex++;%0A    %7D%0A    %0A    private int getStartIndex(final SQLToken sqlToken) %7B%0A        return sqlToken instanceof Substitutable ? ((Substitutable) sqlToken).getStopIndex() + 1 : sqlToken.getStartIndex();%0A    %7D%0A    %0A    /**%0A     * Append initial literal.%0A     *%0A     * @param sqlBuilder SQL builder%0A     */%0A    public void appendInitialLiteral(final SQLBuilder sqlBuilder) %7B%0A        sqlBuilder.appendLiterals(sqlStatement.getLogicSQL().substring(0, sqlTokens.get(0).getStartIndex()));%0A    %7D%0A
 %7D%0A

check error