KickJava   Java API By Example, From Geeks To Geeks.

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


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

67 public final class SQLTinyint
68     extends NumberDataType
69 {
70
71     /*
72      * constants
73      */

74     static final int TINYINT_LENGTH = 1;
75
76     /*
77      * object state
78      */

79     private byte value;
80     private boolean isnull;
81
82     private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLTinyint.class);
83
84     public int estimateMemoryUsage()
85     {
86         return BASE_MEMORY_USAGE;
87     }
88
89     /*
90      * class interface
91      */

92
93     /*
94      * Constructors
95      */

96
97     /**
98      * No-arg constructor, required by Formattable.
99      * This constructor also gets used when we are
100      * allocating space for a byte.
101      */

102     public SQLTinyint()
103     {
104         isnull = true;
105     }
106
107     public SQLTinyint(byte val)
108     {
109         value = val;
110     }
111
112     /* This constructor gets used for the getClone() method */
113     public SQLTinyint(byte val, boolean isnull)
114     {
115         value = val;
116         this.isnull = isnull;
117     }
118
119     //////////////////////////////////////////////////////////////
120
//
121
// DataValueDescriptor interface
122
// (mostly implemented in DataType)
123
//
124
//////////////////////////////////////////////////////////////
125

126     /**
127      * @see DataValueDescriptor#getInt
128      */

129     public int getInt()
130     {
131         return (int) value;
132     }
133
134     /**
135      * @see DataValueDescriptor#getByte
136      */

137     public byte getByte()
138     {
139         return value;
140     }
141
142     /**
143      * @see DataValueDescriptor#getShort
144      */

145     public short getShort()
146     {
147         return (short) value;
148     }
149
150     /**
151      * @see DataValueDescriptor#getLong
152      */

153     public long getLong()
154     {
155         return (long) value;
156     }
157
158     /**
159      * @see DataValueDescriptor#getFloat
160      */

161     public float getFloat()
162     {
163         return (float) value;
164     }
165
166     /**
167      * @see DataValueDescriptor#getDouble
168      */

169     public double getDouble()
170     {
171         return (double) value;
172     }
173
174     /**
175      * @see DataValueDescriptor#getBoolean
176      */

177     public boolean getBoolean()
178     {
179         return (value != 0);
180     }
181
182     /**
183      * @see DataValueDescriptor#getString
184      */

185     public String JavaDoc getString()
186     {
187         return (isNull()) ?
188                     null:
189                     Byte.toString(value);
190     }
191
192     /**
193      * @see DataValueDescriptor#getLength
194      */

195     public int getLength()
196     {
197         return TINYINT_LENGTH;
198     }
199
200     /**
201      * @see DataValueDescriptor#getObject
202      */

203     public Object JavaDoc getObject()
204     {
205         return (isNull()) ?
206                     null:
207                     new Integer JavaDoc(value);
208     }
209
210     // this is for DataType's error generator
211
public String JavaDoc getTypeName()
212     {
213         return TypeId.TINYINT_NAME;
214     }
215
216     /*
217      * Storable interface, implies Externalizable, TypedFormat
218      */

219
220
221     /**
222         Return my format identifier.
223
224         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
225     */

226     public int getTypeFormatId()
227     {
228         return StoredFormatIds.SQL_TINYINT_ID;
229     }
230
231     /**
232      * @see Storable#isNull
233      */

234     public boolean isNull()
235     {
236         return isnull;
237     }
238
239     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
240
241         // never called when value is null
242
if (SanityManager.DEBUG)
243             SanityManager.ASSERT(! isNull());
244
245         out.writeByte(value);
246     }
247
248     /** @see java.io.Externalizable#readExternal */
249     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
250
251         value = in.readByte();
252         isnull = false;
253     }
254     public void readExternalFromArray(ArrayInputStream in) throws IOException JavaDoc {
255
256         value = in.readByte();
257         isnull = false;
258     }
259
260
261     /**
262      * @see Storable#restoreToNull
263      *
264      */

265     public void restoreToNull()
266     {
267         value = 0;
268         isnull = true;
269     }
270
271     /** @exception StandardException Thrown on error */
272     protected int typeCompare(DataValueDescriptor arg) throws StandardException
273     {
274         /* neither are null, get the value */
275         int thisValue, otherValue;
276
277         /* Do comparisons with ints to avoid overflow problems */
278         thisValue = this.getInt();
279         otherValue = arg.getInt();
280         if (thisValue == otherValue)
281             return 0;
282         else if (thisValue > otherValue)
283             return 1;
284         else
285             return -1;
286     }
287
288     /*
289      * DataValueDescriptor interface
290      */

291
292     /** @see DataValueDescriptor#getClone */
293     public DataValueDescriptor getClone()
294     {
295         return new SQLTinyint(value, isnull);
296     }
297
298     /**
299      * @see DataValueDescriptor#getNewNull
300      */

301     public DataValueDescriptor getNewNull()
302     {
303         return new SQLTinyint();
304     }
305
306     /**
307      * @see DataValueDescriptor#setValueFromResultSet
308      *
309      * @exception SQLException Thrown on error
310      */

311     public void setValueFromResultSet(ResultSet JavaDoc resultSet, int colNumber,
312                                       boolean isNullable)
313         throws SQLException JavaDoc
314     {
315             value = resultSet.getByte(colNumber);
316             isnull = (isNullable && resultSet.wasNull());
317     }
318     /**
319         Set the value into a PreparedStatement.
320
321         @exception SQLException Error setting value in PreparedStatement
322     */

323     public final void setInto(PreparedStatement JavaDoc ps, int position) throws SQLException JavaDoc {
324
325         if (isNull()) {
326             ps.setNull(position, java.sql.Types.TINYINT);
327             return;
328         }
329
330         ps.setByte(position, value);
331     }
332     /**
333         Set this value into a ResultSet for a subsequent ResultSet.insertRow
334         or ResultSet.updateRow. This method will only be called for non-null values.
335
336         @exception SQLException thrown by the ResultSet object
337         @exception StandardException thrown by me accessing my value.
338     */

339     public final void setInto(ResultSet JavaDoc rs, int position) throws SQLException JavaDoc, StandardException {
340         rs.updateByte(position, value);
341     }
342
343
344     /**
345         @exception StandardException thrown if string not accepted
346      */

347     public void setValue(String JavaDoc theValue)
348         throws StandardException
349     {
350         if (theValue == null)
351         {
352             value = 0;
353             isnull = true;
354         }
355         else
356         {
357             try {
358                 value = Byte.valueOf(theValue.trim()).byteValue();
359             } catch (NumberFormatException JavaDoc nfe) {
360                 throw invalidFormat();
361             }
362             isnull = false;
363         }
364     }
365
366
367     public void setValue(byte theValue)
368     {
369         value = theValue;
370         isnull = false;
371     }
372
373     /**
374         @exception StandardException if outsideRangeForTinyint
375      */

376     public void setValue(short theValue) throws StandardException
377     {
378         if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE)
379             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT");
380         value = (byte)theValue;
381         isnull = false;
382     }
383
384     /**
385         @exception StandardException if outsideRangeForTinyint
386      */

387     public void setValue(int theValue) throws StandardException
388     {
389         if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE)
390             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT");
391         value = (byte)theValue;
392         isnull = false;
393     }
394
395     /**
396         @exception StandardException if outsideRangeForTinyint
397      */

398     public void setValue(long theValue) throws StandardException
399     {
400         if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE)
401             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT");
402         value = (byte)theValue;
403         isnull = false;
404     }
405
406     /**
407      * @see NumberDataValue#setValue
408      *
409      * @exception StandardException Thrown on error
410      */

411     public void setValue(float theValue) throws StandardException
412     {
413         theValue = NumberDataType.normalizeREAL(theValue);
414
415         if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE)
416             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT");
417
418         float floorValue = (float)Math.floor(theValue);
419
420         value = (byte)floorValue;
421         isnull = false;
422
423     }
424
425     /**
426      * @see NumberDataValue#setValue
427      *
428      * @exception StandardException Thrown on error
429      */

430     public void setValue(double theValue) throws StandardException
431     {
432         theValue = NumberDataType.normalizeDOUBLE(theValue);
433
434         if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE)
435             throw outOfRange();
436
437         double floorValue = Math.floor(theValue);
438
439         value = (byte)floorValue;
440         isnull = false;
441     }
442
443     /**
444      * @see NumberDataValue#setValue
445      *
446      */

447     public void setValue(boolean theValue)
448     {
449         value = theValue?(byte)1:(byte)0;
450         isnull = false;
451     }
452
453     
454     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
455
456         setValue(theValue.getByte());
457     }
458
459
460     /*
461      * DataValueDescriptor interface
462      */

463
464     /** @see DataValueDescriptor#typePrecedence */
465     public int typePrecedence()
466     {
467         return TypeId.TINYINT_PRECEDENCE;
468     }
469
470     /*
471     ** SQL Operators
472     */

473
474     /**
475      * The = operator as called from the language module, as opposed to
476      * the storage module.
477      *
478      * @param left The value on the left side of the =
479      * @param right The value on the right side of the =
480      *
481      * @return A SQL boolean value telling whether the two parameters are equal
482      *
483      * @exception StandardException Thrown on error
484      */

485
486     public BooleanDataValue equals(DataValueDescriptor left,
487                             DataValueDescriptor right)
488             throws StandardException
489     {
490         return SQLBoolean.truthValue(left,
491                                      right,
492                                      left.getByte() == right.getByte());
493     }
494
495     /**
496      * The <> operator as called from the language module, as opposed to
497      * the storage module.
498      *
499      * @param left The value on the left side of the <>
500      * @param right The value on the right side of the <>
501      *
502      * @return A SQL boolean value telling whether the two parameters
503      * are not equal
504      *
505      * @exception StandardException Thrown on error
506      */

507
508     public BooleanDataValue notEquals(DataValueDescriptor left,
509                             DataValueDescriptor right)
510             throws StandardException
511     {
512         return SQLBoolean.truthValue(left,
513                                      right,
514                                      left.getByte() != right.getByte());
515     }
516
517     /**
518      * The < operator as called from the language module, as opposed to
519      * the storage module.
520      *
521      * @param left The value on the left side of the <
522      * @param right The value on the right side of the <
523      *
524      * @return A SQL boolean value telling whether the first operand is less
525      * than the second operand
526      *
527      * @exception StandardException Thrown on error
528      */

529
530     public BooleanDataValue lessThan(DataValueDescriptor left,
531                             DataValueDescriptor right)
532             throws StandardException
533     {
534         return SQLBoolean.truthValue(left,
535                                      right,
536                                      left.getByte() < right.getByte());
537     }
538
539     /**
540      * The > operator as called from the language module, as opposed to
541      * the storage module.
542      *
543      * @param left The value on the left side of the >
544      * @param right The value on the right side of the >
545      *
546      * @return A SQL boolean value telling whether the first operand is greater
547      * than the second operand
548      *
549      * @exception StandardException Thrown on error
550      */

551
552     public BooleanDataValue greaterThan(DataValueDescriptor left,
553                             DataValueDescriptor right)
554             throws StandardException
555     {
556         return SQLBoolean.truthValue(left,
557                                      right,
558                                      left.getByte() > right.getByte());
559     }
560
561     /**
562      * The <= operator as called from the language module, as opposed to
563      * the storage module.
564      *
565      * @param left The value on the left side of the <=
566      * @param right The value on the right side of the <=
567      *
568      * @return A SQL boolean value telling whether the first operand is less
569      * than or equal to the second operand
570      *
571      * @exception StandardException Thrown on error
572      */

573
574     public BooleanDataValue lessOrEquals(DataValueDescriptor left,
575                             DataValueDescriptor right)
576             throws StandardException
577     {
578         return SQLBoolean.truthValue(left,
579                                      right,
580                                      left.getByte() <= right.getByte());
581     }
582
583     /**
584      * The >= operator as called from the language module, as opposed to
585      * the storage module.
586      *
587      * @param left The value on the left side of the >=
588      * @param right The value on the right side of the >=
589      *
590      * @return A SQL boolean value telling whether the first operand is greater
591      * than or equal to the second operand
592      *
593      * @exception StandardException Thrown on error
594      */

595
596     public BooleanDataValue greaterOrEquals(DataValueDescriptor left,
597                             DataValueDescriptor right)
598             throws StandardException
599     {
600         return SQLBoolean.truthValue(left,
601                                      right,
602                                      left.getByte() >= right.getByte());
603     }
604
605
606
607
608
609     /**
610      * This method implements the * operator for "tinyint * tinyint".
611      *
612      * @param left The first value to be multiplied
613      * @param right The second value to be multiplied
614      * @param result The result of a previous call to this method, null
615      * if not called yet
616      *
617      * @return A SQLTinyint containing the result of the multiplication
618      *
619      * @exception StandardException Thrown on error
620      */

621
622     public NumberDataValue times(NumberDataValue left,
623                             NumberDataValue right,
624                             NumberDataValue result)
625                 throws StandardException
626     {
627         if (result == null)
628         {
629             result = new SQLTinyint();
630         }
631
632         if (left.isNull() || right.isNull())
633         {
634             result.setToNull();
635             return result;
636         }
637
638         /*
639         ** Java does not check for overflow with integral types. We have to
640         ** check the result ourselves.
641         **
642         ** The product of 2 bytes is an int, so we check to see if the product
643         ** is in the range of values for a byte.
644         */

645         int product = left.getByte() * right.getByte();
646         result.setValue(product);
647         return result;
648     }
649
650
651     /**
652         mod(tinyint, tinyint)
653     */

654
655     public NumberDataValue mod(NumberDataValue dividend,
656                              NumberDataValue divisor,
657                              NumberDataValue result)
658                 throws StandardException
659     {
660         if (result == null)
661         {
662             result = new SQLTinyint();
663         }
664
665         if (dividend.isNull() || divisor.isNull())
666         {
667             result.setToNull();
668             return result;
669         }
670
671         /* Catch divide by 0 */
672         byte byteDivisor = divisor.getByte();
673         if (byteDivisor == 0)
674         {
675             throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
676         }
677
678         result.setValue(dividend.getByte() % byteDivisor);
679         return result;
680     }
681     /**
682      * This method implements the unary minus operator for tinyint.
683      *
684      * @param result The result of a previous call to this method, null
685      * if not called yet
686      *
687      * @return A SQLTinyint containing the result of the division
688      *
689      * @exception StandardException Thrown on error
690      */

691
692     public NumberDataValue minus(NumberDataValue result)
693                                     throws StandardException
694     {
695         if (result == null)
696         {
697             result = new SQLTinyint();
698         }
699
700         if (this.isNull())
701         {
702             result.setToNull();
703             return result;
704         }
705
706         int operandValue = this.getByte();
707
708         result.setValue(-operandValue);
709         return result;
710     }
711
712     /**
713      * This method implements the isNegative method.
714      *
715      * @return A boolean. If this.value is negative, return true.
716      * For positive values or null, return false.
717      */

718
719     protected boolean isNegative()
720     {
721         return !isNull() && value < 0;
722     }
723
724     /*
725      * String display of value
726      */

727
728     public String JavaDoc toString()
729     {
730         if (isNull())
731             return "NULL";
732         else
733             return Byte.toString(value);
734     }
735
736
737     /*
738      * Hash code
739      */

740     public int hashCode()
741     {
742         return (int) value;
743     }
744 }
745
Popular Tags