KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > Value


1 package com.quadcap.sql.types;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 import java.util.Hashtable JavaDoc;
46
47 import java.sql.SQLException JavaDoc;
48 import java.sql.Types JavaDoc;
49
50 import com.quadcap.sql.file.Datafile;
51
52 import com.quadcap.sql.io.ObjectInputStream;
53 import com.quadcap.sql.io.ObjectOutputStream;
54
55 import com.quadcap.util.Debug;
56
57 /**
58  * A runtime SQL value.
59  *
60  * <p>Types:
61  * <ul>
62  * <li>Numeric
63  * <li>Decimal
64  * <li>Integer
65  * <li>SmallInt
66  *
67  * <li>Float
68  * <li>Double
69  *
70  * <li>Timestamp
71  * <li>Interval
72  *
73  * <li>Char
74  *
75  * <li>Binary
76  *
77  * <li>Boolean
78  *
79  * <li>BLOB</li>
80  * <li>CLOB</li>
81  * </ul>
82  *
83  * @author Stan Bailes
84  */

85 public abstract class Value {
86     public Value() {}
87
88     public static boolean isCaseSensitive = true;
89
90     /**
91      * Two-level virtual operator dispatch. Each subtype implements the
92      * same basic template for this abstract function:
93      * <pre>
94      *
95      * Value binop(int op, Value l) {
96      * return l.binop(op, this);
97      * }
98      *
99      * </pre>
100      * Then, type overloading allows for each type to implement all
101      * ops for which it can be the left hand side for all allowed types
102      * on the right hand side:
103      * Value binop(int op, ValueInteger l);
104      * Value binop(int op, ValueLong l);
105      * ...
106      */

107     abstract public Value binop(int op, Value l) throws ValueException;
108
109     abstract public Object JavaDoc asJavaObject() throws SQLException JavaDoc;
110
111     abstract public Type getType() throws SQLException JavaDoc;
112
113     abstract public void fromJavaObject(Object JavaDoc obj) throws ValueException;
114
115     abstract public void serializeKey(KeyStream out) throws IOException JavaDoc;
116
117     public static String JavaDoc tn(Value v) {
118         String JavaDoc s = v.getClass().getName();
119         int idx = s.lastIndexOf('.');
120         if (idx > 0) s = s.substring(idx+1);
121         return s;
122     }
123     
124     public static final Value binop(int op, Value l, Value r)
125     throws ValueException
126     {
127         Value ret = r.binop(op, l);
128 // Debug.println("binop(" + Op.toString(op) + ", " +
129
// tn(l) + ":" +
130
// l + ", " +
131
// tn(r) + ":" +
132
// r + ") = " +
133
// tn(ret) + ":" +
134
// ret);
135
return ret;
136     }
137
138     final ValueException badType(Type type) {
139     return new ValueException("Not " + type + ": " +
140                  this.getClass().getName());
141     }
142
143     // grep alltypes (also io.Extern)
144
public Value convert(TypeBigInt type) throws ValueException {
145     throw badType(type);
146     }
147
148     public Value convert(TypeBinary type) throws ValueException {
149     throw badType(type);
150     }
151
152     public Value convert(TypeBlob type) throws ValueException {
153     throw badType(type);
154     }
155
156     public Value convert(TypeBoolean type) throws ValueException {
157     throw badType(type);
158     }
159
160     public Value convert(TypeChar type) throws ValueException {
161     String JavaDoc s = this.toString();
162     final int max = type.getMax();
163     if (max > 0 && s.length() > max) s = s.substring(0, max);
164     if (s.length() < max) {
165         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
166         while (sb.length() < max) sb.append(' ');
167         s = sb.toString();
168     }
169     return new ValueString(s);
170     }
171
172     public Value convert(TypeClob type) throws ValueException {
173     throw badType(type);
174     }
175
176     public Value convert(TypeDate type) throws ValueException {
177     throw badType(type);
178     }
179
180     public Value convert(TypeDecimal type) throws ValueException {
181     throw badType(type);
182     }
183
184     public Value convert(TypeInt type) throws ValueException {
185     throw badType(type);
186     }
187
188     public Value convert(TypeInterval type) throws ValueException {
189     throw badType(type);
190     }
191
192     public Value convert(TypeReal type) throws ValueException {
193     throw badType(type);
194     }
195
196     public Value convert(TypeSmallInt type) throws ValueException {
197     throw badType(type);
198     }
199
200     public Value convert(TypeVarChar type) throws ValueException {
201     String JavaDoc s = this.toString();
202     final int max = type.getMax();
203     if (max > 0 && s.length() > max) s = s.substring(0, max);
204     return new ValueString(s);
205     }
206
207     public Value convert(TypeVarBinary type) throws ValueException {
208     throw badType(type);
209     }
210
211     public Value convert(TypeTime type) throws ValueException {
212     throw badType(type);
213     }
214
215     public Value convert(TypeTimestamp type) throws ValueException {
216     throw badType(type);
217     }
218
219     public Value convert(TypeTinyInt type) throws ValueException {
220     throw badType(type);
221     }
222
223
224     public Value binop(int op, ValueBlob v) throws ValueException {
225     throw badBinop(op, v);
226     }
227     
228     public Value binop(int op, ValueBoolean v) throws ValueException {
229     throw badBinop(op, v);
230     }
231     
232     public Value binop(int op, ValueByte v) throws ValueException {
233     throw badBinop(op, v);
234     }
235     
236     public Value binop(int op, ValueClob v) throws ValueException {
237     throw badBinop(op, v);
238     }
239     
240     public Value binop(int op, ValueDate v) throws ValueException {
241     throw badBinop(op, v);
242     }
243     
244     public Value binop(int op, ValueDouble v) throws ValueException {
245     throw badBinop(op, v);
246     }
247     
248     public Value binop(int op, ValueFloat v) throws ValueException {
249     throw badBinop(op, v);
250     }
251     
252     public Value binop(int op, ValueInteger v) throws ValueException {
253     throw badBinop(op, v);
254     }
255     
256     public Value binop(int op, ValueInterval v) throws ValueException {
257     throw badBinop(op, v);
258     }
259
260     public Value binop(int op, ValueLong v) throws ValueException {
261     throw badBinop(op, v);
262     }
263     
264     public Value binop(int op, ValueNull v) throws ValueException {
265     throw badBinop(op, v);
266     }
267     
268     public Value binop(int op, ValueOctets v) throws ValueException {
269     throw badBinop(op, v);
270     }
271     
272     public Value binop(int op, ValuePattern v) throws ValueException {
273     throw badBinop(op, v);
274     }
275     
276     public Value binop(int op, ValueScaledInteger v) throws ValueException {
277     throw badBinop(op, v);
278     }
279      
280     public Value binop(int op, ValueShort v) throws ValueException {
281     throw badBinop(op, v);
282     }
283     
284     public Value binop(int op, ValueString l) throws ValueException {
285     return ValueString.binop_convert(op, l, this);
286     }
287
288     public Value binop(int op, ValueTime v) throws ValueException {
289     throw badBinop(op, v);
290     }
291     
292     public Value binop(int op, ValueTimestamp v) throws ValueException {
293     throw badBinop(op, v);
294     }
295     
296     public Value binop(int op, ValueUnknown v) throws ValueException {
297     return ValueUnknown.valueUnknown;
298     }
299
300     public static final ValueException badBinop(int op, Value l, Value r) {
301     return new ValueException("not implemented: " +
302                   l.getClass().getName() + " " +
303                   Op.toString(op) + " " +
304                   r.getClass().getName());
305     }
306     
307     public final ValueException badBinop(int op, Value v) {
308     return badBinop(op, this, v);
309     }
310
311     public static final boolean isTrue(Value ret) throws ValueException {
312     if (ret instanceof ValueBoolean) {
313         return ((ValueBoolean)ret).isTrue();
314     } else if (ret instanceof ValueUnknown) {
315         return false;
316     }
317     throw new ValueException("Not boolean: " + ret);
318     }
319     
320     public static final boolean boolOp(int op, Value l, Value r)
321     throws ValueException
322     {
323     Value ret = binop(op, l, r);
324         return isTrue(ret);
325     }
326
327     public Value unop(int op) throws ValueException {
328     throw new ValueException("not implemented: " +
329                  getClass().getName() + " unary op: " +
330                  Op.toString(op));
331     }
332
333     public static final boolean isNull(Value val) {
334     return val instanceof ValueNull;
335     }
336
337     public static final byte[] bytes(Value val) {
338     try {
339         ObjectOutputStream os = new ObjectOutputStream(null);
340         os.writeObject(val);
341         return os.toByteArray();
342     } catch (IOException JavaDoc e) {
343         Debug.print(e);
344         throw new RuntimeException JavaDoc(e.toString());
345     }
346     }
347
348     public static final Value fromBytes(byte[] b) {
349     try {
350         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(b);
351         ObjectInputStream is = new ObjectInputStream(bis);
352         return (Value)is.readObject();
353     } catch (Exception JavaDoc e) {
354         Debug.print(e);
355         throw new RuntimeException JavaDoc(e.toString());
356     }
357     }
358
359     static final Hashtable JavaDoc javaTypes = new Hashtable JavaDoc();
360     static final void jc(String JavaDoc javaClass, String JavaDoc valueClass) {
361     try {
362         javaTypes.put(javaClass, Class.forName(valueClass));
363     } catch (ClassNotFoundException JavaDoc e) {
364         Debug.print(e);
365     }
366     }
367
368     static {
369     jc("[B", "com.quadcap.sql.types.ValueOctets");
370     jc("java.lang.Boolean", "com.quadcap.sql.types.ValueBoolean");
371     jc("java.lang.Byte", "com.quadcap.sql.types.ValueByte");
372     jc("java.lang.Double", "com.quadcap.sql.types.ValueDouble");
373     jc("java.lang.Float", "com.quadcap.sql.types.ValueFloat");
374     jc("java.lang.Integer", "com.quadcap.sql.types.ValueInteger");
375     jc("java.lang.Long", "com.quadcap.sql.types.ValueLong");
376     jc("java.lang.Short", "com.quadcap.sql.types.ValueShort");
377     jc("java.lang.String", "com.quadcap.sql.types.ValueString");
378     jc("java.math.BigDecimal", "com.quadcap.sql.types.ValueScaledInteger");
379     jc("java.sql.Date", "com.quadcap.sql.types.ValueDate");
380     jc("java.sql.Time", "com.quadcap.sql.types.ValueTime");
381     jc("java.sql.Timestamp", "com.quadcap.sql.types.ValueTimestamp");
382     jc("java.util.Date", "com.quadcap.sql.types.ValueTimestamp");
383     }
384
385     static final Hashtable JavaDoc jdbcTypes = new Hashtable JavaDoc();
386     static final void jdbcType(int typeNum, Type type) {
387     jdbcTypes.put(new Integer JavaDoc(typeNum), type);
388     }
389     static {
390     jdbcType(Types.BIT, TypeBoolean.typeBoolean);
391     jdbcType(Types.TINYINT, TypeTinyInt.typeTinyInt);
392     jdbcType(Types.SMALLINT, TypeSmallInt.typeSmallInt);
393     jdbcType(Types.INTEGER, TypeInt.typeInt);
394     jdbcType(Types.BIGINT, TypeBigInt.typeBigInt);
395     jdbcType(Types.FLOAT, TypeReal.typeFloat);
396     jdbcType(Types.REAL, TypeReal.typeReal);
397     jdbcType(Types.DOUBLE, TypeReal.typeDouble);
398     jdbcType(Types.NUMERIC, TypeDecimal.typeDecimal);
399     jdbcType(Types.DECIMAL, TypeDecimal.typeDecimal);
400     jdbcType(Types.CHAR, TypeChar.typeChar);
401     jdbcType(Types.VARCHAR, TypeVarChar.typeVarChar);
402     jdbcType(Types.LONGVARCHAR, TypeVarChar.typeVarChar);
403     jdbcType(Types.DATE, TypeDate.typeDate);
404     jdbcType(Types.TIME, TypeTime.typeTime);
405     jdbcType(Types.TIMESTAMP, TypeTimestamp.typeTimestamp);
406     jdbcType(Types.BINARY, TypeBinary.typeBinary);
407     jdbcType(Types.VARBINARY, TypeVarBinary.typeVarBinary);
408     jdbcType(Types.LONGVARBINARY, TypeVarBinary.typeVarBinary);
409     // JAVA_OBJECT
410
// DISTINCT
411
// STRUCT
412
// ARRAY
413
//#ifndef JDK11
414
jdbcType(Types.BLOB, TypeBlob.typeBlob);
415     jdbcType(Types.CLOB, TypeClob.typeClob);
416     //#endif
417
// ref
418
}
419
420     public static final Type typeForJdbcType(int type) {
421     Type t = (Type)jdbcTypes.get(new Integer JavaDoc(type));
422     return t;
423     }
424
425     public void setDatafile(Datafile db) {}
426
427     public static final Value fromObject(Object JavaDoc obj) throws ValueException {
428     if (obj == null) return ValueNull.valueNull;
429     Class JavaDoc valueClass = (Class JavaDoc)javaTypes.get(obj.getClass().getName());
430     if (valueClass != null) {
431         try {
432         Value v = (Value)(valueClass.newInstance());
433         v.fromJavaObject(obj);
434         return v;
435         } catch (IllegalAccessException JavaDoc e) {
436         Debug.print(e);
437         throw new ValueException(e.toString());
438         } catch (InstantiationException JavaDoc e) {
439         Debug.print(e);
440         throw new ValueException(e.toString());
441         }
442     }
443     throw new ValueException("No mapping for class: " +
444                  obj.getClass().getName());
445     }
446
447 }
448
Popular Tags