001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.sql;
030:
031: import com.caucho.db.table.Column;
032:
033: public class Data {
034: private static final int NULL = 0;
035: private static final int BOOLEAN = NULL + 1;
036: private static final int STRING = BOOLEAN + 1;
037: private static final int INTEGER = STRING + 1;
038: private static final int LONG = INTEGER + 1;
039: private static final int DOUBLE = LONG + 1;
040: private static final int EXPR = DOUBLE + 1;
041:
042: private Column _column;
043:
044: private int _type;
045: private boolean _booleanData;
046: private String _stringData;
047: private int _intData;
048: private long _longData;
049: private double _doubleData;
050: private Expr _expr;
051:
052: public void clear() {
053: _type = NULL;
054: }
055:
056: public void setColumn(Column column) {
057: _column = column;
058: }
059:
060: public Column getColumn() {
061: return _column;
062: }
063:
064: /**
065: * Returns treu for a null value.
066: */
067: public boolean isNull() {
068: return _type == NULL;
069: }
070:
071: /**
072: * Sets the value as a string.
073: */
074: public void setString(String value) {
075: if (value == null)
076: _type = NULL;
077: else {
078: _type = STRING;
079: _stringData = value;
080: }
081: }
082:
083: /**
084: * Returns the value as a string.
085: */
086: public String getString() {
087: switch (_type) {
088: case NULL:
089: return null;
090:
091: case BOOLEAN:
092: return _booleanData ? "true" : "false";
093:
094: case INTEGER:
095: return String.valueOf(_intData);
096:
097: case LONG:
098: return String.valueOf(_longData);
099:
100: case DOUBLE:
101: return String.valueOf(_doubleData);
102:
103: case STRING:
104: return _stringData;
105:
106: default:
107: throw new UnsupportedOperationException();
108: }
109: }
110:
111: /**
112: * Sets the value as a boolean.
113: */
114: public void setBoolean(boolean value) {
115: _type = BOOLEAN;
116: _booleanData = value;
117: }
118:
119: /**
120: * Returns the value as a boolean
121: */
122: public int getBoolean() {
123: switch (_type) {
124: case NULL:
125: return Expr.UNKNOWN;
126:
127: case BOOLEAN:
128: return _booleanData ? Expr.TRUE : Expr.FALSE;
129:
130: case INTEGER:
131: return _intData != 0 ? Expr.TRUE : Expr.FALSE;
132:
133: case LONG:
134: return _longData != 0 ? Expr.TRUE : Expr.FALSE;
135:
136: case DOUBLE:
137: return _doubleData != 0 ? Expr.TRUE : Expr.FALSE;
138:
139: case STRING:
140: return _stringData.equalsIgnoreCase("y") ? Expr.TRUE
141: : Expr.FALSE;
142:
143: default:
144: throw new UnsupportedOperationException();
145: }
146: }
147:
148: /**
149: * Sets the value as an integer.
150: */
151: public void setInt(int value) {
152: _type = INTEGER;
153: _intData = value;
154: }
155:
156: /**
157: * Returns the value as an integer.
158: */
159: public int getInt() {
160: switch (_type) {
161: case NULL:
162: return 0;
163:
164: case BOOLEAN:
165: return _booleanData ? 1 : 0;
166:
167: case INTEGER:
168: return _intData;
169:
170: case LONG:
171: return (int) _longData;
172:
173: case DOUBLE:
174: return (int) _doubleData;
175:
176: case STRING:
177: return Integer.parseInt(_stringData);
178:
179: default:
180: throw new UnsupportedOperationException();
181: }
182: }
183:
184: /**
185: * Sets the value as a long.
186: */
187: public void setLong(long value) {
188: _type = LONG;
189: _longData = value;
190: }
191:
192: /**
193: * Returns the value as a long.
194: */
195: public long getLong() {
196: switch (_type) {
197: case NULL:
198: return 0;
199:
200: case BOOLEAN:
201: return _booleanData ? 1 : 0;
202:
203: case INTEGER:
204: return _intData;
205:
206: case LONG:
207: return _longData;
208:
209: case DOUBLE:
210: return (long) _doubleData;
211:
212: case STRING:
213: return Long.parseLong(_stringData);
214:
215: default:
216: throw new UnsupportedOperationException();
217: }
218: }
219:
220: /**
221: * Returns the value as a date.
222: */
223: public long getDate() {
224: switch (_type) {
225: case NULL:
226: return 0;
227:
228: case BOOLEAN:
229: return _booleanData ? 1 : 0;
230:
231: case INTEGER:
232: return _intData;
233:
234: case LONG:
235: return _longData;
236:
237: case DOUBLE:
238: return (long) _doubleData;
239:
240: case STRING:
241: return Long.parseLong(_stringData);
242:
243: default:
244: throw new UnsupportedOperationException();
245: }
246: }
247:
248: /**
249: * Sets the value as a double.
250: */
251: public void setDouble(double value) {
252: _type = DOUBLE;
253: _doubleData = value;
254: }
255:
256: /**
257: * Returns the value as a double.
258: */
259: public double getDouble() {
260: switch (_type) {
261: case NULL:
262: return 0;
263:
264: case BOOLEAN:
265: return _booleanData ? 1 : 0;
266:
267: case INTEGER:
268: return _intData;
269:
270: case LONG:
271: return _longData;
272:
273: case DOUBLE:
274: return _doubleData;
275:
276: case STRING:
277: return Double.parseDouble(_stringData);
278:
279: default:
280: throw new UnsupportedOperationException();
281: }
282: }
283:
284: /**
285: * Returns the value as a double.
286: */
287: public void copyTo(Data dst) {
288: switch (_type) {
289: case NULL:
290: dst.setString(null);
291: break;
292:
293: case BOOLEAN:
294: dst.setBoolean(_booleanData);
295: break;
296:
297: case INTEGER:
298: dst.setInt(_intData);
299: break;
300:
301: case LONG:
302: dst.setLong(_longData);
303: break;
304:
305: case DOUBLE:
306: dst.setDouble(_doubleData);
307: break;
308:
309: case STRING:
310: dst.setString(_stringData);
311: break;
312:
313: default:
314: throw new UnsupportedOperationException();
315: }
316: }
317:
318: /**
319: * Returns a hash code
320: */
321: public int hashCode() {
322: switch (_type) {
323: case NULL:
324: return 17;
325:
326: case BOOLEAN:
327: return _booleanData ? 1 : 0;
328:
329: case INTEGER:
330: return _intData;
331:
332: case LONG:
333: return (int) _longData;
334:
335: case DOUBLE:
336: return (int) _doubleData;
337:
338: case STRING:
339: return _stringData.hashCode();
340:
341: default:
342: return 97;
343: }
344: }
345:
346: /**
347: * Returns the equality test.
348: */
349: public boolean equals(Object o) {
350: if (this == o)
351: return true;
352: else if (!getClass().equals(o.getClass()))
353: return false;
354:
355: Data data = (Data) o;
356:
357: if (_type != data._type)
358: return false;
359:
360: switch (_type) {
361: case NULL:
362: return false;
363:
364: case BOOLEAN:
365: return _booleanData == data._booleanData;
366:
367: case INTEGER:
368: return _intData == data._intData;
369:
370: case LONG:
371: return _longData == data._longData;
372:
373: case DOUBLE:
374: return _doubleData == data._doubleData;
375:
376: case STRING:
377: return _stringData.equals(data._stringData);
378:
379: default:
380: return false;
381: }
382: }
383: }
|