1 2 12 package com.versant.core.jdbc; 13 14 import com.versant.core.metadata.generator.OIDSrcGenerator; 15 import com.versant.core.metadata.ModelMetaData; 16 import com.versant.core.metadata.ClassMetaData; 17 import com.versant.core.metadata.MDStatics; 18 import com.versant.core.metadata.FieldMetaData; 19 import com.versant.core.common.BindingSupportImpl; 20 import com.versant.core.compiler.ClassSpec; 21 import com.versant.core.jdbc.metadata.JdbcColumn; 22 import com.versant.core.jdbc.metadata.JdbcField; 23 import com.versant.core.jdbc.metadata.JdbcClass; 24 25 import java.sql.ResultSet ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 29 32 public class JdbcOIDGenerator extends OIDSrcGenerator { 33 34 public JdbcOIDGenerator(ModelMetaData jmd) { 35 super(jmd); 36 } 37 38 public ClassSpec generateOID(ClassMetaData cmd) { 39 ClassSpec spec = super.generateOID(cmd); 40 41 spec.addImport(JdbcOID.class.getName()); 42 spec.addImport(ResultSet .class.getName()); 43 spec.addImport(PreparedStatement .class.getName()); 44 spec.addImport(SQLException .class.getName()); 45 spec.addImport(JdbcColumn.class.getName()); 46 spec.addImport(JdbcUtils.class.getName()); 47 spec.addImport(JdbcField.class.getName()); 48 spec.addImport(JdbcClass.class.getName()); 49 50 spec.addInterface("JdbcOID"); 51 52 addCopyKeyFields(); 53 addSetParams(); 54 addSetParams2(); 55 addValidateKeyFields(); 56 addCopyKeyFields2(); 57 58 return spec; 59 } 60 61 protected void addInitStaticsBody(StringBuffer buf) { 62 super.addInitStaticsBody(buf); 63 if (cmd.isInHeirachy()) { 65 buf.append("\t\tif (top.storeClass == null) return true;\n"); 66 } else { 67 buf.append("\t\tif (cmd.storeClass == null) return true;\n"); 68 } 69 JdbcColumn[] pkc = ((JdbcClass)cmd.top.storeClass).table.pk; 70 boolean first = true; 71 for (int j = 0; j < pkc.length; j++) { 72 if (pkc[j].converter != null) { 73 if (first) { 74 buf.append("\t\tClassMetaData t = jmd.classes[" + currentCMD.top.index + "];\n"); 75 buf.append("\t\tJdbcColumn[] pkc = ((JdbcClass)t.storeClass).table.pk;\n"); 76 first = false; 77 } 78 buf.append("\t\t" + JDBC_CONVERTER_FIELD_PREFIX + j + 79 " = (" + pkc[j].converter.getClass().getName() + 80 ")pkc[" + j + "].converter;\n"); 81 buf.append("\t\tjdbcCol_" + j + " = pkc[" + j + "];\n"); 82 } 83 } 84 } 85 86 protected void addPopulateObjectIdClassInstance() { 87 StringBuffer buf = new StringBuffer (); 88 buf.append("\n\tpublic void populateObjectIdClassInstance(Object o) {\n"); 89 if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 90 if (cmd.top.objectIdClass != null){ 91 String oidClassName = cmd.top.objectIdClass.getName().replace('$','.'); 92 buf.append("\t\t"+oidClassName+" pk = ("+ oidClassName +")o;\n"); 93 FieldMetaData[] fields = cmd.pkFields; 94 for (int i = 0; i < fields.length; i++) { 95 FieldMetaData field = fields[i]; 96 buf.append("\t\tpk."); 97 buf.append(field.getPkFieldName()); 98 buf.append(" = "); 99 buf.append(getFieldName(i)); 100 buf.append(";\n"); 101 } 102 } 103 } 104 buf.append("\t}\n"); 105 spec.addMethod(buf.toString()); 106 } 107 108 private void addValidateKeyFields() { 109 StringBuffer buf = new StringBuffer (); 110 116 buf.append("\n\tpublic boolean validateKeyFields(ResultSet rs, int firstCol) throws SQLException {\n"); 117 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 118 for (int i = 0; i < pkc.length; i++) { 119 Class fieldType = pkc[i].javaType; 120 if (pkc[i].converter != null) { 125 buf.append("\t\t"); 126 buf.append("jdbcConverter_" + i); 127 buf.append(".get(rs,firstCol++, jdbcCol_" + i); 128 buf.append(");\n\t\t"); 129 buf.append("if (rs.wasNull())return false;\n"); 130 131 } else { boolean isWrapper = wrapperTypesToPrimitive.keySet().contains(fieldType); 133 if (isWrapper) { 134 138 139 buf.append("\t\trs."); 140 buf.append((String ) typeToResultSetGetField.get(wrapperTypesToPrimitive.get(fieldType))); 141 buf.append("(firstCol++);\n"); 142 } else { 143 147 buf.append("\t\trs."); 148 buf.append((String ) typeToResultSetGetField.get(fieldType)); 149 buf.append("(firstCol++);\n"); 150 151 } 152 buf.append("\t\tif (rs.wasNull())return false;\n"); 153 } 154 } 155 buf.append("\t\treturn true;\n\t}\n"); 156 spec.addMethod(buf.toString()); 157 } 158 159 160 private void addCopyKeyFields() { 161 StringBuffer buf = new StringBuffer (); 162 197 198 buf.append("\n\tpublic boolean copyKeyFields(ResultSet rs,int firstCol) throws SQLException{\n"); 199 200 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 201 for (int i = 0; i < pkc.length; i++) { 202 Class fieldType = pkc[i].javaType; 203 boolean isPrim = pkc[i].javaType.isPrimitive(); 204 String fieldName = "_" + i; 205 if (pkc[i].converter != null) { if (isPrim) { 207 214 buf.append("\t\t"); 215 buf.append((String ) primitiveToWrapperTypes.get(fieldType)); 216 buf.append(" prim_"+i); 217 buf.append(" = ("); 218 buf.append((String ) primitiveToWrapperTypes.get(fieldType)); 219 buf.append(")jdbcConverter_"+i); 220 buf.append(".get(rs,firstCol++, jdbcCol_"+i); 221 buf.append(");\n"); 222 buf.append("\t\tif (rs.wasNull()) return false;\n"); 223 buf.append("\t\t"); 224 buf.append(fieldName); 225 buf.append(" = "); 226 buf.append(" prim_" + i); 227 buf.append("."); 228 buf.append((String ) wrapperTypesToValue.get(fieldType)); 229 buf.append("();\n"); 230 } else { 231 235 buf.append("\t\t"); 236 buf.append(fieldName); 237 buf.append(" = ("); 238 buf.append(fieldType.getName()); 239 buf.append(")jdbcConverter_" + i); 240 buf.append(".get(rs,firstCol++, jdbcCol_" + i); 241 buf.append(");\n"); 242 buf.append("\t\tif (rs.wasNull()) return false;\n"); 243 } 244 } else { boolean isWrapper = wrapperTypesToPrimitive.keySet().contains(fieldType); 246 if (isWrapper) { 247 251 buf.append("\t\t"); 252 buf.append(fieldName); 253 buf.append(" = new "); 254 buf.append(fieldType.getName()); 255 buf.append("(rs."); 256 buf.append((String ) typeToResultSetGetField.get( 257 wrapperTypesToPrimitive.get(fieldType))); 258 buf.append("(firstCol++));\n"); 259 } else { 260 264 buf.append("\t\t"); 265 buf.append(fieldName); 266 buf.append(" = rs."); 267 buf.append((String ) typeToResultSetGetField.get(fieldType)); 268 buf.append("(firstCol++);\n"); 269 270 } 271 buf.append("\t\tif (rs.wasNull()) return false;\n"); 272 } 273 } 274 buf.append("\t\treturn true;\n\t}\n"); 275 spec.addMethod(buf.toString()); 276 } 277 278 private void addSetParams() { 279 StringBuffer buf = new StringBuffer (); 280 294 295 buf.append("\n\tpublic int setParams(PreparedStatement ps, int firstParam) throws SQLException {\n"); 296 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 297 for (int i = 0; i < pkc.length; i++) { 298 Class fieldType = pkc[i].javaType; 299 String fieldName = "_" + i; 300 boolean isPrimitive = pkc[i].javaType.isPrimitive(); 301 if (pkc[i].converter != null) { String converterName = JDBC_CONVERTER_FIELD_PREFIX + i; 303 String colName = "jdbcCol_" + i; 304 305 if (isPrimitive) { 306 309 buf.append("\t\t"); 310 buf.append(converterName); 311 buf.append(".set(ps, firstParam++, "); 312 buf.append(colName); 313 buf.append(", new "); 314 buf.append((String ) primitiveToWrapperTypes.get(fieldType)); 315 buf.append("("); 316 buf.append(fieldName); 317 buf.append("));\n"); 318 } else { 319 322 buf.append("\t\t"); 323 buf.append(converterName); 324 buf.append(".set(ps, firstParam++, "); 325 buf.append(colName); 326 buf.append(", "); 327 buf.append(fieldName); 328 buf.append(");\n"); 329 } 330 } else if (isPrimitive) { 334 buf.append("\t\t"); 335 buf.append("ps."); 336 buf.append((String ) typeToPreparedStatementSetField.get(fieldType)); 337 buf.append("(firstParam++, "); 338 buf.append(fieldName); 339 buf.append(");\n"); 340 } else { 345 buf.append("\t\t"); 346 buf.append("if ("); 347 buf.append(fieldName); 348 buf.append(" == null) ps.setNull(firstParam++,"); 349 buf.append(" "+ (pkc[i]).jdbcType); 350 buf.append(");\n"); 351 if (wrapperTypesToPrimitive.containsKey(fieldType)) { 353 356 357 buf.append("\t\t"); 358 buf.append("else ps."); 359 buf.append((String ) typeToPreparedStatementSetField.get( 360 wrapperTypesToPrimitive.get(fieldType))); 361 buf.append("(firstParam++,"); 362 buf.append(fieldName); 363 buf.append("."); 364 buf.append((String ) wrapperTypesToValue.get( 365 wrapperTypesToPrimitive.get(fieldType))); 366 buf.append("());\n"); 367 } else { 368 371 buf.append("\t\t"); 372 buf.append("else ps."); 373 buf.append((String ) typeToPreparedStatementSetField.get(fieldType)); 374 buf.append("(firstParam++,"); 375 buf.append(fieldName); 376 buf.append(");\n"); 377 } 378 } 379 } 380 buf.append("\t\treturn firstParam;\n\t}\n"); 381 spec.addMethod(buf.toString()); 382 } 383 384 385 private void addSetParams2() { 386 StringBuffer buf = new StringBuffer (); 387 409 buf.append("\n\tpublic int setParams(PreparedStatement ps, int firstParam, JdbcColumn[] pkc) throws SQLException {\n"); 410 411 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 412 415 buf.append("\t\tJdbcColumn c = null;\n"); 416 for (int i = 0; i < pkc.length; i++) { 417 Class fieldType = pkc[i].javaType; 418 String fieldName = "_" + i; 419 boolean isPrimitive = pkc[i].javaType.isPrimitive(); 420 427 buf.append("\t\tc = pkc["+i); 428 buf.append("];\n"); 429 430 buf.append("\t\tif (c.isForUpdate()) {\n"); 431 if (isPrimitive) { 432 436 437 buf.append("\t\t\tif (c.converter != null) c.converter.set(ps, firstParam++, c, new "); 438 buf.append((String ) primitiveToWrapperTypes.get(fieldType)); 439 buf.append("("); 440 buf.append(fieldName); 441 buf.append("));\n"); 442 443 buf.append("\t\t\telse JdbcUtils.set(ps, firstParam++, new "); 444 buf.append((String ) primitiveToWrapperTypes.get(fieldType)); 445 buf.append("("); 446 buf.append(fieldName); 447 buf.append("), c.javaTypeCode, c.jdbcType);\n"); 448 449 } else { 450 454 buf.append("\t\t\tif (c.converter != null) c.converter.set(ps, firstParam++, c, "); 455 buf.append(fieldName); 456 buf.append(");\n"); 457 458 buf.append("\t\t\telse JdbcUtils.set(ps, firstParam++, "); 459 buf.append(fieldName); 460 buf.append(", c.javaTypeCode, c.jdbcType);\n"); 461 462 } 463 buf.append("\t\t}\n"); 464 465 } 466 buf.append("\t\treturn firstParam;\n\t}\n"); 467 spec.addMethod(buf.toString()); 468 } 469 470 protected void addCopyKeyFields2(){ 471 StringBuffer buf = new StringBuffer (); 472 buf.append("\n\tpublic boolean copyKeyFields(ResultSet rs, JdbcField[] pks, int[] pkFieldIndexs) throws SQLException {\n"); 473 if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 474 buf.append("\t\tfor (int j = 0; j < pkFieldIndexs.length; j++) {\n"); 475 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 476 buf.append("\t\t\tswitch(j){\n"); 477 for (int i = 0; i < pkc.length; i++) { 478 Class fieldType = pkc[i].javaType; 479 boolean isPrim = pkc[i].javaType.isPrimitive(); 480 String fieldName = "_" + i; 481 buf.append("\t\t\t\tcase " + i + " :\n"); 482 if (pkc[i].converter != null) { if (isPrim) { 484 491 492 buf.append("\t\t\t\t\t"+(String ) primitiveToWrapperTypes.get(fieldType)); 493 buf.append(" prim_" + i); 494 buf.append(" = ("); 495 buf.append((String ) primitiveToWrapperTypes.get(fieldType)); 496 buf.append(")jdbcConverter_" + i); 497 buf.append(".get(rs, pkFieldIndexs[j] + 1, jdbcCol_" + i); 498 buf.append(");\n"); 499 buf.append("\t\t\t\t\t"+fieldName); 500 buf.append(" = "); 501 buf.append(" prim_" + i); 502 buf.append("."); 503 buf.append((String ) wrapperTypesToValue.get(fieldType)); 504 buf.append("();\n"); 505 } else { 506 510 buf.append("\t\t\t\t\t" + fieldName); 511 buf.append(" = ("); 512 buf.append(fieldType.getName()); 513 buf.append(")jdbcConverter_" + i); 514 buf.append(".get(rs,pkFieldIndexs[j] + 1, jdbcCol_" + i); 515 buf.append(");\n"); 516 } 517 } else { boolean isWrapper = wrapperTypesToPrimitive.keySet().contains(fieldType); 519 if (isWrapper) { 520 524 buf.append("\t\t\t\t\t" + fieldName); 525 buf.append(" = new "); 526 buf.append(fieldType.getName()); 527 buf.append("(rs."); 528 buf.append((String ) typeToResultSetGetField.get(wrapperTypesToPrimitive.get(fieldType))); 529 buf.append("(pkFieldIndexs[j] + 1));\n"); 530 } else { 531 535 buf.append("\t\t\t\t\t" + fieldName); 536 buf.append(" = rs."); 537 buf.append((String ) typeToResultSetGetField.get(fieldType)); 538 buf.append("(pkFieldIndexs[j] + 1);\n"); 539 540 } 541 } 542 buf.append("\t\t\t\t\tbreak;\n"); 543 } 544 buf.append("\t\t\t}\n"); 545 buf.append("\t\t\tif (rs.wasNull()) return false;\n"); 546 buf.append("\t\t}\n"); 547 } 548 buf.append("\t\treturn true;\n"); 549 buf.append("\t}\n"); 550 spec.addMethod(buf.toString()); 551 } 552 553 protected void addGetLongPrimaryKey() { 554 StringBuffer buf = new StringBuffer (); 555 buf.append("\n\tpublic long getLongPrimaryKey() {\n"); 556 if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) { 557 JdbcColumn c = ((JdbcClass)currentCMD.storeClass).table.pk[0]; 558 switch (c.javaTypeCode) { 559 case MDStatics.BYTE: 560 case MDStatics.SHORT: 561 case MDStatics.INT: 562 buf.append("\t\treturn (long)_0;\n"); 563 break; 564 565 case MDStatics.LONG: 566 buf.append("\t\treturn _0;\n"); 567 break; 568 } 569 } else { 570 buf.append("\t\treturn (long) -1;\n"); 571 } 572 buf.append("\t}\n"); 573 spec.addMethod(buf.toString()); 574 } 575 576 protected void addSetLongPrimaryKey() { 577 StringBuffer buf = new StringBuffer (); 578 buf.append("\n\tpublic void setLongPrimaryKey(long newPK) {\n"); 579 if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) { 580 JdbcColumn c = ((JdbcClass)currentCMD.storeClass).table.pk[0]; 581 buf.append("\t\t_0 = "); 582 switch (c.javaTypeCode) { 583 case MDStatics.BYTE: 584 buf.append("(byte)"); 585 break; 586 case MDStatics.SHORT: 587 buf.append("(short)"); 588 break; 589 case MDStatics.INT: 590 buf.append("(int)"); 591 break; 592 } 593 buf.append("newPK;\n"); 594 } 595 buf.append("\t}\n"); 596 spec.addMethod(buf.toString()); 597 } 598 599 protected void addfillFromPK() { 600 StringBuffer buf = new StringBuffer (); 601 buf.append("\n\tpublic void fillFromPK(Object pk) {\n"); 602 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 603 if (cmd.top.objectIdClass != null){ 604 String oidClassName = cmd.top.objectIdClass.getName().replace('$','.'); 605 608 buf.append("\t\t"+oidClassName); 609 buf.append(" appPK = ("); 610 buf.append(oidClassName); 611 buf.append(")pk;\n"); 612 613 FieldMetaData[] fields = cmd.pkFields; 614 for (int i = 0; i < fields.length; i++) { 615 FieldMetaData field = fields[i]; 616 619 buf.append("\t\t" + getFieldName(i)); 620 buf.append(" = appPK."); 621 buf.append(field.getPkFieldName()); 622 buf.append(";\n"); 623 } 624 } 625 } 626 buf.append("\t}\n"); 627 spec.addMethod(buf.toString()); 628 } 629 630 protected void addCompareTo() { 631 StringBuffer buf = new StringBuffer (); 632 buf.append("\n\tpublic int compareTo(Object o) {\n"); 633 buf.append("\t\tOID other = (OID)o;\n"); 634 buf.append("\t\tint diff = "+ cmd.top.index +" - other.getBaseClassMetaData().index;\n"); 635 buf.append("\t\tif (diff != 0) return diff;\n"); 636 buf.append("\t\tif (other.isNew()) return 1;\n"); 637 buf.append("\t\telse {\n"); 638 buf.append("\t\t\t"+ className +" original = ("+ className +")other;\n"); 639 640 ClassMetaData currentCMD = getCurrentCMD(); 641 642 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 643 int count = pkc.length - 1; 644 boolean isLast; 645 for (int i = 0; i < pkc.length; i++) { 646 JdbcColumn c = pkc[i]; 647 String fieldName = "_" + i; 648 isLast = i == count; 649 650 switch (c.javaTypeCode) { 651 652 case MDStatics.CHAR: 653 case MDStatics.BYTE: 654 case MDStatics.SHORT: 655 case MDStatics.INT: 656 657 if (isLast) { 658 661 buf.append("\t\t\treturn "+ fieldName +" - original."+ fieldName +";\n"); 662 } else { 663 666 buf.append("\t\t\tif (" + fieldName + " != original." + fieldName + ") return " + fieldName + " - original." + fieldName + ";\n"); 667 } 668 break; 669 case MDStatics.LOCALE: 670 674 buf.append("\t\t\tdiff = "+ fieldName +".toString().compareTo(original."+ fieldName +".toString());\n"); 675 676 if (isLast) { 677 buf.append("\t\t\treturn diff;\n"); 678 } else { 679 buf.append("\t\t\tif (diff != 0) return diff;\n"); 680 } 681 break; 682 683 case MDStatics.CHARW: 684 case MDStatics.BYTEW: 685 case MDStatics.SHORTW: 686 case MDStatics.INTW: 687 case MDStatics.LONGW: 688 case MDStatics.FLOATW: 689 case MDStatics.DOUBLEW: 690 case MDStatics.STRING: 691 case MDStatics.BIGDECIMAL: 692 case MDStatics.BIGINTEGER: 693 case MDStatics.DATE: 694 695 if (isLast) { 696 buf.append("\t\t\treturn "+ fieldName +".compareTo(original."+ fieldName +");\n"); 697 } else { 698 buf.append("\t\t\tdiff = " + fieldName + ".compareTo(original." + fieldName + ");\n"); 699 buf.append("\t\t\tif (diff != 0) return diff;\n"); 700 } 701 break; 702 703 case MDStatics.DOUBLE: 704 case MDStatics.FLOAT: 705 case MDStatics.LONG: 706 707 if (isLast) { 708 711 buf.append("\t\t\treturn (int)(" + fieldName + " - original." + fieldName + ");\n"); 712 } else { 713 716 buf.append("\t\t\tif (" + fieldName + " != original." + fieldName + ") return (int)(" + fieldName + " - original." + fieldName + ");\n"); 717 } 718 break; 719 720 721 case MDStatics.BOOLEANW: 722 723 if (isLast) { 724 buf.append("\t\t\treturn !" + fieldName + ".booleanValue() ? -1 : 1;\n"); 725 } else { 726 729 buf.append("\t\t\tif (!" + fieldName + ".equals(original." + fieldName + ")) return !" + fieldName + ".booleanValue() ? -1 : 1;\n"); 730 } 731 732 break; 733 case MDStatics.BOOLEAN: 734 if (isLast) { 735 buf.append("\t\t\treturn !" + fieldName + " ? -1 : 1;\n"); 736 } else { 737 740 buf.append("\t\t\tif ("+ fieldName +" != original."+ fieldName +") return !"+ fieldName +" ? -1 : 1;\n"); 741 } 742 break; 743 744 745 default: 746 throw BindingSupportImpl.getInstance().runtime("Unable to create a ObjectId for a Type '" 747 + c.javaType.getName() + "'"); 748 } 749 750 } 751 buf.append("\t\t}\n\t}\n"); 752 spec.addMethod(buf.toString()); 753 } 754 755 protected void addToPKString() { 756 StringBuffer buf = new StringBuffer (); 757 buf.append("\n\tpublic String toPkString() {\n"); 758 buf.append("\t\tStringBuffer s = new StringBuffer();\n"); 759 JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk; 760 for (int i = 0; i < pkc.length; i++) { 761 String fieldName = "_" + i; 762 buf.append("\t\ts.append("); 763 buf.append(fieldName); 764 buf.append(");\n"); 765 if (i != (pkc.length - 1)) { 766 buf.append("\t\ts.append(\", \");\n"); 767 } 768 } 769 buf.append("\t\treturn s.toString();\n\t}\n"); 770 spec.addMethod(buf.toString()); 771 } 772 773 protected void addToSString() { 774 StringBuffer buf = new StringBuffer (); 775 buf.append("\n\tpublic String toSString() {\n"); 776 JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk; 777 780 buf.append("\t\tStringBuffer s = new StringBuffer(\""); 781 buf.append("OID:" + currentCMD.qname + " (Table:" + 782 ((JdbcClass)currentCMD.storeClass).tableName); 783 buf.append("\");\n"); 784 785 for (int i = 0; i < pkc.length; i++) { 786 JdbcColumn c = pkc[i]; 787 String fieldName = "_" + i; 788 789 buf.append("\t\ts.append(\""); 790 buf.append(" Column:" + c.name + " = \""); 791 buf.append(");\n"); 792 793 buf.append("\t\ts.append("); 794 buf.append(fieldName); 795 buf.append(");\n"); 796 } 797 buf.append("\t\ts.append(\")\");\n"); 798 buf.append("\t\treturn s.toString();\n"); 799 buf.append("\t}\n"); 800 spec.addMethod(buf.toString()); 801 } 802 803 protected void addGetCopy() { 804 StringBuffer buf = new StringBuffer (); 805 buf.append("\n\tpublic OID copy() {\n"); 806 buf.append("\t\t"+className); 807 buf.append(" copy = new "); 808 buf.append(className); 809 buf.append("();\n"); 810 if (cmd.isInHeirachy()) { 811 buf.append("\t\tcopy.cmd = cmd;\n"); 812 buf.append("\t\tcopy.resolved = resolved;\n"); 813 } 814 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 815 int num = pkc.length; 816 for (int i = 0; i < num; i++) { 817 Class fieldType = pkc[i].javaType; 818 String fieldName = getFieldName(i); 819 boolean isPrimitive = pkc[i].javaType.isPrimitive(); 820 boolean isArray = pkc[i].javaType.isArray(); 821 boolean isCloneable = false; 822 823 if (!isPrimitive) { 824 Class [] interfaces = pkc[i].javaType.getInterfaces(); 825 for (int j = 0; j < interfaces.length; j++) { 826 Class aClass = interfaces[j]; 827 if (aClass.getName().equals("Cloneable")) { 828 isCloneable = true; 829 } 830 } 831 } 832 if (isPrimitive) { 833 836 buf.append("\t\tcopy."); 837 buf.append(fieldName); 838 buf.append(" = "); 839 buf.append(fieldName); 840 buf.append(";\n"); 841 } else if (isArray) { 842 845 buf.append("\t\tSystem.arraycopy((Object)"); 846 buf.append(fieldName); 847 buf.append(", 0, (Object)copy."); 848 buf.append(fieldName); 849 buf.append(", 0, "); 850 buf.append(fieldName); 851 buf.append(".length);\n"); 852 } else if (isCloneable) { 853 856 buf.append("\t\tcopy."); 857 buf.append(fieldName); 858 buf.append(" = ("); 859 buf.append(fieldType.getName()); 860 buf.append(")"); 861 buf.append(fieldName); 862 buf.append(".clone();\n"); 863 } else { 867 buf.append("\t\tcopy."); 868 buf.append(fieldName); 869 buf.append(" = "); 870 buf.append(fieldName); 871 buf.append(";\n"); 872 } 873 } 874 buf.append("\t\treturn copy;\n\t}\n"); 875 spec.addMethod(buf.toString()); 876 } 877 878 protected void addCopyKeyFieldsUpdate() { 879 StringBuffer buf = new StringBuffer (); 880 buf.append("\n\tpublic void copyKeyFieldsUpdate(State state) {\n"); 881 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 882 ClassMetaData currentCMD = getTopPCSuperClassMetaData(); 883 buf.append("\t\t"); 884 buf.append(stateClassName); 885 buf.append(" other = ("); 886 buf.append(stateClassName); 887 buf.append(")state;\n"); 888 FieldMetaData[] pkFields = currentCMD.pkFields; 889 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 890 for (int i = 0; i < pkc.length; i++) { 891 int stateFieldNum = pkFields[i].fieldNo; 892 String stateFieldName = "_" + stateFieldNum; 893 String fieldName = "_" + i; 894 897 buf.append("\t\tif (other.containsField("+ stateFieldNum); 898 buf.append(")) "); 899 buf.append(fieldName); 900 buf.append(" = other."); 901 buf.append(stateFieldName); 902 buf.append(";\n"); 903 } 904 } 905 buf.append("\t}\n"); 906 spec.addMethod(buf.toString()); 907 } 908 909 protected void addCopyKeyFieldsFromState() { 910 StringBuffer buf = new StringBuffer (); 911 buf.append("\n\tpublic void copyKeyFields(State state) {\n"); 912 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 913 ClassMetaData currentCMD = getTopPCSuperClassMetaData(); 914 buf.append("\t\t"); 915 buf.append(stateClassName); 916 buf.append(" other = ("); 917 buf.append(stateClassName); 918 buf.append(")state;\n"); 919 FieldMetaData[] pkFields = currentCMD.pkFields; 920 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 921 for (int i = 0; i < pkc.length; i++) { 922 String stateFieldName = "_" + pkFields[i].fieldNo; 923 String fieldName = "_" + i; 924 buf.append("\t\t"); 925 buf.append(fieldName); 926 buf.append(" = other."); 927 buf.append(stateFieldName); 928 buf.append(";\n"); 929 } 930 } 931 buf.append("\t}\n"); 932 spec.addMethod(buf.toString()); 933 } 934 935 protected void addEqualsObject() { 936 StringBuffer buf = new StringBuffer (); 937 buf.append("\n\tpublic boolean equals(Object object) {\n"); 938 buf.append("\t\t"); 939 buf.append("if (object instanceof "); 940 buf.append(className); 941 buf.append(") {\n"); 942 buf.append("\t\t\t"); 943 buf.append(className); 944 buf.append(" other = ("); 945 buf.append(className); 946 buf.append(")object;\n"); 947 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 948 for (int i = 0; i < pkc.length; i++) { 949 String fieldName = "_" + i; 950 boolean isPrimitive = pkc[i].javaType.isPrimitive(); 951 952 if (isPrimitive) { 953 956 buf.append("\t\t\t"); 957 buf.append("if ("); 958 buf.append(fieldName); 959 buf.append(" != other."); 960 buf.append(fieldName); 961 buf.append(") return false;\n"); 962 963 } else { 970 buf.append("\t\t\t"); 971 buf.append("if ("); 972 buf.append(fieldName); 973 buf.append(" != null){\n"); 974 buf.append("\t\t\t\t"); 975 buf.append("if (!"); 976 buf.append(fieldName); 977 buf.append(".equals(other."); 978 buf.append(fieldName); 979 buf.append(")) return false;\n"); 980 buf.append("\t\t\t"); 981 buf.append("} else if (other."); 982 buf.append(fieldName); 983 buf.append(" != null) return false;\n"); 984 } 985 } 986 buf.append("\t\t\treturn true;\n"); 987 buf.append("\t\t} else {\n\t\t\treturn false;\n\t\t}\n\t}\n"); 988 spec.addMethod(buf.toString()); 989 } 990 991 protected void addHashCode() { 992 StringBuffer buf = new StringBuffer (); 993 buf.append("\n\tpublic int hashCode() {\n"); 994 int noOfVars = 0; 995 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 996 noOfVars = pkc.length; 997 for (int i = 0; i < noOfVars; i++) { 998 Class fieldType = pkc[i].javaType; 999 String fieldName = "_" + i; 1000 boolean isPrimitive = pkc[i].javaType.isPrimitive(); 1001 1002 1003 if (noOfVars == 1) { 1004 if (isPrimitive) { 1005 if (fieldType.equals(long.class) 1006 || fieldType.equals(float.class) 1007 || fieldType.equals(double.class)) { 1008 1011 buf.append("\t\treturn (int)("+ cmd.classId); 1012 buf.append(" * "); 1013 buf.append(fieldName); 1014 buf.append(");\n"); 1015 } else if (fieldType.equals(boolean.class)){ 1016 1019 buf.append("\t\treturn "); 1020 buf.append(fieldName); 1021 buf.append(" ? 1 : -1;\n"); 1022 1023 } else { 1024 1027 buf.append("\t\treturn " + cmd.classId); 1028 buf.append(" * "); 1029 buf.append(fieldName); 1030 buf.append(";\n"); 1031 } 1032 } else { 1036 buf.append("\t\treturn "); 1037 buf.append(fieldName); 1038 buf.append(".hashCode();\n"); 1039 } 1040 } else { 1041 if (i == 0) { 1042 buf.append("\t\tint hashCode = 0;\n"); 1043 } 1044 if (isPrimitive) { 1045 if (fieldType.equals(long.class) 1046 || fieldType.equals(float.class) 1047 || fieldType.equals(double.class)) { 1048 1051 buf.append("\t\thashCode += "); 1052 buf.append(fieldName); 1053 buf.append(";\n"); 1054 } else if (fieldType.equals(boolean.class)) { 1055 1058 buf.append("\t\thashCode += "); 1059 buf.append(fieldName); 1060 buf.append(" ? 1 : -1;\n"); 1061 1062 } else { 1063 1066 buf.append("\t\thashCode += "); 1067 buf.append(fieldName); 1068 buf.append(";\n"); 1069 } 1070 } else { 1074 buf.append("\t\thashCode += "); 1075 buf.append(fieldName); 1076 buf.append(".hashCode();\n"); 1077 } 1078 } 1079 } 1080 if (noOfVars > 1) { 1081 buf.append("\t\treturn hashCode;\n\t}\n"); 1082 } else { 1083 buf.append("\t}\n"); 1084 } 1085 spec.addMethod(buf.toString()); 1086 } 1087 1088 1091 protected void addFields() { 1092 super.addFields(); 1093 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 1094 for (int i = 0; i < pkc.length; i++) { 1095 spec.addField("public " + pkc[i].javaType.getName() + " _" + i); 1096 if (pkc[i].converter != null) { 1097 spec.addField("public static " + 1098 pkc[i].converter.getClass().getName() + " " + 1099 JDBC_CONVERTER_FIELD_PREFIX + i); 1100 spec.addField("public static " + 1101 pkc[i].getClass().getName() + " jdbcCol_" + i); 1102 } 1103 } 1104 } 1105 1106 protected void addCopyKeyFieldsObjects() { 1107 StringBuffer buf = new StringBuffer (); 1108 buf.append("\n\tpublic void copyKeyFields(Object[] data) {\n"); 1109 JdbcColumn[] pkc = ((JdbcClass)currentCMD.storeClass).table.pk; 1110 for (int i = 0; i < pkc.length; i++) { 1111 Class fieldType = pkc[i].javaType; 1112 String fieldName = "_" + i; 1113 boolean isPrimitive = pkc[i].javaType.isPrimitive(); 1114 1115 if (isPrimitive) { 1116 1119 buf.append("\t\t"); 1120 buf.append(fieldName); 1121 buf.append(" = (("); 1122 buf.append((String )primitiveToWrapperTypes.get(fieldType)); 1123 buf.append(")data["+i); 1124 buf.append("])."); 1125 buf.append((String ) primitiveTypesToValue.get(fieldType)); 1126 buf.append("();\n"); 1127 } else { 1128 1131 buf.append("\t\t"); 1132 buf.append(fieldName); 1133 buf.append(" = ("); 1134 buf.append(fieldType.getName()); 1135 buf.append(")data[" + i); 1136 buf.append("];\n"); 1137 } 1138 } 1139 buf.append("\t}\n"); 1140 spec.addMethod(buf.toString()); 1141 } 1142 1143 protected void addToString() { 1144 StringBuffer buf = new StringBuffer (); 1145 buf.append("\n\tpublic String toString() {\n"); 1146 if (cmd.isInHeirachy()) { 1147 buf.append( 1148 "\t\tif (!resolved) {\n" + 1149 "\t\t\tthrow BindingSupportImpl.getInstance().internal(\n" + 1150 "\t\t\t\t\"Called 'toString()' on unresolved oid\");\n" + 1151 "\t\t}\n"); 1152 } 1153 buf.append("\t\tStringBuffer s = new StringBuffer();\n"); 1154 if (currentCMD.isInHeirachy()) { 1155 buf.append("\t\ts.append(cmd.classId);\n"); 1156 } else { 1157 buf.append("\t\ts.append(\""+ cmd.classId); 1158 buf.append("\");\n"); 1159 } 1160 JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk; 1161 for (int i = 0; i < pkc.length; i++) { 1162 String fieldName = "_" + i; 1163 buf.append("\t\ts.append(\"" + MDStatics.OID_STRING_SEPERATOR + "\");\n"); 1164 buf.append("\t\ts.append("+ fieldName +");\n"); 1165 } 1166 buf.append("\t\treturn s.toString();\n\t}\n"); 1167 spec.addMethod(buf.toString()); 1168 } 1169 1170} 1171 1172 | Popular Tags |