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

import com.google.common.base.Optional;
import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement;
import org.apache.shardingsphere.core.parse.sql.segment.dml.InsertValuesSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.rewrite.token.pojo.InsertValuesToken;
import org.apache.shardingsphere.core.rule.BaseRule;

import java.util.Collection;
import java.util.List;

/**
 * Insert values token generator.
 *
 * @author panjuan
 */
public final class InsertValuesTokenGenerator implements OptionalSQLTokenGenerator<BaseRule> {
    
    @Override
    public Optional<InsertValuesToken> generateSQLToken(final OptimizedStatement optimizedStatement, final List<Object> parameters, final BaseRule baseRule) {
        Collection<InsertValuesSegment> insertValuesSegments = optimizedStatement.getSQLStatement().findSQLSegments(InsertValuesSegment.class);
        if (!(optimizedStatement.getSQLStatement() instanceof InsertStatement) || insertValuesSegments.isEmpty()) {
            return Optional.absent();
        }
        return createInsertValuesToken(insertValuesSegments);
    }
    
    private Optional<InsertValuesToken> createInsertValuesToken(final Collection<InsertValuesSegment> insertValuesSegments) {
        int startIndex = insertValuesSegments.iterator().next().getStartIndex();
        int stopIndex = insertValuesSegments.iterator().next().getStopIndex();
        for (InsertValuesSegment each : insertValuesSegments) {
            startIndex = startIndex > each.getStartIndex() ? each.getStartIndex() : startIndex;
            stopIndex = stopIndex < each.getStopIndex() ? each.getStopIndex() : stopIndex;
        }
        return Optional.of(new InsertValuesToken(startIndex, stopIndex));
    }
}

@@ -970,32 +970,246 @@
 mizedStatement;%0A
+import org.apache.shardingsphere.core.optimize.statement.sharding.dml.insert.InsertOptimizeResultUnit;%0Aimport org.apache.shardingsphere.core.optimize.statement.sharding.dml.insert.ShardingInsertOptimizedStatement;%0A
 import org.apach
@@ -1344,32 +1344,114 @@
 nsertStatement;%0A
+import org.apache.shardingsphere.core.rewrite.placeholder.InsertValuePlaceholder;%0A
 import org.apach
@@ -1581,26 +1581,108 @@
 va.util.
-Collection
+ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.LinkedList
 ;%0Aimport
@@ -2403,16 +2403,36 @@
 Segments
+, optimizedStatement
 );%0A    %7D
@@ -2551,32 +2551,77 @@
 rtValuesSegments
+, final OptimizedStatement optimizedStatement
 ) %7B%0A        int 
@@ -3041,72 +3041,702 @@
     
-return Optional.of(new InsertValuesToken(startIndex, stopIndex))
+InsertValuesToken result = new InsertValuesToken(startIndex, stopIndex);%0A        result.getInsertValues().addAll(getInsertValues((ShardingInsertOptimizedStatement) optimizedStatement));%0A        return Optional.of(result);%0A    %7D%0A    %0A    private List%3CInsertValuePlaceholder%3E getInsertValues(final ShardingInsertOptimizedStatement insertOptimizeResult) %7B%0A        List%3CInsertValuePlaceholder%3E insertValues = new LinkedList%3C%3E();%0A        for (InsertOptimizeResultUnit each : insertOptimizeResult.getUnits()) %7B%0A            insertValues.add(new InsertValuePlaceholder(new ArrayList%3C%3E(each.getColumnNames()), Arrays.asList(each.getValues()), each.getDataNodes()));%0A        %7D%0A        return insertValues
 ;%0A  

add more values in InsertValuesTokenGenerator