001: // SQL.java
002: // $Id: SQL.java,v 1.8 2000/08/16 21:37:50 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.tools.jdbc;
006:
007: import java.text.SimpleDateFormat;
008: import java.util.Date;
009: import java.util.TimeZone;
010:
011: /**
012: * @version $Revision: 1.8 $
013: * @author Benoît Mahé (bmahe@w3.org)
014: */
015: public class SQL {
016:
017: public static SimpleDateFormat formatter = null;
018:
019: static {
020: formatter = new SimpleDateFormat("yyyy-MM-dd");
021: }
022:
023: public static String encode(String string) {
024: int len = string.length();
025: StringBuffer buffer = new StringBuffer(len);
026: char c;
027: buffer.append("'");
028: for (int i = 0; i < len; i++) {
029: switch (c = string.charAt(i)) {
030: case '\'':
031: buffer.append("\\'");
032: break;
033: default:
034: buffer.append(c);
035: }
036: }
037: buffer.append("'");
038: return buffer.toString();
039: }
040:
041: public static Object getMatchingValue(Class c, Object value) {
042: if (value == null) {
043: return null;
044: }
045: if (c.isInstance(value)) {
046: return value;
047: }
048: String stringvalue = String.valueOf(value);
049: if (c == String.class) {
050: return stringvalue;
051: } else if ((c == Integer.class) || (c == int.class)) {
052: return new Integer(stringvalue);
053: } else if ((c == Long.class) || (c == long.class)) {
054: return new Long(stringvalue);
055: } else if ((c == Boolean.class) || (c == boolean.class)) {
056: return new Boolean(
057: (stringvalue.equalsIgnoreCase("true")
058: || stringvalue.equalsIgnoreCase("t")
059: || stringvalue.equalsIgnoreCase("y")
060: || stringvalue.equalsIgnoreCase("yes") || stringvalue
061: .equalsIgnoreCase("1")));
062: } else if ((c == Character.class) || (c == char.class)) {
063: return new Character(stringvalue.charAt(0));
064: } else if ((c == Double.class) || (c == double.class)) {
065: return new Double(stringvalue);
066: } else if ((c == Float.class) || (c == float.class)) {
067: return new Float(stringvalue);
068: } else if ((c == Short.class) || (c == short.class)) {
069: return new Short(stringvalue);
070: }
071: return null;
072: }
073:
074: public static String getSQLValue(Object value) {
075: Class c = value.getClass();
076: if (c == String.class) {
077: return encode((String) value);
078: } else if ((c == Date.class) || (c == java.sql.Date.class)) {
079: String date = formatter.format((Date) value);
080: return encode(date);
081: } else {
082: return String.valueOf(value);
083: }
084: }
085:
086: /**
087: * Split the SQL operator and the value, (default operator is '=')
088: * example:<br>
089: * "~*.*toto.*" will become { "~*", ".*toto.*" }<br>
090: * but "\~*.*toto.*" will become { "=", "~*.*toto.*" }
091: * <p>possible operators are:
092: * <table border=0>
093: * <tr><td> < </td><td>Less than?</td></tr>
094: * <tr><td> <= </td><td>Less than or equals?</td></tr>
095: * <tr><td> <> </td><td>Not equal?</td></tr>
096: * <tr><td> = </td><td>Equals?</td></tr>
097: * <tr><td> > </td><td>Greater than?</td></tr>
098: * <tr><td> >= </td><td>Greater than or equals?</td></tr>
099: * <tr><td> ~~ </td><td>LIKE</td></tr>
100: * <tr><td> !~~ </td><td>NOT LIKE</td></tr>
101: * <tr><td> ~ </td><td>Match (regex), case sensitive</td></tr>
102: * <tr><td> ~* </td><td>Match (regex), case insensitive</td></tr>
103: * <tr><td> !~ </td><td>Does not match (regex), case sensitive </td></tr>
104: * <tr><td> !~* </td><td>Does not match (regex), case insensitive</td></tr>
105: * </table>
106: */
107: public static String[] getSQLOperator(Object val) {
108: Class cl = val.getClass();
109: if (cl != String.class) {
110: String split[] = { " = ", getSQLValue(val) };
111: return split;
112: }
113: String value = (String) val;
114: String result[] = new String[2];
115: char c = value.charAt(0);
116: switch (c) {
117: case '~':
118: c = value.charAt(1);
119: if (c == '*') { // ~*
120: result[0] = " ~* ";
121: result[1] = value.substring(2);
122: } else if (c == '~') { // ~~
123: result[0] = " ~~ ";
124: result[1] = value.substring(2);
125: } else { // ~
126: result[0] = " ~ ";
127: result[1] = value.substring(1);
128: }
129: break;
130: case '<':
131: c = value.charAt(1);
132: if (c == '=') { // <=
133: result[0] = " <= ";
134: result[1] = value.substring(2);
135: } else if (c == '>') { // <>
136: result[0] = " <> ";
137: result[1] = value.substring(2);
138: } else { // <
139: result[0] = " < ";
140: result[1] = value.substring(1);
141: }
142: break;
143: case '>':
144: c = value.charAt(1);
145: if (c == '=') { // >=
146: result[0] = " >= ";
147: result[1] = value.substring(2);
148: } else { // >
149: result[0] = " > ";
150: result[1] = value.substring(1);
151: }
152: break;
153: case '!':
154: if (c == '~') {
155: c = value.charAt(2);
156: if (c == '~') { // !~~
157: result[0] = " !~~ ";
158: result[1] = value.substring(3);
159: } else if (c == '*') { // !~*
160: result[0] = " !~* ";
161: result[1] = value.substring(3);
162: } else { // !~
163: result[0] = " !~ ";
164: result[1] = value.substring(2);
165: }
166: break;
167: }
168: case '\\':
169: value = value.substring(1);
170: case '=': // =
171: default:
172: result[0] = " = ";
173: result[1] = value;
174: }
175: result[1] = encode(result[1]);
176: return result;
177: }
178:
179: }
|