/*
 * 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.builder;

import org.apache.shardingsphere.core.rewrite.placeholder.Alterable;
import org.apache.shardingsphere.core.rewrite.placeholder.ShardingPlaceholder;
import org.apache.shardingsphere.core.route.type.RoutingUnit;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * SQL builder.
 *
 * @author gaohongtao
 * @author zhangliang
 * @author maxiaoguang
 * @author panjuan
 */
public final class SQLBuilder {
    
    private final List<Object> segments;
    
    private StringBuilder currentSegment;
    
    public SQLBuilder() {
        segments = new LinkedList<>();
        currentSegment = new StringBuilder();
        segments.add(currentSegment);
    }
    
    /**
     * Append literals.
     *
     * @param literals literals for SQL
     */
    public void appendLiterals(final String literals) {
        currentSegment.append(literals);
    }
    
    /**
     * Append sharding placeholder.
     *
     * @param shardingPlaceholder sharding placeholder
     */
    public void appendPlaceholder(final ShardingPlaceholder shardingPlaceholder) {
        segments.add(shardingPlaceholder);
        currentSegment = new StringBuilder();
        segments.add(currentSegment);
    }
    
    /**
     * Convert to SQL.
     *
     * @return SQL
     */
    public String toSQL() {
        StringBuilder result = new StringBuilder();
        for (Object each : segments) {
            if (each instanceof Alterable) {
                result.append(((Alterable) each).toString(null, Collections.<String, String>emptyMap()));
            } else {
                result.append(each);
            }
        }
        return result.toString();
    }
    
    /**
     * Convert to SQL.
     *
     * @param routingUnit routing unit
     * @param logicAndActualTables logic and actual map
     * @return SQL
     */
    public String toSQL(final RoutingUnit routingUnit, final Map<String, String> logicAndActualTables) {
        StringBuilder result = new StringBuilder();
        for (Object each : segments) {
            if (each instanceof Alterable) {
                result.append(((Alterable) each).toString(routingUnit, logicAndActualTables));
            } else {
                result.append(each);
            }
        }
        return result.toString();
    }
}

@@ -853,16 +853,60 @@
 ilder;%0A%0A
+import lombok.Getter;%0Aimport lombok.Setter;%0A
 import o
@@ -1041,24 +1041,163 @@
 laceholder;%0A
+import org.apache.shardingsphere.core.rewrite.token.pojo.SQLToken;%0Aimport org.apache.shardingsphere.core.rewrite.token.pojo.Substitutable;%0A
 import org.a
@@ -1498,24 +1498,174 @@
 lder %7B%0A    %0A
+    @Setter%0A    private String logicSQL;%0A    %0A    @Getter%0A    private final List%3CSQLToken%3E sqlTokens;%0A    %0A    private int currentSQLTokenIndex;%0A    %0A
     private 
@@ -1767,24 +1767,64 @@
 Builder() %7B%0A
+        sqlTokens = new LinkedList%3C%3E();%0A
         segm
@@ -2475,32 +2475,699 @@
  %7D%0A    %0A    /**%0A
+     * Append literals.%0A     *%0A     * @param sqlToken sql token%0A     */%0A    public void appendLiteral(final SQLToken sqlToken) %7B%0A        int stopIndex = sqlTokens.size() - 1 == currentSQLTokenIndex ? logicSQL.length() : sqlTokens.get(currentSQLTokenIndex + 1).getStartIndex();%0A        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
      * Convert t

add appendLiteral()