1 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 ; 30 import java.sql.ResultSetMetaData ; 31 32 36 public abstract class DataTypeUtilities { 37 38 42 public static int getPrecision(DataTypeDescriptor dtd) { 43 int typeId = dtd.getTypeId().getJDBCTypeId(); 44 45 switch ( typeId ) 46 { 47 case Types.CHAR: case Types.VARCHAR: 49 case Types.LONGVARCHAR: 50 case Types.CLOB: 51 case Types.BINARY: 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 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 91 public static boolean isCurrency(DataTypeDescriptor dtd) { 92 int typeId = dtd.getTypeId().getJDBCTypeId(); 93 94 return ((typeId == Types.DECIMAL) || (typeId == Types.NUMERIC)); 96 } 97 98 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 115 public static int isNullable(DataTypeDescriptor dtd) { 116 return dtd.isNullable() ? 117 ResultSetMetaData.columnNullable : 118 ResultSetMetaData.columnNoNulls; 119 } 120 121 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 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 size = 5; 201 break; 202 default: 203 int w = storageLength; 205 size = (w > 0 ? w : 15); 206 break; 207 } 208 return size; 209 } 210 211 220 public static int computeMaxWidth( int precision, int scale) 221 { 222 return (scale ==0) ? (precision +1) : ((scale == precision) ? (precision + 3) : (precision + 2)); 228 } 229 } 230 231 | Popular Tags |