KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLBoolean
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
25 import org.apache.derby.iapi.services.io.ArrayInputStream;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import org.apache.derby.iapi.services.io.Storable;
30 import org.apache.derby.iapi.services.io.StoredFormatIds;
31
32 import org.apache.derby.iapi.error.StandardException;
33
34 import org.apache.derby.iapi.types.DataValueDescriptor;
35 import org.apache.derby.iapi.types.TypeId;
36 import org.apache.derby.iapi.types.BooleanDataValue;
37
38
39 import org.apache.derby.iapi.services.cache.ClassSize;
40 import org.apache.derby.iapi.util.StringUtil;
41
42 import java.io.ObjectOutput JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 import java.sql.ResultSet JavaDoc;
47 import java.sql.PreparedStatement JavaDoc;
48 import java.sql.SQLException JavaDoc;
49
50 /**
51  * SQLBoolean satisfies the DataValueDescriptor
52  * interfaces (i.e., DataType). It implements a boolean column,
53  * e.g. for * storing a column value; it can be specified
54  * when constructed to not allow nulls. Nullability cannot be changed
55  * after construction, as it affects the storage size and mechanism.
56  * <p>
57  * Because DataType is a subtype of DataType,
58  * SQLBoolean can play a role in either a DataType/Row
59  * or a DataType/Row, interchangeably.
60  * <p>
61  * We assume the store has a flag for nullness of the value,
62  * and simply return a 0-length array for the stored form
63  * when the value is null.
64  * <p>
65  * PERFORMANCE: There are likely alot of performance improvements
66  * possible for this implementation -- it new's Integer
67  * more than it probably wants to.
68  */

69 public final class SQLBoolean
70     extends DataType implements BooleanDataValue
71 {
72     /*
73      * DataValueDescriptor interface
74      * (mostly implemented in DataType)
75      */

76
77     /*
78      * see if the integer value is null.
79      */

80     public boolean isNull()
81     {
82         return isnull;
83     }
84
85     public boolean getBoolean()
86     {
87         return value;
88     }
89
90     private static int makeInt(boolean b)
91     {
92         return (b?1:0);
93     }
94
95     /**
96      * @see DataValueDescriptor#getByte
97      */

98     public byte getByte()
99     {
100         return (byte) makeInt(value);
101     }
102
103     /**
104      * @see DataValueDescriptor#getShort
105      */

106     public short getShort()
107     {
108         return (short) makeInt(value);
109     }
110
111     /**
112      * @see DataValueDescriptor#getInt
113      */

114     public int getInt()
115     {
116         return makeInt(value);
117     }
118
119     /**
120      * @see DataValueDescriptor#getLong
121      */

122     public long getLong()
123     {
124         return (long) makeInt(value);
125     }
126
127     /**
128      * @see DataValueDescriptor#getFloat
129      */

130     public float getFloat()
131     {
132         return (float) makeInt(value);
133     }
134
135     /**
136      * @see DataValueDescriptor#getDouble
137      */

138     public double getDouble()
139     {
140         return (double) makeInt(value);
141     }
142
143     /**
144      * Implementation for BOOLEAN type. Convert to a BigDecimal using long
145      */

146     public int typeToBigDecimal()
147     {
148         return java.sql.Types.BIGINT;
149     }
150     public String JavaDoc getString()
151     {
152         if (isNull())
153             return null;
154         else if (value == true)
155             return "true";
156         else
157             return "false";
158     }
159
160     public Object JavaDoc getObject()
161     {
162         if (isNull())
163             return null;
164         else
165             return new Boolean JavaDoc(value);
166     }
167
168     public int getLength()
169     {
170         return BOOLEAN_LENGTH;
171     }
172
173     // this is for DataType's error generator
174
public String JavaDoc getTypeName()
175     {
176         return TypeId.BOOLEAN_NAME;
177     }
178
179     /*
180      * Storable interface, implies Externalizable, TypedFormat
181      */

182
183
184     /**
185         Return my format identifier.
186
187         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
188     */

189     public int getTypeFormatId() {
190         return StoredFormatIds.SQL_BOOLEAN_ID;
191     }
192
193     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
194
195         // never called when value is null
196
if (SanityManager.DEBUG)
197             SanityManager.ASSERT(! isNull());
198
199         out.writeBoolean(value);
200     }
201
202     /** @see java.io.Externalizable#readExternal */
203     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
204
205         if (SanityManager.DEBUG)
206             SanityManager.ASSERT( ! immutable,
207                         "Attempt to set the value of an immutable SQLBoolean");
208
209         value = in.readBoolean();
210         isnull = false;
211     }
212     public void readExternalFromArray(ArrayInputStream in) throws IOException JavaDoc {
213
214         if (SanityManager.DEBUG)
215             SanityManager.ASSERT( ! immutable,
216                         "Attempt to set the value of an immutable SQLBoolean");
217
218         value = in.readBoolean();
219         isnull = false;
220     }
221
222     /**
223      * @see Storable#restoreToNull
224      *
225      */

226     public void restoreToNull()
227     {
228         if (SanityManager.DEBUG)
229             SanityManager.ASSERT( ! immutable,
230                         "Attempt to set the value of an immutable SQLBoolean");
231
232         value = false;
233         isnull = true;
234     }
235
236     /*
237      * Orderable interface
238      */

239
240     /**
241         @exception StandardException thrown on error
242      */

243     public int compare(DataValueDescriptor other) throws StandardException
244     {
245         /* Use compare method from dominant type, negating result
246          * to reflect flipping of sides.
247          */

248         if (typePrecedence() < other.typePrecedence())
249         {
250             return - (other.compare(this));
251         }
252
253         boolean thisNull, otherNull;
254         thisNull = this.isNull();
255         otherNull = other.isNull();
256
257         /*
258          * thisNull otherNull thisValue thatValue return
259          * T T X X 0 (this == other)
260          * F T X X 1 (this > other)
261          * T F X X -1 (this < other)
262          *
263          * F F T T 0 (this == other)
264          * F F T F 1 (this > other)
265          * F F F T -1 (this < other)
266          * F F F F 0 (this == other)
267          */

268         if (thisNull || otherNull)
269         {
270             if (!thisNull) // otherNull must be true
271
return 1;
272             if (!otherNull) // thisNull must be true
273
return -1;
274             return 0;
275         }
276
277         /* neither are null, get the value */
278         boolean thisValue;
279         boolean otherValue = false;
280         thisValue = this.getBoolean();
281
282         otherValue = other.getBoolean();
283
284         if (thisValue == otherValue)
285             return 0;
286         else if (thisValue && !otherValue)
287             return 1;
288         else
289             return -1;
290     }
291
292     /**
293         @exception StandardException thrown on error
294      */

295     public boolean compare(int op,
296                            DataValueDescriptor other,
297                            boolean orderedNulls,
298                            boolean unknownRV)
299         throws StandardException
300     {
301         if (!orderedNulls) // nulls are unordered
302
{
303             if (this.isNull() || other.isNull())
304                 return unknownRV;
305         }
306         /* Do the comparison */
307         return super.compare(op, other, orderedNulls, unknownRV);
308     }
309
310     /*
311      * DataValueDescriptor interface
312      */

313
314     /** @see DataValueDescriptor#getClone */
315     public DataValueDescriptor getClone()
316     {
317         return new SQLBoolean(value, isnull);
318     }
319
320     /**
321      * @see DataValueDescriptor#getNewNull
322      */

323     public DataValueDescriptor getNewNull()
324     {
325         return new SQLBoolean();
326     }
327
328     /**
329      * @see DataValueDescriptor#setValueFromResultSet
330      *
331      * @exception SQLException Thrown on error
332      */

333     public void setValueFromResultSet(ResultSet JavaDoc resultSet, int colNumber,
334                                       boolean isNullable)
335         throws SQLException JavaDoc
336     {
337             value = resultSet.getBoolean(colNumber);
338             isnull = (isNullable && resultSet.wasNull());
339     }
340     /**
341         Set the value into a PreparedStatement.
342
343         @exception SQLException Error setting value in PreparedStatement
344     */

345     public final void setInto(PreparedStatement JavaDoc ps, int position) throws SQLException JavaDoc {
346
347         if (isNull()) {
348             ps.setNull(position, java.sql.Types.BIT);
349             return;
350         }
351
352         ps.setBoolean(position, value);
353     }
354     /*
355      * class interface
356      */

357
358     /*
359      * constructors
360      */

361
362     /* NOTE - other data types have both (type value) and (boolean nulls),
363      * (value, nulls)
364      * We can't do both (boolean value) and (boolean nulls) here,
365      * so we'll skip over (boolean value) and have (Boolean value) so
366      * that we can support (boolean nulls).
367      */

368
369     public SQLBoolean()
370     {
371         isnull = true;
372     }
373
374     public SQLBoolean(boolean val)
375     {
376         value = val;
377     }
378     public SQLBoolean(Boolean JavaDoc obj) {
379         if (isnull = (obj == null))
380             ;
381         else
382             value = obj.booleanValue();
383     }
384
385     /* This constructor gets used for the getClone() method */
386     private SQLBoolean(boolean val, boolean isnull)
387     {
388         value = val;
389         this.isnull = isnull;
390     }
391
392     /** @see BooleanDataValue#setValue */
393     public void setValue(boolean theValue)
394     {
395         if (SanityManager.DEBUG)
396             SanityManager.ASSERT( ! immutable,
397                         "Attempt to set the value of an immutable SQLBoolean");
398         value = theValue;
399         isnull = false;
400
401     }
402
403     public void setValue(Boolean JavaDoc theValue)
404     {
405         if (SanityManager.DEBUG)
406             SanityManager.ASSERT( ! immutable,
407                         "Attempt to set the value of an immutable SQLBoolean");
408         if (theValue == null)
409         {
410             value = false;
411             isnull = true;
412         }
413         else
414         {
415             value = theValue.booleanValue();
416             isnull = false;
417         }
418
419     }
420
421     // REMIND: do we need this, or is long enough?
422
public void setValue(byte theValue)
423     {
424         if (SanityManager.DEBUG)
425             SanityManager.ASSERT( ! immutable,
426                         "Attempt to set the value of an immutable SQLBoolean");
427         value = theValue != 0;
428         isnull = false;
429
430     }
431
432
433     // REMIND: do we need this, or is long enough?
434
public void setValue(short theValue)
435     {
436         if (SanityManager.DEBUG)
437             SanityManager.ASSERT( ! immutable,
438                         "Attempt to set the value of an immutable SQLBoolean");
439         value = theValue != 0;
440         isnull = false;
441
442     }
443
444
445     // REMIND: do we need this, or is long enough?
446
public void setValue(int theValue)
447     {
448         if (SanityManager.DEBUG)
449             SanityManager.ASSERT( ! immutable,
450                         "Attempt to set the value of an immutable SQLBoolean");
451         value = theValue != 0;
452         isnull = false;
453
454     }
455
456     public void setValue(long theValue)
457     {
458         if (SanityManager.DEBUG)
459             SanityManager.ASSERT( ! immutable,
460                         "Attempt to set the value of an immutable SQLBoolean");
461         value = theValue != 0;
462         isnull = false;
463
464     }
465
466     // REMIND: do we need this, or is double enough?
467
public void setValue(float theValue)
468     {
469         if (SanityManager.DEBUG)
470             SanityManager.ASSERT( ! immutable,
471                         "Attempt to set the value of an immutable SQLBoolean");
472         value = theValue != 0;
473         isnull = false;
474
475     }
476
477     public void setValue(double theValue)
478     {
479         if (SanityManager.DEBUG)
480             SanityManager.ASSERT( ! immutable,
481                         "Attempt to set the value of an immutable SQLBoolean");
482         value = theValue != 0;
483         isnull = false;
484
485     }
486
487     public void setBigDecimal(Number JavaDoc bigDecimal) throws StandardException
488     {
489         if (SanityManager.DEBUG)
490             SanityManager.ASSERT( ! immutable,
491                         "Attempt to set the value of an immutable SQLBoolean");
492         if (bigDecimal == null)
493         {
494             value = false;
495             isnull = true;
496         }
497         else
498         {
499             DataValueDescriptor tempDecimal = NumberDataType.ZERO_DECIMAL.getNewNull();
500             tempDecimal.setBigDecimal(bigDecimal);
501             value = NumberDataType.ZERO_DECIMAL.compare(tempDecimal) != 0;
502             isnull = false;
503         }
504
505     }
506
507     /**
508      * Set the value of this BooleanDataValue to the given byte array value
509      *
510      * @param theValue The value to set this BooleanDataValue to
511      */

512     public void setValue(byte[] theValue)
513     {
514         if (SanityManager.DEBUG)
515             SanityManager.ASSERT( ! immutable,
516                         "Attempt to set the value of an immutable SQLBoolean");
517
518         if (theValue != null)
519         {
520             isnull = false;
521             int length = theValue.length;
522     
523             /*
524             ** Step through all bytes. As soon
525             ** as we get one with something other
526             ** than 0, then we know we have a 'true'
527             */

528             for (int i = 0; i < length; i++)
529             {
530                 if (theValue[i] != 0)
531                 {
532                     value = true;
533                     return;
534                 }
535             }
536         }
537         else
538         {
539             isnull = true;
540         }
541         value = false;
542
543     }
544
545
546     /**
547      * Set the value of this BooleanDataValue to the given String.
548      * String is trimmed and upcased. If resultant string is not
549      * TRUE or FALSE, then an error is thrown.
550      *
551      * @param theValue The value to set this BooleanDataValue to
552      *
553      * @exception StandardException Thrown on error
554      */

555     public void setValue(String JavaDoc theValue)
556         throws StandardException
557     {
558         if (SanityManager.DEBUG)
559             SanityManager.ASSERT( ! immutable,
560                         "Attempt to set the value of an immutable SQLBoolean");
561         if (theValue == null)
562         {
563             value = false;
564             isnull = true;
565         }
566         else
567         {
568             /*
569             ** Note: cannot use getBoolean(String) here because
570             ** it doesn't trim, and doesn't throw exceptions.
571             */

572             String JavaDoc cleanedValue = StringUtil.SQLToUpperCase(theValue.trim());
573             if (cleanedValue.equals("TRUE"))
574             {
575                 value = true;
576             }
577             else if (cleanedValue.equals("FALSE"))
578             {
579                 value = false;
580             }
581             else
582             {
583                 throw invalidFormat();
584             }
585             isnull = false;
586         }
587
588     }
589
590     private void setValueCore(Number JavaDoc theValue)
591     {
592         if (SanityManager.DEBUG)
593             SanityManager.ASSERT( ! immutable,
594                         "Attempt to set the value of an immutable SQLBoolean");
595
596         if (theValue == null)
597         {
598             isnull = true;
599             value = false;
600         }
601         else
602         {
603             value = (theValue.intValue() != 0);
604             isnull = false;
605         }
606     }
607
608     /**
609      * @see DataValueDescriptor#setValue
610      */

611     void setObject(Object JavaDoc theValue)
612     {
613         setValue((Boolean JavaDoc) theValue);
614     }
615     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
616
617         setValue(theValue.getBoolean());
618     }
619
620
621     /*
622     ** SQL Operators
623     */

624
625     /**
626      * The = operator as called from the language module, as opposed to
627      * the storage module.
628      *
629      * @param left The value on the left side of the =
630      * @param right The value on the right side of the =
631      *
632      * @return A SQL boolean value telling whether the two parameters are equal
633      *
634      * @exception StandardException Thrown on error
635      */

636
637     public BooleanDataValue equals(DataValueDescriptor left,
638                              DataValueDescriptor right)
639                 throws StandardException
640     {
641         return truthValue(left,
642                             right,
643                             left.getBoolean() == right.getBoolean());
644     }
645
646     /**
647      * The <> operator as called from the language module, as opposed to
648      * the storage module.
649      *
650      * @param left The value on the left side of the <>
651      * @param right The value on the right side of the <>
652      *
653      * @return A SQL boolean value telling whether the two parameters are
654      * not equal
655      *
656      * @exception StandardException Thrown on error
657      */

658
659     public BooleanDataValue notEquals(DataValueDescriptor left,
660                              DataValueDescriptor right)
661                 throws StandardException
662     {
663         return truthValue(left,
664                             right,
665                             left.getBoolean() != right.getBoolean());
666     }
667
668     /**
669      * The < operator as called from the language module, as opposed to
670      * the storage module.
671      *
672      * @param left The value on the left side of the <
673      * @param right The value on the right side of the <
674      *
675      * @return A SQL boolean value telling whether the left operand is
676      * less than the right operand
677      *
678      * @exception StandardException Thrown on error
679      */

680
681     public BooleanDataValue lessThan(DataValueDescriptor left,
682                              DataValueDescriptor right)
683                 throws StandardException
684     {
685         /* We must call getBoolean() on both sides in order
686          * to catch any invalid casts.
687          */

688         boolean leftBoolean = left.getBoolean();
689         boolean rightBoolean = right.getBoolean();
690         /* By convention, false is less than true */
691         return truthValue(left,
692                             right,
693                             leftBoolean == false && rightBoolean == true);
694     }
695
696     /**
697      * The > operator as called from the language module, as opposed to
698      * the storage module.
699      *
700      * @param left The value on the left side of the >
701      * @param right The value on the right side of the >
702      *
703      * @return A SQL boolean value telling whether the left operand is
704      * greater than the right operand
705      *
706      * @exception StandardException Thrown on error
707      */

708
709     public BooleanDataValue greaterThan(DataValueDescriptor left,
710                              DataValueDescriptor right)
711                 throws StandardException
712     {
713         /* We must call getBoolean() on both sides in order
714          * to catch any invalid casts.
715          */

716         boolean leftBoolean = left.getBoolean();
717         boolean rightBoolean = right.getBoolean();
718         /* By convention, true is greater than false */
719         return truthValue(left,
720                             right,
721                             leftBoolean == true && rightBoolean == false);
722     }
723
724     /**
725      * The <= operator as called from the language module, as opposed to
726      * the storage module.
727      *
728      * @param left The value on the left side of the <=
729      * @param right The value on the right side of the <=
730      *
731      * @return A SQL boolean value telling whether the left operand is
732      * less than or equal to the right operand
733      *
734      * @exception StandardException Thrown on error
735      */

736
737     public BooleanDataValue lessOrEquals(DataValueDescriptor left,
738                              DataValueDescriptor right)
739                 throws StandardException
740     {
741         /* We must call getBoolean() on both sides in order
742          * to catch any invalid casts.
743          */

744         boolean leftBoolean = left.getBoolean();
745         boolean rightBoolean = right.getBoolean();
746         /* By convention, false is less than true */
747         return truthValue(left,
748                             right,
749                             leftBoolean == false || rightBoolean == true);
750     }
751
752     /**
753      * The >= operator as called from the language module, as opposed to
754      * the storage module.
755      *
756      * @param left The value on the left side of the >=
757      * @param right The value on the right side of the >=
758      *
759      * @return A SQL boolean value telling whether the left operand is
760      * greater than or equal to the right operand
761      *
762      * @exception StandardException Thrown on error
763      */

764
765     public BooleanDataValue greaterOrEquals(DataValueDescriptor left,
766                              DataValueDescriptor right)
767                 throws StandardException
768     {
769         /* We must call getBoolean() on both sides in order
770          * to catch any invalid casts.
771          */

772         boolean leftBoolean = left.getBoolean();
773         boolean rightBoolean = right.getBoolean();
774         /* By convention, true is greater than false */
775         return truthValue(left,
776                             right,
777                             leftBoolean == true || rightBoolean == false);
778     }
779
780     /**
781      * The AND operator. This implements SQL semantics for AND with unknown
782      * truth values - consult any standard SQL reference for an explanation.
783      *
784      * @param otherValue The other boolean to AND with this one
785      *
786      * @return this AND otherValue
787      *
788      */

789
790     public BooleanDataValue and(BooleanDataValue otherValue)
791     {
792         /*
793         ** Catch those cases where standard SQL null semantics don't work.
794         */

795         if (this.equals(false) || otherValue.equals(false))
796         {
797             return BOOLEAN_FALSE;
798         }
799         else
800         {
801             return truthValue(this,
802                             otherValue,
803                             this.getBoolean() && otherValue.getBoolean());
804         }
805     }
806
807     /**
808      * The OR operator. This implements SQL semantics for OR with unknown
809      * truth values - consult any standard SQL reference for an explanation.
810      *
811      * @param otherValue The other boolean to OR with this one
812      *
813      * @return this OR otherValue
814      *
815      */

816
817     public BooleanDataValue or(BooleanDataValue otherValue)
818     {
819         /*
820         ** Catch those cases where standard SQL null semantics don't work.
821         */

822         if (this.equals(true) || otherValue.equals(true))
823         {
824             return BOOLEAN_TRUE;
825         }
826         else
827         {
828             return truthValue(this,
829                             otherValue,
830                             this.getBoolean() || otherValue.getBoolean());
831         }
832     }
833
834     /**
835      * The SQL IS operator - consult any standard SQL reference for an explanation.
836      *
837      * Implements the following truth table:
838      *
839      * otherValue
840      * | TRUE | FALSE | UNKNOWN
841      * this |----------------------------
842      * |
843      * TRUE | TRUE | FALSE | FALSE
844      * FALSE | FALSE | TRUE | FALSE
845      * UNKNOWN | FALSE | FALSE | TRUE
846      *
847      *
848      * @param otherValue BooleanDataValue to compare to. May be TRUE, FALSE, or UNKNOWN.
849      *
850      * @return whether this IS otherValue
851      *
852      */

853     public BooleanDataValue is(BooleanDataValue otherValue)
854     {
855         if ( this.equals(true) && otherValue.equals(true) )
856         { return BOOLEAN_TRUE; }
857
858         if ( this.equals(false) && otherValue.equals(false) )
859         { return BOOLEAN_TRUE; }
860
861         if ( this.isNull() && otherValue.isNull() )
862         { return BOOLEAN_TRUE; }
863
864         return BOOLEAN_FALSE;
865     }
866
867     /**
868      * Implements NOT IS. This reverses the sense of the is() call.
869      *
870      *
871      * @param otherValue BooleanDataValue to compare to. May be TRUE, FALSE, or UNKNOWN.
872      *
873      * @return NOT( this IS otherValue )
874      *
875      */

876     public BooleanDataValue isNot(BooleanDataValue otherValue)
877     {
878         BooleanDataValue isValue = is( otherValue );
879
880         if ( isValue.equals(true) ) { return BOOLEAN_FALSE; }
881         else { return BOOLEAN_TRUE; }
882     }
883
884     /**
885      * Throw an exception with the given SQLState if this BooleanDataValue
886      * is false. This method is useful for evaluating constraints.
887      *
888      * @param sqlState The SQLState of the exception to throw if
889      * this SQLBoolean is false.
890      * @param tableName The name of the table to include in the exception
891      * message.
892      * @param constraintName The name of the failed constraint to include
893      * in the exception message.
894      *
895      * @return this
896      *
897      * @exception StandardException Thrown if this BooleanDataValue
898      * is false.
899      */

900     public BooleanDataValue throwExceptionIfFalse(
901                                     String JavaDoc sqlState,
902                                     String JavaDoc tableName,
903                                     String JavaDoc constraintName)
904                             throws StandardException
905     {
906         if ( ( ! isNull() ) && (value == false) )
907         {
908             throw StandardException.newException(sqlState,
909                                                 tableName,
910                                                 constraintName);
911         }
912
913         return this;
914     }
915
916     /*
917      * DataValueDescriptor interface
918      */

919
920     /** @see DataValueDescriptor#typePrecedence */
921     public int typePrecedence()
922     {
923         return TypeId.BOOLEAN_PRECEDENCE;
924     }
925
926     /*
927     ** Support functions
928     */

929
930     /**
931      * Return the SQL truth value for a comparison.
932      *
933      * This method first looks at the operands - if either is null, it
934      * returns the unknown truth value. This implements "normal" SQL
935      * null semantics, where if any operand is null, the result is null.
936      * Note that there are cases where these semantics are incorrect -
937      * for example, NULL AND FALSE is supposed to be FALSE, not NULL
938      * (the NULL truth value is the same as the UNKNOWN truth value).
939      *
940      * If neither operand is null, it returns a static final variable
941      * containing the SQLBoolean truth value. It returns different values
942      * depending on whether the truth value is supposed to be nullable.
943      *
944      * This method always returns a pre-allocated static final SQLBoolean.
945      * This is practical because there are so few possible return values.
946      * Using pre-allocated values allows us to avoid constructing new
947      * SQLBoolean values during execution.
948      *
949      * @param leftOperand The left operand of the binary comparison
950      * @param rightOperand The right operand of the binary comparison
951      * @param truth The truth value of the comparison
952      *
953      * @return A SQLBoolean containing the desired truth value.
954      */

955
956     public static SQLBoolean truthValue(
957                                 DataValueDescriptor leftOperand,
958                                 DataValueDescriptor rightOperand,
959                                 boolean truth)
960     {
961         /* Return UNKNOWN if either operand is null */
962         if (leftOperand.isNull() || rightOperand.isNull())
963         {
964             return unknownTruthValue();
965         }
966
967         /* Return the appropriate SQLBoolean for the given truth value */
968         if (truth == true)
969         {
970             return BOOLEAN_TRUE;
971         }
972         else
973         {
974             return BOOLEAN_FALSE;
975         }
976     }
977
978     /**
979      * same as above, but takes a Boolean, if it is null, unknownTruthValue is returned
980      */

981     public static SQLBoolean truthValue(
982                                 DataValueDescriptor leftOperand,
983                                 DataValueDescriptor rightOperand,
984                                 Boolean JavaDoc truth)
985     {
986         /* Return UNKNOWN if either operand is null */
987         if (leftOperand.isNull() || rightOperand.isNull() || truth==null)
988         {
989             return unknownTruthValue();
990         }
991
992         /* Return the appropriate SQLBoolean for the given truth value */
993         if (truth == Boolean.TRUE)
994         {
995             return BOOLEAN_TRUE;
996         }
997         else
998         {
999             return BOOLEAN_FALSE;
1000        }
1001    }
1002
1003    /**
1004     * Get a truth value.
1005     *
1006     * @param value The value of the SQLBoolean
1007     *
1008     * @return A SQLBoolean with the given truth value
1009     */

1010    public static SQLBoolean truthValue(boolean value)
1011    {
1012        /*
1013        ** Return the non-nullable versions of TRUE and FALSE, since they
1014        ** can never be null.
1015        */

1016        if (value == true)
1017            return BOOLEAN_TRUE;
1018        else
1019            return BOOLEAN_FALSE;
1020    }
1021
1022    /**
1023     * Return an unknown truth value. Check to be sure the return value is
1024     * nullable.
1025     *
1026     * @return A SQLBoolean representing the UNKNOWN truth value
1027     */

1028    public static SQLBoolean unknownTruthValue()
1029    {
1030        return UNKNOWN;
1031    }
1032
1033    /**
1034     * Return a false truth value.
1035     *
1036     *
1037     * @return A SQLBoolean representing the FALSE truth value
1038     */

1039    public static SQLBoolean falseTruthValue()
1040    {
1041        return BOOLEAN_FALSE;
1042    }
1043
1044    /**
1045     * Return a true truth value.
1046     *
1047     *
1048     * @return A SQLBoolean representing the TRUE truth value
1049     */

1050    public static SQLBoolean trueTruthValue()
1051    {
1052        return BOOLEAN_TRUE;
1053    }
1054    
1055    /**
1056     * Determine whether this SQLBoolean contains the given boolean value.
1057     *
1058     * This method is used by generated code to determine when to do
1059     * short-circuiting for an AND or OR.
1060     *
1061     * @param val The value to look for
1062     *
1063     * @return true if the given value equals the value in this SQLBoolean,
1064     * false if not
1065     */

1066
1067    public boolean equals(boolean val)
1068    {
1069        if (isNull())
1070            return false;
1071        else
1072            return value == val;
1073    }
1074    
1075    /**
1076     * Return an immutable BooleanDataValue with the same value as this.
1077     * @return An immutable BooleanDataValue with the same value as this.
1078     */

1079    public BooleanDataValue getImmutable()
1080    {
1081        if (isNull())
1082            return SQLBoolean.UNKNOWN;
1083        
1084        return value ? SQLBoolean.BOOLEAN_TRUE : SQLBoolean.BOOLEAN_FALSE;
1085    }
1086
1087    /*
1088     * String display of value
1089     */

1090
1091    public String JavaDoc toString()
1092    {
1093        if (isNull())
1094            return "NULL";
1095        else if (value == true)
1096            return "true";
1097        else
1098            return "false";
1099    }
1100
1101    /*
1102     * Hash code
1103     */

1104    public int hashCode()
1105    {
1106        if (isNull())
1107        {
1108            return -1;
1109        }
1110
1111        return (value) ? 1 : 0;
1112    }
1113
1114    /*
1115     * useful constants...
1116     */

1117    static final int BOOLEAN_LENGTH = 1; // must match the number of bytes written by DataOutput.writeBoolean()
1118

1119    private static final SQLBoolean BOOLEAN_TRUE = new SQLBoolean(true);
1120    private static final SQLBoolean BOOLEAN_FALSE = new SQLBoolean(false);
1121    static final SQLBoolean UNKNOWN = new SQLBoolean();
1122
1123    /* Static initialization block */
1124    static
1125    {
1126        /* Mark all the static SQLBooleans as immutable */
1127        BOOLEAN_TRUE.immutable = true;
1128        BOOLEAN_FALSE.immutable = true;
1129        UNKNOWN.immutable = true;
1130    }
1131
1132    private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLBoolean.class);
1133
1134    public int estimateMemoryUsage()
1135    {
1136        return BASE_MEMORY_USAGE;
1137    }
1138
1139    /*
1140     * object state
1141     */

1142    private boolean value;
1143    private boolean isnull;
1144    private boolean immutable;
1145}
1146
Popular Tags