Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / tools / jdbc / SQL.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/jdbc/SQL.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/jdbc/SQL.java
new file mode 100644 (file)
index 0000000..e1b682b
--- /dev/null
@@ -0,0 +1,180 @@
+// SQL.java\r
+// $Id: SQL.java,v 1.1 2010/06/15 12:27:29 smhuang Exp $\r
+// (c) COPYRIGHT MIT, INRIA and Keio, 2000.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+package org.w3c.tools.jdbc;\r
+\r
+import java.text.SimpleDateFormat;\r
+import java.util.Date;\r
+import java.util.TimeZone;\r
+\r
+/**\r
+ * @version $Revision: 1.1 $\r
+ * @author  Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class SQL {\r
+\r
+    public static SimpleDateFormat formatter = null;\r
+\r
+    static {\r
+       formatter = new SimpleDateFormat("yyyy-MM-dd");\r
+    }\r
+\r
+    public static String encode(String string) {\r
+       int          len    = string.length();\r
+       StringBuffer buffer = new StringBuffer(len);\r
+       char         c;\r
+       buffer.append("'");\r
+       for (int i = 0 ; i < len ; i++) {\r
+           switch (c = string.charAt(i)) \r
+               {\r
+               case '\'':\r
+                   buffer.append("\\'");\r
+                   break;\r
+               default:\r
+                   buffer.append(c);\r
+               }\r
+       }\r
+       buffer.append("'");\r
+       return buffer.toString();\r
+    }\r
+\r
+    public static Object getMatchingValue(Class c, Object value) {\r
+       if (value == null) {\r
+           return null;\r
+       }\r
+       if (c.isInstance(value)) {\r
+           return value;\r
+       }\r
+       String stringvalue = String.valueOf(value);\r
+       if (c == String.class) {\r
+           return stringvalue;\r
+       } else if ((c == Integer.class) || (c == int.class)) {\r
+           return new Integer(stringvalue);\r
+       } else if ((c == Long.class) || (c == long.class)) {\r
+           return new Long(stringvalue);\r
+       } else if ((c == Boolean.class) || (c == boolean.class)) {\r
+           return new Boolean ((stringvalue.equalsIgnoreCase("true") ||\r
+                                stringvalue.equalsIgnoreCase("t") ||\r
+                                stringvalue.equalsIgnoreCase("y") ||\r
+                                stringvalue.equalsIgnoreCase("yes") ||\r
+                                stringvalue.equalsIgnoreCase("1")));\r
+       } else if ((c == Character.class) || (c == char.class)) {\r
+           return new Character(stringvalue.charAt(0));\r
+       } else if ((c == Double.class) || (c == double.class)) {\r
+           return new Double(stringvalue);\r
+       } else if ((c == Float.class) || (c == float.class)) {\r
+           return new Float(stringvalue);\r
+       } else if ((c == Short.class) || (c == short.class)) {\r
+           return new Short(stringvalue);\r
+       }\r
+       return null;\r
+    }\r
+\r
+    public static String getSQLValue(Object value) {\r
+       Class c = value.getClass();\r
+       if (c == String.class) {\r
+           return encode((String)value);\r
+       } else if ((c == Date.class) || (c == java.sql.Date.class)) {\r
+           String date = formatter.format((Date)value);\r
+           return encode(date);\r
+       } else {\r
+           return String.valueOf(value);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Split the SQL operator and the value, (default operator is '=')\r
+     * example:<br>\r
+     * "~*.*toto.*" will become { "~*", ".*toto.*" }<br>\r
+     * but "\~*.*toto.*" will become { "=", "~*.*toto.*" }\r
+     * <p>possible operators are:\r
+     * <table border=0>\r
+     * <tr><td> &lt; </td><td>Less than?</td></tr> \r
+     * <tr><td> &lt;= </td><td>Less than or equals?</td></tr>  \r
+     * <tr><td> &lt;&gt; </td><td>Not equal?</td></tr>  \r
+     * <tr><td> = </td><td>Equals?</td></tr>  \r
+     * <tr><td> &gt; </td><td>Greater than?</td></tr>\r
+     * <tr><td> &gt;= </td><td>Greater than or equals?</td></tr>\r
+     * <tr><td> ~~ </td><td>LIKE</td></tr>\r
+     * <tr><td> !~~ </td><td>NOT LIKE</td></tr>\r
+     * <tr><td> ~ </td><td>Match (regex), case sensitive</td></tr>\r
+     * <tr><td> ~* </td><td>Match (regex), case insensitive</td></tr>\r
+     * <tr><td> !~ </td><td>Does not match (regex), case sensitive </td></tr>\r
+     * <tr><td> !~* </td><td>Does not match (regex), case insensitive</td></tr>\r
+     * </table>\r
+     */\r
+    public static String[] getSQLOperator(Object val) {\r
+       Class cl = val.getClass();\r
+       if (cl != String.class) {\r
+           String split[] = { " = ", getSQLValue(val) };\r
+           return split;\r
+       }\r
+       String value = (String) val;\r
+       String result[] = new String[2];\r
+       char c = value.charAt(0);\r
+       switch (c) \r
+           {\r
+           case '~':\r
+               c = value.charAt(1);\r
+               if (c == '*') {                       // ~*\r
+                   result[0] = " ~* ";\r
+                   result[1] = value.substring(2);\r
+               } else if (c == '~') {                // ~~\r
+                   result[0] = " ~~ ";\r
+                   result[1] = value.substring(2);\r
+               } else {                              // ~\r
+                   result[0] = " ~ ";\r
+                   result[1] = value.substring(1);\r
+               }\r
+               break;\r
+           case '<':\r
+               c = value.charAt(1);\r
+               if (c == '=') {                       // <=\r
+                   result[0] = " <= ";\r
+                   result[1] = value.substring(2);\r
+               } else if (c == '>') {                // <>\r
+                   result[0] = " <> ";\r
+                   result[1] = value.substring(2);\r
+               } else {                              // <\r
+                   result[0] = " < ";\r
+                   result[1] = value.substring(1);\r
+               }\r
+               break;\r
+           case '>':\r
+               c = value.charAt(1);\r
+               if (c == '=') {                      // >=\r
+                   result[0] = " >= ";\r
+                   result[1] = value.substring(2);\r
+               } else {                             // >\r
+                   result[0] = " > ";\r
+                   result[1] = value.substring(1);\r
+               }\r
+               break;\r
+           case '!':\r
+               if (c == '~') {\r
+                   c = value.charAt(2);\r
+                   if (c == '~') {                 // !~~\r
+                       result[0] = " !~~ ";\r
+                       result[1] = value.substring(3);\r
+                   } else if (c == '*') {          // !~* \r
+                       result[0] = " !~* ";\r
+                       result[1] = value.substring(3);\r
+                   } else {                        // !~\r
+                       result[0] = " !~ ";\r
+                       result[1] = value.substring(2);\r
+                   }\r
+                   break;\r
+               } \r
+           case '\\':\r
+               value = value.substring(1);\r
+           case '=':                               // =\r
+           default:\r
+               result[0] = " = ";\r
+               result[1] = value;\r
+           }\r
+       result[1] = encode(result[1]);\r
+       return result;\r
+    }\r
+\r
+}\r