| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.cmpgen2; |
| 34 | | | |
| 35 | | | import java.math.BigDecimal; |
| 36 | | | import java.sql.Date; |
| 37 | | | import java.sql.Timestamp; |
| 38 | | | import java.util.Arrays; |
| 39 | | | import java.util.HashSet; |
| 40 | | | import java.util.List; |
| 41 | | | import java.util.Set; |
| 42 | | | |
| 43 | | | import org.jcoderz.commons.util.Constants; |
| 44 | | | import org.jcoderz.phoenix.sqlparser.ColumnSpec; |
| 45 | | | import org.jcoderz.phoenix.sqlparser.NumericAttribute; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | @author |
| 50 | | | |
| 51 | | | public final class TypeMapping |
| 52 | | | { |
| 53 | 0 | | private static final String[] JAVA_PRIMITIVE_TYPES = { |
| 54 | | | Byte.TYPE.getName(), |
| 55 | | | Short.TYPE.getName(), |
| 56 | | | Integer.TYPE.getName(), |
| 57 | | | Long.TYPE.getName(), |
| 58 | | | Float.TYPE.getName(), |
| 59 | | | Double.TYPE.getName(), |
| 60 | | | Character.TYPE.getName(), |
| 61 | | | Boolean.TYPE.getName(), |
| 62 | | | "byte[]" |
| 63 | | | }; |
| 64 | | | |
| 65 | 0 | | private static final String[] PRIMITIVE_TYPE_WRAPPERS = { |
| 66 | | | Byte.class.getName(), |
| 67 | | | Short.class.getName(), |
| 68 | | | Integer.class.getName(), |
| 69 | | | Long.class.getName(), |
| 70 | | | Float.class.getName(), |
| 71 | | | Double.class.getName(), |
| 72 | | | Character.class.getName(), |
| 73 | | | Boolean.class.getName(), |
| 74 | | | "byte[]" |
| 75 | | | }; |
| 76 | | | |
| 77 | | | |
| 78 | 0 | | private static final String[] STRING_TYPES = { |
| 79 | | | "CHAR", |
| 80 | | | "CHAR2", |
| 81 | | | "NCHAR", |
| 82 | | | "NCHAR2", |
| 83 | | | "VARCHAR", |
| 84 | | | "VARCHAR2", |
| 85 | | | "NVARCHAR", |
| 86 | | | "NVARCHAR2" |
| 87 | | | }; |
| 88 | | | |
| 89 | 0 | | private static final String[] TIMESTAMP_TYPES = { |
| 90 | | | "TIMESTAMP" |
| 91 | | | }; |
| 92 | | | |
| 93 | 0 | | private static final String[] DATE_TYPES = { |
| 94 | | | "DATE" |
| 95 | | | }; |
| 96 | | | |
| 97 | | | |
| 98 | 0 | | private static final String[] NUMERIC_TYPES = { |
| 99 | | | "NUMBER", |
| 100 | | | "NUMERIC", |
| 101 | | | "DECIMAL", |
| 102 | | | "INTEGER", |
| 103 | | | "INT", |
| 104 | | | "FLOAT", |
| 105 | | | "REAL" |
| 106 | | | }; |
| 107 | | | |
| 108 | 0 | | private static final String[] FLOAT_TYPES = { |
| 109 | | | "FLOAT", |
| 110 | | | "REAL" |
| 111 | | | }; |
| 112 | | | |
| 113 | | | private static final int INTEGER_PRECISION_LIMIT = 10; |
| 114 | | | private static final int LONG_PRECISION_LIMIT = 19; |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | 0 | | private static final Set STRING_TYPE_SET = new HashSet(); |
| 119 | 0 | | private static final Set NUMERIC_TYPE_SET = new HashSet(); |
| 120 | 0 | | private static final Set TIMESTAMP_TYPE_SET = new HashSet(); |
| 121 | 0 | | private static final Set DATE_TYPE_SET = new HashSet(); |
| 122 | 0 | | private static final Set FLOAT_TYPE_SET = new HashSet(); |
| 123 | 0 | | private static final Set JAVA_PRIMITIVE_TYPE_SET = new HashSet(); |
| 124 | | | |
| 125 | | | static |
| 126 | | | { |
| 127 | 0 | | STRING_TYPE_SET.addAll(Arrays.asList(STRING_TYPES)); |
| 128 | 0 | | NUMERIC_TYPE_SET.addAll(Arrays.asList(NUMERIC_TYPES)); |
| 129 | 0 | | TIMESTAMP_TYPE_SET.addAll(Arrays.asList(TIMESTAMP_TYPES)); |
| 130 | 0 | | DATE_TYPE_SET.addAll(Arrays.asList(DATE_TYPES)); |
| 131 | 0 | | FLOAT_TYPE_SET.addAll(Arrays.asList(FLOAT_TYPES)); |
| 132 | 0 | | JAVA_PRIMITIVE_TYPE_SET.addAll(Arrays.asList(JAVA_PRIMITIVE_TYPES)); |
| 133 | 0 | | } |
| 134 | | | |
| 135 | | | |
| 136 | | | |
| 137 | | | |
| 138 | | | private TypeMapping () |
| 139 | 0 | | { |
| 140 | 0 | | } |
| 141 | | | |
| 142 | | | |
| 143 | | | |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | | | @param |
| 149 | | | @param |
| 150 | | | |
| 151 | | | @return |
| 152 | | | @throws |
| 153 | | | |
| 154 | | (1) | public static String getJavaType ( |
| 155 | | | ColumnSpec column, |
| 156 | | | boolean fullyQualified) |
| 157 | | | throws CmpGeneratorException |
| 158 | | | { |
| 159 | 0 | | String javaType = column.getJavaType(); |
| 160 | 0 | | if (javaType == null) |
| 161 | | | { |
| 162 | | | |
| 163 | 0 | | javaType = getTypeMapping(column.getColumnType()); |
| 164 | | | |
| 165 | | | |
| 166 | 0 | | if (javaType == null) |
| 167 | | | { |
| 168 | 0 | | final List sqlTypeAttributes = column.getDatatypeAttributes(); |
| 169 | 0 | | int att1 = 0, att2 = 0; |
| 170 | 0 | (2) | if (sqlTypeAttributes.size() > 0) |
| 171 | | | { |
| 172 | 0 | | att1 = ((NumericAttribute) sqlTypeAttributes.get(0)).getNumber(); |
| 173 | 0 | (3) | if (sqlTypeAttributes.size() > 1) |
| 174 | | | { |
| 175 | 0 | | att2 = ((NumericAttribute) sqlTypeAttributes.get(1)) |
| 176 | | | .getNumber(); |
| 177 | | | } |
| 178 | | | } |
| 179 | 0 | | javaType = |
| 180 | | | getNumberTypeMapping( |
| 181 | | | column.getColumnType(), |
| 182 | | | att1, |
| 183 | | | att2); |
| 184 | 0 | | } |
| 185 | | | } |
| 186 | | | else |
| 187 | | | { |
| 188 | | | |
| 189 | 0 | | final String loadMethod = column.getLoadMethod(); |
| 190 | 0 | | if (loadMethod != null) |
| 191 | | | { |
| 192 | | | |
| 193 | | | |
| 194 | | | |
| 195 | 0 | | final int openParen = loadMethod.indexOf('('); |
| 196 | 0 | | final int closeParen = loadMethod.indexOf(')'); |
| 197 | 0 | | if (openParen == -1 |
| 198 | | | || closeParen == -1 |
| 199 | | | || closeParen <= openParen) |
| 200 | | | { |
| 201 | 0 | | throw new CmpGeneratorException( |
| 202 | | | loadMethod + " is an invalid load method signature"); |
| 203 | | | } |
| 204 | 0 | | javaType = loadMethod.substring(openParen + 1, closeParen).trim(); |
| 205 | 0 | | if (javaType.length() <= 0) |
| 206 | | | { |
| 207 | 0 | | throw new CmpGeneratorException( |
| 208 | | | loadMethod + " is an invalid load method signature"); |
| 209 | | | } |
| 210 | | | } |
| 211 | | | } |
| 212 | | | |
| 213 | 0 | | if (javaType == null) |
| 214 | | | { |
| 215 | 0 | | throw new CmpGeneratorException("No type mapping found for column " |
| 216 | | | + column); |
| 217 | | | } |
| 218 | | | |
| 219 | | | |
| 220 | | | |
| 221 | | | |
| 222 | 0 | | if (isPrimitiveType(javaType) && !column.isNotNull()) |
| 223 | | | { |
| 224 | 0 | | javaType = primitiveToObject(javaType); |
| 225 | | | } |
| 226 | | | |
| 227 | | | |
| 228 | 0 | | if (!fullyQualified) |
| 229 | | | { |
| 230 | 0 | | javaType = unqualifyType(javaType); |
| 231 | | | } |
| 232 | | | |
| 233 | 0 | | return javaType; |
| 234 | | | } |
| 235 | | | |
| 236 | | | |
| 237 | | | |
| 238 | | | @param |
| 239 | | | @return |
| 240 | | | |
| 241 | | | |
| 242 | | | public static String unqualifyType (String typeName) |
| 243 | | | { |
| 244 | 0 | | final int dotIndex = typeName.lastIndexOf('.'); |
| 245 | 0 | | return typeName.substring(dotIndex + 1); |
| 246 | | | } |
| 247 | | | |
| 248 | | | |
| 249 | | | |
| 250 | | | |
| 251 | | | @param |
| 252 | | | @return |
| 253 | | | |
| 254 | | | |
| 255 | | | public static String getTypeMapping (String sqlType) |
| 256 | | | { |
| 257 | | | String javaType; |
| 258 | | | |
| 259 | 0 | | final String s = sqlType.toUpperCase(Constants.SYSTEM_LOCALE); |
| 260 | 0 | | if (STRING_TYPE_SET.contains(s)) |
| 261 | | | { |
| 262 | 0 | | javaType = String.class.getName(); |
| 263 | | | } |
| 264 | 0 | | else if (TIMESTAMP_TYPE_SET.contains(s)) |
| 265 | | | { |
| 266 | 0 | | javaType = Timestamp.class.getName(); |
| 267 | | | } |
| 268 | 0 | | else if (DATE_TYPE_SET.contains(s)) |
| 269 | | | { |
| 270 | 0 | | javaType = Date.class.getName(); |
| 271 | | | } |
| 272 | 0 | | else if (NUMERIC_TYPE_SET.contains(s)) |
| 273 | | | { |
| 274 | 0 | | javaType = null; |
| 275 | | | } |
| 276 | | | else |
| 277 | | | { |
| 278 | 0 | | javaType = Object.class.getName(); |
| 279 | | | } |
| 280 | | | |
| 281 | 0 | | return javaType; |
| 282 | | | } |
| 283 | | | |
| 284 | | | |
| 285 | | | |
| 286 | | | |
| 287 | | | @param |
| 288 | | | @param |
| 289 | | | @param |
| 290 | | | @return |
| 291 | | | |
| 292 | | (4) | public static String getNumberTypeMapping ( |
| 293 | | | String sqlType, |
| 294 | | | int precision, |
| 295 | | | int scale) |
| 296 | | | { |
| 297 | | | String javaType; |
| 298 | 0 | | final String s = sqlType.toUpperCase(Constants.SYSTEM_LOCALE); |
| 299 | | | |
| 300 | 0 | | if (precision < 0 || scale < 0) |
| 301 | | | { |
| 302 | 0 | (5) | throw new IllegalArgumentException( |
| 303 | | | "Scale and precision must be non-negative"); |
| 304 | | | } |
| 305 | | | |
| 306 | 0 | | if (NUMERIC_TYPE_SET.contains(s)) |
| 307 | | | { |
| 308 | 0 | | if (scale > 0 || FLOAT_TYPE_SET.contains(s)) |
| 309 | | | { |
| 310 | 0 | | return BigDecimal.class.getName(); |
| 311 | | | } |
| 312 | | | else |
| 313 | | | { |
| 314 | 0 | | if (precision < INTEGER_PRECISION_LIMIT) |
| 315 | | | { |
| 316 | 0 | | javaType = Integer.TYPE.getName(); |
| 317 | | | } |
| 318 | 0 | | else if (precision < LONG_PRECISION_LIMIT) |
| 319 | | | { |
| 320 | 0 | | javaType = Long.TYPE.getName(); |
| 321 | | | } |
| 322 | | | else |
| 323 | | | { |
| 324 | 0 | | javaType = BigDecimal.class.getName(); |
| 325 | | | } |
| 326 | | | } |
| 327 | | | } |
| 328 | | | else |
| 329 | | | { |
| 330 | 0 | | throw new IllegalArgumentException(s + " is not a numeric SQL type"); |
| 331 | | | } |
| 332 | | | |
| 333 | 0 | | return javaType; |
| 334 | | | } |
| 335 | | | |
| 336 | | | |
| 337 | | | |
| 338 | | | |
| 339 | | | @param |
| 340 | | | @return |
| 341 | | | |
| 342 | | | public static boolean isPrimitiveType (String type) |
| 343 | | | { |
| 344 | 0 | | boolean result = false; |
| 345 | 0 | | if (JAVA_PRIMITIVE_TYPE_SET.contains(type)) |
| 346 | | | { |
| 347 | 0 | | result = true; |
| 348 | | | } |
| 349 | 0 | | return result; |
| 350 | | | } |
| 351 | | | |
| 352 | | | |
| 353 | | | |
| 354 | | | @param |
| 355 | | | @return |
| 356 | | | @throws |
| 357 | | | |
| 358 | | | public static String primitiveToObject (String primitiveType) |
| 359 | | | { |
| 360 | 0 | | if (!isPrimitiveType(primitiveType)) |
| 361 | | | { |
| 362 | 0 | | throw new IllegalArgumentException( |
| 363 | | | "Can't map " |
| 364 | | | + primitiveType |
| 365 | | | + " to its Object wrapper because it is not a primitive type"); |
| 366 | | | } |
| 367 | 0 | | String objectWrapperName = null; |
| 368 | 0 | | for (int i = 0; i < JAVA_PRIMITIVE_TYPES.length; i++) |
| 369 | | | { |
| 370 | 0 | | if (JAVA_PRIMITIVE_TYPES[i].equals(primitiveType)) |
| 371 | | | { |
| 372 | 0 | | objectWrapperName = PRIMITIVE_TYPE_WRAPPERS[i]; |
| 373 | 0 | | break; |
| 374 | | | } |
| 375 | | | } |
| 376 | 0 | | return objectWrapperName; |
| 377 | | | } |
| 378 | | | |
| 379 | | | } |