--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.types.DataTypeUtilities\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.types;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.reference.JDBC30Translation;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+\r
+import java.sql.Types;\r
+import java.sql.ResultSetMetaData;\r
+\r
+/**\r
+ A set of static utility methods for data types.\r
+ */\r
+public abstract class DataTypeUtilities {\r
+\r
+ /**\r
+ Get the precision of the datatype.\r
+ @param dtd data type descriptor\r
+ */\r
+ public static int getPrecision(DataTypeDescriptor dtd) {\r
+ int typeId = dtd.getTypeId().getJDBCTypeId();\r
+\r
+ switch ( typeId )\r
+ {\r
+ case Types.CHAR: // CHAR et alia return their # characters...\r
+ case Types.VARCHAR:\r
+ case Types.LONGVARCHAR:\r
+ case Types.CLOB:\r
+ case Types.BINARY: // BINARY types return their # bytes...\r
+ case Types.VARBINARY:\r
+ case Types.LONGVARBINARY:\r
+ case Types.BLOB:\r
+ case StoredFormatIds.XML_TYPE_ID:\r
+ return dtd.getMaximumWidth();\r
+ case Types.SMALLINT:\r
+ return 5;\r
+ case JDBC30Translation.SQL_TYPES_BOOLEAN:\r
+ return 1;\r
+ }\r
+ \r
+ return dtd.getPrecision();\r
+ }\r
+\r
+ /**\r
+ Get the precision of the datatype, in decimal digits\r
+ This is used by EmbedResultSetMetaData.\r
+ @param dtd data type descriptor\r
+ */\r
+ public static int getDigitPrecision(DataTypeDescriptor dtd) {\r
+ int typeId = dtd.getTypeId().getJDBCTypeId();\r
+\r
+ switch ( typeId )\r
+ {\r
+ case Types.FLOAT:\r
+ case Types.DOUBLE:\r
+ return TypeId.DOUBLE_PRECISION_IN_DIGITS;\r
+ case Types.REAL:\r
+ return TypeId.REAL_PRECISION_IN_DIGITS;\r
+ default: return getPrecision(dtd);\r
+ }\r
+\r
+ }\r
+\r
+\r
+ /**\r
+ Is the data type currency.\r
+ @param dtd data type descriptor\r
+ */\r
+ public static boolean isCurrency(DataTypeDescriptor dtd) {\r
+ int typeId = dtd.getTypeId().getJDBCTypeId();\r
+\r
+ // Only the NUMERIC and DECIMAL types are currency\r
+ return ((typeId == Types.DECIMAL) || (typeId == Types.NUMERIC));\r
+ }\r
+\r
+ /**\r
+ Is the data type case sensitive.\r
+ @param dtd data type descriptor\r
+ */\r
+ public static boolean isCaseSensitive(DataTypeDescriptor dtd) {\r
+ int typeId = dtd.getTypeId().getJDBCTypeId();\r
+\r
+ return (typeId == Types.CHAR ||\r
+ typeId == Types.VARCHAR ||\r
+ typeId == Types.CLOB ||\r
+ typeId == Types.LONGVARCHAR ||\r
+ typeId == StoredFormatIds.XML_TYPE_ID);\r
+ }\r
+ /**\r
+ Is the data type nullable.\r
+ @param dtd data type descriptor\r
+ */\r
+ public static int isNullable(DataTypeDescriptor dtd) {\r
+ return dtd.isNullable() ?\r
+ ResultSetMetaData.columnNullable :\r
+ ResultSetMetaData.columnNoNulls;\r
+ }\r
+\r
+ /**\r
+ Is the data type signed.\r
+ @param dtd data type descriptor\r
+ */\r
+ public static boolean isSigned(DataTypeDescriptor dtd) {\r
+ int typeId = dtd.getTypeId().getJDBCTypeId();\r
+\r
+ return ( typeId == Types.INTEGER ||\r
+ typeId == Types.FLOAT ||\r
+ typeId == Types.DECIMAL ||\r
+ typeId == Types.SMALLINT ||\r
+ typeId == Types.BIGINT ||\r
+ typeId == Types.TINYINT ||\r
+ typeId == Types.NUMERIC ||\r
+ typeId == Types.REAL ||\r
+ typeId == Types.DOUBLE );\r
+ }\r
+\r
+ /**\r
+ * Gets the display width of a column of a given type.\r
+ *\r
+ * @param dtd data type descriptor\r
+ *\r
+ * @return associated column display width\r
+ */\r
+ public static int getColumnDisplaySize(DataTypeDescriptor dtd)\r
+ {\r
+ int typeId = dtd.getTypeId().getJDBCTypeId();\r
+ int storageLength = dtd.getMaximumWidth();\r
+ return DataTypeUtilities.getColumnDisplaySize(typeId, storageLength);\r
+ }\r
+\r
+ public static int getColumnDisplaySize(int typeId, int storageLength)\r
+ {\r
+ int size;\r
+ switch (typeId)\r
+ {\r
+ case Types.TIMESTAMP:\r
+ size = 26;\r
+ break;\r
+ case Types.DATE:\r
+ size = 10;\r
+ break; \r
+ case Types.TIME:\r
+ size = 8;\r
+ break;\r
+ case Types.INTEGER:\r
+ size = 11;\r
+ break;\r
+ case Types.SMALLINT :\r
+ size = 6;\r
+ break;\r
+ case Types.REAL :\r
+ case Types.FLOAT :\r
+ size = 13;\r
+ break;\r
+ case Types.DOUBLE:\r
+ size = 22;\r
+ break;\r
+ case Types.TINYINT :\r
+ size = 15;\r
+ break;\r
+\r
+ case Types.BINARY:\r
+ case Types.VARBINARY:\r
+ case Types.LONGVARBINARY:\r
+ case Types.BLOB:\r
+ size = 2*storageLength;\r
+ if (size < 0)\r
+ size = Integer.MAX_VALUE;\r
+ break;\r
+\r
+ case Types.BIGINT:\r
+ size = 20;\r
+ break;\r
+ case Types.BIT:\r
+ case JDBC30Translation.SQL_TYPES_BOOLEAN:\r
+ // Types.BIT == SQL BOOLEAN, so 5 chars for 'false'\r
+ // In JDBC 3.0, Types.BIT or Types.BOOLEAN = SQL BOOLEAN\r
+ size = 5;\r
+ break;\r
+ default: \r
+ // MaximumWidth is -1 when it is unknown.\r
+ int w = storageLength;\r
+ size = (w > 0 ? w : 15);\r
+ break;\r
+ }\r
+ return size;\r
+ }\r
+\r
+ /**\r
+ * Compute the maximum width (column display width) of a decimal or numeric data value,\r
+ * given its precision and scale.\r
+ *\r
+ * @param precision The precision (number of digits) of the data value.\r
+ * @param scale The number of fractional digits (digits to the right of the decimal point).\r
+ *\r
+ * @return The maximum number of chracters needed to display the value.\r
+ */\r
+ public static int computeMaxWidth( int precision, int scale)\r
+ {\r
+ // There are 3 possible cases with respect to finding the correct max\r
+ // width for DECIMAL type.\r
+ // 1. If scale = 0, only sign should be added to precision.\r
+ // 2. scale=precision, 3 should be added to precision for sign, decimal and an additional char '0'.\r
+ // 3. precision > scale > 0, 2 should be added to precision for sign and decimal.\r
+ return (scale ==0) ? (precision +1) : ((scale == precision) ? (precision + 3) : (precision + 2));\r
+ }\r
+}\r
+\r