/*
 * 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.parse.filler.encrypt.dml.insert;

import lombok.Setter;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parse.filler.api.EncryptRuleAwareFiller;
import org.apache.shardingsphere.core.parse.filler.api.SQLSegmentFiller;
import org.apache.shardingsphere.core.parse.filler.api.ShardingTableMetaDataAwareFiller;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.InsertColumnsSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertValuesToken;
import org.apache.shardingsphere.core.rule.EncryptRule;

/**
 * Insert columns filler.
 *
 * @author zhangliang
 * @author panjuan
 */
@Setter
public final class EncryptInsertColumnsFiller implements SQLSegmentFiller<InsertColumnsSegment>, EncryptRuleAwareFiller, ShardingTableMetaDataAwareFiller {
    
    private EncryptRule encryptRule;
    
    private ShardingTableMetaData shardingTableMetaData;
    
    @Override
    public void fill(final InsertColumnsSegment sqlSegment, final SQLStatement sqlStatement) {
        if (sqlStatement instanceof InsertStatement) {
            InsertStatement insertStatement = (InsertStatement) sqlStatement;
            if (sqlSegment.getColumns().isEmpty()) {
                fillFromMetaData(insertStatement);
            } else {
                fillFromSQL(sqlSegment, insertStatement);
            }
            insertStatement.getSQLTokens().add(new InsertValuesToken(sqlSegment.getStartIndex(), sqlSegment.getStopIndex()));
        }
    }
    
    private void fillFromMetaData(final InsertStatement insertStatement) {
        String tableName = insertStatement.getTables().getSingleTableName();
        for (String each : shardingTableMetaData.getAllColumnNames(tableName)) {
            insertStatement.getColumnNames().add(each);
        }
    }
    
    private void fillFromSQL(final InsertColumnsSegment sqlSegment, final InsertStatement insertStatement) {
        for (ColumnSegment each : sqlSegment.getColumns()) {
            insertStatement.getColumnNames().add(each.getName());
        }
    }
}

@@ -869,16 +869,56 @@
 nsert;%0A%0A
+import com.google.common.base.Optional;%0A
 import l
@@ -1631,29 +1631,30 @@
 .impl.Insert
-Value
+Column
 sToken;%0Aimpo
@@ -2395,32 +2395,166 @@
 sertStatement);%0A
+                insertStatement.getSQLTokens().add(createInsertColumnsTokenFromMetaData(insertStatement, sqlSegment.getStopIndex()));%0A
             %7D el
@@ -2620,26 +2620,16 @@
 t);%0A    
-        %7D%0A
         
@@ -2671,56 +2671,55 @@
 add(
-new InsertValuesToken(sqlSegment.getStartIndex()
+createInsertColumnsTokenFromSQL(insertStatement
 , sq
@@ -2737,32 +2737,46 @@
 tStopIndex()));%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -3342,10 +3342,1277 @@
 %7D%0A    %7D%0A
+    %0A    private InsertColumnsToken createInsertColumnsTokenFromMetaData(final InsertStatement insertStatement, final int startIndex) %7B%0A        InsertColumnsToken result = new InsertColumnsToken(startIndex, false);%0A        result.getColumns().addAll(insertStatement.getColumnNames());%0A        fillQueryAssistedColumn(insertStatement, result);%0A        return result;%0A    %7D%0A    %0A    private InsertColumnsToken createInsertColumnsTokenFromSQL(final InsertStatement insertStatement, final int startIndex) %7B%0A        InsertColumnsToken result = new InsertColumnsToken(startIndex, true);%0A        fillQueryAssistedColumn(insertStatement, result);%0A        return result;%0A    %7D%0A    %0A    private void fillQueryAssistedColumn(final InsertStatement insertStatement, final InsertColumnsToken insertColumnsToken) %7B%0A        for (String each : insertStatement.getColumnNames()) %7B%0A            Optional%3CString%3E assistedColumnName = encryptRule.getEncryptorEngine().getAssistedQueryColumn(insertStatement.getTables().getSingleTableName(), each);%0A            if (assistedColumnName.isPresent()) %7B%0A                insertColumnsToken.getColumns().remove(assistedColumnName.get());%0A                insertColumnsToken.getColumns().add(assistedColumnName.get());%0A            %7D%0A        %7D%0A    %7D%0A
 %7D%0A

create InsertColumnsToken