/*
 * 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.SQLUnit;
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 final ParameterBuilder parameterBuilder;
    
    private StringBuilder currentSegment;
    
    public SQLBuilder() {
        this(null);
    }
    
    public SQLBuilder(final ParameterBuilder parameterBuilder) {
        segments = new LinkedList<>();
        this.parameterBuilder = parameterBuilder;
        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 unit.
     *
     * @param logicAndActualTables logic and actual tables
     * @return SQL unit
     */
    public SQLUnit toSQL(final Map<String, String> logicAndActualTables) {
        StringBuilder result = new StringBuilder();
        for (Object each : segments) {
            if (each instanceof Alterable) {
                result.append(((Alterable) each).toString(null, logicAndActualTables));
            } else {
                result.append(each);
            }
        }
        return new SQLUnit(result.toString(), Collections.emptyList());
    }
    
    /**
     * Convert to SQL unit.
     *
     * @return SQL unit
     */
    public SQLUnit 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 new SQLUnit(result.toString(), parameterBuilder.getParameters());
    }
    
    /**
     * Convert to SQL unit.
     *
     * @param routingUnit routing unit
     * @param logicAndActualTables logic and actual map
     * @return SQL unit
     */
    public SQLUnit 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 new SQLUnit(result.toString(), parameterBuilder.getParameters(routingUnit));
    }
}

@@ -853,16 +853,38 @@
 ilder;%0A%0A
+import lombok.Getter;%0A
 import o
@@ -944,16 +944,16 @@
 erable;%0A
-
 import o
@@ -1436,24 +1436,36 @@
 ments;%0A    %0A
+    @Getter%0A
     private 
@@ -1561,65 +1561,8 @@
    %0A
-    public SQLBuilder() %7B%0A        this(null);%0A    %7D%0A    %0A
     
@@ -2279,32 +2279,32 @@
 tringBuilder();%0A
+
         segments
@@ -2383,606 +2383,8 @@
   *%0A
-     * @param logicAndActualTables logic and actual tables%0A     * @return SQL unit%0A     */%0A    public SQLUnit toSQL(final Map%3CString, String%3E logicAndActualTables) %7B%0A        StringBuilder result = new StringBuilder();%0A        for (Object each : segments) %7B%0A            if (each instanceof Alterable) %7B%0A                result.append(((Alterable) each).toString(null, logicAndActualTables));%0A            %7D else %7B%0A                result.append(each);%0A            %7D%0A        %7D%0A        return new SQLUnit(result.toString(), Collections.emptyList());%0A    %7D%0A    %0A    /**%0A     * Convert to SQL unit.%0A     *%0A
     

delete invalid functions