KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > CrossConverters


1 /*
2
3    Derby - Class org.apache.derby.client.am.CrossConverters
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21
22 package org.apache.derby.client.am;
23
24 import org.apache.derby.shared.common.reference.SQLState;
25
26 // All currently supported derby types are mapped to one of the following jdbc types:
27
// java.sql.Types.SMALLINT;
28
// java.sql.Types.INTEGER;
29
// java.sql.Types.BIGINT;
30
// java.sql.Types.REAL;
31
// java.sql.Types.DOUBLE;
32
// java.sql.Types.DECIMAL;
33
// java.sql.Types.DATE;
34
// java.sql.Types.TIME;
35
// java.sql.Types.TIMESTAMP;
36
// java.sql.Types.CHAR;
37
// java.sql.Types.VARCHAR;
38
// java.sql.Types.LONGVARCHAR;
39
// java.sql.Types.CLOB;
40
// java.sql.Types.BLOB;
41
//
42

43 final class CrossConverters {
44
45     /**
46      * Value used to signal unknown length of data.
47      */

48     public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE;
49
50     private final static java.math.BigDecimal JavaDoc bdMaxByteValue__ =
51             java.math.BigDecimal.valueOf(Byte.MAX_VALUE);
52     private final static java.math.BigDecimal JavaDoc bdMinByteValue__ =
53             java.math.BigDecimal.valueOf(Byte.MIN_VALUE);
54     private final static java.math.BigDecimal JavaDoc bdMaxShortValue__ =
55             java.math.BigDecimal.valueOf(Short.MAX_VALUE);
56     private final static java.math.BigDecimal JavaDoc bdMinShortValue__ =
57             java.math.BigDecimal.valueOf(Short.MIN_VALUE);
58     private final static java.math.BigDecimal JavaDoc bdMaxIntValue__ =
59             java.math.BigDecimal.valueOf(Integer.MAX_VALUE);
60     private final static java.math.BigDecimal JavaDoc bdMinIntValue__ =
61             java.math.BigDecimal.valueOf(Integer.MIN_VALUE);
62     private final static java.math.BigDecimal JavaDoc bdMaxLongValue__ =
63             java.math.BigDecimal.valueOf(Long.MAX_VALUE);
64     private final static java.math.BigDecimal JavaDoc bdMinLongValue__ =
65             java.math.BigDecimal.valueOf(Long.MIN_VALUE);
66     private final static java.math.BigDecimal JavaDoc bdMaxFloatValue__ =
67             new java.math.BigDecimal JavaDoc(Float.MAX_VALUE);
68     private final static java.math.BigDecimal JavaDoc bdMinFloatValue__ =
69             new java.math.BigDecimal JavaDoc(-Float.MAX_VALUE);
70     private final static java.math.BigDecimal JavaDoc bdMaxDoubleValue__ =
71             new java.math.BigDecimal JavaDoc(Double.MAX_VALUE);
72     private final static java.math.BigDecimal JavaDoc bdMinDoubleValue__ =
73             new java.math.BigDecimal JavaDoc(-Double.MAX_VALUE);
74
75     // Since BigDecimals are immutable, we can return pointers to these canned 0's and 1's.
76
private final static java.math.BigDecimal JavaDoc bdZero__ = java.math.BigDecimal.valueOf(0);
77     private final static java.math.BigDecimal JavaDoc bdOne__ = java.math.BigDecimal.valueOf(1);
78
79     // ---------------------- state ----------------------------------------------
80

81     Agent agent_;
82
83     // ----------------------constructors/finalizer-------------------------------
84

85     CrossConverters(Agent agent) {
86         agent_ = agent;
87     }
88
89     // ---------------------------------------------------------------------------
90
// The following methods are used for input cross conversion.
91
// ---------------------------------------------------------------------------
92

93     //---------------------------- setObject() methods ---------------------------
94

95     // Convert from boolean source to target type.
96
// In support of PS.setBoolean().
97
// See differences.html for DNC setBoolean() semantics.
98
final Object JavaDoc setObject(int targetType, boolean source) throws SqlException {
99         return setObject(targetType, (short) (source ? 1 : 0));
100     }
101
102     // Convert from byte source to target type
103
// In support of PS.setByte()
104
final Object JavaDoc setObject(int targetType, byte source) throws SqlException {
105         return setObject(targetType, (short) source);
106     }
107
108     // Convert from short source to target type
109
// In support of PS.setShort()
110
final Object JavaDoc setObject(int targetType, short source) throws SqlException {
111         switch (targetType) {
112         case Types.SMALLINT:
113             return new Short JavaDoc(source);
114
115         case Types.INTEGER:
116             return new Integer JavaDoc(source);
117
118         case Types.BIGINT:
119             return new Long JavaDoc(source);
120
121         case Types.REAL:
122             return new Float JavaDoc(source);
123
124         case Types.DOUBLE:
125             return new Double JavaDoc(source);
126
127         case Types.DECIMAL:
128             return java.math.BigDecimal.valueOf(source);
129
130         case Types.CHAR:
131         case Types.VARCHAR:
132         case Types.LONGVARCHAR:
133             return String.valueOf(source);
134
135         default:
136             throw new SqlException(agent_.logWriter_,
137                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
138                 "byte", Types.getTypeString(targetType));
139         }
140     }
141
142     // Convert from integer source to target type
143
// In support of PS.setInt()
144
final Object JavaDoc setObject(int targetType, int source) throws SqlException {
145         switch (targetType) {
146         case Types.SMALLINT:
147             if (Configuration.rangeCheckCrossConverters &&
148                     (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) {
149                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
150             }
151             return new Short JavaDoc((short) source);
152
153         case Types.INTEGER:
154             return new Integer JavaDoc(source);
155
156         case Types.BIGINT:
157             return new Long JavaDoc(source);
158
159         case Types.REAL:
160             return new Float JavaDoc(source);
161
162         case Types.DOUBLE:
163             return new Double JavaDoc(source);
164
165         case Types.DECIMAL:
166             return java.math.BigDecimal.valueOf(source);
167
168         case Types.CHAR:
169         case Types.VARCHAR:
170         case Types.LONGVARCHAR:
171             return String.valueOf(source);
172
173         default:
174             throw new SqlException(agent_.logWriter_,
175                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
176                     "int", Types.getTypeString(targetType));
177         }
178     }
179
180     // This method is used in lieu of setObject(targetType, sourceObject) because we
181
// don't support the BIT/BOOLEAN as underlying DERBY targetTypes.
182
final boolean setBooleanFromObject(Object JavaDoc source, int sourceType) throws SqlException {
183         switch (sourceType) {
184         case Types.SMALLINT:
185             return getBooleanFromShort(((Short JavaDoc) source).shortValue());
186         case Types.INTEGER:
187             return getBooleanFromInt(((Integer JavaDoc) source).intValue());
188         case Types.BIGINT:
189             return getBooleanFromLong(((java.math.BigInteger JavaDoc) source).longValue());
190         case Types.REAL:
191             return getBooleanFromFloat(((Float JavaDoc) source).floatValue());
192         case Types.DOUBLE:
193             return getBooleanFromDouble(((Double JavaDoc) source).doubleValue());
194         case Types.DECIMAL:
195             return getBooleanFromLong(((java.math.BigDecimal JavaDoc) source).longValue());
196         case Types.CHAR:
197         case Types.VARCHAR:
198         case Types.LONGVARCHAR:
199             return getBooleanFromString((String JavaDoc) source);
200         default:
201             throw new ColumnTypeConversionException(agent_.logWriter_,
202                 Types.getTypeString(sourceType), "boolean");
203         }
204     }
205
206     // This method is used in lieu of setObject(targetType, sourceObject) because we
207
// don't support the BIT/BOOLEAN as underlying DERBY targetTypes.
208
final byte setByteFromObject(Object JavaDoc source, int sourceType) throws SqlException {
209         switch (sourceType) {
210         case Types.SMALLINT:
211             return getByteFromShort(((Short JavaDoc) source).shortValue());
212         case Types.INTEGER:
213             return getByteFromInt(((Integer JavaDoc) source).intValue());
214         case Types.BIGINT:
215             return getByteFromLong(((java.math.BigInteger JavaDoc) source).longValue());
216         case Types.REAL:
217             return getByteFromFloat(((Float JavaDoc) source).floatValue());
218         case Types.DOUBLE:
219             return getByteFromDouble(((Double JavaDoc) source).doubleValue());
220         case Types.DECIMAL:
221             return getByteFromLong(((java.math.BigDecimal JavaDoc) source).longValue());
222         case Types.CHAR:
223         case Types.VARCHAR:
224         case Types.LONGVARCHAR:
225             return getByteFromString((String JavaDoc) source);
226         default:
227             throw new ColumnTypeConversionException(agent_.logWriter_,
228                 Types.getTypeString(sourceType), "byte");
229         }
230     }
231
232     // Convert from long source to target type
233
// In support of PS.setLong()
234
final Object JavaDoc setObject(int targetType, long source) throws SqlException {
235         switch (targetType) {
236         case Types.SMALLINT:
237             if (Configuration.rangeCheckCrossConverters &&
238                     (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) {
239                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
240             }
241             return new Short JavaDoc((short) source);
242
243         case Types.INTEGER:
244             if (Configuration.rangeCheckCrossConverters &&
245                     (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) {
246                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
247             }
248             return new Integer JavaDoc((int) source);
249
250         case Types.BIGINT:
251             return new Long JavaDoc(source);
252
253         case Types.REAL:
254             return new Float JavaDoc(source);
255
256         case Types.DOUBLE:
257             return new Double JavaDoc(source);
258
259         case Types.DECIMAL:
260             return java.math.BigDecimal.valueOf(source);
261
262         case Types.CHAR:
263         case Types.VARCHAR:
264         case Types.LONGVARCHAR:
265             return String.valueOf(source);
266
267         default:
268             throw new SqlException(agent_.logWriter_,
269                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
270                 "long", Types.getTypeString(targetType));
271         }
272     }
273
274     // Convert from floating point source to target type
275
// In support of PS.setFloat()
276
final Object JavaDoc setObject(int targetType, float source) throws SqlException {
277         switch (targetType) {
278         case Types.SMALLINT:
279             if (Configuration.rangeCheckCrossConverters &&
280                     (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) {
281                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
282             }
283             return new Short JavaDoc((short) source);
284
285         case Types.INTEGER:
286             if (Configuration.rangeCheckCrossConverters &&
287                     (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) {
288                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
289             }
290             return new Integer JavaDoc((int) source);
291
292         case Types.BIGINT:
293             if (Configuration.rangeCheckCrossConverters &&
294                     (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) {
295                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
296             }
297             return new Long JavaDoc((long) source);
298
299         case Types.REAL:
300             if (Configuration.rangeCheckCrossConverters &&
301                     // change the check from (source > Float.MAX_VALUE || source < -Float.MIN_VALUE))
302
// to the following:
303
//-----------------------------------------------------------------------------------
304
// -infinity 0 +infinity
305
// |__________________________|======|________________________|
306
// <-3.4E+38| | | |>+3.4E+38
307
// | | |_________________ |
308
// | |-1.4E-45 <X< +1.4E-45
309
// | |________________________
310
//-----------------------------------------------------------------------------------
311
(source == Float.POSITIVE_INFINITY || source == Float.NEGATIVE_INFINITY)) {
312                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
313             }
314             return new Float JavaDoc(source);
315
316         case Types.DOUBLE:
317             if (Configuration.rangeCheckCrossConverters &&
318                     //-------------------------------------------------------------------------------------
319
// -infinity 0 +infinity
320
// |__________________________|======|________________________|
321
// <-1.79E+308| | | |>+1.79E+308
322
// | | |_________________ |
323
// | |-4.9E-324 <X< +4.9E-324
324
// | |________________________
325
//-------------------------------------------------------------------------------------
326
(source == Double.POSITIVE_INFINITY || source == Double.NEGATIVE_INFINITY)) {
327                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
328             }
329             // source passed in is a float, do we need to check if the source already contains "infinity"??
330
return new Double JavaDoc(String.valueOf(source));
331
332         case Types.DECIMAL:
333             // Can't use the following commented out line because it changes precision of the result.
334
//return new java.math.BigDecimal (source);
335
return new java.math.BigDecimal JavaDoc(String.valueOf(source)); // This matches derby semantics
336

337         case Types.CHAR:
338         case Types.VARCHAR:
339         case Types.LONGVARCHAR:
340             return String.valueOf(source);
341
342         default:
343             throw new SqlException(agent_.logWriter_,
344                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
345                 "float", Types.getTypeString(targetType));
346         }
347     }
348
349     // Convert from double floating point source to target type
350
// In support of PS.setDouble()
351
final Object JavaDoc setObject(int targetType, double source) throws SqlException {
352         switch (targetType) {
353         case Types.SMALLINT:
354             if (Configuration.rangeCheckCrossConverters &&
355                     (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) {
356                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
357             }
358             return new Short JavaDoc((short) source);
359
360         case Types.INTEGER:
361             if (Configuration.rangeCheckCrossConverters &&
362                     (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) {
363                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
364             }
365             return new Integer JavaDoc((int) source);
366
367         case Types.BIGINT:
368             if (Configuration.rangeCheckCrossConverters &&
369                     (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) {
370                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
371             }
372             return new Long JavaDoc((long) source);
373
374         case Types.REAL:
375             if (Configuration.rangeCheckCrossConverters &&
376                     (source > Float.MAX_VALUE || source < -Float.MAX_VALUE)) {
377                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
378             }
379             return new Float JavaDoc((float) source);
380
381         case Types.DOUBLE:
382             if (Configuration.rangeCheckCrossConverters &&
383                     // change the check from (source > Double.MAX_VALUE || source < -Double.MIN_VALUE))
384
// to the following:
385
//-------------------------------------------------------------------------------------
386
// -infinity 0 +infinity
387
// |__________________________|======|________________________|
388
// <-1.79E+308| | | |>+1.79E+308
389
// | | |_________________ |
390
// | |-4.9E-324 <X< +4.9E-324
391
// | |________________________
392
//-------------------------------------------------------------------------------------
393
(source == Double.POSITIVE_INFINITY || source == Double.NEGATIVE_INFINITY)) {
394                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
395             }
396             return new Double JavaDoc(source);
397
398         case Types.DECIMAL:
399             return new java.math.BigDecimal JavaDoc(String.valueOf(source)); // This matches derby semantics
400
case Types.CHAR:
401         case Types.VARCHAR:
402         case Types.LONGVARCHAR:
403             return String.valueOf(source);
404
405         default:
406             throw new SqlException(agent_.logWriter_,
407                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
408                 "double", Types.getTypeString(targetType));
409         }
410     }
411
412     // Convert from big decimal source to target type
413
// In support of PS.setBigDecimal()
414
final Object JavaDoc setObject(int targetType, java.math.BigDecimal JavaDoc source) throws SqlException {
415         switch (targetType) {
416         case Types.SMALLINT:
417             if (Configuration.rangeCheckCrossConverters &&
418                     (source.compareTo(bdMaxShortValue__) == 1 || source.compareTo(bdMinShortValue__) == -1)) {
419                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
420             }
421             return new Short JavaDoc(source.shortValue());
422
423         case Types.INTEGER:
424             if (Configuration.rangeCheckCrossConverters &&
425                     (source.compareTo(bdMaxIntValue__) == 1 || source.compareTo(bdMinIntValue__) == -1)) {
426                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
427             }
428             return new Integer JavaDoc(source.intValue());
429
430         case Types.BIGINT:
431             if (Configuration.rangeCheckCrossConverters &&
432                     (source.compareTo(bdMaxLongValue__) == 1 || source.compareTo(bdMinLongValue__) == -1)) {
433                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
434             }
435             return new Long JavaDoc(source.longValue());
436
437         case Types.REAL:
438             if (Configuration.rangeCheckCrossConverters &&
439                     (source.compareTo(bdMaxFloatValue__) == 1 || source.compareTo(bdMinFloatValue__) == -1)) {
440                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
441             }
442             return new Float JavaDoc(source.floatValue());
443
444         case Types.DOUBLE:
445             if (Configuration.rangeCheckCrossConverters &&
446                     (source.compareTo(bdMaxDoubleValue__) == 1 || source.compareTo(bdMinDoubleValue__) == -1)) {
447                 throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
448             }
449             return new Double JavaDoc(source.doubleValue());
450
451         case Types.DECIMAL:
452             return source;
453
454         case Types.CHAR:
455         case Types.VARCHAR:
456         case Types.LONGVARCHAR:
457             return String.valueOf(source);
458
459         default:
460             throw new SqlException(agent_.logWriter_,
461                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
462                 "java.Math.BigDecimal", Types.getTypeString(targetType));
463         }
464     }
465
466     // Convert from date source to target type
467
// In support of PS.setDate()
468
final Object JavaDoc setObject(int targetType, java.sql.Date JavaDoc source) throws SqlException {
469         switch (targetType) {
470
471         case java.sql.Types.DATE:
472             return source;
473
474         case java.sql.Types.TIMESTAMP:
475             return new java.sql.Timestamp JavaDoc(source.getTime());
476
477         case java.sql.Types.CHAR:
478         case java.sql.Types.VARCHAR:
479         case java.sql.Types.LONGVARCHAR:
480             return String.valueOf(source);
481
482         default:
483             throw new SqlException(agent_.logWriter_,
484                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
485                 "java.sql.Date", Types.getTypeString(targetType));
486         }
487     }
488
489     // Convert from time source to target type
490
// In support of PS.setTime()
491
final Object JavaDoc setObject(int targetType, java.sql.Time JavaDoc source) throws SqlException {
492         switch (targetType) {
493
494         case java.sql.Types.TIME:
495             return source;
496
497         case java.sql.Types.CHAR:
498         case java.sql.Types.VARCHAR:
499         case java.sql.Types.LONGVARCHAR:
500             return String.valueOf(source);
501
502         default:
503             throw new SqlException(agent_.logWriter_,
504                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
505                 "java.sql.Time", Types.getTypeString(targetType));
506         }
507     }
508
509     // Convert from date source to target type
510
// In support of PS.setTimestamp()
511
final Object JavaDoc setObject(int targetType, java.sql.Timestamp JavaDoc source) throws SqlException {
512         switch (targetType) {
513
514         case java.sql.Types.TIMESTAMP:
515             return source;
516
517         case java.sql.Types.TIME:
518             return new java.sql.Time JavaDoc(source.getTime());
519
520         case java.sql.Types.DATE:
521             return new java.sql.Date JavaDoc(source.getTime());
522
523         case java.sql.Types.CHAR:
524         case java.sql.Types.VARCHAR:
525         case java.sql.Types.LONGVARCHAR:
526             return String.valueOf(source);
527
528         default:
529             throw new SqlException(agent_.logWriter_,
530                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
531                 "java.sql.Timestamp", Types.getTypeString(targetType));
532         }
533     }
534
535     // setString() against BINARY columns cannot be implemented consistently because w/out metadata, we'll send char encoding bytes.
536
// So we refuse setString() requests altogether.
537
// Convert from string source to target type.
538
// In support of PS.setString()
539
final Object JavaDoc setObject(int targetDriverType, String JavaDoc source) throws SqlException {
540         try {
541             switch (targetDriverType) {
542             case Types.SMALLINT:
543                 return Short.valueOf(source);
544
545             case Types.INTEGER:
546                 return Integer.valueOf(source);
547
548             case Types.BIGINT:
549                 return Long.valueOf(source);
550
551             case Types.REAL:
552                 return Float.valueOf(source);
553
554             case Types.DOUBLE:
555                 return Double.valueOf(source);
556
557             case Types.DECIMAL:
558                 return new java.math.BigDecimal JavaDoc(source);
559
560             case java.sql.Types.DATE:
561                 return date_valueOf(source);
562
563             case java.sql.Types.TIME:
564                 return time_valueOf(source);
565
566             case java.sql.Types.TIMESTAMP:
567                 return timestamp_valueOf(source);
568
569             case Types.CHAR:
570             case Types.VARCHAR:
571             case Types.LONGVARCHAR:
572                 return source;
573
574             case Types.CLOB:
575                 return new Clob(agent_, source);
576
577                 // setString() against BINARY columns is problematic because w/out metadata, we'll send char encoding bytes.
578
// So we refuse setString() requests altogether.
579
case Types.BINARY:
580             case Types.VARBINARY:
581             case Types.LONGVARBINARY:
582             case Types.BLOB:
583             default:
584             throw new SqlException(agent_.logWriter_,
585                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
586                 "String", Types.getTypeString(targetDriverType));
587             }
588         } catch (java.lang.NumberFormatException JavaDoc e) {
589             throw new SqlException(agent_.logWriter_,
590                     new ClientMessageId
591                     (SQLState.LANG_FORMAT_EXCEPTION),
592                     Types.getTypeString(targetDriverType),
593                     e);
594         }
595     }
596
597     // ------ method to convert to targetJdbcType ------
598
/**
599      * Convert the input targetJdbcType to the correct JdbcType used by CrossConverters.
600      */

601     public static int getInputJdbcType(int jdbcType) {
602         switch (jdbcType) {
603         case java.sql.Types.BIT:
604         case java.sql.Types.BOOLEAN:
605         case java.sql.Types.TINYINT:
606         case java.sql.Types.SMALLINT:
607             return java.sql.Types.INTEGER;
608         case java.sql.Types.NUMERIC:
609             return java.sql.Types.DECIMAL;
610         case java.sql.Types.FLOAT:
611             return java.sql.Types.DOUBLE;
612         default:
613             return jdbcType;
614         }
615
616     }
617
618
619     // -- methods in support of setObject(String)/getString() on BINARY columns---
620

621
622     // Convert from byte[] source to target type
623
// In support of PS.setBytes()
624
final Object JavaDoc setObject(int targetType, byte[] source) throws SqlException {
625         switch (targetType) {
626         case Types.BINARY:
627         case Types.VARBINARY:
628         case Types.LONGVARBINARY:
629             return source;
630         case Types.BLOB:
631             return new Blob(source, agent_, 0);
632         default:
633             throw new SqlException(agent_.logWriter_,
634                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
635                 "byte[]", Types.getTypeString(targetType));
636         }
637     }
638
639     // Convert from Reader source to target type
640
// In support of PS.setCharacterStream()
641
final Object JavaDoc setObject(int targetType, java.io.Reader JavaDoc source, int length) throws SqlException {
642         switch (targetType) {
643         case Types.CHAR:
644         case Types.VARCHAR:
645         case Types.LONGVARCHAR:
646             return setStringFromReader(source, length);
647         case Types.CLOB:
648             if (length == CrossConverters.UNKNOWN_LENGTH) {
649                 return new Clob(agent_, source);
650             }
651             return new Clob(agent_, source, length);
652             // setCharacterStream() against BINARY columns is problematic because w/out metadata, we'll send char encoding bytes.
653
// There's no clean solution except to just not support setObject(String/Reader/Stream)
654
case Types.BINARY:
655         case Types.VARBINARY:
656         case Types.LONGVARBINARY:
657         case Types.BLOB:
658         default:
659             throw new SqlException(agent_.logWriter_,
660                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
661                 "java.io.Reader", Types.getTypeString(targetType));
662         }
663     }
664
665     // create a String by reading all of the bytes from reader
666
private final String JavaDoc setStringFromReader(java.io.Reader JavaDoc r, int length) throws SqlException {
667         java.io.StringWriter JavaDoc sw = new java.io.StringWriter JavaDoc();
668         try {
669             int read = r.read();
670             int totalRead = 0;
671             while (read != -1) {
672                 totalRead++;
673                 sw.write(read);
674                 read = r.read();
675             }
676             if (length != CrossConverters.UNKNOWN_LENGTH &&
677                     length != totalRead) {
678                 throw new SqlException(agent_.logWriter_,
679                         new ClientMessageId (SQLState.READER_UNDER_RUN));
680             }
681             return sw.toString();
682         } catch (java.io.IOException JavaDoc e) {
683             throw SqlException.javaException(agent_.logWriter_, e);
684         }
685     }
686
687     // Convert from InputStream source to target type.
688
// In support of PS.setAsciiStream, PS.setUnicodeStream
689
// Note: PS.setCharacterStream() is handled by setObject(Reader)
690
final Object JavaDoc setObjectFromCharacterStream(int targetType, java.io.InputStream JavaDoc source, String JavaDoc encoding, int length) throws SqlException {
691         switch (targetType) {
692         case Types.CHAR:
693         case Types.VARCHAR:
694         case Types.LONGVARCHAR:
695             return setStringFromStream(source, encoding, length);
696         case Types.CLOB:
697             if (length == CrossConverters.UNKNOWN_LENGTH) {
698                 return new Clob(agent_, source, encoding);
699             }
700             return new Clob(agent_, source, encoding, length);
701         case Types.BINARY:
702         case Types.VARBINARY:
703         case Types.LONGVARBINARY:
704         case Types.BLOB:
705         default:
706             throw new SqlException(agent_.logWriter_,
707                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
708                 "java.io.InputStream", Types.getTypeString(targetType));
709         }
710     }
711
712
713     // create a String by reading all of the bytes from inputStream, applying encoding
714
private final String JavaDoc setStringFromStream(java.io.InputStream JavaDoc is, String JavaDoc encoding, int length) throws SqlException {
715         try {
716             java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
717             int totalRead = 0;
718
719             try {
720                 int read = is.read();
721                 while (read != -1) {
722                     totalRead++;
723                     baos.write(read);
724                     read = is.read();
725                 }
726             } catch (java.io.IOException JavaDoc e) {
727                 throw new SqlException(agent_.logWriter_,
728                     new ClientMessageId(SQLState.JAVA_EXCEPTION),
729                     e.getClass().getName(), e.getMessage(), e);
730             }
731
732             if (length != CrossConverters.UNKNOWN_LENGTH &&
733                     length != totalRead) {
734                 throw new SqlException(agent_.logWriter_,
735                         new ClientMessageId (SQLState.READER_UNDER_RUN));
736             }
737
738             return new String JavaDoc(baos.toByteArray(), encoding);
739         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
740             throw new SqlException(agent_.logWriter_,
741                 new ClientMessageId (SQLState.UNSUPPORTED_ENCODING),
742                     "java.io.InputStream", "String", e);
743         }
744     }
745
746     // Convert from Blob source to target type
747
// In support of PS.setBlob()
748
final Object JavaDoc setObject(int targetType, java.sql.Blob JavaDoc source) throws SqlException {
749         switch (targetType) {
750         case Types.BLOB:
751             return source;
752         case Types.BINARY:
753         case Types.VARBINARY:
754         case Types.LONGVARBINARY:
755             try {
756                 return source.getBytes(1L, (int) source.length());
757             } catch (java.sql.SQLException JavaDoc e) {
758                 throw new SqlException(e);
759             }
760         default:
761             throw new SqlException(agent_.logWriter_,
762                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
763                 "java.sql.Blob", Types.getTypeString(targetType));
764         }
765     }
766
767     // Convert from InputStream source to target type
768
// In support of PS.setBinaryStream()
769
final Object JavaDoc setObjectFromBinaryStream(int targetType, java.io.InputStream JavaDoc source, int length) throws SqlException {
770         switch (targetType) {
771         case Types.BINARY:
772         case Types.VARBINARY:
773         case Types.LONGVARBINARY:
774             return setBytesFromStream(source, length);
775         case Types.BLOB:
776             if (length == CrossConverters.UNKNOWN_LENGTH) {
777                 return new Blob(agent_, source);
778             }
779             return new Blob(agent_, source, length);
780         default:
781             throw new SqlException(agent_.logWriter_,
782                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
783                 "java.io.InputStream", Types.getTypeString(targetType));
784         }
785     }
786
787     // create a byte[] by reading all of the bytes from inputStream
788
private final byte[] setBytesFromStream(java.io.InputStream JavaDoc is, int length) throws SqlException {
789         java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
790         int totalRead = 0;
791
792         try {
793             int read = is.read();
794             while (read != -1) {
795                 totalRead++;
796                 baos.write(read);
797                 read = is.read();
798             }
799
800             if (length != CrossConverters.UNKNOWN_LENGTH &&
801                     length != totalRead) {
802                 throw new SqlException(agent_.logWriter_,
803                         new ClientMessageId (SQLState.READER_UNDER_RUN));
804             }
805         } catch (java.io.IOException JavaDoc e) {
806             throw SqlException.javaException(agent_.logWriter_, e);
807         }
808         return baos.toByteArray();
809     }
810
811     // Convert from Clob source to target type
812
// In support of PS.setClob()
813
final Object JavaDoc setObject(int targetType, java.sql.Clob JavaDoc source) throws SqlException {
814         switch (targetType) {
815         case Types.CLOB:
816             return source;
817         case Types.CHAR:
818         case Types.VARCHAR:
819         case Types.LONGVARCHAR:
820             return source.toString();
821         default:
822             throw new SqlException(agent_.logWriter_,
823                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
824                 "java.sql.Clob", Types.getTypeString(targetType));
825         }
826     }
827
828     // The Java compiler uses static binding, so we can't rely on the strongly
829
// typed setObject() methods above for each of the Java Object instance types.
830
final Object JavaDoc setObject(int targetType, Object JavaDoc source) throws SqlException {
831         if (source == null) {
832             return null;
833         } else if (source instanceof Boolean JavaDoc) {
834             return setObject(targetType, ((Boolean JavaDoc) source).booleanValue());
835         } else if (source instanceof Integer JavaDoc) {
836             return setObject(targetType, ((Integer JavaDoc) source).intValue());
837         } else if (source instanceof Long JavaDoc) {
838             return setObject(targetType, ((Long JavaDoc) source).longValue());
839         } else if (source instanceof Float JavaDoc) {
840             return setObject(targetType, ((Float JavaDoc) source).floatValue());
841         } else if (source instanceof Double JavaDoc) {
842             return setObject(targetType, ((Double JavaDoc) source).doubleValue());
843         } else if (source instanceof java.math.BigDecimal JavaDoc) {
844             return setObject(targetType, (java.math.BigDecimal JavaDoc) source);
845         } else if (source instanceof java.sql.Date JavaDoc) {
846             return setObject(targetType, (java.sql.Date JavaDoc) source);
847         } else if (source instanceof java.sql.Time JavaDoc) {
848             return setObject(targetType, (java.sql.Time JavaDoc) source);
849         } else if (source instanceof java.sql.Timestamp JavaDoc) {
850             return setObject(targetType, (java.sql.Timestamp JavaDoc) source);
851         } else if (source instanceof String JavaDoc) {
852             return setObject(targetType, (String JavaDoc) source);
853         } else if (source instanceof byte[]) {
854             return setObject(targetType, (byte[]) source);
855         } else if (source instanceof java.sql.Blob JavaDoc) {
856             return setObject(targetType, (java.sql.Blob JavaDoc) source);
857         } else if (source instanceof java.sql.Clob JavaDoc) {
858             return setObject(targetType, (java.sql.Clob JavaDoc) source);
859         } else if (source instanceof java.sql.Array JavaDoc) {
860             return setObject(targetType, (java.sql.Array JavaDoc) source);
861         } else if (source instanceof java.sql.Ref JavaDoc) {
862             return setObject(targetType, (java.sql.Ref JavaDoc) source);
863         } else if (source instanceof Short JavaDoc) {
864             return setObject(targetType, ((Short JavaDoc) source).shortValue());
865         } else if (source instanceof Byte JavaDoc) {
866             return setObject(targetType, ((Byte JavaDoc) source).byteValue());
867         } else {
868             throw new SqlException(agent_.logWriter_,
869                 new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
870                 source.getClass().getName(), Types.getTypeString(targetType));
871         }
872     }
873
874     // move all these to Cursor and rename to crossConvertFrom*To*()
875
// ---------------------------------------------------------------------------
876
// The following methods are used for output cross conversion.
877
// ---------------------------------------------------------------------------
878

879     //---------------------------- getBoolean*() methods -------------------------
880

881     final boolean getBooleanFromByte(byte source) throws SqlException {
882         return source != 0;
883     }
884
885     final boolean getBooleanFromShort(short source) throws SqlException {
886         return source != 0;
887     }
888
889     final boolean getBooleanFromInt(int source) throws SqlException {
890         return source != 0;
891     }
892
893     final boolean getBooleanFromLong(long source) throws SqlException {
894         return source != 0;
895     }
896
897     final boolean getBooleanFromFloat(float source) throws SqlException {
898         return source != 0;
899     }
900
901     final boolean getBooleanFromDouble(double source) throws SqlException {
902         return source != 0;
903     }
904
905     final boolean getBooleanFromBigDecimal(java.math.BigDecimal JavaDoc source) throws SqlException {
906         return source.intValue() != 0;
907     }
908
909     // See differences.html for DNC getBoolean() semantics.
910
final boolean getBooleanFromString(String JavaDoc source) throws SqlException {
911         return !(source.trim().equals("0") || source.trim().equals("false"));
912     }
913
914     //---------------------------- getByte*() methods ----------------------------
915

916     final byte getByteFromShort(short source) throws SqlException {
917         if (Configuration.rangeCheckCrossConverters &&
918                 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) {
919             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
920         }
921
922         return (byte) source;
923     }
924
925     final byte getByteFromInt(int source) throws SqlException {
926         if (Configuration.rangeCheckCrossConverters &&
927                 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) {
928             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
929         }
930
931         return (byte) source;
932     }
933
934     final byte getByteFromLong(long source) throws SqlException {
935         if (Configuration.rangeCheckCrossConverters &&
936                 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) {
937             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
938         }
939
940         return (byte) source;
941     }
942
943     final byte getByteFromFloat(float source) throws SqlException {
944         if (Configuration.rangeCheckCrossConverters &&
945                 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) {
946             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
947         }
948
949         return (byte) source;
950     }
951
952     final byte getByteFromDouble(double source) throws SqlException {
953         if (Configuration.rangeCheckCrossConverters &&
954                 (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_VALUE)) {
955             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
956         }
957
958         return (byte) source;
959     }
960
961     final byte getByteFromBigDecimal(java.math.BigDecimal JavaDoc source) throws SqlException {
962         if (Configuration.rangeCheckCrossConverters &&
963                 (source.compareTo(bdMaxByteValue__) == 1 || source.compareTo(bdMinByteValue__) == -1)) {
964             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
965         }
966         return (byte) source.intValue();
967     }
968
969     final byte getByteFromBoolean(boolean source) throws SqlException {
970         return source ? (byte) 1 : (byte) 0;
971     }
972
973     final byte getByteFromString(String JavaDoc source) throws SqlException {
974         try {
975             return parseByte(source);
976         } catch (java.lang.NumberFormatException JavaDoc e) {
977             throw new SqlException(agent_.logWriter_,
978                     new ClientMessageId
979                     (SQLState.LANG_FORMAT_EXCEPTION), "byte", e);
980         }
981     }
982
983     //---------------------------- getShort*() methods ---------------------------
984

985     final short getShortFromInt(int source) throws SqlException {
986         if (Configuration.rangeCheckCrossConverters &&
987                 (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) {
988             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
989         }
990
991         return (short) source;
992     }
993
994     final short getShortFromLong(long source) throws SqlException {
995         if (Configuration.rangeCheckCrossConverters &&
996                 (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) {
997             throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
998         }
999
1000        return (short) source;
1001    }
1002
1003    final short getShortFromFloat(float source) throws SqlException {
1004        if (Configuration.rangeCheckCrossConverters &&
1005                (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) {
1006            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1007        }
1008
1009        return (short) source;
1010    }
1011
1012    final short getShortFromDouble(double source) throws SqlException {
1013        if (Configuration.rangeCheckCrossConverters &&
1014                (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) {
1015            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1016        }
1017
1018        return (short) source;
1019    }
1020
1021    final short getShortFromBigDecimal(java.math.BigDecimal JavaDoc source) throws SqlException {
1022        if (Configuration.rangeCheckCrossConverters &&
1023                (source.compareTo(bdMaxShortValue__) == 1 || source.compareTo(bdMinShortValue__) == -1)) {
1024            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1025        }
1026        return (short) source.intValue();
1027    }
1028
1029    final short getShortFromBoolean(boolean source) throws SqlException {
1030        return source ? (short) 1 : (short) 0;
1031    }
1032
1033    final short getShortFromString(String JavaDoc source) throws SqlException {
1034        try {
1035            return parseShort(source);
1036        } catch (java.lang.NumberFormatException JavaDoc e) {
1037            throw new SqlException(agent_.logWriter_,
1038                    new ClientMessageId
1039                    (SQLState.LANG_FORMAT_EXCEPTION),
1040                    "short", e);
1041        }
1042    }
1043
1044    //---------------------------- getInt*() methods -----------------------------
1045

1046    final int getIntFromLong(long source) throws SqlException {
1047        if (Configuration.rangeCheckCrossConverters &&
1048                (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) {
1049            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1050        }
1051
1052        return (int) source;
1053    }
1054
1055    final int getIntFromFloat(float source) throws SqlException {
1056        if (Configuration.rangeCheckCrossConverters &&
1057                (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) {
1058            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1059        }
1060
1061        return (int) source;
1062    }
1063
1064    final int getIntFromDouble(double source) throws SqlException {
1065        if (Configuration.rangeCheckCrossConverters &&
1066                (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) {
1067            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1068        }
1069
1070        return (int) source;
1071    }
1072
1073    final int getIntFromBigDecimal(java.math.BigDecimal JavaDoc source) throws SqlException {
1074        if (Configuration.rangeCheckCrossConverters &&
1075                (source.compareTo(bdMaxIntValue__) == 1 || source.compareTo(bdMinIntValue__) == -1)) {
1076            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1077        }
1078        return source.intValue();
1079    }
1080
1081    final int getIntFromBoolean(boolean source) throws SqlException {
1082        return source ? (int) 1 : (int) 0;
1083    }
1084
1085    final int getIntFromString(String JavaDoc source) throws SqlException {
1086        try {
1087            return parseInt(source);
1088        } catch (java.lang.NumberFormatException JavaDoc e) {
1089            throw new SqlException(agent_.logWriter_,
1090                    new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
1091                    "int", e);
1092        }
1093    }
1094
1095    //---------------------------- getLong*() methods ----------------------------
1096

1097    final long getLongFromFloat(float source) throws SqlException {
1098        if (Configuration.rangeCheckCrossConverters &&
1099                (source > java.lang.Long.MAX_VALUE || source < java.lang.Long.MIN_VALUE)) {
1100            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1101        }
1102
1103        return (long) source;
1104    }
1105
1106    final long getLongFromDouble(double source) throws SqlException {
1107        if (Configuration.rangeCheckCrossConverters &&
1108                (source > java.lang.Long.MAX_VALUE || source < java.lang.Long.MIN_VALUE)) {
1109            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1110        }
1111
1112        return (long) source;
1113    }
1114
1115    final long getLongFromBigDecimal(java.math.BigDecimal JavaDoc source) throws SqlException {
1116        if (Configuration.rangeCheckCrossConverters &&
1117                (source.compareTo(bdMaxLongValue__) == 1 || source.compareTo(bdMinLongValue__) == -1)) {
1118            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1119        }
1120        return source.longValue();
1121    }
1122
1123    final long getLongFromBoolean(boolean source) throws SqlException {
1124        return source ? (long) 1 : (long) 0;
1125    }
1126
1127    final long getLongFromString(String JavaDoc source) throws SqlException {
1128        try {
1129            return parseLong(source);
1130        } catch (java.lang.NumberFormatException JavaDoc e) {
1131            throw new SqlException(agent_.logWriter_,
1132                    new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
1133                    "long", e);
1134        }
1135    }
1136
1137    //---------------------------- getFloat*() methods ---------------------------
1138

1139    final float getFloatFromDouble(double source) throws SqlException {
1140        if (Configuration.rangeCheckCrossConverters &&
1141                Float.isInfinite((float)source)) {
1142            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1143        }
1144
1145        return (float) source;
1146    }
1147
1148    final float getFloatFromBigDecimal(java.math.BigDecimal JavaDoc source) throws SqlException {
1149        if (Configuration.rangeCheckCrossConverters &&
1150                (source.compareTo(bdMaxFloatValue__) == 1 || source.compareTo(bdMinFloatValue__) == -1)) {
1151            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1152        }
1153        return source.floatValue();
1154    }
1155
1156    final float getFloatFromBoolean(boolean source) throws SqlException {
1157        return source ? (float) 1 : (float) 0;
1158    }
1159
1160    final float getFloatFromString(String JavaDoc source) throws SqlException {
1161        try {
1162            return Float.parseFloat(source.trim());
1163        } catch (java.lang.NumberFormatException JavaDoc e) {
1164            throw new SqlException(agent_.logWriter_,
1165                    new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
1166                    "float", e);
1167        }
1168    }
1169
1170    //---------------------------- getDouble*() methods --------------------------
1171

1172    final double getDoubleFromBigDecimal(java.math.BigDecimal JavaDoc source) throws SqlException {
1173        if (Configuration.rangeCheckCrossConverters &&
1174                (source.compareTo(bdMaxDoubleValue__) == 1 || source.compareTo(bdMinDoubleValue__) == -1)) {
1175            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
1176        }
1177        return source.doubleValue();
1178    }
1179
1180    final double getDoubleFromBoolean(boolean source) throws SqlException {
1181        return source ? (double) 1 : (double) 0;
1182    }
1183
1184    final double getDoubleFromString(String JavaDoc source) throws SqlException {
1185        try {
1186            return Double.parseDouble(source.trim());
1187        } catch (java.lang.NumberFormatException JavaDoc e) {
1188            throw new SqlException(agent_.logWriter_,
1189                    new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
1190                    "double", e);
1191        }
1192    }
1193
1194    //---------------------------- getBigDecimal*() methods ----------------------
1195

1196    final java.math.BigDecimal JavaDoc getBigDecimalFromBoolean(boolean source) throws SqlException {
1197        return source ? bdOne__ : bdZero__;
1198    }
1199
1200    final java.math.BigDecimal JavaDoc getBigDecimalFromString(String JavaDoc source) throws SqlException {
1201        try {
1202            // Unfortunately, the big decimal constructor calls java.lang.Long.parseLong(),
1203
// which doesn't like spaces, so we have to call trim() to get rid of the spaces from CHAR columns.
1204
return new java.math.BigDecimal JavaDoc(source.trim());
1205        } catch (java.lang.NumberFormatException JavaDoc e) {
1206            throw new SqlException(agent_.logWriter_,
1207                    new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
1208                    "java.math.BigDecimal", e);
1209        }
1210    }
1211
1212    //---------------------------- getString*() methods --------------------------
1213

1214    final String JavaDoc getStringFromBoolean(boolean source) throws SqlException {
1215        return source ? "1" : "0";
1216    }
1217
1218    final String JavaDoc getStringFromBytes(byte[] bytes) throws SqlException {
1219        StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc(bytes.length * 2);
1220        for (int i = 0; i < bytes.length; i++) {
1221            String JavaDoc hexForByte = Integer.toHexString(bytes[i] & 0xff);
1222            // If the byte is x0-F, prepend a "0" in front to ensure 2 char representation
1223
if (hexForByte.length() == 1) {
1224                stringBuffer.append('0');
1225            }
1226            stringBuffer.append(hexForByte);
1227        }
1228        return stringBuffer.toString();
1229    }
1230
1231
1232    // All Numeric, and Date/Time types use String.valueOf (source)
1233

1234    //---------------------------- getDate*() methods ----------------------------
1235

1236    final java.sql.Date JavaDoc getDateFromString(String JavaDoc source) throws SqlException {
1237        try {
1238            return date_valueOf(source);
1239        } catch (java.lang.IllegalArgumentException JavaDoc e) { // subsumes NumberFormatException
1240
throw new SqlException(agent_.logWriter_,
1241                    new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), e);
1242        }
1243    }
1244
1245    final java.sql.Date JavaDoc getDateFromTime(java.sql.Time JavaDoc source) throws SqlException {
1246        return new java.sql.Date JavaDoc(source.getTime());
1247    }
1248
1249    final java.sql.Date JavaDoc getDateFromTimestamp(java.sql.Timestamp JavaDoc source) throws SqlException {
1250        return new java.sql.Date JavaDoc(source.getTime());
1251    }
1252
1253    //---------------------------- getTime*() methods ----------------------------
1254

1255    final java.sql.Time JavaDoc getTimeFromString(String JavaDoc source) throws SqlException {
1256        try {
1257            return time_valueOf(source);
1258        } catch (java.lang.IllegalArgumentException JavaDoc e) { // subsumes NumberFormatException
1259
throw new SqlException(agent_.logWriter_,
1260                    new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), e);
1261        }
1262    }
1263
1264    final java.sql.Time JavaDoc getTimeFromTimestamp(java.sql.Timestamp JavaDoc source) throws SqlException {
1265        return new java.sql.Time JavaDoc(source.getTime());
1266    }
1267
1268    //---------------------------- getTimestamp*() methods -----------------------
1269

1270    final java.sql.Timestamp JavaDoc getTimestampFromString(String JavaDoc source) throws SqlException {
1271        try {
1272            return timestamp_valueOf(source);
1273        } catch (java.lang.IllegalArgumentException JavaDoc e) { // subsumes NumberFormatException
1274
throw new SqlException(agent_.logWriter_,
1275                    new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), e);
1276        }
1277    }
1278
1279    final java.sql.Timestamp JavaDoc getTimestampFromTime(java.sql.Time JavaDoc source) throws SqlException {
1280        return new java.sql.Timestamp JavaDoc(source.getTime());
1281    }
1282
1283    final java.sql.Timestamp JavaDoc getTimestampFromDate(java.sql.Date JavaDoc source) throws SqlException {
1284        return new java.sql.Timestamp JavaDoc(source.getTime());
1285    }
1286
1287    final java.sql.Date JavaDoc date_valueOf(String JavaDoc s) throws java.lang.IllegalArgumentException JavaDoc {
1288        String JavaDoc formatError = "JDBC Date format must be yyyy-mm-dd";
1289        if (s == null) {
1290            throw new java.lang.IllegalArgumentException JavaDoc(formatError);
1291        }
1292        s = s.trim();
1293        return java.sql.Date.valueOf(s);
1294    }
1295
1296
1297    final java.sql.Time JavaDoc time_valueOf(String JavaDoc s) throws java.lang.IllegalArgumentException JavaDoc, NumberFormatException JavaDoc {
1298        String JavaDoc formatError = "JDBC Time format must be hh:mm:ss";
1299        if (s == null) {
1300            throw new java.lang.IllegalArgumentException JavaDoc();
1301        }
1302        s = s.trim();
1303        return java.sql.Time.valueOf(s);
1304    }
1305
1306    final java.sql.Timestamp JavaDoc timestamp_valueOf(String JavaDoc s) throws java.lang.IllegalArgumentException JavaDoc, NumberFormatException JavaDoc {
1307        String JavaDoc formatError = "JDBC Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";
1308        if (s == null) {
1309            throw new java.lang.IllegalArgumentException JavaDoc();
1310        }
1311
1312        s = s.trim();
1313        return java.sql.Timestamp.valueOf(s);
1314    }
1315
1316    private final byte parseByte(String JavaDoc s) throws NumberFormatException JavaDoc {
1317        int i = parseInt(s);
1318        if (i < Byte.MIN_VALUE || i > Byte.MAX_VALUE) {
1319            throw new NumberFormatException JavaDoc();
1320        }
1321        return (byte) i;
1322    }
1323
1324    private final short parseShort(String JavaDoc s) throws NumberFormatException JavaDoc {
1325        int i = parseInt(s);
1326        if (i < Short.MIN_VALUE || i > Short.MAX_VALUE) {
1327            throw new NumberFormatException JavaDoc();
1328        }
1329        return (short) i;
1330    }
1331
1332    // Custom version of java.lang.parseInt() that allows for space padding of char fields.
1333
private final int parseInt(String JavaDoc s) throws NumberFormatException JavaDoc {
1334        if (s == null) {
1335            throw new NumberFormatException JavaDoc("null");
1336        }
1337
1338        int result = 0;
1339        boolean negative = false;
1340        int i = 0;
1341        int max = s.length();
1342        int limit;
1343        int multmin;
1344        int digit;
1345
1346        if (max == 0) {
1347            throw new NumberFormatException JavaDoc(s);
1348        }
1349
1350        if (s.charAt(0) == '-') {
1351            negative = true;
1352            limit = Integer.MIN_VALUE;
1353            i++;
1354        } else {
1355            limit = -Integer.MAX_VALUE;
1356        }
1357        multmin = limit / 10;
1358        // Special handle the first digit to get things started.
1359
if (i < max) {
1360            digit = Character.digit(s.charAt(i++), 10);
1361            if (digit < 0) {
1362                throw new NumberFormatException JavaDoc(s);
1363            } else {
1364                result = -digit;
1365            }
1366        }
1367        // Now handle all the subsequent digits or space padding.
1368
while (i < max) {
1369            char c = s.charAt(i++);
1370            if (c == ' ') {
1371                skipPadding(s, i, max);
1372                break;
1373            }
1374            // Accumulating negatively avoids surprises near MAX_VALUE
1375
digit = Character.digit(c, 10);
1376            if (digit < 0) {
1377                throw new NumberFormatException JavaDoc(s);
1378            }
1379            if (result < multmin) {
1380                throw new NumberFormatException JavaDoc(s);
1381            }
1382            result *= 10;
1383            if (result < limit + digit) {
1384                throw new NumberFormatException JavaDoc(s);
1385            }
1386            result -= digit;
1387        }
1388        if (negative) {
1389            if (i > 1) {
1390                return result;
1391            } else { // Only got "-"
1392
throw new NumberFormatException JavaDoc(s);
1393            }
1394        } else {
1395            return -result;
1396        }
1397    }
1398
1399    private final long parseLong(String JavaDoc s) throws NumberFormatException JavaDoc {
1400        if (s == null) {
1401            throw new NumberFormatException JavaDoc("null");
1402        }
1403
1404        long result = 0;
1405        boolean negative = false;
1406        int i = 0, max = s.length();
1407        long limit;
1408        long multmin;
1409        int digit;
1410
1411        if (max == 0) {
1412            throw new NumberFormatException JavaDoc(s);
1413        }
1414
1415        if (s.charAt(0) == '-') {
1416            negative = true;
1417            limit = Long.MIN_VALUE;
1418            i++;
1419        } else {
1420            limit = -Long.MAX_VALUE;
1421        }
1422        multmin = limit / 10;
1423        if (i < max) {
1424            digit = Character.digit(s.charAt(i++), 10);
1425            if (digit < 0) {
1426                throw new NumberFormatException JavaDoc(s);
1427            } else {
1428                result = -digit;
1429            }
1430        }
1431        while (i < max) {
1432            char c = s.charAt(i++);
1433            if (c == ' ') {
1434                skipPadding(s, i, max);
1435                break;
1436            }
1437            // Accumulating negatively avoids surprises near MAX_VALUE
1438
digit = Character.digit(c, 10);
1439            if (digit < 0) {
1440                throw new NumberFormatException JavaDoc(s);
1441            }
1442            if (result < multmin) {
1443                throw new NumberFormatException JavaDoc(s);
1444            }
1445            result *= 10;
1446            if (result < limit + digit) {
1447                throw new NumberFormatException JavaDoc(s);
1448            }
1449            result -= digit;
1450        }
1451        if (negative) {
1452            if (i > 1) {
1453                return result;
1454            } else { // Only got "-"
1455
throw new NumberFormatException JavaDoc(s);
1456            }
1457        } else {
1458            return -result;
1459        }
1460    }
1461
1462    private final void skipPadding(String JavaDoc s, int i, int length) throws NumberFormatException JavaDoc {
1463        while (i < length) {
1464            if (s.charAt(i++) != ' ') {
1465                throw new NumberFormatException JavaDoc(s);
1466            }
1467        }
1468    }
1469}
1470
Popular Tags