KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > types > DataType


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.DataType
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.iapi.types;
23
24 import org.apache.derby.iapi.types.DataValueDescriptor;
25 import org.apache.derby.iapi.types.DataTypeDescriptor;
26 import org.apache.derby.iapi.types.BooleanDataValue;
27 import org.apache.derby.iapi.types.CloneableObject;
28 import org.apache.derby.iapi.types.Orderable;
29
30 import org.apache.derby.iapi.reference.SQLState;
31 import org.apache.derby.iapi.error.StandardException;
32 import org.apache.derby.iapi.services.i18n.MessageService;
33 import org.apache.derby.iapi.services.sanity.SanityManager;
34
35 import org.apache.derby.iapi.services.i18n.LocaleFinder;
36
37 import java.io.InputStream JavaDoc;
38 import java.sql.Date JavaDoc;
39 import java.sql.Time JavaDoc;
40 import java.sql.Timestamp JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.SQLException JavaDoc;
43 import java.sql.ResultSet JavaDoc;
44
45 import java.util.Calendar JavaDoc;
46
47 /**
48  *
49  * DataType is the superclass for all data types.
50  * It provides common behavior
51  * for datavalue descriptors -- it throws
52  * exceptions for all of the get* and setvalue(*) methods of
53  * DataValueDescriptor; the subtypes need only
54  * override the one for the type they represent
55  * and all types it can also be returned as,
56  * and the methods dealing with nulls.
57  *
58  * Since all types satisfy getString
59  * DataType does not define that
60  * interfaces of DataValueDescriptor.
61  *
62  * DataType is a little glue for columns to hold
63  * values with.
64  *
65  */

66 public abstract class DataType
67     implements DataValueDescriptor, CloneableObject
68 {
69     /*
70      * DataValueDescriptor Interface
71      */

72
73     /**
74      * Gets the value in the data value descriptor as a boolean.
75      * Throws an exception if the data value is not receivable as a boolean.
76      *
77      * @return The data value as a boolean.
78      *
79      * @exception StandardException Thrown on error
80      */

81     public boolean getBoolean() throws StandardException
82     {
83         throw dataTypeConversion("boolean");
84     }
85
86     /**
87      * Gets the value in the data value descriptor as a byte.
88      * Throws an exception if the data value is not receivable as a byte.
89      *
90      * @return The data value as a byte.
91      *
92      * @exception StandardException Thrown on error
93      */

94     public byte getByte() throws StandardException
95     {
96         throw dataTypeConversion("byte");
97     }
98
99     /**
100      * Gets the value in the data value descriptor as a short.
101      * Throws an exception if the data value is not receivable as a short.
102      *
103      * @return The data value as a short.
104      *
105      * @exception StandardException Thrown on error
106      */

107     public short getShort() throws StandardException
108     {
109         throw dataTypeConversion("short");
110     }
111
112     /**
113      * Gets the value in the data value descriptor as a int.
114      * Throws an exception if the data value is not receivable as a int.
115      *
116      * @return The data value as a int.
117      *
118      * @exception StandardException Thrown on error
119      */

120     public int getInt() throws StandardException
121     {
122         throw dataTypeConversion("int");
123     }
124
125     /**
126      * Gets the value in the data value descriptor as a long.
127      * Throws an exception if the data value is not receivable as a long.
128      *
129      * @return The data value as a long.
130      *
131      * @exception StandardException Thrown on error
132      */

133     public long getLong() throws StandardException
134     {
135         throw dataTypeConversion("long");
136     }
137
138     /**
139      * Gets the value in the data value descriptor as a float.
140      * Throws an exception if the data value is not receivable as a float.
141      *
142      * @return The data value as a float.
143      *
144      * @exception StandardException Thrown on error
145      */

146     public float getFloat() throws StandardException
147     {
148         throw dataTypeConversion("float");
149     }
150
151     /**
152      * Gets the value in the data value descriptor as a double.
153      * Throws an exception if the data value is not receivable as a double.
154      *
155      * @return The data value as a double.
156      *
157      * @exception StandardException Thrown on error
158      */

159     public double getDouble() throws StandardException
160     {
161         throw dataTypeConversion("double");
162     }
163
164     public int typeToBigDecimal() throws StandardException
165     {
166         throw dataTypeConversion("java.math.BigDecimal");
167     }
168     /**
169      * Gets the value in the data value descriptor as a byte[].
170      * Throws an exception if the data value is not receivable as a Binary or Varbinary.
171      *
172      * @return The Binary value as a byte[].
173      *
174      * @exception StandardException Thrown on error
175      */

176     public byte[] getBytes() throws StandardException
177     {
178         throw dataTypeConversion("byte[]");
179     }
180
181     /**
182      * Gets the value in the data value descriptor as a java.sql.Date.
183      * Throws an exception if the data value is not receivable as a Date.
184      * @param cal calendar for object creation
185      * @return The data value as a java.sql.Date.
186      *
187      * @exception StandardException Thrown on error
188      */

189     public Date JavaDoc getDate( Calendar JavaDoc cal) throws StandardException
190     {
191         throw dataTypeConversion("java.sql.Date");
192     }
193
194     /**
195      * Gets the value in the data value descriptor as a java.sql.Time.
196      * Throws an exception if the data value is not receivable as a Time.
197      * @param cal calendar for object creation
198      * @return The data value as a java.sql.Time.
199      *
200      * @exception StandardException Thrown on error
201      */

202     public Time JavaDoc getTime( Calendar JavaDoc cal) throws StandardException
203     {
204         throw dataTypeConversion("java.sql.Time");
205     }
206
207     /**
208      * Gets the value in the data value descriptor as a java.sql.Timestamp.
209      * Throws an exception if the data value is not receivable as a Timestamp.
210      * @param cal calendar for object creation
211      * @return The data value as a java.sql.Timestamp.
212      *
213      * @exception StandardException Thrown on error
214      */

215     public Timestamp JavaDoc getTimestamp( Calendar JavaDoc cal) throws StandardException
216     {
217         throw dataTypeConversion("java.sql.Timestamp");
218     }
219
220     /**
221      * Gets the value in the data stream descriptor as an InputStream.
222      * Throws an exception if the data value is not receivable as a stream.
223      *
224      * @return The data value as an InputStream.
225      *
226      * @exception StandardException Thrown on error
227      */

228     public InputStream JavaDoc getStream() throws StandardException
229     {
230         throw dataTypeConversion(
231             MessageService.getTextMessage(SQLState.LANG_STREAM));
232     }
233
234     /**
235      * Gets the value in the data stream descriptor as a trace string.
236      * This default implementation simply forwards the call to
237      * <code>getString</code>.
238      *
239      * @return The data value in a representation suitable for tracing.
240      * @throws StandardException if getting the data value fails.
241      * @see DataValueDescriptor#getString
242      */

243     public String JavaDoc getTraceString() throws StandardException {
244         return getString();
245     }
246
247     /*
248      * Column interface
249      */

250     
251     /**
252      * The is null operator as called from the language module, as opposed to
253      * the storage module.
254      *
255      *
256      * @return A SQL boolean value telling whether the operand is null
257      *
258      */

259
260     public final BooleanDataValue isNullOp()
261     {
262         return SQLBoolean.truthValue(isNull());
263     }
264
265     /**
266      * The is not null operator as called from the language module, as opposed to
267      * the storage module.
268      *
269      *
270      * @return A SQL boolean value telling whether the operand is not null
271      *
272      */

273
274     public final BooleanDataValue isNotNull()
275     {
276         return SQLBoolean.truthValue(!isNull());
277     }
278
279     /**
280      * Set the value of this DataValueDescriptor.
281      * At DataType level just throws an error lower classes will override
282      *
283      * @param theValue The Time value to set this DataValueDescriptor to
284      */

285     public void setValue(Time JavaDoc theValue) throws StandardException
286     {
287         setValue( theValue, (Calendar JavaDoc) null);
288     }
289
290     /**
291      * Set the value of this DataValueDescriptor.
292      * At DataType level just throws an error lower classes will override
293      *
294      * @param theValue The Time value to set this DataValueDescriptor to
295      * @param cal The time zone from the calendar is used to construct the database time value
296      */

297     public void setValue(Time JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
298     {
299         throwLangSetMismatch("java.sql.Time");
300     }
301     
302     /**
303      * Set the value of this DataValueDescriptor.
304      * At DataType level just throws an error lower classes will override
305      *
306      * @param theValue The Timestamp value to set this DataValueDescriptor to
307      */

308     public void setValue(Timestamp JavaDoc theValue) throws StandardException
309     {
310         setValue( theValue, (Calendar JavaDoc) null);
311     }
312     
313     /**
314      * Set the value of this DataValueDescriptor.
315      * At DataType level just throws an error lower classes will override
316      *
317      * @param theValue The Timestamp value to set this DataValueDescriptor to
318      * @param cal The time zone from the calendar is used to construct the database timestamp value
319      */

320     public void setValue(Timestamp JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
321     {
322         throwLangSetMismatch("java.sql.Timestamp");
323     }
324     
325     /**
326      * Set the value of this DataValueDescriptor.
327      * At DataType level just throws an error lower classes will override
328      *
329      * @param theValue The Date value to set this DataValueDescriptor to
330      */

331     public void setValue(Date JavaDoc theValue) throws StandardException
332     {
333         setValue( theValue, (Calendar JavaDoc) null);
334     }
335     
336     /**
337      * Set the value of this DataValueDescriptor.
338      * At DataType level just throws an error lower classes will override
339      *
340      * @param theValue The Date value to set this DataValueDescriptor to
341      * @param cal The time zone from the calendar is used to construct the database date value
342      */

343     public void setValue(Date JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
344     {
345         throwLangSetMismatch("java.sql.Date");
346     }
347
348     /**
349      * Set the value of this DataValueDescriptor.
350      * At DataType level just throws an error lower classes will override
351      *
352      * @param theValue The BigDecimal value to set this DataValueDescriptor to
353      */

354     public void setValue(String JavaDoc theValue) throws StandardException
355     {
356         throwLangSetMismatch("java.lang.String");
357     }
358
359
360     /**
361      * Set the value of this DataValueDescriptor to the given int value
362      * At DataType level just throws an error lower classes will override
363      *
364      * @param theValue The value to set this DataValueDescriptor to
365      *
366      * @exception StandardException Thrown on error
367      */

368     public void setValue(int theValue) throws StandardException
369     {
370         throwLangSetMismatch("int");
371     }
372
373     /**
374      * Set the value of this DataValueDescriptor to the given double value
375      * At DataType level just throws an error lower classes will override
376      *
377      * @param theValue The value to set this DataValueDescriptor to
378      *
379      * @exception StandardException Thrown on error
380      */

381     public void setValue(double theValue) throws StandardException
382     {
383         throwLangSetMismatch("double");
384     }
385
386     /**
387      * Set the value of this DataValueDescriptor to the given float value
388      * At DataType level just throws an error lower classes will override
389      *
390      * @param theValue The value to set this DataValueDescriptor to
391      *
392      * @exception StandardException Thrown on error
393      */

394     public void setValue(float theValue) throws StandardException
395     {
396         throwLangSetMismatch("float");
397     }
398  
399     /**
400      * Set the value of this DataValueDescriptor to the given short value
401      * At DataType level just throws an error lower classes will override
402      *
403      * @param theValue The value to set this DataValueDescriptor to
404      *
405      * @exception StandardException Thrown on error
406      */

407     public void setValue(short theValue) throws StandardException
408     {
409         throwLangSetMismatch("short");
410     }
411     /**
412      * Set the value of this DataValueDescriptor to the given long value
413      * At DataType level just throws an error lower classes will override
414      *
415      * @param theValue The value to set this DataValueDescriptor to
416      *
417      * @exception StandardException Thrown on error
418      */

419     public void setValue(long theValue) throws StandardException
420     {
421         throwLangSetMismatch("long");
422     }
423
424     /**
425      * Set the value of this DataValueDescriptor to the given byte value
426      * At DataType level just throws an error lower classes will override
427      *
428      * @param theValue The value to set this DataValueDescriptor to
429      *
430      * @exception StandardException Thrown on error
431      */

432     public void setValue(byte theValue) throws StandardException
433     {
434         throwLangSetMismatch("byte");
435     }
436
437     /**
438      * Set the value.
439      * At DataType level just throws an error lower classes will override
440      *
441      * @param theValue Contains the boolean value to set this to
442      *
443      */

444     public void setValue(boolean theValue) throws StandardException
445     {
446         throwLangSetMismatch("boolean");
447     }
448
449     /**
450      * Set the value of this DataValueDescriptor.
451      * At DataType level just throws an error lower classes will override
452      *
453      * @param theValue The byte value to set this DataValueDescriptor to
454      *
455      */

456     public void setValue(byte[] theValue) throws StandardException
457     {
458         throwLangSetMismatch("byte[]");
459     }
460
461     /**
462         Only to be called when the application sets a value using BigDecimal
463     */

464     public void setBigDecimal(Number JavaDoc bigDecimal) throws StandardException
465     {
466         throwLangSetMismatch("java.math.BigDecimal");
467     }
468
469
470     public final void setValue(DataValueDescriptor dvd) throws StandardException {
471
472         if (dvd.isNull())
473         {
474             setToNull();
475             return;
476         }
477
478         try {
479             setFrom(dvd);
480         } catch (StandardException se) {
481             String JavaDoc msgId = se.getMessageId();
482
483             if (SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE.equals(msgId))
484                 throw outOfRange();
485
486             if (SQLState.LANG_FORMAT_EXCEPTION.equals(msgId))
487                 throw invalidFormat();
488
489             throw se;
490
491         }
492     }
493
494     /**
495      * Set the value of this DataValueDescriptor based on the value
496      * of the specified DataValueDescriptor.
497      *
498      * @param dvd The DataValueDescriptor that holds the value to
499      * which we want to set this DataValueDescriptor's value.
500      *
501      */

502     protected void setFrom(DataValueDescriptor dvd) throws StandardException
503     {
504         throw StandardException.newException(SQLState.NOT_IMPLEMENTED);
505     }
506
507     /**
508      * @see DataValueDescriptor#setToNull
509      */

510      public void setToNull()
511      {
512         restoreToNull();
513      }
514
515     /**
516      * @see DataValueDescriptor#setObjectForCast
517      *
518      * @exception StandardException
519      * thrown on failure
520      *
521      */

522     public void setObjectForCast(Object JavaDoc theValue, boolean instanceOfResultType,
523             String JavaDoc resultTypeClassName) throws StandardException {
524         
525         if (theValue == null)
526         {
527             setToNull();
528             return;
529         }
530             
531         /*
532          * Is the object of the right type? (only do the check if value is
533          * non-null
534          */

535         if (!instanceOfResultType) {
536                 throw StandardException.newException(
537                         SQLState.LANG_DATA_TYPE_SET_MISMATCH,
538                         theValue.getClass().getName(), getTypeName(resultTypeClassName));
539         }
540
541         setObject(theValue);
542     }
543         
544     /**
545      * Set the value from an non-null object. Usually overridden.
546      * This implementation throws an exception.
547      * The object will have been correctly typed from the call to setObjectForCast.
548      */

549     void setObject(Object JavaDoc theValue)
550         throws StandardException
551     {
552         genericSetObject(theValue);
553     }
554     
555     /**
556      * Get the type name of this value, possibly overriding
557      * with the passed in class name (for user/java types).
558      * @param className
559      */

560     String JavaDoc getTypeName(String JavaDoc className)
561     {
562         return getTypeName();
563     }
564
565
566     /**
567      * Gets the value in the data value descriptor as a int.
568      * Throws an exception if the data value is not receivable as a int.
569      *
570      * @return The data value as a int.
571      *
572      * @exception StandardException Thrown on error
573      */

574     public Object JavaDoc getObject() throws StandardException
575     {
576         throw dataTypeConversion("java.lang.Object");
577     }
578
579
580     void genericSetObject(Object JavaDoc theValue) throws StandardException {
581
582         throwLangSetMismatch(theValue);
583     }
584
585     /**
586      * From CloneableObject
587      *
588      * @return clone of me as an Object
589      */

590     public Object JavaDoc cloneObject()
591     {
592         return getClone();
593     }
594
595     // International support
596

597     /**
598      * International version of getString(). Overridden for date, time,
599      * and timestamp in SQLDate, SQLTime, SQLTimestamp.
600      *
601      * @exception StandardException Thrown on error
602      */

603     protected String JavaDoc getNationalString(LocaleFinder localeFinder) throws StandardException
604     {
605         return getString();
606     }
607
608     public void throwLangSetMismatch(Object JavaDoc value) throws StandardException {
609         throwLangSetMismatch(value.getClass().getName());
610     }
611
612     void throwLangSetMismatch(String JavaDoc argTypeName) throws StandardException
613     {
614         throw StandardException.newException(SQLState.LANG_DATA_TYPE_SET_MISMATCH,
615                                        argTypeName, this.getTypeName());
616         
617     }
618
619     public void setInto(PreparedStatement JavaDoc ps, int position) throws SQLException JavaDoc, StandardException {
620
621         ps.setObject(position, getObject());
622     }
623
624     /**
625         Set this value into a ResultSet for a subsequent ResultSet.insertRow
626         or ResultSet.updateRow. This method will only be called for non-null values.
627
628         @exception SQLException thrown by the ResultSet object
629         @exception StandardException thrown by me accessing my value.
630     */

631     public void setInto(ResultSet JavaDoc rs, int position) throws SQLException JavaDoc, StandardException {
632         rs.updateObject(position, getObject());
633     }
634
635
636     /**
637      * Default normalization method. No information needed from DataTypeDescriptor.
638      *
639      * @param desiredType The type to normalize the source column to
640      * @param source The value to normalize
641      *
642      *
643      * @exception StandardException Thrown normalization error.
644      */

645
646     public void normalize(
647                 DataTypeDescriptor desiredType,
648                 DataValueDescriptor source)
649                     throws StandardException
650     {
651         ((DataValueDescriptor) this).setValue(source);
652     }
653     /**
654      * Each built-in type in JSQL has a precedence. This precedence determines
655      * how to do type promotion when using binary operators. For example, float
656      * has a higher precedence than int, so when adding an int to a float, the
657      * result type is float.
658      *
659      * The precedence for some types is arbitrary. For example, it doesn't
660      * matter what the precedence of the boolean type is, since it can't be
661      * mixed with other types. But the precedence for the number types is
662      * critical. The SQL standard requires that exact numeric types be
663      * promoted to approximate numeric when one operator uses both. Also,
664      * the precedence is arranged so that one will not lose precision when
665      * promoting a type.
666      *
667      * @return The precedence of this type.
668      */

669     public int typePrecedence() {
670         return -1;
671     }
672
673     /**
674      * The = operator as called from the language module, as opposed to
675      * the storage module. This default implementations uses compare().
676      *
677      * @param left The value on the left side of the =
678      * @param right The value on the right side of the =
679      *
680      * @return A SQL boolean value telling whether the two parameters are equal
681      *
682      * @exception StandardException Thrown on error
683      */

684
685     public BooleanDataValue equals(DataValueDescriptor left,
686                             DataValueDescriptor right)
687             throws StandardException
688     {
689         return SQLBoolean.truthValue(left,
690                                      right,
691                                      left.compare(right) == 0);
692     }
693
694     /**
695      * The <> operator as called from the language module, as opposed to
696      * the storage module. This default implementations uses compare().
697      *
698      * @param left The value on the left side of the <>
699      * @param right The value on the right side of the <>
700      *
701      * @return A SQL boolean value telling whether the two parameters
702      * are not equal
703      *
704      * @exception StandardException Thrown on error
705      */

706
707     public BooleanDataValue notEquals(DataValueDescriptor left,
708                             DataValueDescriptor right)
709             throws StandardException
710     {
711         return SQLBoolean.truthValue(left,
712                                      right,
713                                      left.compare(right) != 0);
714     }
715     /**
716     * The < operator as called from the language module, as opposed to
717     * the storage module.
718     *
719     * @param left The value on the left side of the <
720     * @param right The value on the right side of the <
721     *
722     * @return A SQL boolean value telling whether the first operand is less
723     * than the second operand
724     *
725     * @exception StandardException Thrown on error
726     */

727
728     public BooleanDataValue lessThan(DataValueDescriptor left,
729        DataValueDescriptor right)
730     throws StandardException
731     {
732         return SQLBoolean.truthValue(left,
733           right,
734           left.compare(right) < 0);
735     }
736     /**
737      * The > operator as called from the language module, as opposed to
738      * the storage module. This default implementations uses compare().
739      *
740      * @param left The value on the left side of the >
741      * @param right The value on the right side of the >
742      *
743      * @return A SQL boolean value telling whether the first operand is greater
744      * than the second operand
745      *
746      * @exception StandardException Thrown on error
747      */

748
749     public BooleanDataValue greaterThan(DataValueDescriptor left,
750                             DataValueDescriptor right)
751             throws StandardException
752     {
753         return SQLBoolean.truthValue(left,
754                                      right,
755                                      left.compare(right) > 0);
756     }
757
758     /**
759      * The <= operator as called from the language module, as opposed to
760      * the storage module. This default implementations uses compare().
761      *
762      * @param left The value on the left side of the <=
763      * @param right The value on the right side of the <=
764      *
765      * @return A SQL boolean value telling whether the first operand is less
766      * than or equal to the second operand
767      *
768      * @exception StandardException Thrown on error
769      */

770
771     public BooleanDataValue lessOrEquals(DataValueDescriptor left,
772                             DataValueDescriptor right)
773             throws StandardException
774     {
775         return SQLBoolean.truthValue(left,
776                                      right,
777                                      left.compare(right) <= 0);
778     }
779
780     /**
781      * The >= operator as called from the language module, as opposed to
782      * the storage module. This default implementation uses compare().
783      *
784      * @param left The value on the left side of the >=
785      * @param right The value on the right side of the >=
786      *
787      * @return A SQL boolean value telling whether the first operand is greater
788      * than or equal to the second operand
789      *
790      * @exception StandardException Thrown on error
791      */

792
793     public BooleanDataValue greaterOrEquals(DataValueDescriptor left,
794                             DataValueDescriptor right)
795             throws StandardException
796     {
797         return SQLBoolean.truthValue(left,
798                                      right,
799                                      left.compare(right) >= 0);
800     }
801     public boolean compare(int op,
802                            DataValueDescriptor other,
803                            boolean orderedNulls,
804                            boolean unknownRV)
805         throws StandardException
806     {
807         /* Use compare method from dominant type, flipping the operator
808          * to reflect flipping of sides.
809          */

810         if (typePrecedence() < other.typePrecedence())
811         {
812             return other.compare(flip(op), this, orderedNulls, unknownRV);
813         }
814
815         int result = compare(other);
816
817         switch(op)
818         {
819         case ORDER_OP_LESSTHAN:
820             return (result < 0); // this < other
821
case ORDER_OP_EQUALS:
822             return (result == 0); // this == other
823
case ORDER_OP_LESSOREQUALS:
824             return (result <= 0); // this <= other
825
// flipped operators
826
case ORDER_OP_GREATERTHAN:
827             return (result > 0); // this > other
828
case ORDER_OP_GREATEROREQUALS:
829             return (result >= 0); // this >= other
830
default:
831             if (SanityManager.DEBUG)
832                 SanityManager.THROWASSERT("Invalid Operator");
833             return false;
834         }
835     }
836
837     /**
838      * Flip the operator used in a comparison (< -> >).
839      * This is useful when flipping a comparison due to
840      * type precedence.
841      *
842      * @param operator The operator to flip.
843      *
844      * @return The flipped operator.
845      */

846     protected static int flip(int operator)
847     {
848         switch (operator)
849         {
850             case Orderable.ORDER_OP_LESSTHAN:
851                 // < -> >
852
return Orderable.ORDER_OP_GREATERTHAN;
853             case Orderable.ORDER_OP_LESSOREQUALS:
854                 // <= -> >=
855
return Orderable.ORDER_OP_GREATEROREQUALS;
856             case Orderable.ORDER_OP_EQUALS:
857                 // = -> =
858
return Orderable.ORDER_OP_EQUALS;
859             default:
860                 // These operators only appear due to flipping.
861
// They should never be flipped themselves.
862
if (SanityManager.DEBUG)
863                 {
864                     SanityManager.THROWASSERT(
865                         "Attempting to flip an operator that is not " +
866                         "expected to be flipped.");
867                 }
868                 return operator;
869         }
870     }
871
872     /*
873      * DataValueDescriptor interface
874      */

875
876     /**
877      * @see DataValueDescriptor#coalesce
878      * @exception StandardException Thrown on error
879      */

880     public DataValueDescriptor coalesce(DataValueDescriptor[] argumentsList, DataValueDescriptor returnValue)
881                         throws StandardException
882     {
883         // arguments list should have at least 2 arguments
884
if (SanityManager.DEBUG)
885         {
886             SanityManager.ASSERT(argumentsList != null,
887                 "argumentsList expected to be non-null");
888             SanityManager.ASSERT(argumentsList.length > 1,
889                 "argumentsList.length expected to be > 1");
890         }
891
892         /* Walk the arguments list until we find a non-null value. Otherwise we will return null
893          */

894         int index;
895         for (index = 0; index < argumentsList.length; index++)
896         {
897             if (!(argumentsList[index].isNull()))
898             {
899                 returnValue.setValue(argumentsList[index]);
900                 return returnValue;
901             }
902         }
903
904         returnValue.setToNull();
905         return returnValue;
906
907     }
908
909     /**
910      * @see DataValueDescriptor#in
911      * @exception StandardException Thrown on error
912      */

913     public BooleanDataValue in(DataValueDescriptor left,
914                                DataValueDescriptor[] inList,
915                                boolean orderedList)
916                         throws StandardException
917     {
918         BooleanDataValue retval = null;
919
920         // in list should be non-empty
921
if (SanityManager.DEBUG)
922         {
923             SanityManager.ASSERT(inList != null,
924                 "inList expected to be non-null");
925             SanityManager.ASSERT(inList.length > 0,
926                 "inList.length expected to be > 0");
927         }
928
929         // if left is null then just return false
930
if (left.isNull())
931         {
932             return SQLBoolean.truthValue(left,
933                                      inList[0],
934                                      false);
935         }
936
937         int start = 0;
938         int finish = inList.length;
939
940         /* Do a binary search if the list is ordered until the
941          * range of values to search is 3 or less.
942          * NOTE: We've ensured that the IN list and the left all have
943          * the same precedence at compile time. If we don't enforce
944          * the same precendence then
945          * we could get the wrong result when doing a binary search.
946          */

947         if (orderedList)
948         {
949             while (finish - start > 2)
950             {
951                 int mid = ((finish - start) / 2) + start;
952                 // Search left
953
retval = equals(left, inList[mid]);
954                 if (retval.equals(true))
955                 {
956                     return retval;
957                 }
958                 BooleanDataValue goLeft = greaterThan(inList[mid], left);
959                 if (goLeft.equals(true))
960                 {
961                     // search left
962
finish = mid;
963                 }
964                 else
965                 {
966                     // search right
967
start = mid;
968                 }
969             }
970         }
971
972         /* Walk the in list comparing the values. Return as soon as we
973          * find a match. If the list is ordered, return as soon as the left
974          * value is greater than an element in the in list.
975          */

976         for (int index = start; index < finish; index++)
977         {
978             retval = equals(left, inList[index]);
979             if (retval.equals(true))
980             {
981                 break;
982             }
983
984             // Can we stop searching?
985
if (orderedList)
986             {
987                 BooleanDataValue stop = greaterThan(inList[index], left);
988                 if (stop.equals(true))
989                 {
990                     break;
991                 }
992             }
993         }
994
995         return retval;
996     }
997
998     /*
999      * equals
1000     */

1001    public boolean equals(Object JavaDoc other)
1002    {
1003        if (! (other instanceof DataValueDescriptor))
1004        {
1005            return false;
1006        }
1007
1008        try
1009        {
1010            return compare(ORDER_OP_EQUALS, (DataValueDescriptor) other, true, false);
1011        }
1012        catch (StandardException se)
1013        {
1014            return false;
1015        }
1016    }
1017
1018    public void setValue(InputStream JavaDoc theStream, int valueLength) throws StandardException
1019    {
1020        throwLangSetMismatch("java.io.InputStream");
1021    }
1022
1023    /**
1024        Check the value to seem if it conforms to the restrictions
1025        imposed by DB2/JCC on host variables for this type.
1026
1027        @exception StandardException Variable is too big.
1028    */

1029    public void checkHostVariable(int declaredLength) throws StandardException
1030    {
1031    }
1032
1033
1034    /**
1035        Return an conversion exception from this type to another.
1036    */

1037    protected final StandardException dataTypeConversion(String JavaDoc targetType) {
1038        return StandardException.newException(SQLState.LANG_DATA_TYPE_GET_MISMATCH,
1039            targetType, this.getTypeName());
1040
1041    }
1042
1043    /**
1044        Return an out of range exception for this type.
1045    */

1046    protected final StandardException outOfRange()
1047    {
1048        return StandardException.newException(
1049                SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, getTypeName());
1050    }
1051
1052    /**
1053        Return an out of range exception for this type.
1054    */

1055    protected final StandardException invalidFormat()
1056    {
1057        return StandardException.newException(
1058                SQLState.LANG_FORMAT_EXCEPTION, getTypeName());
1059    }
1060}
1061
Popular Tags