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

import lombok.Getter;

/**
 * Insert values token.
 *
 * @author maxiaoguang
 * @author panjuan
 */
@Getter
public final class InsertValuesToken extends SQLToken implements Substitutable {
    
    private final int stopIndex;
    
    public InsertValuesToken(final int startIndex, final int stopIndex) {
        super(startIndex);
        this.stopIndex = stopIndex;
    }
}

@@ -877,16 +877,357 @@
 .Getter;
+%0Aimport org.apache.shardingsphere.core.rewrite.placeholder.Alterable;%0Aimport org.apache.shardingsphere.core.rewrite.placeholder.InsertValuePlaceholder;%0Aimport org.apache.shardingsphere.core.route.type.RoutingUnit;%0Aimport org.apache.shardingsphere.core.rule.DataNode;%0A%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -1383,16 +1383,27 @@
 itutable
+, Alterable
  %7B%0A    %0A
@@ -1436,24 +1436,111 @@
 Index;%0A    %0A
+    private final List%3CInsertValuePlaceholder%3E insertValues = new LinkedList%3C%3E();%0A    %0A
     public I
@@ -1670,10 +1670,1098 @@
 ;%0A    %7D%0A
+    %0A    @Override%0A    public String toString(final RoutingUnit routingUnit, final Map%3CString, String%3E logicAndActualTables) %7B%0A        StringBuilder result = new StringBuilder();%0A        appendUnits(routingUnit, result);%0A        result.delete(result.length() - 2, result.length());%0A        return result.toString();%0A    %7D%0A    %0A    private void appendUnits(final RoutingUnit routingUnit, final StringBuilder result) %7B%0A        for (InsertValuePlaceholder each : insertValues) %7B%0A            if (isToAppendInsertOptimizeResult(routingUnit, each)) %7B%0A                result.append(each).append(%22, %22);%0A            %7D%0A        %7D%0A    %7D%0A    %0A    private boolean isToAppendInsertOptimizeResult(final RoutingUnit routingUnit, final InsertValuePlaceholder unit) %7B%0A        if (unit.getDataNodes().isEmpty() %7C%7C null == routingUnit) %7B%0A            return true;%0A        %7D%0A        for (DataNode each : unit.getDataNodes()) %7B%0A            if (routingUnit.getTableUnit(each.getDataSourceName(), each.getTableName()).isPresent()) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A
 %7D%0A

add insertValues