KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLSmallint
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.types.DataValueDescriptor;
29 import org.apache.derby.iapi.types.TypeId;
30 import org.apache.derby.iapi.types.NumberDataValue;
31 import org.apache.derby.iapi.types.BooleanDataValue;
32
33 import org.apache.derby.iapi.services.io.StoredFormatIds;
34 import org.apache.derby.iapi.services.io.Storable;
35
36 import org.apache.derby.iapi.error.StandardException;
37 import org.apache.derby.iapi.services.sanity.SanityManager;
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  * SQLSmallint satisfies the DataValueDescriptor
54  * interfaces (i.e., OrderableDataType). It implements a smallint 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  * SQLSmallint 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  * <p>
67  * PERFORMANCE: There are likely alot of performance improvements
68  * possible for this implementation -- it new's Short
69  * more than it probably wants to.
70  */

71 public final class SQLSmallint
72     extends NumberDataType
73 {
74     /*
75      * DataValueDescriptor interface
76      * (mostly implemented in DataType)
77      */

78
79
80         // JDBC is lax in what it permits and what it
81
// returns, so we are similarly lax
82

83     /**
84      * @see DataValueDescriptor#getInt
85      */

86     public int getInt()
87     {
88         return (int) value;
89     }
90
91     /**
92      * @exception StandardException thrown on failure to convert
93      * @see DataValueDescriptor#getByte
94      */

95     public byte getByte() throws StandardException
96     {
97         if (value > Byte.MAX_VALUE || value < Byte.MIN_VALUE)
98             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT");
99         return (byte) value;
100     }
101
102     /**
103      * @see DataValueDescriptor#getShort
104      */

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

113     public long getLong()
114     {
115         return (long) value;
116     }
117
118     /**
119      * @see DataValueDescriptor#getFloat
120      */

121     public float getFloat()
122     {
123         return (float) value;
124     }
125
126     /**
127      * @see DataValueDescriptor#getDouble
128      */

129     public double getDouble()
130     {
131         return (double) value;
132     }
133
134     // for lack of a specification: 0 or null is false,
135
// all else is true
136
/**
137      * @see DataValueDescriptor#getBoolean
138      */

139     public boolean getBoolean()
140     {
141         return (value != 0);
142     }
143
144     /**
145      * @see DataValueDescriptor#getString
146      */

147     public String JavaDoc getString()
148     {
149         if (isNull())
150             return null;
151         else
152             return Short.toString(value);
153     }
154
155     /**
156      * @see DataValueDescriptor#getLength
157      */

158     public int getLength()
159     {
160         return SMALLINT_LENGTH;
161     }
162
163     /**
164      * @see DataValueDescriptor#getObject
165      */

166     public Object JavaDoc getObject()
167     {
168         if (isNull())
169             return null;
170         else
171             return new Integer JavaDoc(value);
172     }
173
174     // this is for DataType's error generator
175
public String JavaDoc getTypeName()
176     {
177         return TypeId.SMALLINT_NAME;
178     }
179
180     /*
181      * Storable interface, implies Externalizable, TypedFormat
182      */

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

190     public int getTypeFormatId() {
191         return StoredFormatIds.SQL_SMALLINT_ID;
192     }
193
194     /**
195      * always false for non-nullable columns
196      @see Storable#isNull
197     */

198     public boolean isNull()
199     {
200         return isnull;
201     }
202
203     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
204
205         // never called when value is null
206
if (SanityManager.DEBUG)
207             SanityManager.ASSERT(! isNull());
208
209         out.writeShort(value);
210     }
211
212     /** @see java.io.Externalizable#readExternal */
213     public void readExternalFromArray(ArrayInputStream in) throws IOException JavaDoc {
214
215         value = in.readShort();
216         isnull = false;
217     }
218     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
219
220         value = in.readShort();
221         isnull = false;
222     }
223
224     /**
225      * @see Storable#restoreToNull
226      *
227      */

228     public void restoreToNull()
229     {
230         value = 0;
231         isnull = true;
232     }
233
234
235     /** @exception StandardException Thrown on error */
236     protected int typeCompare(DataValueDescriptor arg) throws StandardException
237     {
238         /* neither are null, get the value */
239
240         /* Do comparisons with ints to avoid overflow problems */
241         int thisValue = this.getInt();
242         int otherValue = arg.getInt();
243         if (thisValue == otherValue)
244             return 0;
245         else if (thisValue > otherValue)
246             return 1;
247         else
248             return -1;
249     }
250
251     /*
252      * DataValueDescriptor interface
253      */

254
255     /** @see DataValueDescriptor#getClone */
256     public DataValueDescriptor getClone()
257     {
258         return new SQLSmallint(value, isnull);
259     }
260
261     /**
262      * @see DataValueDescriptor#getNewNull
263      */

264     public DataValueDescriptor getNewNull()
265     {
266         return new SQLSmallint();
267     }
268
269     /**
270      * @see DataValueDescriptor#setValueFromResultSet
271      *
272      * @exception SQLException Thrown on error
273      */

274     public void setValueFromResultSet(ResultSet JavaDoc resultSet, int colNumber,
275                                       boolean isNullable)
276         throws SQLException JavaDoc
277     {
278         try {
279             value = resultSet.getShort(colNumber);
280             isnull = (isNullable && resultSet.wasNull());
281         } catch (SQLException JavaDoc selq) {
282             int i = resultSet.getInt(colNumber);
283             value = (short) i;
284             isnull = false;
285
286         }
287     }
288     /**
289         Set the value into a PreparedStatement.
290
291         @exception SQLException Error setting value in PreparedStatement
292     */

293     public final void setInto(PreparedStatement JavaDoc ps, int position) throws SQLException JavaDoc {
294
295         if (isNull()) {
296             ps.setNull(position, java.sql.Types.SMALLINT);
297             return;
298         }
299
300         ps.setShort(position, value);
301     }
302     /**
303         Set this value into a ResultSet for a subsequent ResultSet.insertRow
304         or ResultSet.updateRow. This method will only be called for non-null values.
305
306         @exception SQLException thrown by the ResultSet object
307         @exception StandardException thrown by me accessing my value.
308     */

309     public final void setInto(ResultSet JavaDoc rs, int position) throws SQLException JavaDoc, StandardException {
310         rs.updateShort(position, value);
311     }
312
313
314     /*
315      * class interface
316      */

317
318     /*
319      * constructors
320      */

321
322     /**
323         No-arg constructor, required by Formattable.
324     // This constructor also gets used when we are
325     // allocating space for a short.
326     */

327     public SQLSmallint()
328     {
329         isnull = true;
330     }
331
332     public SQLSmallint(short val)
333     {
334         value = val;
335     }
336
337     /* This constructor gets used for the getClone() method */
338     public SQLSmallint(short val, boolean isnull)
339     {
340         value = val;
341         this.isnull = isnull;
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 = Short.valueOf(theValue.trim()).shortValue();
359             } catch (NumberFormatException JavaDoc nfe) {
360                 throw invalidFormat();
361             }
362             isnull = false;
363         }
364
365     }
366
367     public void setValue(short theValue)
368     {
369         value = theValue;
370         isnull = false;
371     }
372
373     public void setValue(byte theValue)
374     {
375         value = theValue;
376         isnull = false;
377     }
378
379     /**
380         @exception StandardException if outsideRangeForSmallint
381      */

382     public void setValue(int theValue) throws StandardException
383     {
384         if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE)
385             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT");
386         value = (short)theValue;
387         isnull = false;
388     }
389
390     /**
391         @exception StandardException if outsideRangeForSmallint
392      */

393     public void setValue(long theValue) throws StandardException
394     {
395         if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE)
396             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT");
397         value = (short)theValue;
398         isnull = false;
399     }
400
401     /**
402      * @see NumberDataValue#setValue
403      *
404      * @exception StandardException Thrown on error
405      */

406     public void setValue(float theValue) throws StandardException
407     {
408         theValue = NumberDataType.normalizeREAL(theValue);
409
410         if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE)
411             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT");
412
413         float floorValue = (float)Math.floor(theValue);
414
415         value = (short)floorValue;
416         isnull = false;
417     }
418
419     /**
420      * @see NumberDataValue#setValue
421      *
422      * @exception StandardException Thrown on error
423      */

424     public void setValue(double theValue) throws StandardException
425     {
426         theValue = NumberDataType.normalizeDOUBLE(theValue);
427
428         if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE)
429             throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT");
430
431         double floorValue = Math.floor(theValue);
432
433         value = (short)floorValue;
434         isnull = false;
435     }
436
437     /**
438      * @see NumberDataValue#setValue
439      *
440      */

441     public void setValue(boolean theValue)
442     {
443         value = theValue?(short)1:(short)0;
444         isnull = false;
445     }
446
447     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
448
449         setValue(theValue.getShort());
450     }
451
452     /*
453      * DataValueDescriptor interface
454      */

455
456     /** @see DataValueDescriptor#typePrecedence */
457     public int typePrecedence()
458     {
459         return TypeId.SMALLINT_PRECEDENCE;
460     }
461
462
463     /*
464     ** SQL Operators
465     */

466
467     /**
468      * The = operator as called from the language module, as opposed to
469      * the storage module.
470      *
471      * @param left The value on the left side of the =
472      * @param right The value on the right side of the =
473      *
474      * @return A SQL boolean value telling whether the two parameters are equal
475      *
476      * @exception StandardException Thrown on error
477      */

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

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

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

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

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

589
590     public BooleanDataValue greaterOrEquals(DataValueDescriptor left,
591                             DataValueDescriptor right)
592             throws StandardException
593     {
594         return SQLBoolean.truthValue(left,
595                                      right,
596                                      left.getShort() >= right.getShort());
597     }
598
599
600
601
602     /**
603      * This method implements the * operator for "smallint * smallint".
604      *
605      * @param left The first value to be multiplied
606      * @param right The second value to be multiplied
607      * @param result The result of a previous call to this method, null
608      * if not called yet
609      *
610      * @return A SQLSmallint containing the result of the multiplication
611      *
612      * @exception StandardException Thrown on error
613      */

614
615     public NumberDataValue times(NumberDataValue left,
616                             NumberDataValue right,
617                             NumberDataValue result)
618                 throws StandardException
619     {
620         if (result == null)
621         {
622             result = new SQLSmallint();
623         }
624
625         if (left.isNull() || right.isNull())
626         {
627             result.setToNull();
628             return result;
629         }
630
631         /*
632         ** Java does not check for overflow with integral types. We have to
633         ** check the result ourselves.
634         **
635             The setValue(int) will perform the overflow check.
636         */

637         int product = left.getShort() * right.getShort();
638         result.setValue(product);
639         return result;
640     }
641
642
643     /**
644         mod(smallint, smallint)
645     */

646     public NumberDataValue mod(NumberDataValue dividend,
647                              NumberDataValue divisor,
648                              NumberDataValue result)
649                 throws StandardException
650     {
651         if (result == null)
652         {
653             result = new SQLSmallint();
654         }
655
656         if (dividend.isNull() || divisor.isNull())
657         {
658             result.setToNull();
659             return result;
660         }
661
662         /* Catch divide by 0 */
663         short shortDivisor = divisor.getShort();
664         if (shortDivisor == 0)
665         {
666             throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
667         }
668
669         result.setValue(dividend.getShort() % shortDivisor);
670         return result;
671     }
672     /**
673      * This method implements the unary minus operator for smallint.
674      *
675      * @param result The result of a previous call to this method, null
676      * if not called yet
677      *
678      * @return A SQLSmalllint containing the result of the division
679      *
680      * @exception StandardException Thrown on error
681      */

682
683     public NumberDataValue minus(NumberDataValue result)
684                                     throws StandardException
685     {
686         if (result == null)
687         {
688             result = new SQLSmallint();
689         }
690
691         if (this.isNull())
692         {
693             result.setToNull();
694             return result;
695         }
696
697         int operandValue = this.getShort();
698
699         result.setValue(-operandValue);
700         return result;
701     }
702
703     /**
704      * This method implements the isNegative method.
705      *
706      * @return A boolean. If this.value is negative, return true.
707      * For positive values or null, return false.
708      */

709
710     protected boolean isNegative()
711     {
712         return !isNull() && value < 0;
713     }
714
715     /*
716      * String display of value
717      */

718
719     public String JavaDoc toString()
720     {
721         if (isNull())
722             return "NULL";
723         else
724             return Short.toString(value);
725     }
726
727     /*
728      * Hash code
729      */

730     public int hashCode()
731     {
732         return (int) value;
733     }
734
735     /*
736      * useful constants...
737      */

738     static final int SMALLINT_LENGTH = 2;
739
740     private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLSmallint.class);
741
742     public int estimateMemoryUsage()
743     {
744         return BASE_MEMORY_USAGE;
745     }
746
747     /*
748      * object state
749      */

750     private short value;
751     private boolean isnull;
752 }
753
Popular Tags