1 22 package org.jboss.ejb.plugins.cmp.jdbc.metadata; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Collection ; 27 28 import org.jboss.deployment.DeploymentException; 29 import org.jboss.metadata.MetaData; 30 import org.w3c.dom.Element ; 31 32 40 public final class JDBCTypeMappingMetaData 41 { 42 private static final String [] PRIMITIVES = { 43 "boolean", "byte", "char", "short", "int", "long", "float", "double" 44 }; 45 46 private static final String [] PRIMITIVE_CLASSES = { 47 "java.lang.Boolean", "java.lang.Byte", "java.lang.Character", 48 "java.lang.Short", "java.lang.Integer", "java.lang.Long", 49 "java.lang.Float", "java.lang.Double" 50 }; 51 52 public static final String CONCAT = "concat"; 53 public static final String SUBSTRING = "substring"; 54 public static final String LCASE = "lcase"; 55 public static final String UCASE = "ucase"; 56 public static final String LENGTH = "length"; 57 public static final String LOCATE = "locate"; 58 public static final String ABS = "abs"; 59 public static final String SQRT = "sqrt"; 60 public static final String COUNT = "count"; 61 62 public static JDBCFunctionMappingMetaData COUNT_FUNC; 63 public static JDBCFunctionMappingMetaData MAX_FUNC; 64 public static JDBCFunctionMappingMetaData MIN_FUNC; 65 public static JDBCFunctionMappingMetaData AVG_FUNC; 66 public static JDBCFunctionMappingMetaData SUM_FUNC; 67 public static JDBCFunctionMappingMetaData MOD_FUNC; 68 69 static 70 { 71 try 72 { 73 COUNT_FUNC = new JDBCFunctionMappingMetaData("count", "count(?1 ?2)"); 74 MAX_FUNC = new JDBCFunctionMappingMetaData("max", "max(?1 ?2)"); 75 MIN_FUNC = new JDBCFunctionMappingMetaData("min", "min(?1 ?2)"); 76 AVG_FUNC = new JDBCFunctionMappingMetaData("avg", "avg(?1 ?2)"); 77 SUM_FUNC = new JDBCFunctionMappingMetaData("sum", "sum(?1 ?2)"); 78 MOD_FUNC = new JDBCFunctionMappingMetaData("mod", "mod(?1, ?2)"); 79 } 80 catch(DeploymentException e) 81 { 82 throw new IllegalStateException (e.getMessage()); 83 } 84 } 85 86 private final String name; 87 88 private final HashMap mappings = new HashMap (); 89 90 private final HashMap functionMappings = new HashMap (); 91 92 private final String aliasHeaderPrefix; 93 private final String aliasHeaderSuffix; 94 private final int aliasMaxLength; 95 96 private final boolean subquerySupported; 97 98 private final String trueMapping; 99 private final String falseMapping; 100 private final int maxKeysInDelete; 101 102 private JDBCFunctionMappingMetaData rowLocking = null; 103 private JDBCFunctionMappingMetaData fkConstraint = null; 104 private JDBCFunctionMappingMetaData pkConstraint = null; 105 private JDBCFunctionMappingMetaData autoIncrement = null; 106 private JDBCFunctionMappingMetaData addColumn = null; 107 private JDBCFunctionMappingMetaData dropColumn = null; 108 private JDBCFunctionMappingMetaData alterColumn = null; 109 110 118 public JDBCTypeMappingMetaData(Element element) throws DeploymentException 119 { 120 name = MetaData.getUniqueChildContent(element, "name"); 122 123 String rowLockingSQL = MetaData.getUniqueChildContent(element, "row-locking-template"); 125 if(rowLockingSQL != null && !rowLockingSQL.trim().equals("")) 126 { 127 rowLocking = new JDBCFunctionMappingMetaData("row-locking", rowLockingSQL); 128 } 129 130 String pkConstraintSQL = MetaData.getUniqueChildContent(element, "pk-constraint-template"); 132 if(pkConstraintSQL != null && !pkConstraintSQL.trim().equals("")) 133 { 134 pkConstraint = new JDBCFunctionMappingMetaData("pk-constraint", pkConstraintSQL); 135 } 136 137 String fkConstraintSQL = MetaData.getUniqueChildContent(element, "fk-constraint-template"); 139 if(fkConstraintSQL != null && !fkConstraintSQL.trim().equals("")) 140 { 141 fkConstraint = new JDBCFunctionMappingMetaData("fk-constraint", fkConstraintSQL); 142 } 143 144 String alterColumnSQL = MetaData.getOptionalChildContent(element, "add-column-template"); 146 if(alterColumnSQL != null && !alterColumnSQL.trim().equals("")) 147 { 148 addColumn = new JDBCFunctionMappingMetaData("add-column-template", alterColumnSQL); 149 } 150 else 151 { 152 addColumn = new JDBCFunctionMappingMetaData("add-column-template", "ALTER TABLE ?1 ADD ?2 ?3"); 153 } 154 alterColumnSQL = MetaData.getOptionalChildContent(element, "alter-column-template"); 155 if(alterColumnSQL != null && !alterColumnSQL.trim().equals("")) 156 { 157 alterColumn = new JDBCFunctionMappingMetaData("alter-column-template", alterColumnSQL); 158 } 159 else 160 { 161 alterColumn = new JDBCFunctionMappingMetaData("alter-column-template", "ALTER TABLE ?1 ALTER ?2 TYPE ?3"); 162 } 163 alterColumnSQL = MetaData.getOptionalChildContent(element, "drop-column-template"); 164 if(alterColumnSQL != null && !alterColumnSQL.trim().equals("")) 165 { 166 dropColumn = new JDBCFunctionMappingMetaData("drop-column-template", alterColumnSQL); 167 } 168 else 169 { 170 dropColumn = new JDBCFunctionMappingMetaData("drop-column-template", "ALTER TABLE ?1 DROP ?2"); 171 } 172 173 String autoIncrementSQL = MetaData.getOptionalChildContent(element, "auto-increment-template"); 176 if(autoIncrementSQL != null && !autoIncrementSQL.trim().equals("")) 177 { 178 autoIncrement = new JDBCFunctionMappingMetaData("auto-increment", autoIncrementSQL); 179 } 180 181 Iterator iterator = MetaData.getChildrenByTagName(element, "mapping"); 183 while(iterator.hasNext()) 184 { 185 Element mappingElement = (Element )iterator.next(); 186 JDBCMappingMetaData mapping = new JDBCMappingMetaData(mappingElement); 187 mappings.put(mapping.getJavaType(), mapping); 188 } 189 190 addDefaultFunctionMapping(); 191 192 Iterator functions = MetaData.getChildrenByTagName(element, "function-mapping"); 194 while(functions.hasNext()) 195 { 196 Element mappingElement = (Element )functions.next(); 197 JDBCFunctionMappingMetaData functionMapping = new JDBCFunctionMappingMetaData(mappingElement); 198 functionMappings.put(functionMapping.getFunctionName().toLowerCase(), functionMapping); 199 } 200 201 aliasHeaderPrefix = MetaData.getUniqueChildContent(element, "alias-header-prefix"); 202 203 aliasHeaderSuffix = MetaData.getUniqueChildContent(element, "alias-header-suffix"); 204 205 String aliasMaxLengthString = MetaData.getUniqueChildContent(element, "alias-max-length"); 206 try 207 { 208 aliasMaxLength = Integer.parseInt(aliasMaxLengthString); 209 } 210 catch(NumberFormatException e) 211 { 212 throw new DeploymentException("Invalid number format in " + 213 "alias-max-length " + aliasMaxLengthString + "': " + e); 214 } 215 216 String subquerySupportedStr = MetaData.getUniqueChildContent(element, "subquery-supported"); 217 subquerySupported = Boolean.valueOf(subquerySupportedStr).booleanValue(); 218 trueMapping = MetaData.getUniqueChildContent(element, "true-mapping"); 219 falseMapping = MetaData.getUniqueChildContent(element, "false-mapping"); 220 221 String str = MetaData.getOptionalChildContent(element, "max-keys-in-delete"); 222 if(str != null) 223 { 224 try 225 { 226 maxKeysInDelete = Integer.parseInt(str); 227 } 228 catch(NumberFormatException e) 229 { 230 throw new DeploymentException("Failed to parse int value '" + str + "' for max-keys-in-delete", e); 231 } 232 233 if(maxKeysInDelete < 0) 234 { 235 throw new DeploymentException("The value of max-keys-in-delete cannot be less than 0: " + maxKeysInDelete); 236 } 237 } 238 else 239 { 240 maxKeysInDelete = 0; 241 } 242 } 243 244 250 public String getName() 251 { 252 return name; 253 } 254 255 263 public String getAliasHeaderPrefix() 264 { 265 return aliasHeaderPrefix; 266 } 267 268 276 public String getAliasHeaderSuffix() 277 { 278 return aliasHeaderSuffix; 279 } 280 281 286 public int getAliasMaxLength() 287 { 288 return aliasMaxLength; 289 } 290 291 294 public boolean isSubquerySupported() 295 { 296 return subquerySupported; 297 } 298 299 302 public String getTrueMapping() 303 { 304 return trueMapping; 305 } 306 307 310 public String getFalseMapping() 311 { 312 return falseMapping; 313 } 314 315 public int getMaxKeysInDelete() 316 { 317 return maxKeysInDelete; 318 } 319 320 public JDBCMappingMetaData getTypeMappingMetaData(Class type) 321 { 322 String javaType = type.getName(); 323 324 for(int i = 0; i < PRIMITIVES.length; i++) 326 { 327 if(javaType.equals(PRIMITIVES[i])) 328 { 329 javaType = PRIMITIVE_CLASSES[i]; 331 break; 332 } 333 } 334 335 JDBCMappingMetaData mapping = (JDBCMappingMetaData)mappings.get(javaType); 337 338 if(mapping == null) 340 { 341 mapping = (JDBCMappingMetaData)mappings.get("java.lang.Object"); 342 } 343 344 return mapping; 345 } 346 347 public JDBCFunctionMappingMetaData getFunctionMapping(String name) 348 { 349 JDBCFunctionMappingMetaData funcMapping = (JDBCFunctionMappingMetaData)functionMappings.get(name.toLowerCase()); 350 if(funcMapping == null) 351 throw new IllegalStateException ("Function " + name + " is not defined for " + this.name); 352 return funcMapping; 353 } 354 355 356 359 public JDBCFunctionMappingMetaData getRowLockingTemplate() 360 { 361 return rowLocking; 362 } 363 364 367 public JDBCFunctionMappingMetaData getPkConstraintTemplate() 368 { 369 return pkConstraint; 370 } 371 372 375 public JDBCFunctionMappingMetaData getFkConstraintTemplate() 376 { 377 return fkConstraint; 378 } 379 380 383 public JDBCFunctionMappingMetaData getAutoIncrementTemplate() 384 { 385 return autoIncrement; 386 } 387 388 391 public JDBCFunctionMappingMetaData getAddColumnTemplate() 392 { 393 return addColumn; 394 } 395 396 399 public JDBCFunctionMappingMetaData getDropColumnTemplate() 400 { 401 return dropColumn; 402 } 403 404 407 public JDBCFunctionMappingMetaData getAlterColumnTemplate() 408 { 409 return alterColumn; 410 } 411 412 public Collection getMappings() 413 { 414 return mappings.values(); 415 } 416 417 private void addDefaultFunctionMapping() 418 { 419 JDBCFunctionMappingMetaData function; 420 421 function = new JDBCFunctionMappingMetaData("concat", 423 new String []{ 424 "{fn concat(", 425 ", ", 426 ")}" 427 }, 428 new int[]{0, 1}); 429 functionMappings.put(function.getFunctionName().toLowerCase(), function); 430 431 function = new JDBCFunctionMappingMetaData("substring", 433 new String []{ 434 "{fn substring(", 435 ", ", 436 ", ", 437 ")}" 438 }, 439 new int[]{0, 1, 2}); 440 functionMappings.put(function.getFunctionName().toLowerCase(), function); 441 442 function = new JDBCFunctionMappingMetaData("lcase", 444 new String []{ 445 "{fn lcase(", 446 ")}" 447 }, 448 new int[]{0}); 449 functionMappings.put(function.getFunctionName().toLowerCase(), function); 450 451 function = new JDBCFunctionMappingMetaData("ucase", 453 new String []{ 454 "{fn ucase(", 455 ")}" 456 }, 457 new int[]{0}); 458 functionMappings.put(function.getFunctionName().toLowerCase(), function); 459 460 function = new JDBCFunctionMappingMetaData("length", 462 new String []{ 463 "{fn length(", 464 ")}" 465 }, 466 new int[]{0}); 467 functionMappings.put(function.getFunctionName().toLowerCase(), function); 468 469 function = new JDBCFunctionMappingMetaData("locate", 471 new String []{ 472 "{fn locate(", 473 ", ", 474 ", ", 475 ")}" 476 }, 477 new int[]{0, 1, 2}); 478 functionMappings.put(function.getFunctionName().toLowerCase(), function); 479 480 function = new JDBCFunctionMappingMetaData("abs", 482 new String []{ 483 "{fn abs(", 484 ")}" 485 }, 486 new int[]{0}); 487 functionMappings.put(function.getFunctionName().toLowerCase(), function); 488 489 function = new JDBCFunctionMappingMetaData("sqrt", 491 new String []{ 492 "{fn sqrt(", 493 ")}" 494 }, 495 new int[]{0}); 496 functionMappings.put(function.getFunctionName().toLowerCase(), function); 497 } 498 } 499 | Popular Tags |