1 23 24 package org.objectweb.jorm.mapper.rdb.genclass; 25 26 import org.objectweb.jorm.api.PAccessor; 27 import org.objectweb.jorm.api.PBinding; 28 import org.objectweb.jorm.api.PBindingCtrl; 29 import org.objectweb.jorm.api.PClassMapping; 30 import org.objectweb.jorm.api.PException; 31 import org.objectweb.jorm.api.PExceptionIO; 32 import org.objectweb.jorm.api.PExceptionProtocol; 33 import org.objectweb.jorm.api.PGenClassAccessor; 34 import org.objectweb.jorm.api.PIndexedElem; 35 import org.objectweb.jorm.api.PMappingCallback; 36 import org.objectweb.jorm.api.PMappingStructuresManager; 37 import org.objectweb.jorm.api.PNameIterator; 38 import org.objectweb.jorm.api.PStateGraph; 39 import org.objectweb.jorm.genclass.api.FieldDesc; 40 import org.objectweb.jorm.genclass.lib.GenClassMapping; 41 import org.objectweb.jorm.lib.PBindingImpl; 42 import org.objectweb.jorm.metainfo.api.MetaObject; 43 import org.objectweb.jorm.naming.api.PExceptionNaming; 44 import org.objectweb.jorm.naming.api.PName; 45 import org.objectweb.jorm.naming.api.PNameGetter; 46 import org.objectweb.jorm.naming.lib.CTHelper; 47 import org.objectweb.jorm.type.api.PExceptionTyping; 48 import org.objectweb.jorm.type.api.PType; 49 import org.objectweb.jorm.mapper.rdb.adapter.api.JoinedTable; 50 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 51 import org.objectweb.jorm.mapper.rdb.lib.RdbConnectionWrapper; 52 import org.objectweb.jorm.mapper.rdb.lib.RdbPPolymorphicClass; 53 import org.objectweb.jorm.mapper.rdb.lib.RdbPrefetchablePCM; 54 import org.objectweb.jorm.mapper.rdb.lib.RdbTupleCollection; 55 import org.objectweb.medor.api.MedorException; 56 import org.objectweb.medor.tuple.api.TupleCollection; 57 import org.objectweb.util.monolog.api.BasicLevel; 58 59 import java.io.IOException ; 60 import java.sql.Connection ; 61 import java.sql.PreparedStatement ; 62 import java.sql.ResultSet ; 63 import java.sql.SQLException ; 64 import java.util.ArrayList ; 65 import java.util.HashMap ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 import java.util.Properties ; 69 70 76 77 public class RdbGenClassMapping 78 extends GenClassMapping 79 implements RdbGenClassProp { 80 81 static final byte ID = 0x01; 82 83 static final byte INDEX = 0x02; 84 85 static final byte ELEM = 0x04; 86 87 static final byte ELEMEXT = 0x08; 88 89 static final byte PREFETCH = 0x16; 90 91 static final byte PREFETCH_ID = 0x32; 92 93 static final String SEPAND = " AND "; 94 95 static final String SEPSCOL = ", "; 96 97 static final String QUOTE = "'"; 98 99 static final String ASSIGNVAL = " = ?"; 100 101 static final String ASSIGN_NULL = " = null"; 102 103 107 protected String tableName = null; 108 109 protected String refExtTableName = null; 110 111 protected String [] mainCols, extCols; 112 113 protected RdbPrefetchablePCM prefetchedPCM; 114 protected int nbPrefetchCol = 0; 115 protected boolean prefetchActivated = true; 116 117 120 protected RdbAdapter typeConverter = null; 121 122 protected boolean useBatch = true; 123 124 protected boolean colocated = false; 125 126 protected boolean colocatedMaster = false; 127 128 protected boolean readOnly = false; 129 130 private String selectQuery = null; 131 private String selectPrefetchQuery = null; 132 133 private String selectAllNamesQuery = null; 134 135 private String insertQuery = null; 136 137 private String deleteAllQuery = null; 138 139 private String deleteElemQuery = null; 140 141 private String updateQuery = null; 142 143 private String updateNullAllQuery = null; 144 145 private String updateNullElemQuery = null; 146 147 public void setPrefetchActivated(boolean prefetch) { 148 prefetchActivated = prefetch; 149 } 150 151 String getSelectQuery(boolean withPrefetch) throws PException { 152 if ((withPrefetch && selectPrefetchQuery ==null) 153 || (!withPrefetch && selectQuery == null)) { 154 synchronized (this) { 155 if ((withPrefetch && selectPrefetchQuery ==null) 156 || (!withPrefetch && selectQuery == null)) { 157 StringBuffer select = new StringBuffer (); 159 boolean prefetchWithTab = prefetchedPCM instanceof RdbPPolymorphicClass; 160 if (withPrefetch) { 161 String [] cols = prefetchedPCM.getPrefetchSelectCols(); 162 nbPrefetchCol = cols.length; 163 for (int i = 0; i < nbPrefetchCol; i++) { 164 if (prefetchWithTab) { 165 select.append("ELEM_INNER."); 166 } 167 select.append(cols[i]); 168 select.append(SEPSCOL); 169 } 170 } 171 defineAllNames(select, (byte) (INDEX | ELEM)); 172 173 List jts = new ArrayList (); 175 boolean needJoinForPrefetch = false; 176 jts.add(new JoinedTable(tableName)); 177 if (withPrefetch) { 178 if (prefetchWithTab) { 179 jts.add(new JoinedTable( 180 ((RdbPPolymorphicClass) prefetchedPCM).getExtentQuery(true), 181 "ELEM_INNER")); 182 } else { 183 List newjts = new ArrayList (prefetchedPCM.getPrefetchTables()); 184 needJoinForPrefetch = true; 185 for (int i = 0; i < newjts.size();) { 186 if (tableName.equals(((JoinedTable) newjts.get(i)).tableName)) { 187 newjts.remove(i); 188 needJoinForPrefetch = false; 189 } else { 190 i++; 191 } 192 } 193 jts.addAll(newjts); 194 } 195 } 196 197 StringBuffer where = new StringBuffer (); 199 defineIdentifierClause(where); 200 if (withPrefetch) { 201 String w = prefetchedPCM.getPrefetchWhereClause(); 202 if (w != null && w.length()>0) { 203 where.append(SEPAND); 204 where.append(w); 205 } 206 if (needJoinForPrefetch) { 207 where.append(SEPAND); 208 Properties m = prefetchedPCM.getCNId2Column(); 209 if (elemFields.length >1) { 210 where.append("("); 211 where.append(tableName); 212 where.append("."); 213 where.append(((RdbFieldDesc) elemFields[1]).columnName); 214 where.append(" = "); 215 String colName = null; 216 if (elemFields[1].compositeName == null) { 217 colName = m.getProperty(""); 218 } else { 219 colName = m.getProperty(elemFields[1].compositeName); 220 } 221 where.append(colName); 222 where.append(")"); 223 } 224 if (elemFields.length >2) { 225 for (int i = 2; i < elemFields.length; i++) { 226 where.append(SEPAND); 227 where.append("("); 228 where.append(tableName); 229 where.append("."); 230 where.append(((RdbFieldDesc) elemFields[i]).columnName); 231 where.append(" = "); 232 where.append(m.getProperty(elemFields[i].compositeName)); 233 where.append(")"); 234 } 235 } 236 } 237 } 238 239 String query = typeConverter.getQuery(select.toString(), 241 jts, where.toString(), false, false); 242 if (withPrefetch) { 243 selectPrefetchQuery = query; 244 } else { 245 selectQuery = query; 246 } 247 } 248 } 249 } 250 return (withPrefetch ? selectPrefetchQuery : selectQuery); 251 } 252 253 String getSelectAllNamesQuery() { 254 if (selectAllNamesQuery == null) { 255 synchronized (this) { 256 if (selectAllNamesQuery == null) { 257 StringBuffer sqlstring = new StringBuffer ("SELECT "); 258 defineAllNames(sqlstring, ID); 259 sqlstring.append(" FROM " + tableName); 260 selectAllNamesQuery = sqlstring.toString(); 261 } 262 } 263 } 264 return selectAllNamesQuery; 265 } 266 267 String getInsertQuery() { 268 if (insertQuery == null) { 269 synchronized (this) { 270 if (insertQuery == null) { 271 StringBuffer sqlstring = new StringBuffer ("INSERT INTO " 272 + tableName); 273 sqlstring.append("("); 277 defineNames(getIdentifierFields(), sqlstring, true, 278 (short) 0, false, false); 279 defineNames(getElemFields(), sqlstring, false, 280 (short) (getElemFields().length == 1 ? 0 : 1), 281 false, false); 282 defineNames(getIndexFields(), sqlstring, false, (short) 0, 283 false, false); 284 sqlstring.append(") "); 285 sqlstring.append("VALUES ("); 286 int nq = getIdentifierFields().length; 287 nq += (getElemFields().length == 1 ? 1 288 : getElemFields().length - 1); 289 nq += getIndexFields().length; 290 String sep = ""; 291 for (; nq > 0; nq--) { 292 sqlstring.append(sep); 293 sep = SEPSCOL; 294 sqlstring.append("?"); 295 } 296 sqlstring.append(")"); 297 insertQuery = sqlstring.toString(); 298 } 299 } 300 } 301 return insertQuery; 302 } 303 304 String getDeleteAllQuery() { 305 if (deleteAllQuery == null) { 306 synchronized (this) { 307 if (deleteAllQuery == null) { 308 StringBuffer sqlstring = new StringBuffer ("DELETE FROM " 309 + tableName + " WHERE "); 310 defineIdentifierClause(sqlstring); 311 deleteAllQuery = sqlstring.toString(); 312 } 313 } 314 } 315 return deleteAllQuery; 316 } 317 318 String getDeleteElemQuery() { 319 if (deleteElemQuery == null) { 320 synchronized (this) { 321 if (deleteElemQuery == null) { 322 StringBuffer sqlstring = new StringBuffer ("DELETE FROM " 323 + tableName + " WHERE "); 324 defineIdentifierClause(sqlstring); 325 defineIndexClause(sqlstring); 326 defineElemClause(sqlstring, false); 327 deleteElemQuery = sqlstring.toString(); 328 } 329 } 330 } 331 return deleteElemQuery; 332 } 333 334 String getUpdateQuery() { 335 if (updateQuery == null) { 336 synchronized (this) { 337 if (updateQuery == null) { 338 StringBuffer sqlstring = new StringBuffer ("UPDATE " 339 + tableName + " SET "); 340 if (colocated) { 341 defineIdentifierSet(sqlstring); 342 defineIndexSet(sqlstring); 343 sqlstring.append(" WHERE "); 344 defineElemClause(sqlstring, true); 345 } else { 346 defineElemSet(sqlstring); 347 sqlstring.append(" WHERE "); 348 defineIdentifierClause(sqlstring); 349 defineIndexClause(sqlstring); 350 } 351 updateQuery = sqlstring.toString(); 352 } 353 } 354 } 355 return updateQuery; 356 } 357 358 String getUpdateNullAllQuery() { 359 if (updateNullAllQuery == null) { 360 synchronized (this) { 361 if (updateNullAllQuery == null) { 362 StringBuffer sqlstring = new StringBuffer ("UPDATE " 363 + tableName + " SET "); 364 defineIdentifierSetNull(sqlstring); 365 defineIndexSetNull(sqlstring); 366 sqlstring.append(" WHERE "); 367 defineIdentifierClause(sqlstring); 368 updateNullAllQuery = sqlstring.toString(); 369 } 370 } 371 } 372 return updateNullAllQuery; 373 } 374 375 String getUpdateNullElemQuery() { 376 if (updateNullElemQuery == null) { 377 synchronized (this) { 378 if (updateNullElemQuery == null) { 379 StringBuffer sqlstring = new StringBuffer ("UPDATE " 380 + tableName + " SET "); 381 defineIdentifierSetNull(sqlstring); 382 defineIndexSetNull(sqlstring); 383 sqlstring.append(" WHERE "); 384 defineIdentifierClause(sqlstring); 385 defineIndexClause(sqlstring); 386 defineElemClause(sqlstring, false); 387 updateNullElemQuery = sqlstring.toString(); 388 } 389 } 390 } 391 return updateNullElemQuery; 392 } 393 394 396 401 public PBinding createPBinding() throws PException { 402 return new PBindingImpl(this); 403 } 404 405 414 public PNameIterator getPNameIterator(Object conn, boolean withSubType, 415 boolean prefetching, Object txctx) throws PException { 416 Connection sqlconn = RdbConnectionWrapper.narrow2SQL(conn); 417 try { 418 PreparedStatement ps = sqlconn 419 .prepareStatement(getSelectAllNamesQuery()); 420 return new RdbGenClassPNGIterator(ps, ps.executeQuery(), 421 identifierFields, this, prefetching, typeConverter); 422 } catch (SQLException e) { 423 throw new PExceptionIO(e, 424 "SQL problem while computing the PName Iterator."); 425 } 426 } 427 428 431 public PClassMapping[] getSubPCMs() throws PException { 432 return null; 433 } 434 435 443 public boolean isConform(String mappername) { 444 return mappername.equals("rdb"); 445 } 446 447 public boolean exist(PBinding pb, Object conn) throws PException { 448 byte status = pb.getStatus(); 449 byte nextstate = PStateGraph.nextStatePBinding(status, 450 PBinding.ACTION_EXIST); 451 if (nextstate == PBinding.LIFECYCLE_ERROR) 452 throw new PExceptionProtocol( 453 "Cannot perform exist on this GenClass PBinding: " + status); 454 ((PBindingCtrl) pb).setStatus(nextstate); 455 return true; 456 } 457 458 public void read(PBinding pb, Object conn, PAccessor pa) throws PException { 459 read(pb, conn, pa, null); 460 } 461 public void read(PBinding pb, Object conn, PAccessor pa, Object txctx) throws PException { 462 PGenClassAccessor paccessorGenClass = (PGenClassAccessor) pa; 463 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), PBinding.ACTION_READ); 464 if (nextstate == PBinding.LIFECYCLE_ERROR) 465 throw new PExceptionProtocol( 466 "Cannot perform read on this GenClass PBinding: " + pb.getStatus()); 467 PreparedStatement pstmt = null; 468 ResultSet rs = null; 469 PException resex = null; 470 boolean withPrefetch = prefetchActivated 471 && txctx != null 472 && prefetchedPCM != null; 473 try { 474 475 Connection sqlconn = RdbConnectionWrapper.narrow2SQL(conn); 476 pstmt = sqlconn.prepareStatement(getSelectQuery(withPrefetch)); 477 defineIdentifierVal(pb.getPName(), pstmt, 1); 478 rs = pstmt.executeQuery(); 479 int size = typeConverter.fetchResultSetSize(rs); 480 paccessorGenClass.paSetNbElem(size); 481 PNameGetter png = getPNameGetter(rs); 482 RdbTupleCollection tc = null; 483 if (withPrefetch) { 484 try { 486 tc = new RdbTupleCollection(txctx, rs, pstmt, 487 nbPrefetchCol + 1, 488 logger, prefetchedPCM, true, png); 489 } catch (MedorException e1) { 490 throw new PException(e1); 491 } 492 } 493 boolean hasNext = false; 494 try { 495 hasNext = (tc == null ? rs.next() : tc.next()); 496 } catch (MedorException e1) { 497 throw new PException(e1); 498 } 499 while (hasNext) { 500 passValuesToAccessor(conn, rs, paccessorGenClass, png, tc); 501 try { 502 hasNext = (tc == null ? rs.next() : tc.next()); 503 } catch (MedorException e1) { 504 throw new PException(e1); 505 } 506 } 507 } catch (SQLException e) { 508 resex = new PExceptionIO(e, "SQL problem while reading the DSI."); 509 } catch (IOException e) { 510 resex = new PExceptionIO(e, 511 "Stream IO problem while reading the DSI."); 512 } finally { 513 if (!withPrefetch) { 514 try { 515 if (rs != null) { 516 rs.close(); 517 } 518 if (pstmt != null) { 519 pstmt.close(); 520 } 521 } catch (SQLException e) { 522 if (resex == null) { 523 resex = new PExceptionIO(e, 524 "Problem while closing ResultSet or Statment."); 525 } 526 } 527 } 528 if (resex != null) { 529 throw resex; 530 } 531 } 532 ((PBindingCtrl) pb).setStatus(nextstate); 533 } 534 535 public void write(PBinding pb, Object conn, PAccessor pa) throws PException { 536 PGenClassAccessor paccessorGenClass = (PGenClassAccessor) pa; 537 byte status = pb.getStatus(); 538 byte nextstate = PStateGraph.nextStatePBinding(status, PBinding.ACTION_WRITE); 539 if (nextstate == PBinding.LIFECYCLE_ERROR) 540 throw new PExceptionProtocol( 541 "Cannot perform write on this GenClass PBinding: " + status); 542 if (readOnly) { 543 ((PBindingCtrl) pb).setStatus(nextstate); 544 return; 545 } 546 try { 547 Connection sqlconn = RdbConnectionWrapper.narrow2SQL(conn); 548 switch (status) { 549 case PBinding.LIFECYCLE_NEWTOWRITE: 550 createGcObject(pb.getPName(), sqlconn, paccessorGenClass); 551 break; 552 case PBinding.LIFECYCLE_DELTOWRITE: 553 deleteGcObject(pb.getPName(),sqlconn); 554 break; 555 case PBinding.LIFECYCLE_ACTIVEFORIO: 556 if (paccessorGenClass.paDeltaSupported()) 557 updateGcObject(pb.getPName(), sqlconn, paccessorGenClass); 558 else { 559 deleteGcObject(pb.getPName(), sqlconn); 560 createGcObject(pb.getPName(), sqlconn, paccessorGenClass); 561 } 562 break; 563 default : 564 throw new PExceptionProtocol("unmanaged status: " + status); 565 } 566 } catch (SQLException e) { 567 throw new PExceptionIO(e, "SQL problem while writing the DSI."); 568 } catch (IOException e) { 569 throw new PExceptionIO(e, "Stream IO problem while writing the DSI."); 570 } 571 ((PBindingCtrl) pb).setStatus(nextstate); 572 } 573 574 576 public void setTableName(String tn) { 577 tableName = tn; 578 } 579 580 public void setRefExtTableName(String retn) { 581 refExtTableName = retn; 582 } 583 584 public void setColocated(boolean colocated) { 585 this.colocated = colocated; 586 } 587 588 public void setColocatedMaster(boolean cm) { 589 this.colocatedMaster = cm; 590 } 591 592 public void setReadOnly(boolean readonly) { 593 this.readOnly = readonly; 594 } 595 596 public void setTypeConverter(RdbAdapter tc) { 597 typeConverter = tc; 598 if (typeConverter != null) { 599 useBatch = typeConverter.supportBatchPreparedStatement(); 600 } 601 } 602 603 public void defineJoinColumns(String [] maincols, String [] extcols) { 604 mainCols = maincols; 605 extCols = extcols; 606 if (mainCols.length != extCols.length) { 607 throw new RuntimeException ("Need arrays with same size!!"); 608 } 609 } 610 611 612 public void setPrefetchElementPCM(RdbPrefetchablePCM pcm) { 613 prefetchedPCM = pcm; 614 } 615 616 618 621 private void createGcObject(PName pName, Connection sqlconn, 622 PGenClassAccessor paccessorGenClass) 623 throws SQLException , PException, IOException { 624 int size = paccessorGenClass.paGetNbElem(); 625 if (size == 0) { 626 return; 628 } 629 PreparedStatement pstmt_ins = null; 630 PreparedStatement pstmt_upd = null; 631 try { 632 if (colocated) { 633 pstmt_upd = sqlconn.prepareStatement(getUpdateQuery()); 634 } 635 pstmt_ins = sqlconn.prepareStatement(getInsertQuery()); 636 Iterator iterelem = paccessorGenClass.paIterator(); 638 int paramindex; 639 boolean batchused = false; 640 while (iterelem.hasNext()) { 641 PIndexedElem pie = (PIndexedElem) iterelem.next(); 642 if (colocated) { 643 paramindex = defineIdentifierVal(pName, pstmt_upd, 1); 644 paramindex = defineIndexVal(pie, pstmt_upd, paramindex); 645 defineElemVal(pie, pstmt_upd, paramindex); 646 if (pstmt_upd.executeUpdate() > 0) { 647 continue; 648 } 649 } 650 paramindex = defineIdentifierVal(pName, pstmt_ins, 1); 651 paramindex = defineElemVal(pie, pstmt_ins, paramindex); 652 defineIndexVal(pie, pstmt_ins, paramindex); 653 if (useBatch && !colocated) { 654 batchused = true; 655 pstmt_ins.addBatch(); 656 } else { 657 pstmt_ins.execute(); 658 } 659 } 660 if (batchused && useBatch) { 661 pstmt_ins.executeBatch(); 662 } 663 } finally { 664 if (pstmt_ins != null) { 665 pstmt_ins.close(); 666 } 667 if (pstmt_upd != null) { 668 pstmt_upd.close(); 669 } 670 } 671 } 672 673 676 private void deleteGcObject(PName pName, Connection sqlconn) 677 throws SQLException , PException, IOException { 678 PreparedStatement pstmt = null; 679 try { 680 pstmt = sqlconn.prepareStatement(colocated 681 ? getUpdateNullAllQuery() 682 : getDeleteAllQuery()); 683 defineIdentifierVal(pName, pstmt, 1); 685 pstmt.execute(); 686 } finally { 687 if (pstmt != null) { 688 pstmt.close(); 689 } 690 } 691 } 692 693 696 private void updateGcObject(PName pName, Connection sqlconn, 697 PGenClassAccessor paccessorGenClass) 698 throws SQLException , PException, IOException { 699 PreparedStatement pstmt_ins = null; 700 PreparedStatement pstmt_del = null; 701 PreparedStatement pstmt_upd = null; 702 PreparedStatement pstmt_upd_null = null; 703 try { 704 Iterator iterelem = paccessorGenClass.paIterator(); 705 int paramindex; 706 while (iterelem.hasNext()) { 707 PIndexedElem pie = (PIndexedElem) iterelem.next(); 708 switch (pie.getElemStatus()) { 709 case PIndexedElem.ELEM_CREATED: 710 if (pstmt_ins == null) { 711 pstmt_ins = sqlconn.prepareStatement(getInsertQuery()); 712 } 713 if (colocated) { 714 if (pstmt_upd == null) { 715 pstmt_upd = sqlconn.prepareStatement(getUpdateQuery()); 716 } 717 paramindex = defineElemVal(pie, pstmt_upd, 1); 718 paramindex = defineIdentifierVal(pName, pstmt_upd, paramindex); 719 defineIndexVal(pie, pstmt_upd, paramindex); 720 if (pstmt_upd.executeUpdate() == 0) { 721 paramindex = defineIdentifierVal(pName, pstmt_ins, 1); 722 paramindex = defineElemVal(pie, pstmt_ins, paramindex); 723 defineIndexVal(pie, pstmt_ins, paramindex); 724 pstmt_ins.execute(); 725 } 726 } else { 727 paramindex = defineIdentifierVal(pName, pstmt_ins, 1); 728 paramindex = defineElemVal(pie, pstmt_ins, paramindex); 729 defineIndexVal(pie, pstmt_ins, paramindex); 730 if (useBatch) { 731 pstmt_ins.addBatch(); 732 } else { 733 pstmt_ins.execute(); 734 } 735 } 736 break; 737 case PIndexedElem.ELEM_DELETED: 738 if (colocated) { 739 if (pstmt_upd_null == null) { 740 pstmt_upd_null = sqlconn.prepareStatement(getUpdateNullElemQuery()); 741 } 742 paramindex = defineIdentifierVal(pName, pstmt_upd_null, 1); 743 paramindex = defineIndexVal(pie, pstmt_upd_null, paramindex); 744 defineElemVal(pie, pstmt_upd_null, paramindex); 745 if (useBatch) { 746 pstmt_upd_null.addBatch(); 747 } else { 748 pstmt_upd_null.execute(); 749 } 750 } else { 751 if (pstmt_del == null) { 752 pstmt_del = sqlconn.prepareStatement(getDeleteElemQuery()); 753 } 754 paramindex = defineIdentifierVal(pName, pstmt_del, 1); 755 paramindex = defineIndexVal(pie, pstmt_del, paramindex); 756 defineElemVal(pie, pstmt_del, paramindex); 757 if (useBatch) { 758 pstmt_del.addBatch(); 759 } else { 760 pstmt_del.execute(); 761 } 762 } 763 break; 764 case PIndexedElem.ELEM_MODIFIED: 765 if (pstmt_upd == null) { 766 pstmt_upd = sqlconn.prepareStatement(getUpdateQuery()); 767 } 768 if (colocated) { 769 paramindex = defineIdentifierVal(pName, pstmt_upd, 1); 770 paramindex = defineIndexVal(pie, pstmt_upd, paramindex); 771 defineElemVal(pie, pstmt_upd, paramindex); 772 } else { 773 paramindex = defineElemVal(pie, pstmt_upd, 1); 774 paramindex = defineIdentifierVal(pName, pstmt_upd, paramindex); 775 defineIndexVal(pie, pstmt_upd, paramindex); 776 } 777 if (useBatch) { 778 pstmt_upd.addBatch(); 779 } else { 780 pstmt_upd.execute(); 781 } 782 break; 783 case PIndexedElem.ELEM_UNMODIFIED: 784 continue; 785 default : 786 throw new PExceptionProtocol( 787 "Unknown modification status for PIndexedElem."); 788 } 789 } 790 if (useBatch) { 791 if (pstmt_ins != null) { 792 pstmt_ins.executeBatch(); 793 } 794 if (pstmt_upd_null != null) { 795 pstmt_upd_null.executeBatch(); 796 } 797 if (pstmt_del != null) { 798 pstmt_del.executeBatch(); 799 } 800 if (pstmt_upd != null) { 801 pstmt_upd.executeBatch(); 802 } 803 } 804 } finally { 805 if (pstmt_ins != null) { 806 pstmt_ins.close(); 807 } 808 if (pstmt_upd_null != null) { 809 pstmt_upd_null.close(); 810 } 811 if (pstmt_del != null) { 812 pstmt_del.close(); 813 } 814 if (pstmt_upd != null) { 815 pstmt_upd.close(); 816 } 817 } 818 } 819 820 831 void defineColumns(FieldDesc[] fields, StringBuffer sqlstring, short start) 832 throws PExceptionProtocol { 833 RdbFieldDesc field; 834 for (short i = start; i < fields.length; i++) { 835 field = (RdbFieldDesc) fields[i]; 836 sqlstring.append(field.columnName); 837 sqlstring.append(" "); 838 try { 839 sqlstring.append(typeConverter.getSqlType((short) field.type 840 .getTypeCode(), true, field.size, field.scale)); 841 } catch (org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException e) { 842 throw new PExceptionProtocol(e, 843 "Impossible to compute the column type of " 844 + field.columnName); 845 } 846 sqlstring.append(", "); 847 } 848 } 849 850 867 boolean defineNames(FieldDesc[] fields, StringBuffer sqlstring, 868 boolean firstname, short start, boolean ext, boolean withtabprefix) { 869 RdbFieldDesc field; 870 for (short i = start; i < fields.length; i++) { 871 field = (RdbFieldDesc) fields[i]; 872 if ((!ext) && field.isRefExtField) { 873 continue; 874 } 875 if (firstname) 876 firstname = false; 877 else 878 sqlstring.append(", "); 879 if (withtabprefix) { 880 sqlstring.append(field.isRefExtField 881 ? refExtTableName 882 : tableName); 883 sqlstring.append('.'); 884 } 885 sqlstring.append(field.columnName); 886 } 887 return firstname; 888 } 889 890 boolean defineNames(FieldDesc[] fields, StringBuffer sqlstring, 891 boolean firstname, short start) { 892 return defineNames(fields, sqlstring, firstname, start, false, true); 893 } 894 895 905 void defineAllNames(StringBuffer sqlstring, byte whichnames) { 906 boolean firstname = true; 907 if ((whichnames & ID) == ID) { 908 firstname = defineNames(identifierFields, sqlstring, firstname, 909 (short) 0); 910 } 911 if ((whichnames & ELEM) == ELEM) { 912 firstname = defineNames(elemFields, sqlstring, firstname, 918 (short) (elemFields.length == 1 ? 0 : 1)); 919 } 920 if ((whichnames & INDEX) == INDEX) { 921 firstname = defineNames(indexFields, sqlstring, firstname, 922 (short) 0); 923 } 924 if ((whichnames & ELEMEXT) == ELEMEXT) { 925 926 } 927 } 928 929 936 void defineIdentifierClause(StringBuffer sqlstring) { 937 if (identifierFields.length == 1) { 938 sqlstring.append("("); 939 sqlstring.append(tableName); 940 sqlstring.append("."); 941 sqlstring.append(((RdbFieldDesc) identifierFields[0]).columnName); 942 sqlstring.append(ASSIGNVAL); 943 sqlstring.append(")"); 944 return; 945 } 946 for (short i = 0; i < identifierFields.length; i++) { 948 if (i != 0) { 949 sqlstring.append(" "); 950 sqlstring.append(SEPAND); 951 sqlstring.append(" "); 952 } 953 sqlstring.append("("); 954 sqlstring.append(tableName); 955 sqlstring.append("."); 956 sqlstring.append(((RdbFieldDesc) identifierFields[i]).columnName); 957 sqlstring.append(ASSIGNVAL); 958 sqlstring.append(")"); 959 } 960 } 961 962 969 void defineIndexClause(StringBuffer sqlstring) { 970 for (short i = 0; i < indexFields.length; i++) { 971 sqlstring.append(" "); 972 sqlstring.append(SEPAND); 973 sqlstring.append(" "); 974 sqlstring.append("("); 975 sqlstring.append(((RdbFieldDesc) indexFields[i]).columnName); 976 sqlstring.append(ASSIGNVAL); 977 sqlstring.append(")"); 978 } 979 } 980 981 988 void defineElemSet(StringBuffer sqlstring) { 989 if (elemFields.length == 1) { 990 sqlstring.append(((RdbFieldDesc) elemFields[0]).columnName); 991 sqlstring.append(ASSIGNVAL); 992 } else if (elemFields.length == 2) { 993 sqlstring.append(((RdbFieldDesc) elemFields[1]).columnName); 994 sqlstring.append(ASSIGNVAL); 995 } else { 996 for (int i = 1; i < elemFields.length; i++) { 998 if (((RdbFieldDesc) elemFields[i]).isRefExtField) { 999 continue; 1000 } 1001 if (i != 1) { 1002 sqlstring.append(SEPSCOL); 1003 } 1004 sqlstring.append(((RdbFieldDesc) elemFields[i]).columnName); 1005 sqlstring.append(ASSIGNVAL); 1006 } 1007 } 1008 } 1009 1010 1017 void defineIdentifierSetNull(StringBuffer sqlstring) { 1018 if (identifierFields.length == 1) { 1019 sqlstring.append(((RdbFieldDesc) identifierFields[0]).columnName); 1020 sqlstring.append(ASSIGN_NULL); 1021 } else { 1022 for (int i = 1; i < identifierFields.length; i++) { 1024 if (i != 1) { 1025 sqlstring.append(SEPSCOL); 1026 } 1027 sqlstring 1028 .append(((RdbFieldDesc) identifierFields[i]).columnName); 1029 sqlstring.append(ASSIGN_NULL); 1030 } 1031 } 1032 } 1033 1034 1041 void defineIdentifierSet(StringBuffer sqlstring) { 1042 if (identifierFields.length == 1) { 1043 sqlstring.append(((RdbFieldDesc) identifierFields[0]).columnName); 1044 sqlstring.append(ASSIGNVAL); 1045 } else { 1046 for (int i = 0; i < identifierFields.length; i++) { 1048 if (i != 0) { 1049 sqlstring.append(SEPSCOL); 1050 } 1051 sqlstring 1052 .append(((RdbFieldDesc) identifierFields[i]).columnName); 1053 sqlstring.append(ASSIGNVAL); 1054 } 1055 } 1056 } 1057 1058 1065 void defineIndexSetNull(StringBuffer sqlstring) { 1066 for (int i = 1; i < indexFields.length; i++) { 1067 sqlstring.append(SEPSCOL); 1068 sqlstring.append(((RdbFieldDesc) indexFields[i]).columnName); 1069 sqlstring.append(ASSIGN_NULL); 1070 } 1071 } 1072 1073 1080 void defineIndexSet(StringBuffer sqlstring) { 1081 for (int i = 1; i < indexFields.length; i++) { 1082 sqlstring.append(SEPSCOL); 1083 sqlstring.append(((RdbFieldDesc) indexFields[i]).columnName); 1084 sqlstring.append(ASSIGNVAL); 1085 } 1086 } 1087 1088 1101 int defineIdentifierVal(PName pname, PreparedStatement pstmt, int paramindex) 1102 throws PException, SQLException , IOException { 1103 if (identifierFields.length == 1) { 1104 if (!pname.codingSupported(CTHelper.ptc2ct(identifierFields[0].type 1105 .getTypeCode()))) { 1106 throw new PExceptionNaming("Coding format not supported: " 1107 + "\n-pname=" 1108 + pname 1109 + "\n-identifier field type: " 1110 + identifierFields[0].type.getJormName() 1111 + "(coding type:" 1112 + CTHelper.ptc2ct(identifierFields[0].type 1113 .getTypeCode()) + ")"); 1114 } 1115 defineValEncode(pname, pstmt, paramindex, identifierFields[0], 1116 ((RdbFieldDesc) identifierFields[0]).columnType); 1117 paramindex++; 1118 } else { 1119 PNameGetter png = (PNameGetter) pname.encodeAbstract(); 1121 for (short i = 0; i < identifierFields.length; i++) { 1122 defineValPng(png, pstmt, paramindex, identifierFields[i], 1123 ((RdbFieldDesc) identifierFields[i]).compositeName); 1124 paramindex++; 1125 } 1126 } 1127 return paramindex; 1128 } 1129 1130 1143 int defineElemVal(PIndexedElem pie, PreparedStatement pstmt, int paramindex) 1144 throws PException, SQLException , IOException { 1145 if (elemFields.length == 1) { 1146 defineValBasic(pie, pstmt, paramindex, elemFields[0].type 1147 .getTypeCode()); 1148 paramindex++; 1149 } else if (elemFields.length == 2) { 1150 defineValEncode(pie.pieGetRefElem(), pstmt, paramindex, 1151 elemFields[1], ((RdbFieldDesc) elemFields[1]).columnType); 1152 paramindex++; 1153 } else { 1154 PNameGetter png = (PNameGetter) pie.pieGetRefElem() 1156 .encodeAbstract(); 1157 for (int i = 1; i < elemFields.length; i++) { 1158 if (((RdbFieldDesc) elemFields[i]).isRefExtField) { 1159 continue; 1160 } 1161 defineValPng(png, pstmt, paramindex, elemFields[i], 1162 ((RdbFieldDesc) elemFields[i]).compositeName); 1163 paramindex++; 1164 } 1165 } 1166 return paramindex; 1167 } 1168 1169 1176 void defineElemClause(StringBuffer sqlstring, boolean isfirst) { 1177 if (elemFields.length == 1) { 1178 if (!isfirst) { 1179 sqlstring.append(" "); 1180 sqlstring.append(SEPAND); 1181 sqlstring.append(" "); 1182 } 1183 sqlstring.append("("); 1184 sqlstring.append(((RdbFieldDesc) elemFields[0]).columnName); 1185 sqlstring.append(ASSIGNVAL); 1186 sqlstring.append(")"); 1187 } else { 1188 for (short i = 1; i < elemFields.length; i++) { 1190 if (((RdbFieldDesc) elemFields[i]).isRefExtField) { 1191 continue; 1192 } 1193 if (!isfirst || i > 1) { 1194 sqlstring.append(" "); 1195 sqlstring.append(SEPAND); 1196 sqlstring.append(" "); 1197 } 1198 sqlstring.append("("); 1199 sqlstring.append(((RdbFieldDesc) elemFields[i]).columnName); 1200 sqlstring.append(ASSIGNVAL); 1201 sqlstring.append(")"); 1202 } 1203 } 1204 } 1205 1206 1219 int defineIndexVal(PIndexedElem pie, PreparedStatement pstmt, int paramindex) 1220 throws PException, SQLException , IOException { 1221 for (int i = 0; i < indexFields.length; i++) { 1222 defineValIndex(pie, pstmt, paramindex, indexFields[i], 1223 ((RdbFieldDesc) indexFields[i]).name); 1224 paramindex++; 1225 } 1226 return paramindex; 1227 } 1228 1229 1234 void defineValBasic(PIndexedElem pie, PreparedStatement pstmt, 1235 int paramindex, int typecode) throws PException, SQLException , 1236 IOException { 1237 switch (typecode) { 1238 case PType.TYPECODE_BOOLEAN: 1239 typeConverter 1240 .setBoolean(pstmt, paramindex, pie.pieGetBooleanElem()); 1241 break; 1242 case PType.TYPECODE_OBJBOOLEAN: 1243 typeConverter.setOboolean(pstmt, paramindex, pie 1244 .pieGetObooleanElem()); 1245 break; 1246 case PType.TYPECODE_BYTE: 1247 typeConverter.setByte(pstmt, paramindex, pie.pieGetByteElem()); 1248 break; 1249 case PType.TYPECODE_OBJBYTE: 1250 typeConverter.setObyte(pstmt, paramindex, pie.pieGetObyteElem()); 1251 break; 1252 case PType.TYPECODE_CHAR: 1253 typeConverter.setChar(pstmt, paramindex, pie.pieGetCharElem()); 1254 break; 1255 case PType.TYPECODE_OBJCHAR: 1256 typeConverter.setOchar(pstmt, paramindex, pie.pieGetOcharElem()); 1257 break; 1258 case PType.TYPECODE_SHORT: 1259 typeConverter.setShort(pstmt, paramindex, pie.pieGetShortElem()); 1260 break; 1261 case PType.TYPECODE_OBJSHORT: 1262 typeConverter.setOshort(pstmt, paramindex, pie.pieGetOshortElem()); 1263 break; 1264 case PType.TYPECODE_INT: 1265 typeConverter.setInt(pstmt, paramindex, pie.pieGetIntElem()); 1266 break; 1267 case PType.TYPECODE_OBJINT: 1268 typeConverter.setOint(pstmt, paramindex, pie.pieGetOintElem()); 1269 break; 1270 case PType.TYPECODE_LONG: 1271 typeConverter.setLong(pstmt, paramindex, pie.pieGetLongElem()); 1272 break; 1273 case PType.TYPECODE_OBJLONG: 1274 typeConverter.setOlong(pstmt, paramindex, pie.pieGetOlongElem()); 1275 break; 1276 case PType.TYPECODE_FLOAT: 1277 typeConverter.setFloat(pstmt, paramindex, pie.pieGetFloatElem()); 1278 break; 1279 case PType.TYPECODE_OBJFLOAT: 1280 typeConverter.setOfloat(pstmt, paramindex, pie.pieGetOfloatElem()); 1281 break; 1282 case PType.TYPECODE_DOUBLE: 1283 typeConverter.setDouble(pstmt, paramindex, pie.pieGetDoubleElem()); 1284 break; 1285 case PType.TYPECODE_OBJDOUBLE: 1286 typeConverter 1287 .setOdouble(pstmt, paramindex, pie.pieGetOdoubleElem()); 1288 break; 1289 case PType.TYPECODE_STRING: 1290 typeConverter.setString(pstmt, paramindex, pie.pieGetStringElem()); 1291 break; 1292 case PType.TYPECODE_DATE: 1293 typeConverter.setDate(pstmt, paramindex, pie.pieGetDateElem(), 1294 ((RdbFieldDesc) elemFields[0]).columnType); 1295 break; 1296 case PType.TYPECODE_BYTEARRAY: 1297 typeConverter.setByteArray(pstmt, paramindex, pie 1298 .pieGetByteArrayElem()); 1299 break; 1300 case PType.TYPECODE_CHARARRAY: 1301 typeConverter.setCharArray(pstmt, paramindex, pie 1302 .pieGetCharArrayElem()); 1303 break; 1304 case PType.TYPECODE_BIGDECIMAL: 1305 typeConverter.setBigDecimal(pstmt, paramindex, pie 1306 .pieGetBigDecimalElem()); 1307 break; 1308 case PType.TYPECODE_SERIALIZED: 1309 typeConverter.setSerialized(pstmt, paramindex, pie 1310 .pieGetSerializedElem()); 1311 break; 1312 default: 1313 throw new PExceptionTyping("Wrong field type."); 1314 } 1315 } 1316 1317 1322 void defineValEncode(PName pname, PreparedStatement pstmt, int paramindex, 1323 FieldDesc fd, String sqlcoltype) throws PException, SQLException , 1324 IOException { 1325 switch (fd.type.getTypeCode()) { 1326 case PType.TYPECODE_BYTE: 1327 typeConverter.setByte(pstmt, paramindex, pname.encodeByte()); 1328 break; 1329 case PType.TYPECODE_OBJBYTE: 1330 typeConverter.setObyte(pstmt, paramindex, pname.encodeObyte()); 1331 break; 1332 case PType.TYPECODE_CHAR: 1333 typeConverter.setChar(pstmt, paramindex, pname.encodeChar()); 1334 break; 1335 case PType.TYPECODE_OBJCHAR: 1336 typeConverter.setOchar(pstmt, paramindex, pname.encodeOchar()); 1337 break; 1338 case PType.TYPECODE_SHORT: 1339 typeConverter.setShort(pstmt, paramindex, pname.encodeShort()); 1340 break; 1341 case PType.TYPECODE_OBJSHORT: 1342 typeConverter.setOshort(pstmt, paramindex, pname.encodeOshort()); 1343 break; 1344 case PType.TYPECODE_INT: 1345 typeConverter.setInt(pstmt, paramindex, pname.encodeInt()); 1346 break; 1347 case PType.TYPECODE_OBJINT: 1348 typeConverter.setOint(pstmt, paramindex, pname.encodeOint()); 1349 break; 1350 case PType.TYPECODE_LONG: 1351 typeConverter.setLong(pstmt, paramindex, pname.encodeLong()); 1352 break; 1353 case PType.TYPECODE_OBJLONG: 1354 typeConverter.setOlong(pstmt, paramindex, pname.encodeOlong()); 1355 break; 1356 case PType.TYPECODE_STRING: 1357 typeConverter.setString(pstmt, paramindex, pname.encodeString()); 1358 break; 1359 case PType.TYPECODE_DATE: 1360 typeConverter.setDate(pstmt, paramindex, pname.encodeDate(), 1361 ((RdbFieldDesc) fd).columnType); 1362 break; 1363 case PType.TYPECODE_BYTEARRAY: 1364 typeConverter.setByteArray(pstmt, paramindex, pname.encode()); 1365 break; 1366 case PType.TYPECODE_CHARARRAY: 1367 typeConverter.setCharArray(pstmt, paramindex, pname 1368 .encodeCharArray()); 1369 break; 1370 default: 1371 throw new PExceptionTyping("Wrong field type."); 1372 } 1373 } 1374 1375 1380 void defineValPng(PNameGetter png, PreparedStatement pstmt, int paramindex, 1381 FieldDesc fd, String compfield) throws PException, SQLException , 1382 IOException { 1383 1384 switch (fd.type.getTypeCode()) { 1385 case PType.TYPECODE_BYTE: 1386 typeConverter.setByte(pstmt, paramindex, png.pngetByteField( 1387 compfield, null)); 1388 break; 1389 case PType.TYPECODE_OBJBYTE: 1390 typeConverter.setObyte(pstmt, paramindex, png.pngetObyteField( 1391 compfield, null)); 1392 break; 1393 case PType.TYPECODE_CHAR: 1394 typeConverter.setChar(pstmt, paramindex, png.pngetCharField( 1395 compfield, null)); 1396 break; 1397 case PType.TYPECODE_OBJCHAR: 1398 typeConverter.setOchar(pstmt, paramindex, png.pngetOcharField( 1399 compfield, null)); 1400 break; 1401 case PType.TYPECODE_SHORT: 1402 typeConverter.setShort(pstmt, paramindex, png.pngetShortField( 1403 compfield, null)); 1404 break; 1405 case PType.TYPECODE_OBJSHORT: 1406 typeConverter.setOshort(pstmt, paramindex, png.pngetOshortField( 1407 compfield, null)); 1408 break; 1409 case PType.TYPECODE_INT: 1410 typeConverter.setInt(pstmt, paramindex, png.pngetIntField( 1411 compfield, null)); 1412 break; 1413 case PType.TYPECODE_OBJINT: 1414 typeConverter.setOint(pstmt, paramindex, png.pngetOintField( 1415 compfield, null)); 1416 break; 1417 case PType.TYPECODE_LONG: 1418 typeConverter.setLong(pstmt, paramindex, png.pngetLongField( 1419 compfield, null)); 1420 break; 1421 case PType.TYPECODE_OBJLONG: 1422 typeConverter.setOlong(pstmt, paramindex, png.pngetOlongField( 1423 compfield, null)); 1424 break; 1425 case PType.TYPECODE_STRING: 1426 typeConverter.setString(pstmt, paramindex, png.pngetStringField( 1427 compfield, null)); 1428 break; 1429 case PType.TYPECODE_DATE: 1430 typeConverter.setDate(pstmt, paramindex, png.pngetDateField( 1431 compfield, null), ((RdbFieldDesc) fd).columnType); 1432 break; 1433 case PType.TYPECODE_BYTEARRAY: 1434 typeConverter.setByteArray(pstmt, paramindex, png 1435 .pngetByteArrayField(compfield, null)); 1436 break; 1437 case PType.TYPECODE_CHARARRAY: 1438 typeConverter.setCharArray(pstmt, paramindex, png 1439 .pngetCharArrayField(compfield, null)); 1440 break; 1441 default: 1442 throw new PExceptionTyping("Wrong field type."); 1443 } 1444 } 1445 1446 1451 void defineValIndex(PIndexedElem pie, PreparedStatement pstmt, 1452 int paramindex, FieldDesc fd, String indexfield) throws PException, 1453 SQLException , IOException { 1454 switch (fd.type.getTypeCode()) { 1455 case PType.TYPECODE_BYTE: 1456 typeConverter.setByte(pstmt, paramindex, pie 1457 .pieGetByteIndexField(indexfield)); 1458 break; 1459 case PType.TYPECODE_OBJBYTE: 1460 typeConverter.setObyte(pstmt, paramindex, pie 1461 .pieGetObyteIndexField(indexfield)); 1462 break; 1463 case PType.TYPECODE_CHAR: 1464 typeConverter.setChar(pstmt, paramindex, pie 1465 .pieGetCharIndexField(indexfield)); 1466 break; 1467 case PType.TYPECODE_OBJCHAR: 1468 typeConverter.setOchar(pstmt, paramindex, pie 1469 .pieGetOcharIndexField(indexfield)); 1470 break; 1471 case PType.TYPECODE_SHORT: 1472 typeConverter.setShort(pstmt, paramindex, pie 1473 .pieGetShortIndexField(indexfield)); 1474 break; 1475 case PType.TYPECODE_OBJSHORT: 1476 typeConverter.setOshort(pstmt, paramindex, pie 1477 .pieGetOshortIndexField(indexfield)); 1478 break; 1479 case PType.TYPECODE_INT: 1480 typeConverter.setInt(pstmt, paramindex, pie 1481 .pieGetIntIndexField(indexfield)); 1482 break; 1483 case PType.TYPECODE_OBJINT: 1484 typeConverter.setOint(pstmt, paramindex, pie 1485 .pieGetOintIndexField(indexfield)); 1486 break; 1487 case PType.TYPECODE_LONG: 1488 typeConverter.setLong(pstmt, paramindex, pie 1489 .pieGetLongIndexField(indexfield)); 1490 break; 1491 case PType.TYPECODE_OBJLONG: 1492 typeConverter.setOlong(pstmt, paramindex, pie 1493 .pieGetOlongIndexField(indexfield)); 1494 break; 1495 case PType.TYPECODE_STRING: 1496 typeConverter.setString(pstmt, paramindex, pie 1497 .pieGetStringIndexField(indexfield)); 1498 break; 1499 case PType.TYPECODE_DATE: 1500 typeConverter.setDate(pstmt, paramindex, pie 1501 .pieGetDateIndexField(indexfield), 1502 ((RdbFieldDesc) fd).columnType); 1503 break; 1504 1515 default: 1516 throw new PExceptionTyping("Wrong field type."); 1517 } 1518 } 1519 1520 1530 public void init(PMappingCallback mapper, MetaObject metaclass) 1531 throws PException { 1532 super.init(mapper, metaclass); 1533 } 1534 1535 1560 void passValuesToAccessor(Object conn, ResultSet rs, 1561 PGenClassAccessor accessor, PNameGetter png, RdbTupleCollection tc) throws PException { 1562 PIndexedElem pie = accessor.createPIndexedElem(); 1563 if (logger.isLoggable(BasicLevel.DEBUG)) 1564 logger.log(BasicLevel.DEBUG, "passValuesToAccessor"); 1565 try { 1566 int indexStart = (elemFields.length == 1 1571 ? 2 1572 : elemFields.length) 1573 + nbPrefetchCol; 1574 for (short i = 0; i < indexFields.length; i++) { 1575 if (logger.isLoggable(BasicLevel.DEBUG)) 1576 logger.log(BasicLevel.DEBUG, "index[" + i + "]:" 1577 + indexFields[i].name); 1578 switch (indexFields[i].type.getTypeCode()) { 1579 case PType.TYPECODE_BYTE: 1580 pie.pieSetByteIndexField(indexFields[i].name, typeConverter 1581 .getByte(rs, i + indexStart, (byte) -1)); 1582 break; 1583 case PType.TYPECODE_OBJBYTE: 1584 pie.pieSetObyteIndexField(indexFields[i].name, 1585 typeConverter.getObyte(rs, i + indexStart, null)); 1586 break; 1587 case PType.TYPECODE_CHAR: 1588 pie.pieSetCharIndexField(indexFields[i].name, typeConverter 1589 .getChar(rs, i + indexStart, (char) 0)); 1590 break; 1591 case PType.TYPECODE_OBJCHAR: 1592 pie.pieSetOcharIndexField(indexFields[i].name, 1593 typeConverter.getOchar(rs, i + indexStart, null)); 1594 break; 1595 case PType.TYPECODE_SHORT: 1596 pie.pieSetShortIndexField(indexFields[i].name, 1597 typeConverter.getShort(rs, i + indexStart, 1598 (short) -1)); 1599 break; 1600 case PType.TYPECODE_OBJSHORT: 1601 pie.pieSetOshortIndexField(indexFields[i].name, 1602 typeConverter.getOshort(rs, i + indexStart, null)); 1603 break; 1604 case PType.TYPECODE_INT: 1605 pie.pieSetIntIndexField(indexFields[i].name, typeConverter 1606 .getInt(rs, i + indexStart, -1)); 1607 break; 1608 case PType.TYPECODE_OBJINT: 1609 pie.pieSetOintIndexField(indexFields[i].name, typeConverter 1610 .getOint(rs, i + indexStart, null)); 1611 break; 1612 case PType.TYPECODE_LONG: 1613 pie.pieSetLongIndexField(indexFields[i].name, typeConverter 1614 .getLong(rs, i + indexStart, ((long) -1))); 1615 break; 1616 case PType.TYPECODE_OBJLONG: 1617 pie.pieSetOlongIndexField(indexFields[i].name, 1618 typeConverter.getOlong(rs, i + indexStart, null)); 1619 break; 1620 case PType.TYPECODE_STRING: 1621 pie.pieSetStringIndexField(indexFields[i].name, 1622 typeConverter.getString(rs, i + indexStart, null)); 1623 break; 1624 case PType.TYPECODE_DATE: 1625 pie.pieSetDateIndexField(indexFields[i].name, typeConverter 1626 .getDate(rs, i + indexStart, 1627 ((RdbFieldDesc) indexFields[i]).columnType, 1628 null)); 1629 break; 1630 default: 1631 throw new PExceptionProtocol( 1632 "Wrong type for an index field of a GenClass"); 1633 } 1634 } 1635 int elemStart = 1 + nbPrefetchCol; 1636 if (logger.isLoggable(BasicLevel.DEBUG)) 1637 logger.log(BasicLevel.DEBUG, "element type:" 1638 + elemFields[0].type.getJormName()); 1639 switch (elemFields[0].type.getTypeCode()) { 1640 case PType.TYPECODE_BOOLEAN: 1641 pie.pieSetBooleanElem(typeConverter.getBoolean(rs, elemStart, false)); 1642 break; 1643 case PType.TYPECODE_OBJBOOLEAN: 1644 pie.pieSetObooleanElem(typeConverter.getOboolean(rs, elemStart, null)); 1645 break; 1646 case PType.TYPECODE_CHAR: 1647 pie.pieSetCharElem(typeConverter.getChar(rs, elemStart, (char) 0)); 1648 break; 1649 case PType.TYPECODE_OBJCHAR: 1650 pie.pieSetOcharElem(typeConverter.getOchar(rs, elemStart, null)); 1651 break; 1652 case PType.TYPECODE_BYTE: 1653 pie.pieSetByteElem(typeConverter.getByte(rs, elemStart, (byte) -1)); 1654 break; 1655 case PType.TYPECODE_OBJBYTE: 1656 pie.pieSetObyteElem(typeConverter.getObyte(rs, elemStart, null)); 1657 break; 1658 case PType.TYPECODE_SHORT: 1659 pie.pieSetShortElem(typeConverter.getShort(rs, elemStart, (short) -1)); 1660 break; 1661 case PType.TYPECODE_OBJSHORT: 1662 pie.pieSetOshortElem(typeConverter.getOshort(rs, elemStart, null)); 1663 break; 1664 case PType.TYPECODE_INT: 1665 pie.pieSetIntElem(typeConverter.getInt(rs, elemStart, -1)); 1666 break; 1667 case PType.TYPECODE_OBJINT: 1668 pie.pieSetOintElem(typeConverter.getOint(rs, elemStart, null)); 1669 break; 1670 case PType.TYPECODE_LONG: 1671 pie.pieSetLongElem(typeConverter.getLong(rs, elemStart, ((long) -1))); 1672 break; 1673 case PType.TYPECODE_OBJLONG: 1674 pie.pieSetOlongElem(typeConverter.getOlong(rs, elemStart, null)); 1675 break; 1676 case PType.TYPECODE_FLOAT: 1677 pie.pieSetFloatElem(typeConverter 1678 .getFloat(rs, elemStart, ((float) 0.0))); 1679 break; 1680 case PType.TYPECODE_OBJFLOAT: 1681 pie.pieSetOfloatElem(typeConverter.getOfloat(rs, elemStart, null)); 1682 break; 1683 case PType.TYPECODE_DOUBLE: 1684 pie.pieSetDoubleElem(typeConverter.getDouble(rs, elemStart, 1685 ((double) 0.0))); 1686 break; 1687 case PType.TYPECODE_OBJDOUBLE: 1688 pie.pieSetOdoubleElem(typeConverter.getOdouble(rs, elemStart, null)); 1689 break; 1690 case PType.TYPECODE_STRING: 1691 pie.pieSetStringElem(typeConverter.getString(rs, elemStart, null)); 1692 break; 1693 case PType.TYPECODE_DATE: 1694 pie.pieSetDateElem(typeConverter.getDate(rs, elemStart, 1695 ((RdbFieldDesc) elemFields[0]).columnType, null)); 1696 break; 1697 case PType.TYPECODE_CHARARRAY: 1698 pie.pieSetCharArrayElem( 1699 typeConverter.getCharArray(rs, elemStart, null)); 1700 break; 1701 case PType.TYPECODE_BYTEARRAY: 1702 pie.pieSetByteArrayElem( 1703 typeConverter.getByteArray(rs, elemStart, null)); 1704 break; 1705 case PType.TYPECODE_SERIALIZED: 1706 pie.pieSetSerializedElem( 1707 typeConverter.getSerialized(rs, elemStart, null)); 1708 break; 1709 case PType.TYPECODE_BIGDECIMAL: 1710 pie.pieSetBigDecimalElem( 1711 typeConverter.getBigDecimal(rs, elemStart, null)); 1712 break; 1713 case PType.TYPECODE_REFERENCE: 1714 if (logger.isLoggable(BasicLevel.DEBUG)) 1715 logger.log(BasicLevel.DEBUG, "reference size:" 1716 + (elemFields.length - 1)); 1717 if (elemFields.length == 2) { 1718 pie.pieSetRefElem(decodeSingle(elemFields[1], rs, elemStart)); 1719 } else { 1720 pie.pieSetRefElem(elemNameCoder.decodeAbstract(png, null)); 1721 } 1722 break; 1723 default: 1724 throw new PExceptionProtocol( 1725 "Wrong type for an element field of a GenClass"); 1726 } 1727 } catch (SQLException e) { 1728 throw new PExceptionIO(e, "SQL problem while reading an element."); 1729 } catch (IOException e) { 1730 throw new PExceptionIO(e, 1731 "Stream IO problem while reading an element."); 1732 } catch (ClassNotFoundException e) { 1733 throw new PExceptionIO(e, 1734 "Deserialization problem while reading an element."); 1735 } 1736 if (tc != null) { 1737 try { 1740 tc.getPrefetchBuffer().addPrefetchTuple(); 1741 } catch (MedorException e1) { 1742 throw new PException(e1); 1743 } 1744 } 1745 accessor.paAdd(pie, conn); 1746 } 1747 1748 1755 PNameGetter getPNameGetter(ResultSet rs) { 1756 if (elemFields.length < 2) { 1757 return null; 1758 } 1759 if (elemFields.length == 2) { 1760 if (elemFields[1].compositeName== null 1761 || elemFields[1].compositeName.length()==0) { 1762 return null; 1763 } 1764 } 1765 return new RdbGenClassNameGetter(null, rs, elemFields, nbPrefetchCol, typeConverter); 1766 } 1767 1768 PName decodeSingle(FieldDesc fd, ResultSet resultSet, int idx) 1769 throws PExceptionProtocol, PExceptionNaming, SQLException { 1770 switch (fd.type.getTypeCode()) { 1771 case PType.TYPECODE_BYTE: 1772 return elemNameCoder.decodeByte(typeConverter.getByte(resultSet, 1773 idx, ((byte) -1))); 1774 case PType.TYPECODE_CHAR: 1775 return elemNameCoder.decodeChar(typeConverter.getChar(resultSet, 1776 idx, (char) 0)); 1777 case PType.TYPECODE_SHORT: 1778 return elemNameCoder.decodeShort(typeConverter.getShort(resultSet, 1779 idx, (short) -1)); 1780 case PType.TYPECODE_INT: 1781 return elemNameCoder.decodeInt(typeConverter.getInt(resultSet, idx, 1782 -1)); 1783 case PType.TYPECODE_LONG: 1784 return elemNameCoder.decodeLong(typeConverter.getLong(resultSet, 1785 idx, ((long) -1))); 1786 case PType.TYPECODE_OBJBYTE: 1787 return elemNameCoder.decodeObyte(typeConverter.getObyte(resultSet, 1788 idx, null)); 1789 case PType.TYPECODE_OBJCHAR: 1790 return elemNameCoder.decodeOchar(typeConverter.getOchar(resultSet, 1791 idx, null)); 1792 case PType.TYPECODE_OBJSHORT: 1793 return elemNameCoder.decodeOshort(typeConverter.getOshort( 1794 resultSet, idx, null)); 1795 case PType.TYPECODE_OBJINT: 1796 return elemNameCoder.decodeOint(typeConverter.getOint(resultSet, 1797 idx, null)); 1798 case PType.TYPECODE_OBJLONG: 1799 return elemNameCoder.decodeOlong(typeConverter.getOlong(resultSet, 1800 idx, null)); 1801 case PType.TYPECODE_STRING: 1802 return elemNameCoder.decodeString(typeConverter.getString( 1803 resultSet, idx, null)); 1804 case PType.TYPECODE_DATE: 1805 return elemNameCoder.decodeDate(typeConverter.getDate(resultSet, 1806 idx, ((RdbFieldDesc) fd).columnType, null)); 1807 case PType.TYPECODE_BYTEARRAY: 1808 return elemNameCoder.decode(typeConverter.getByteArray(resultSet, 1809 idx, null)); 1810 case PType.TYPECODE_CHARARRAY: 1811 return elemNameCoder.decodeCharArray(typeConverter.getCharArray( 1812 resultSet, idx, null)); 1813 default: 1814 throw new PExceptionProtocol( 1815 "The jorm naming does not manage this type " 1816 + fd.type.getJormName()); 1817 } 1818 } 1819 1820 public HashMap getAssociationTable() { 1821 return null; 1822 } 1823 1824 public void addAssociation(PClassMapping targetClass, int[] indexes) { 1825 } 1826 1827 public int[] getIndexesTable(PClassMapping targetClass) { 1828 return null; 1829 } 1830 1831 public PName resolve(Object conn, PName pname) throws PException { 1832 return null; 1833 } 1834 1835 public boolean match(Object obj, boolean b) throws PException{ 1836 return false; 1837 } 1838 1839 public PName getDecodedPName(TupleCollection tc, PName pname, boolean b) throws PException { 1840 return null; 1841 } 1842 1843 public void init(PMappingStructuresManager pmsm) throws PException { 1844 } 1845 1846 public void classDefined(PMappingStructuresManager pmsm) throws PException { 1847 } 1848} 1849 | Popular Tags |