1 30 31 32 package org.hsqldb; 33 34 import java.io.Serializable ; 35 import java.lang.reflect.Method ; 36 37 import org.hsqldb.lib.HashMap; 38 import org.hsqldb.lib.HsqlArrayList; 39 import org.hsqldb.resources.BundleHandler; 40 import org.hsqldb.store.ValuePool; 41 import org.hsqldb.types.Binary; 42 43 44 45 57 final class DIProcedureInfo { 58 59 static final String conClsName = "java.sql.Connection"; 61 static final int procedureResultUnknown = 0; 62 static final int procedureNoResult = 1; 63 static final int procedureReturnsResult = 2; 64 static final int procedureColumnUnknown = 0; 65 static final int procedureColumnIn = 1; 66 static final int procedureColumnInOut = 2; 67 static final int procedureColumnResult = 3; 68 static final int procedureColumnOut = 4; 69 static final int procedureColumnReturn = 5; 70 static final int procedureNoNulls = 0; 71 static final int procedureNullable = 1; 72 static final int procedureNullableUnknown = 2; 73 private Class clazz; 74 private Class [] colClasses; 75 private int[] colTypes; 76 private int colOffset; 77 private int colCount; 78 private boolean colsResolved; 79 private String fqn; 80 private String specificName; 81 private int hnd_remarks; 82 private Method method; 83 private String sig; 84 private DINameSpace nameSpace; 85 private final HashMap typeMap = new HashMap(); 86 87 public DIProcedureInfo(DINameSpace ns) throws HsqlException { 88 setNameSpace(ns); 89 } 90 91 private int colOffset() { 92 93 if (!colsResolved) { 94 resolveCols(); 95 } 96 97 return colOffset; 98 } 99 100 HsqlArrayList getAliases() { 101 return (HsqlArrayList) nameSpace.getInverseAliasMap().get(getFQN()); 102 } 103 104 Class getColClass(int i) { 105 return colClasses[i + colOffset()]; 106 } 107 108 int getColCount() { 109 110 if (!colsResolved) { 111 resolveCols(); 112 } 113 114 return colCount; 115 } 116 117 Integer getColDataType(int i) { 118 return ValuePool.getInt(getColTypeCode(i)); 119 } 120 121 Integer getColLen(int i) { 122 123 int size; 124 int type; 125 126 type = getColTypeCode(i); 127 128 switch (type) { 129 130 case Types.BINARY : 131 case Types.LONGVARBINARY : 132 case Types.VARBINARY : { 133 size = Integer.MAX_VALUE; 134 135 break; 136 } 137 case Types.BIGINT : 138 case Types.DOUBLE : 139 case Types.DATE : 140 case Types.FLOAT : 141 case Types.TIME : { 142 size = 8; 143 144 break; 145 } 146 case Types.TIMESTAMP : { 147 size = 12; 148 149 break; 150 } 151 case Types.REAL : 152 case Types.INTEGER : { 153 size = 4; 154 155 break; 156 } 157 case Types.SMALLINT : { 158 size = 2; 159 160 break; 161 } 162 case Types.TINYINT : 163 case Types.BOOLEAN : { 164 size = 1; 165 166 break; 167 } 168 default : 169 size = 0; 170 } 171 172 return (size == 0) ? null 173 : ValuePool.getInt(size); 174 } 175 176 String getColName(int i) { 177 return CompiledStatement.PCOL_PREFIX + (i + colOffset()); 178 } 179 180 Integer getColNullability(int i) { 181 182 int cn; 183 184 cn = getColClass(i).isPrimitive() ? procedureNoNulls 185 : procedureNullable; 186 187 return ValuePool.getInt(cn); 188 } 189 190 String getColRemark(int i) { 191 192 String key; 193 StringBuffer sb; 194 195 sb = new StringBuffer (getSignature()); 196 key = sb.append('@').append(i + colOffset()).toString(); 197 198 return BundleHandler.getString(hnd_remarks, key); 199 } 200 201 int getColSequence(int i) { 210 211 return (i + colOffset() == 0) ? colCount 213 : i; 214 } 215 216 int getColTypeCode(int i) { 217 218 i += colOffset(); 219 220 return colTypes[i]; 221 } 222 223 Integer getColUsage(int i) { 224 225 switch (i + colOffset()) { 226 227 case 0 : { 228 229 return ValuePool.getInt(procedureColumnResult); 232 } 233 234 default : { 237 238 return ValuePool.getInt(procedureColumnIn); 242 } 243 } 244 } 245 246 Class getDeclaringClass() { 247 return this.clazz; 248 } 249 250 String getFQN() { 251 252 StringBuffer sb; 253 254 if (fqn == null) { 255 sb = new StringBuffer (); 256 fqn = sb.append(clazz.getName()).append('.').append( 257 method.getName()).toString(); 258 } 259 260 return fqn; 261 } 262 263 String getSpecificName() { 264 265 if (specificName == null) { 266 specificName = clazz.getName() + "." + getSignature(); 267 } 268 269 return specificName; 270 } 271 272 Integer getInputParmCount() { 273 return ValuePool.getInt(method.getParameterTypes().length); 274 } 275 276 Method getMethod() { 277 return this.method; 278 } 279 280 String getOrigin(String srcType) { 281 return (nameSpace.isBuiltin(clazz) ? "BUILTIN " 282 : "USER DEFINED ") + srcType; 283 } 284 285 Integer getOutputParmCount() { 286 287 return ValuePool.getInt(0); 289 } 290 291 String getRemark() { 292 return BundleHandler.getString(hnd_remarks, getSignature()); 293 } 294 295 Integer getResultSetCount() { 296 return (method.getReturnType() == Void.TYPE) ? ValuePool.getInt(0) 297 : ValuePool.getInt(1); 298 } 299 300 Integer getResultType(String origin) { 301 302 int type; 303 304 type = !"ROUTINE".equals(origin) ? procedureResultUnknown 305 : method.getReturnType() 306 == Void.TYPE ? procedureNoResult 307 : procedureReturnsResult; 308 309 return ValuePool.getInt(type); 310 } 311 312 String getSignature() { 313 314 if (sig == null) { 315 sig = DINameSpace.getSignature(method); 316 } 317 318 return sig; 319 } 320 321 327 static String getMethodSpecificName(Method m) { 328 329 return m == null ? null 330 : m.getDeclaringClass().getName() + '.' 331 + DINameSpace.getSignature(m); 332 } 333 334 DINameSpace getNameSpace() { 335 return nameSpace; 336 } 337 338 void setNameSpace(DINameSpace ns) throws HsqlException { 339 340 nameSpace = ns; 341 342 Class c; 343 Integer type; 344 345 try { 351 c = nameSpace.classForName("org.hsqldb.jdbc.jdbcArray"); 352 353 typeMap.put(c, ValuePool.getInt(Types.ARRAY)); 354 } catch (Exception e) {} 355 356 type = ValuePool.getInt(Types.BIGINT); 358 359 typeMap.put(Long.TYPE, type); 360 typeMap.put(Long .class, type); 361 362 type = ValuePool.getInt(Types.BOOLEAN); 364 365 typeMap.put(Boolean.TYPE, type); 366 typeMap.put(Boolean .class, type); 367 368 type = ValuePool.getInt(Types.BLOB); 370 371 try { 372 c = nameSpace.classForName("org.hsqldb.jdbc.jdbcBlob"); 373 374 typeMap.put(c, type); 375 } catch (Exception e) {} 376 377 type = ValuePool.getInt(Types.CHAR); 379 380 typeMap.put(Character.TYPE, type); 381 typeMap.put(Character .class, type); 382 typeMap.put(Character [].class, type); 383 typeMap.put(char[].class, type); 384 385 type = ValuePool.getInt(Types.CLOB); 387 388 try { 389 c = nameSpace.classForName("org.hsqldb.jdbc.jdbcClob"); 390 391 typeMap.put(c, type); 392 } catch (Exception e) {} 393 394 type = ValuePool.getInt(Types.DATALINK); 396 397 typeMap.put(java.net.URL .class, type); 398 399 type = ValuePool.getInt(Types.DATE); 401 402 typeMap.put(java.util.Date .class, type); 403 typeMap.put(java.sql.Date .class, type); 404 405 type = ValuePool.getInt(Types.DECIMAL); 407 408 try { 409 c = nameSpace.classForName("java.math.BigDecimal"); 410 411 typeMap.put(c, type); 412 } catch (Exception e) {} 413 414 try { 416 c = nameSpace.classForName("org.hsqldb.jdbc.jdbcDistinct"); 417 418 typeMap.put(c, ValuePool.getInt(Types.DISTINCT)); 419 } catch (Exception e) {} 420 421 type = ValuePool.getInt(Types.DOUBLE); 423 424 typeMap.put(Double.TYPE, type); 425 typeMap.put(Double .class, type); 426 427 type = ValuePool.getInt(Types.FLOAT); 429 430 typeMap.put(Float.TYPE, type); 431 typeMap.put(Float .class, type); 432 433 type = ValuePool.getInt(Types.INTEGER); 435 436 typeMap.put(Integer.TYPE, type); 437 typeMap.put(Integer .class, type); 438 439 type = ValuePool.getInt(Types.JAVA_OBJECT); 441 442 typeMap.put(Object .class, type); 443 444 type = ValuePool.getInt(Types.LONGVARBINARY); 446 447 typeMap.put(byte[].class, type); 448 typeMap.put(Binary.class, type); 449 450 type = ValuePool.getInt(Types.LONGVARCHAR); 452 453 typeMap.put(String .class, type); 454 455 type = ValuePool.getInt(Types.NULL); 457 458 typeMap.put(Void.TYPE, type); 459 typeMap.put(Void .class, type); 460 461 type = ValuePool.getInt(Types.REF); 463 464 try { 465 c = nameSpace.classForName("org.hsqldb.jdbc.jdbcRef"); 466 467 typeMap.put(c, type); 468 } catch (Exception e) {} 469 470 type = ValuePool.getInt(Types.SMALLINT); 472 473 typeMap.put(Short.TYPE, type); 474 typeMap.put(Short .class, type); 475 476 type = ValuePool.getInt(Types.STRUCT); 478 479 try { 480 c = nameSpace.classForName("org.hsqldb.jdbc.jdbcStruct"); 481 482 typeMap.put(c, type); 483 } catch (Exception e) {} 484 485 type = ValuePool.getInt(Types.TIME); 487 488 typeMap.put(java.sql.Time .class, type); 489 490 type = ValuePool.getInt(Types.TIMESTAMP); 492 493 typeMap.put(java.sql.Timestamp .class, type); 494 495 type = ValuePool.getInt(Types.TINYINT); 497 498 typeMap.put(Byte.TYPE, type); 499 typeMap.put(Byte .class, type); 500 501 type = ValuePool.getInt(Types.XML); 503 504 try { 505 c = nameSpace.classForName("org.w3c.dom.Document"); 506 507 typeMap.put(c, type); 508 509 c = nameSpace.classForName("org.w3c.dom.DocumentFragment"); 510 511 typeMap.put(c, type); 512 } catch (Exception e) {} 513 } 514 515 private void resolveCols() { 516 517 Class rType; 518 Class [] pTypes; 519 Class clazz; 520 int ptlen; 521 int pclen; 522 boolean isFPCON; 523 524 rType = method.getReturnType(); 525 pTypes = method.getParameterTypes(); 526 ptlen = pTypes.length; 527 isFPCON = ptlen > 0 && pTypes[0].getName().equals(conClsName); 528 pclen = 1 + ptlen - (isFPCON ? 1 529 : 0); 530 colClasses = new Class [pclen]; 531 colTypes = new int[pclen]; 532 colClasses[0] = rType; 533 colTypes[0] = typeForClass(rType); 534 535 for (int i = isFPCON ? 1 536 : 0, idx = 1; i < ptlen; i++, idx++) { 537 clazz = pTypes[i]; 538 colClasses[idx] = clazz; 539 colTypes[idx] = typeForClass(clazz); 540 } 541 542 colOffset = rType == Void.TYPE ? 1 543 : 0; 544 colCount = pclen - colOffset; 545 } 546 547 553 void setMethod(Method m) { 554 555 String remarkKey; 556 557 method = m; 558 clazz = method.getDeclaringClass(); 559 fqn = null; 560 specificName = null; 561 sig = null; 562 colsResolved = false; 563 remarkKey = clazz.getName().replace('.', '_'); 564 hnd_remarks = BundleHandler.getBundleHandle(remarkKey, null); 565 } 566 567 int typeForClass(Class c) { 568 569 Class to; 570 Integer type = (Integer ) typeMap.get(c); 571 572 if (type != null) { 573 return type.intValue(); 574 } 575 576 if (c.isArray() &&!c.getComponentType().isArray()) { 583 return Types.ARRAY; 584 } 585 586 try { 587 to = Class.forName("java.sql.Array"); 588 589 if (to.isAssignableFrom(c)) { 590 return Types.ARRAY; 591 } 592 } catch (Exception e) {} 593 594 if (Number .class.isAssignableFrom(c)) { 599 return Types.NUMERIC; 600 } 601 602 try { 604 to = Class.forName("java.sql.Timestamp"); 605 606 if (to.isAssignableFrom(c)) { 607 return Types.TIMESTAMP; 608 } 609 } catch (Exception e) {} 610 611 try { 613 to = Class.forName("java.sql.Time"); 614 615 if (to.isAssignableFrom(c)) { 616 return Types.TIMESTAMP; 617 } 618 } catch (Exception e) {} 619 620 try { 622 to = Class.forName("java.sql.Date"); 623 624 if (to.isAssignableFrom(c)) { 625 return Types.DATE; 626 } 627 } catch (Exception e) {} 628 629 try { 631 to = Class.forName("java.sql.Blob"); 632 633 if (to.isAssignableFrom(c)) { 634 return Types.BLOB; 635 } 636 } catch (Exception e) {} 637 638 try { 640 to = Class.forName("java.sql.Clob"); 641 642 if (to.isAssignableFrom(c)) { 643 return Types.CLOB; 644 } 645 } catch (Exception e) {} 646 647 try { 649 to = Class.forName("java.sql.Ref"); 650 651 if (to.isAssignableFrom(c)) { 652 return Types.REF; 653 } 654 } catch (Exception e) {} 655 656 try { 658 to = Class.forName("java.sql.Struct"); 659 660 if (to.isAssignableFrom(c)) { 661 return Types.STRUCT; 662 } 663 } catch (Exception e) {} 664 665 if (Binary.class.isAssignableFrom(c)) { 667 return Types.LONGVARBINARY; 668 } 669 670 try { 672 673 to = Class.forName("java.lang.CharSequence"); 675 676 if (to.isAssignableFrom(c)) { 677 return Types.LONGVARCHAR; 678 } 679 } catch (Exception e) {} 680 681 if (Serializable .class.isAssignableFrom(c)) { 684 685 return Types.OTHER; 687 } 688 689 return Types.JAVA_OBJECT; 694 } 695 } 696 | Popular Tags |