KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.DataTypeUtilities
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.error.StandardException;
25
26 import org.apache.derby.iapi.reference.JDBC30Translation;
27 import org.apache.derby.iapi.services.io.StoredFormatIds;
28
29 import java.sql.Types JavaDoc;
30 import java.sql.ResultSetMetaData JavaDoc;
31
32 /**
33     A set of static utility methods for data types.
34  * @author djd
35  */

36 public abstract class DataTypeUtilities {
37
38     /**
39         Get the precision of the datatype.
40         @param dtd data type descriptor
41     */

42     public static int getPrecision(DataTypeDescriptor dtd) {
43         int typeId = dtd.getTypeId().getJDBCTypeId();
44
45         switch ( typeId )
46         {
47         case Types.CHAR: // CHAR et alia return their # characters...
48
case Types.VARCHAR:
49         case Types.LONGVARCHAR:
50         case Types.CLOB:
51         case Types.BINARY: // BINARY types return their # bytes...
52
case Types.VARBINARY:
53         case Types.LONGVARBINARY:
54         case Types.BLOB:
55         case StoredFormatIds.XML_TYPE_ID:
56                 return dtd.getMaximumWidth();
57             case Types.SMALLINT:
58                 return 5;
59             case JDBC30Translation.SQL_TYPES_BOOLEAN:
60                 return 1;
61         }
62         
63         return dtd.getPrecision();
64     }
65
66     /**
67         Get the precision of the datatype, in decimal digits
68         This is used by EmbedResultSetMetaData.
69         @param dtd data type descriptor
70     */

71     public static int getDigitPrecision(DataTypeDescriptor dtd) {
72         int typeId = dtd.getTypeId().getJDBCTypeId();
73
74         switch ( typeId )
75         {
76             case Types.FLOAT:
77             case Types.DOUBLE:
78                 return TypeId.DOUBLE_PRECISION_IN_DIGITS;
79             case Types.REAL:
80                 return TypeId.REAL_PRECISION_IN_DIGITS;
81             default: return getPrecision(dtd);
82         }
83
84     }
85
86
87     /**
88         Is the data type currency.
89         @param dtd data type descriptor
90     */

91     public static boolean isCurrency(DataTypeDescriptor dtd) {
92         int typeId = dtd.getTypeId().getJDBCTypeId();
93
94         // Only the NUMERIC and DECIMAL types are currency
95
return ((typeId == Types.DECIMAL) || (typeId == Types.NUMERIC));
96     }
97
98     /**
99         Is the data type case sensitive.
100         @param dtd data type descriptor
101     */

102     public static boolean isCaseSensitive(DataTypeDescriptor dtd) {
103         int typeId = dtd.getTypeId().getJDBCTypeId();
104
105         return (typeId == Types.CHAR ||
106                   typeId == Types.VARCHAR ||
107                   typeId == Types.CLOB ||
108                   typeId == Types.LONGVARCHAR ||
109                   typeId == StoredFormatIds.XML_TYPE_ID);
110     }
111     /**
112         Is the data type nullable.
113         @param dtd data type descriptor
114     */

115     public static int isNullable(DataTypeDescriptor dtd) {
116         return dtd.isNullable() ?
117                 ResultSetMetaData.columnNullable :
118                 ResultSetMetaData.columnNoNulls;
119     }
120
121     /**
122         Is the data type signed.
123         @param dtd data type descriptor
124     */

125     public static boolean isSigned(DataTypeDescriptor dtd) {
126         int typeId = dtd.getTypeId().getJDBCTypeId();
127
128         return ( typeId == Types.INTEGER ||
129                     typeId == Types.FLOAT ||
130                     typeId == Types.DECIMAL ||
131                     typeId == Types.SMALLINT ||
132                     typeId == Types.BIGINT ||
133                     typeId == Types.TINYINT ||
134                     typeId == Types.NUMERIC ||
135                     typeId == Types.REAL ||
136                     typeId == Types.DOUBLE );
137     }
138
139     /**
140       * Gets the display width of a column of a given type.
141       *
142       * @param dtd data type descriptor
143       *
144       * @return associated column display width
145       */

146     public static int getColumnDisplaySize(DataTypeDescriptor dtd)
147     {
148         int typeId = dtd.getTypeId().getJDBCTypeId();
149         int storageLength = dtd.getMaximumWidth();
150         return DataTypeUtilities.getColumnDisplaySize(typeId, storageLength);
151     }
152
153     public static int getColumnDisplaySize(int typeId, int storageLength)
154     {
155         int size;
156         switch (typeId)
157         {
158             case Types.TIMESTAMP:
159                 size = 26;
160                 break;
161             case Types.DATE:
162                 size = 10;
163                 break;
164             case Types.TIME:
165                 size = 8;
166                 break;
167             case Types.INTEGER:
168                 size = 11;
169                 break;
170             case Types.SMALLINT :
171                 size = 6;
172                 break;
173             case Types.REAL :
174             case Types.FLOAT :
175                 size = 13;
176                 break;
177             case Types.DOUBLE:
178                 size = 22;
179                 break;
180             case Types.TINYINT :
181                 size = 15;
182                 break;
183
184             case Types.BINARY:
185             case Types.VARBINARY:
186             case Types.LONGVARBINARY:
187             case Types.BLOB:
188                 size = 2*storageLength;
189                 if (size < 0)
190                     size = Integer.MAX_VALUE;
191                 break;
192
193             case Types.BIGINT:
194                 size = 20;
195                 break;
196             case Types.BIT:
197             case JDBC30Translation.SQL_TYPES_BOOLEAN:
198                 // Types.BIT == SQL BOOLEAN, so 5 chars for 'false'
199
// In JDBC 3.0, Types.BIT or Types.BOOLEAN = SQL BOOLEAN
200
size = 5;
201                 break;
202             default:
203                 // MaximumWidth is -1 when it is unknown.
204
int w = storageLength;
205                 size = (w > 0 ? w : 15);
206                 break;
207         }
208         return size;
209     }
210
211     /**
212      * Compute the maximum width (column display width) of a decimal or numeric data value,
213      * given its precision and scale.
214      *
215      * @param precision The precision (number of digits) of the data value.
216      * @param scale The number of fractional digits (digits to the right of the decimal point).
217      *
218      * @return The maximum number of chracters needed to display the value.
219      */

220     public static int computeMaxWidth( int precision, int scale)
221     {
222     // There are 3 possible cases with respect to finding the correct max
223
// width for DECIMAL type.
224
// 1. If scale = 0, only sign should be added to precision.
225
// 2. scale=precision, 3 should be added to precision for sign, decimal and an additional char '0'.
226
// 3. precision > scale > 0, 2 should be added to precision for sign and decimal.
227
return (scale ==0) ? (precision +1) : ((scale == precision) ? (precision + 3) : (precision + 2));
228     }
229 }
230
231
Popular Tags