/*
 * 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.test.integration.cases.value;

import lombok.Getter;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * SQL value.
 */
@Getter
public final class SQLValue {
    
    private final Object value;
    
    private final int index;
    
    public SQLValue(final String value, final String type, final int index) throws ParseException {
        this.value = getValue(value, type);
        this.index = index;
    }
    
    private Object getValue(final String value, final String type) throws ParseException {
        if (null == type || "varchar".equals(type) || "char".equals(type) || "String".equals(type)) {
            return value;
        }
        if ("int".equals(type)) {
            return Integer.valueOf(value);
        }
        if ("numeric".equals(type) && !value.contains("//.")) {
            return Long.valueOf(value);
        }
        if ("numeric".equals(type) && value.contains("//.")) {
            return Double.valueOf(value);
        }
        if ("datetime".equals(type)) {
            return new Date(new SimpleDateFormat("yyyy-MM-dd").parse(value).getTime());
        }
        throw new UnsupportedOperationException(String.format("Cannot support type: `%s`", type));
    }
    
    @Override
    public String toString() {
        if (value instanceof String) {
            return "'" + value + "'";
        }
        if (value instanceof Date) {
            return new SimpleDateFormat("yyyy-MM-dd").format(value);
        }
        return value.toString();
    }
}

@@ -973,16 +973,41 @@
 eFormat;
+%0Aimport java.util.Locale;
 %0A%0A/**%0A *
@@ -1256,16 +1256,39 @@
 .value =
+ null == type ? value :
  getValu
@@ -1445,102 +1445,108 @@
     
-if (null == type %7C%7C %22varchar%22.equals(type) %7C%7C %22char%22.equals(type) %7C%7C %22String%22.equals(type)) %7B%0A
+switch (type) %7B%0A            case %22String%22:%0A            case %22varchar%22:%0A            case %22char%22:%0A    
     
@@ -1579,44 +1579,28 @@
     
-%7D%0A
     
-    if (%22int%22.equals(type)) %7B%0A
+case %22int%22:%0A    
     
@@ -1622,23 +1622,24 @@
 Integer.
-valueOf
+parseInt
 (value);
@@ -1651,74 +1651,102 @@
     
-%7D%0A        if (%22numeric%22.equals(type) && !value.contains(%22//.%22)) %7B%0A
+    case %22long%22:%0A                return Long.parseLong(value);%0A            case %22double%22:%0A    
     
@@ -1764,20 +1764,26 @@
 urn 
-Long.valueOf
+Double.parseDouble
 (val
@@ -1795,30 +1795,25 @@
         
-%7D%0A
     
-    if (
+case 
 %22numeric
@@ -1817,141 +1817,166 @@
 ric%22
-.equals(type) && value.contains(%22//.%22)) %7B%0A            return 
+:%0A                return value.contains(%22//.%22) ? Double.parse
 Double
-.
+(
 value
-Of(value);%0A        %7D%0A        if (%22datetime%22.equals(type)) %7B%0A
+) : Long.parseLong(value);%0A            case %22Date%22:%0A            case %22datetime%22:%0A    
     
@@ -2032,16 +2032,37 @@
 y-MM-dd%22
+, Locale.getDefault()
 ).parse(
@@ -2080,34 +2080,53 @@
 ime());%0A        
-%7D%0A
+    default:%0A        
         throw ne
@@ -2204,24 +2204,34 @@
 %60%22, type));%0A
+        %7D%0A
     %7D%0A    %0A 
@@ -2450,16 +2450,37 @@
 y-MM-dd%22
+, Locale.getDefault()
 ).format

Simplify SQLValue (#9424)