01: package org.obe.sql;
02:
03: import java.io.IOException;
04: import java.io.Writer;
05: import java.text.ParseException;
06: import java.util.Date;
07: import org.obe.util.DateUtilities;
08:
09: /**
10: * @author Adrian Price
11: */
12: public class SQLLiteral extends SimpleNode {
13: boolean parameter;
14: Object value;
15:
16: public SQLLiteral(int id) {
17: super (id);
18: }
19:
20: public void setString(String s) {
21: s = s.substring(1, s.length() - 1);
22: try {
23: // See if we can parse the string as a date literal
24: value = DateUtilities.getInstance().parse(s);
25: } catch (ParseException e) {
26: // If not, just store it as a normal string.
27: value = s;
28: }
29: }
30:
31: public void setInteger(String s) {
32: value = Integer.valueOf(s);
33: }
34:
35: public void setDouble(String s) {
36: value = Double.valueOf(s);
37: }
38:
39: public void setParameter() {
40: parameter = true;
41: }
42:
43: public boolean isParameter() {
44: return parameter;
45: }
46:
47: public void write(Writer out) throws IOException {
48: if (parameter) {
49: out.write('?');
50: } else if (value instanceof Date) {
51: out.write('\'');
52: out.write(DateUtilities.getInstance().format((Date) value));
53: out.write('\'');
54: } else if (value instanceof String) {
55: out.write('\'');
56: out.write(value.toString());
57: out.write('\'');
58: } else {
59: out.write(value.toString());
60: }
61: }
62:
63: public Object execute(Object context) {
64: return value;
65: }
66: }
|