001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.jdbc.server.sql;
020:
021: import java.sql.*;
022:
023: /**
024: * Represents a constant term, e.g. 'Smith', 3, etc.
025: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
026: * @version 3.3.2 <29 December 2004>
027: * @since 3.0
028: */
029:
030: public class Value extends ColumnTerm {
031:
032: private String stringValue = null;
033: private int type = Types.VARCHAR;
034:
035: /**
036: * Constructor.
037: * @param stringValue the value as string
038: */
039: public Value(String stringValue) {
040: this .stringValue = stringValue;
041: }
042:
043: /**
044: * Constructor.
045: * @param stringValue the value as string
046: * @param type the type
047: */
048: public Value(String stringValue, int type) {
049: super ();
050: this .stringValue = stringValue;
051: this .type = type;
052: }
053:
054: /**
055: * Compares objects.
056: * @param obj another object.
057: * @return a boolean
058: */
059: public boolean sameAs(Object obj) {
060: if (obj != null && obj instanceof Value) {
061: Value s = (Value) obj;
062: boolean result = stringValue == null ? s.stringValue == null
063: : stringValue.equals(s.stringValue);
064: return result;
065: }
066: return false;
067: }
068:
069: /** Getter for property stringValue.
070: * @return Value of property stringValue.
071: *
072: */
073: public java.lang.String getStringValue() {
074: return stringValue;
075: }
076:
077: /** Setter for property stringValue.
078: * @param stringValue New value of property stringValue.
079: *
080: */
081: public void setStringValue(java.lang.String stringValue) {
082: this .stringValue = stringValue;
083: }
084:
085: /**
086: * Gather the host variables.
087: * @param variables the list used to collect the variables
088: */
089: public void prepare(java.util.List variables) {
090: // nothing to do here
091: }
092:
093: /**
094: * @return int
095: */
096: public int getType() {
097: return type;
098: }
099:
100: /**
101: * Sets the type.
102: * @param type The type to set
103: */
104: public void setType(int type) {
105: this .type = type;
106: }
107:
108: /**
109: * Get the value as an instance of the class representing the actual type.
110: * Wrapper classes are used for primitive types.
111: * @return the converted value.
112: */
113: public Object getConvertedValue() {
114: if (type == Types.VARCHAR)
115: return stringValue;
116: if (type == Types.INTEGER)
117: return new Integer(stringValue);
118: if (type == Types.DECIMAL)
119: return new Double(stringValue);
120: if (type == Types.DOUBLE)
121: return new Double(stringValue);
122: if (type == Types.FLOAT)
123: return new Float(stringValue);
124: if (type == Types.BOOLEAN)
125: return new Boolean(stringValue);
126: if (type == Types.DATE)
127: return Date.valueOf(stringValue);
128: if (type == Types.TIME)
129: return Time.valueOf(stringValue);
130: if (type == Types.TIMESTAMP)
131: return Timestamp.valueOf(stringValue);
132: // todo support more types
133: else
134: throw new IllegalStateException(
135: "The literal type is not (yet) supported by "
136: + this );
137:
138: }
139:
140: /**
141: * Print the object on a buffer in order to display the parsed SQL.
142: * @param out a string bufer to print on
143: */
144: public void print(StringBuffer out) {
145: out.append(getStringValue());
146: }
147: }
|