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

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import lombok.Getter;
import org.apache.shardingsphere.core.parse.lexer.token.DefaultKeyword;
import org.apache.shardingsphere.core.parse.parser.expression.SQLExpression;
import org.apache.shardingsphere.core.parse.parser.expression.SQLNumberExpression;
import org.apache.shardingsphere.core.parse.parser.expression.SQLPlaceholderExpression;
import org.apache.shardingsphere.core.parse.parser.expression.SQLTextExpression;
import org.apache.shardingsphere.core.rule.DataNode;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Insert column values.
 *
 * @author panjuan
 */
@Getter
public final class InsertColumnValues {
    
    private final DefaultKeyword type;
    
    private final Set<String> columnNames = new LinkedHashSet<>();
    
    private final List<InsertColumnValue> columnValues = new LinkedList<>();
    
    public InsertColumnValues(final DefaultKeyword type, final List<String> columnNames) {
        this.type = type;
        this.columnNames.addAll(columnNames);
    }
    
    /**
     * Add insert column value.
     *
     * @param columnValues column values
     * @param columnParameters column parameters
     */
    public void addInsertColumnValue(final List<SQLExpression> columnValues, final List<Object> columnParameters) {
        this.columnValues.add(new InsertColumnValue(columnValues, columnParameters));
    }
    
    @Getter
    public final class InsertColumnValue {
        
        private final SQLExpression[] values;
        
        private final Object[] parameters;
        
        private final List<DataNode> dataNodes = new LinkedList<>();
        
        public InsertColumnValue(final List<SQLExpression> values, final List<Object> parameters) {
            this.values = (SQLExpression[]) values.toArray();
            this.parameters = parameters.toArray();
        }
        
        /**
         * Set column value.
         *
         * @param columnName column name
         * @param columnValue column value
         */
        public void setColumnValue(final String columnName, final Object columnValue) {
            SQLExpression sqlExpression = values[getColumnIndex(columnName)];
            if (sqlExpression instanceof SQLPlaceholderExpression) {
                parameters[getParameterIndex(sqlExpression)] = columnValue;
            } else {
                SQLExpression columnExpression = String.class == columnValue.getClass() ? new SQLTextExpression(String.valueOf(columnValue)) : new SQLNumberExpression((Number) columnValue);
                values[getColumnIndex(columnName)] = columnExpression;
            }
        }
    
        private int getColumnIndex(final String columnName) {
            return new ArrayList<>(columnNames).indexOf(columnName);
        }
    
        private int getParameterIndex(final SQLExpression sqlExpression) {
            int result = 0;
            for (int i = 0; i < values.length; i++) {
                if (sqlExpression == values[i]) {
                    return result;
                } else if (values[i] instanceof SQLPlaceholderExpression) {
                    result++;
                }
            }
        }
        
        /**
         * Get column value.
         *
         * @param columnName column Name
         * @return column value
         */
        public Object getColumnValue(final String columnName) {
            SQLExpression sqlExpression = values[getColumnIndex(columnName)];
            if (sqlExpression instanceof SQLPlaceholderExpression) {
                return parameters[getParameterIndex(sqlExpression)];
            } else if (sqlExpression instanceof SQLTextExpression) {
                return ((SQLTextExpression) sqlExpression).getText();
            } else {
                return ((SQLNumberExpression) sqlExpression).getNumber();
            }
        }
        
        @Override
        public String toString() {
            StringBuilder result = new StringBuilder();
            if (DefaultKeyword.SET == type) {
                fillResultBySet(result);
            } else {
                fillResultByValues(result);
            }
            return result.toString();
        }
        
        private void fillResultBySet(final StringBuilder result) {
            for (int i = 0; i < columnNames.size(); i++) {
                result.append(String.format("%s = %s", new ArrayList<>(columnNames).get(i), getColumnSQLExpressionValue(i))).append(", ");
            }
            result.delete(result.length() - 2, result.length());
        }
        
        private void fillResultByValues(final StringBuilder result) {
            result.append("(");
            for (int i = 0; i < columnNames.size(); i++) {
                result.append(getColumnSQLExpressionValue(i)).append(", ");
            }
            result.delete(result.length() - 2, result.length()).append(")");
        }
        
        private String getColumnSQLExpressionValue(final int columnValueIndex) {
            SQLExpression sqlExpression = values[columnValueIndex];
            if (sqlExpression instanceof SQLPlaceholderExpression) {
                return "?";
            } else if (sqlExpression instanceof SQLTextExpression) {
                return String.format("'%s'", ((SQLTextExpression) sqlExpression).getText());
            } else {
                return String.valueOf(((SQLNumberExpression) sqlExpression).getNumber());
            }
        }
    }
}

@@ -864,40 +864,21 @@
 ort 
-com.google.common.base.Predica
+lombok.Get
 te
+r
 ;%0Aim
@@ -886,68 +886,66 @@
 ort 
-com.google.common.collect.Collections2;%0Aimport lombok.Getter
+org.apache.shardingsphere.core.exception.ShardingException
 ;%0Aim
@@ -2835,32 +2835,446 @@
 ay();%0A        %7D%0A
+    %0A        /**%0A         * Add column value.%0A         * %0A         * @param sqlExpression sql expression%0A         */%0A        public void addColumnValue(final SQLExpression sqlExpression) %7B%0A            int count = 0;%0A            for (SQLExpression each : values) %7B%0A                if (null == each) %7B%0A                    count++;%0A                %7D%0A            %7D%0A            values%5Bcount%5D = sqlExpression;%0A        %7D%0A
         %0A       
@@ -4306,41 +4306,35 @@
 or (
-int i = 0; i %3C values.length; i++
+SQLExpression each : values
 ) %7B%0A
@@ -4370,25 +4370,20 @@
 sion == 
-values%5Bi%5D
+each
 ) %7B%0A    
@@ -4444,17 +4444,12 @@
 if (
-values%5Bi%5D
+each
  ins
@@ -4538,32 +4538,105 @@
 %7D%0A            %7D%0A
+            throw new ShardingException(%22Can not get parameter index.%22);%0A
         %7D%0A      

add addColumnValue