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

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.constant.ShardingOperator;

import java.util.Collection;
import java.util.Map;

/**
 * Encrypt Column placeholder for rewrite.
 *
 * @author panjuan
 */
@RequiredArgsConstructor
@Getter
public final class EncryptColumnPlaceholder implements ShardingPlaceholder {
    
    private final String logicTableName;
    
    private final String columnName;
    
    private final Map<Integer, Comparable<?>> indexValues;
    
    private final Collection<Integer> placeholderIndex;
    
    private final ShardingOperator operator;
    
    @Override
    public String toString() {
        switch (operator) {
            case EQUAL:
                return placeholderIndex.isEmpty() ? String.format("%s %s %s", columnName, operator.name(), indexValues.get(0)) : String.format("%s = ?", columnName);
            case BETWEEN:
                return toStringFromBetween();
            case IN:
                
    
        }
       
    }
    
    private String toStringFromBetween() {
        if (placeholderIndex.isEmpty()) {
            return String.format("%s %s %s", indexValues.get(0), operator.name(), indexValues.get(1));
        }
        if (2 == placeholderIndex.size()) {
            return String.format("? %s ?", operator.name());
        }
        if (0 == placeholderIndex.iterator().next()) {
            return String.format("? %s %s", operator.name(), indexValues.get(0));
        }
        return String.format("%s %s ?", indexValues.get(0), operator.name());
    }
}

@@ -1657,18 +1657,22 @@
 (%22%25s %25s 
-%25s
+%5C%22%25s%5C%22
 %22, colum
@@ -1863,21 +1863,88 @@
         
-%0A    
+return toStringFromIn();%0A            default:%0A                return %22%22;
 %0A       
@@ -1950,16 +1950,8 @@
   %7D%0A
-       %0A
     
@@ -2086,31 +2086,38 @@
  %25s 
-%25s%22, indexValues.get(0)
+%5C%22%25s%5C%22 AND %5C%22%25s%5C%22%22, columnName
 , op
@@ -2122,32 +2122,52 @@
 operator.name(),
+ indexValues.get(0),
  indexValues.get
@@ -2264,15 +2264,34 @@
 at(%22
-?
+%25s
  %25s ?
-%22
+ AND ?%22, columnName
 , op
@@ -2409,16 +2409,39 @@
 at(%22
-?
+%25s
  %25s 
-%25s%22
+? AND %5C%22%25s%5C%22%22, columnName
 , op
@@ -2526,47 +2526,724 @@
  %25s 
-?%22, indexValues.get(0), operator.name()
+%5C%22%25s%5C%22 AND ?%22, columnName, operator.name(), indexValues.get(0));%0A    %7D%0A    %0A    private String toStringFromIn() %7B%0A        StringBuilder stringBuilder = new StringBuilder();%0A        stringBuilder.append(columnName).append(%22 %22).append(operator.name()).append(%22%EF%BC%88%22);%0A        for (int i = 0; i %3C indexValues.size() + placeholderIndex.size(); i++) %7B%0A            if (placeholderIndex.contains(i)) %7B%0A                stringBuilder.append(%22?%22);%0A            %7D else %7B%0A                stringBuilder.append('%22').append(indexValues.get(i)).append('%22');%0A            %7D%0A            stringBuilder.append(%22, %22);%0A        %7D%0A        stringBuilder.deleteCharAt(stringBuilder.length() - 1).append(%22)%22);%0A        return stringBuilder.toString(
 );%0A 

rewrite toString()