1 23 24 package org.objectweb.jorm.mapper.rdb.generator; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.generator.lib.CommonHelper; 28 import org.objectweb.jorm.mapper.rdb.metainfo.RdbClassMapping; 29 import org.objectweb.jorm.mapper.rdb.metainfo.RdbFilter; 30 import org.objectweb.jorm.mapper.rdb.metainfo.RdbMapping; 31 import org.objectweb.jorm.mapper.rdb.metainfo.RdbPrimitiveElementMapping; 32 import org.objectweb.jorm.metainfo.api.Class; 33 import org.objectweb.jorm.metainfo.api.ClassMapping; 34 import org.objectweb.jorm.metainfo.api.ParentClassMapping; 35 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 36 import org.objectweb.jorm.naming.api.PNameGetter; 37 import org.objectweb.medor.expression.api.Expression; 38 import org.objectweb.medor.expression.api.ExpressionException; 39 import org.objectweb.medor.expression.api.ParameterOperand; 40 import org.objectweb.medor.expression.api.VariableOperand; 41 import org.objectweb.medor.expression.converter.rdb.Expression2WhereClauseImpl; 42 import org.objectweb.medor.expression.lib.ExpressionPrinter; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 import org.objectweb.util.monolog.api.Logger; 45 46 import java.util.ArrayList ; 47 import java.util.Collection ; 48 import java.util.Collections ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 import java.util.Map ; 53 54 60 public class RdbGenInfos { 61 private static final String COLUMN = ", "; 62 67 public Map tables = new HashMap (); 68 72 public RdbGenTable mainTable = null; 73 79 public List readableColumns = new ArrayList (); 80 83 public RdbGenRef genId = null; 84 87 public RdbMapping mapping = null; 88 93 public Map genRefs = new HashMap (); 94 95 96 String filterExpr = null; 97 98 public Logger logger; 99 public boolean debug = false; 100 101 public static CommonHelper commonHelper = new CommonHelper(); 102 103 104 public RdbGenInfos() { 105 } 106 107 public Map getTables() { 108 return tables; 109 } 110 111 public RdbGenTable getReferenceTable() { 112 return mainTable; 113 } 114 115 public List getReadableColumns() { 116 return readableColumns; 117 } 118 119 public RdbGenRef getGenId() { 120 return genId; 121 } 122 123 public Map getGenRefs() { 124 return genRefs; 125 } 126 127 133 public List getTableCompositeGenRefs(RdbGenTable rgt) { 134 return getTableCompositeGenRefs(rgt, null); 135 } 136 137 public List getTableCompositeGenRefs(RdbGenTable rgt, RdbGenJoin rgj) { 138 ArrayList res = new ArrayList (); 139 Iterator it = genRefs.values().iterator(); 140 while (it.hasNext()) { 141 RdbGenRef rgr = (RdbGenRef) it.next(); 142 if (rgr.refColumn == null && rgr.join == rgj && !res.contains(rgr)) { 143 res.add(rgr); 144 } 145 } 146 return res; 147 } 148 149 156 public int getColumnPosition(RdbGenColumn irgc) { 157 return getColumnPosition(irgc, null); 158 } 159 160 public int getColumnPosition(RdbGenColumn irgc, RdbGenJoin rgj) { 161 int idx = 1; 162 if (debug) { 163 logger.log(BasicLevel.DEBUG, "getColumnPosition(" + irgc.columnName + ", rgj=" + rgj +")"); 164 } 165 for (int i = 0; i < readableColumns.size(); i++) { 166 RdbGenColumn rgc = (RdbGenColumn) readableColumns.get(i); 167 if (debug) { 168 logger.log(BasicLevel.DEBUG, "\tcurrent column: " + rgc.columnName); 169 logger.log(BasicLevel.DEBUG, "\tpirgc=" + irgc.toString() + " - current=" + rgc.toString()); 170 } 171 if (rgc.equals(irgc)) { 172 if (rgj != null) { 173 if (debug) { 174 logger.log(BasicLevel.DEBUG, "\t\tadd join idx: " + rgj.joinIdx); 175 } 176 idx += rgj.joinIdx; 177 } 178 if (debug) { 179 logger.log(BasicLevel.DEBUG, "\treturn " + idx); 180 } 181 return idx; 182 } else if (rgc.joins != null) { 183 if (debug) { 184 logger.log(BasicLevel.DEBUG, "\t\tcurrent has joins: " + rgc.joins.size()); 185 } 186 idx += rgc.joins.size(); 187 } else { 188 if (debug) { 189 logger.log(BasicLevel.DEBUG, "\t\tidx++"); 190 } 191 idx++; 192 } 193 if (debug) { 194 logger.log(BasicLevel.DEBUG, "\t\tidx=" + idx); 195 } 196 } 197 return -1; 198 } 199 200 205 public String getExistStatement() { 206 StringBuffer sb = new StringBuffer ("SELECT "); 207 String sep = ""; 208 Iterator it; 209 String tabname = null; 210 if (genId.refColumn != null) { 211 sb.append(sep); 212 appendFQColName(sb, genId.refColumn); 213 tabname = genId.refColumn.table.tableName; 214 } else { 215 it = genId.cnFieldColumns.keySet().iterator(); 216 while (it.hasNext()) { 217 String cnfn = (String ) it.next(); 218 RdbGenColumn rgc = (RdbGenColumn) genId.cnFieldColumns.get(cnfn); 219 sb.append(sep); 220 sep = COLUMN; 221 appendFQColName(sb, rgc); 222 if (tabname == null) { 223 tabname = rgc.table.tableName; 224 } 225 } 226 } 227 sb.append(" FROM "); 228 sb.append(tabname); 229 sb.append(" WHERE "); 230 sep = ""; 231 if (genId.refColumn != null) { 232 sb.append(sep); 233 appendFQColName(sb, genId.refColumn); 234 sb.append(" = ?"); 235 } else { 236 it = genId.cnFieldColumns.keySet().iterator(); 237 while (it.hasNext()) { 238 String cnfn = (String ) it.next(); 239 RdbGenColumn rgc = (RdbGenColumn) genId.cnFieldColumns.get(cnfn); 240 sb.append(sep); 241 sep = " AND "; 242 appendFQColName(sb, rgc); 243 sb.append(" = ?"); 244 } 245 } 246 return sb.toString(); 247 } 248 249 252 public String getReadSelectFields() { 253 StringBuffer select = new StringBuffer (); 254 String sep = ""; 255 for (int i = 0; i < readableColumns.size(); i++) { 256 RdbGenColumn rgc = (RdbGenColumn) readableColumns.get(i); 257 if (rgc.joins != null && rgc.joins.size() > 1) { 258 String [] s = new String [rgc.joins.size()]; 259 for (int j = 0; j < rgc.joins.size(); j++) { 260 RdbGenJoin rgj = (RdbGenJoin) rgc.joins.get(j); 261 s[rgj.joinIdx] = getFQColName(rgc, "_J" + rgj.joinIdx); 262 } 263 for (int j = 0; j < s.length; j++) { 264 select.append(sep); 265 sep = COLUMN; 266 select.append(s[j]); 267 } 268 } else if (rgc.constant == null) { select.append(sep); 270 sep = COLUMN; 271 appendFQColName(select, rgc); 272 } 273 } 274 return select.toString(); 275 } 276 277 public String getReadSelectFieldsAsColArray() { 278 StringBuffer select = new StringBuffer ("new String[]{"); 279 String sep = ""; 280 for (int i = 0; i < readableColumns.size(); i++) { 281 RdbGenColumn rgc = (RdbGenColumn) readableColumns.get(i); 282 if (rgc.joins != null && rgc.joins.size() > 1) { 283 String [] s = new String [rgc.joins.size()]; 284 for (int j = 0; j < rgc.joins.size(); j++) { 285 RdbGenJoin rgj = (RdbGenJoin) rgc.joins.get(j); 286 s[rgj.joinIdx] = getFQColName(rgc, "_J" + rgj.joinIdx); 287 } 288 for (int j = 0; j < s.length; j++) { 289 select.append(sep); 290 sep = COLUMN; 291 select.append("\"").append(s[j]).append("\""); 292 } 293 } else if (rgc.constant == null) { select.append(sep); 295 sep = COLUMN; 296 select.append("\""); 297 appendFQColName(select, rgc); 298 select.append("\""); 299 } 300 } 301 select.append("}"); 302 return select.toString(); 303 } 304 305 306 307 310 public List getConstantVisibleColumns() { 311 List constantCols = new ArrayList (); 312 for (int i = 0; i < readableColumns.size(); i++) { 313 RdbGenColumn rgc = (RdbGenColumn) readableColumns.get(i); 314 if (rgc.constant != null && !rgc.hiddenField) { 315 constantCols.add(rgc); 316 } 317 } 318 return constantCols; 319 } 320 321 324 public int getReadSelectNbFields() { 325 int res = 0; 326 for (int i = 0; i < readableColumns.size(); i++) { 327 RdbGenColumn rgc = (RdbGenColumn) readableColumns.get(i); 328 if (rgc.joins != null && rgc.joins.size() > 1) { 329 String [] s = new String [rgc.joins.size()]; 330 for (int j = 0; j < rgc.joins.size(); j++) { 331 RdbGenJoin rgj = (RdbGenJoin) rgc.joins.get(j); 332 s[rgj.joinIdx] = getFQColName(rgc, "_J" + rgj.joinIdx); 333 } 334 for (int j = 0; j < s.length; j++) { 335 res++; 336 } 337 } else if (rgc.constant == null) { res++; 339 } 340 } 341 return res; 342 } 343 344 349 public List getReadFromTables() { 350 ArrayList al = new ArrayList (); 351 al.add("JoinedTable jt = new JoinedTable(\"" 352 + mainTable.tableName + "\");"); 353 al.add("JoinedTable.Join jtj = null;"); 354 Iterator it = tables.values().iterator(); 355 while (it.hasNext()) { 356 RdbGenTable rgt = (RdbGenTable) it.next(); 357 if (rgt.joins != null) { 358 boolean multiJoin = rgt.joins.size() > 1; 359 for (int i = 0; i < rgt.joins.size(); i++) { 360 RdbGenJoin join = (RdbGenJoin) rgt.joins.get(i); 361 String tableSuffix = null; 362 if (multiJoin) { 363 tableSuffix = "_J" + ((RdbGenJoin) rgt.joins.get(i)).joinIdx; 364 al.add("jtj = jt.createChildren(\"" 365 + rgt.tableName + "\", \"" 366 + rgt.tableName + tableSuffix + "\");"); 367 } else { 368 al.add("jtj = jt.createChildren(\"" 369 + rgt.tableName + "\");"); 370 } 371 for (int j = 0; j < join.joinColumnsInMain.size(); j++) { 372 al.add("jtj.addJoinColumn(\"" 373 + ((RdbGenColumn) join.joinColumnsInMain.get(j)).columnName 374 + "\", \"" 375 + ((RdbGenColumn) join.joinColumnsInExt.get(j)).columnName 376 + "\");"); 377 } 378 } 379 } 380 } 381 return al; 382 } 383 384 385 public String getReadWhereClause() { 386 StringBuffer where = new StringBuffer (); 388 String sep = ""; 389 if (genId.refColumn != null) { 391 where.append(sep); 392 appendFQColName(where, genId.refColumn); 393 where.append(" = ?"); 394 } else { 395 Iterator it = genId.cnFieldColumns.keySet().iterator(); 396 while (it.hasNext()) { 397 String cnfn = (String ) it.next(); 398 RdbGenColumn rgc = (RdbGenColumn) genId.cnFieldColumns.get(cnfn); 399 where.append(sep); 400 sep = " AND "; 401 appendFQColName(where, rgc); 402 where.append(" = ?"); 403 } 404 } 405 return where.toString(); 406 } 407 408 413 public String getPNameIteratorSelectClause() { 414 StringBuffer select = new StringBuffer (); 415 if (genId.getCnFieldColumns() == null) { 416 appendFQColName(select, genId.getRefColumn()); 417 } else { 418 String sep = ""; 419 Iterator it = genId.getCnFieldColumns().keySet().iterator(); 420 while (it.hasNext()) { 421 RdbGenColumn rgc = (RdbGenColumn) genId.getCnFieldColumns().get(it.next()); 422 if (!colInGenId(rgc)) { 423 continue; 424 } 425 select.append(sep); 426 sep = COLUMN; 427 appendFQColName(select, rgc); 428 } 429 } 430 return select.toString(); 431 } 432 433 434 public String getPNameIteratorSelectClauseWithPrefetch() { 435 StringBuffer select = new StringBuffer (); 436 select.append(getReadSelectFields()); 438 if (genId.getCnFieldColumns() == null) { 439 select.append(COLUMN); 440 appendFQColName(select, genId.getRefColumn()); 441 select.append("\" + adapter.getColumnAliasExpr(\"pk0\") + \""); 442 } else { 443 Iterator it = genId.getCnFieldColumns().keySet().iterator(); 444 int i = 0; 445 while (it.hasNext()) { 446 RdbGenColumn rgc = (RdbGenColumn) genId.getCnFieldColumns().get(it.next()); 447 if (!colInGenId(rgc)) { 448 continue; 449 } 450 select.append(COLUMN); 451 appendFQColName(select, rgc); 452 select.append("\" + adapter.getColumnAliasExpr(\"pk"); 453 select.append(i); 454 select.append("\") + \""); 455 } 456 } 457 return select.toString(); 458 } 459 460 461 464 public Expression getFilterMedorExpression() { 465 Expression exp = null; 466 try { 467 RdbFilter rdbFilter = ((RdbClassMapping) mapping.getClassMapping()) 469 .getRdbFilter(); 470 if (rdbFilter != null) { exp = rdbFilter.getExpression(); 472 if (debug) { 473 logger.log(BasicLevel.DEBUG, "Rdb filter specified for the class " 474 + mapping.getClassMapping().getJormClass().getFQName() 475 + ", expression=" + ExpressionPrinter.e2str(exp)); 476 } 477 } else { 478 if (((RdbClassMapping) mapping.getClassMapping()).inheritsStructures() 481 || !mapping.getClassMapping().getJormClass().getSubClasses().isEmpty()) { 482 exp = ((RdbClassMapping) mapping.getClassMapping()) 484 .getMappingFilterFromNamingFilter(); 485 if (debug) { 486 logger.log(BasicLevel.DEBUG, "Class " 487 + mapping.getClassMapping().getJormClass().getFQName() 488 + " has a filtered mapping and the filter is computed from the naming filter:" 489 + ExpressionPrinter.e2str(exp)); 490 } 491 } else if (debug) { 492 logger.log(BasicLevel.DEBUG, "Class " 493 + mapping.getClassMapping().getJormClass().getFQName() 494 + " has no filter"); 495 } 496 } 497 return exp; 498 } catch (ExpressionException e) { 499 throw new InternalError ("Cannot compute filter expression: " + e); 500 } 501 } 502 503 506 public String getFilterExpression() { 507 if (filterExpr != null) { 508 return filterExpr; 509 } 510 try { 511 Expression exp = getFilterMedorExpression(); 512 if (exp == null) { 513 filterExpr = ""; 514 } else { 515 StringBuffer sb = new StringBuffer (); 516 new JormExpression2WhereClause() 517 .convertExp2WhereClauseBuilder(exp, "adapter", sb); 518 filterExpr = sb.toString(); 519 } 520 return filterExpr; 521 } catch (ExpressionException e) { 522 throw new InternalError ("Cannot compute filter expression: " + e); 523 } 524 } 525 526 String pnameIteratorWhereClause = null; 527 528 public String getPNameIteratorWhereClause() throws ExpressionException { 529 if (pnameIteratorWhereClause == null) { 530 pnameIteratorWhereClause = getFilterExpression(); 531 } 532 if (pnameIteratorWhereClause != null 533 && pnameIteratorWhereClause.length() > 0) { 534 return pnameIteratorWhereClause; 535 } else { 536 return null; 537 } 538 } 539 540 541 542 543 549 public String getUpdateStatement(RdbGenTable rgt) throws PException { 550 StringBuffer sb = new StringBuffer ("UPDATE "); 551 sb.append(rgt.tableName); 552 sb.append(" SET "); 553 String sep = ""; 554 for (int i = 0; i < rgt.inheritedColumns.size(); i++) { 555 if (updatable((RdbGenColumn) rgt.inheritedColumns.get(i))) { 556 sb.append(sep); 557 sep = COLUMN; 558 sb.append(((RdbGenColumn) rgt.inheritedColumns.get(i)).columnName); 559 sb.append(" = ?"); 560 } 561 } 562 for (int i = 0; i < rgt.columns.size(); i++) { 563 if (updatable((RdbGenColumn) rgt.columns.get(i))) { 564 sb.append(sep); 565 sep = COLUMN; 566 sb.append(((RdbGenColumn) rgt.columns.get(i)).columnName); 567 sb.append(" = ?"); 568 } 569 } 570 appendModificationWhereClause(sb, rgt); 571 return sb.toString(); 572 } 573 574 public String getUpdateStatement(RdbGenJoin rgj) throws PException { 575 StringBuffer sb = new StringBuffer ("UPDATE "); 576 sb.append(rgj.table.tableName); 577 sb.append(" SET "); 578 String sep = ""; 579 if (rgj.table.colocatedTable) { 580 for (int i = 0; i < rgj.joinColumnsInExt.size(); i++) { 581 sb.append(sep); 582 sep = COLUMN; 583 sb.append(((RdbGenColumn) rgj.joinColumnsInExt.get(i)).columnName); 584 sb.append(" = ?"); 585 } 586 587 } else { 588 for (int i = 0; i < rgj.table.columns.size(); i++) { 589 RdbGenColumn rgc = (RdbGenColumn) rgj.table.columns.get(i); 590 if (rgc.joinCol == null) { 591 sb.append(sep); 592 sep = COLUMN; 593 sb.append(rgc.columnName); 594 sb.append(" = ?"); 595 } 596 } 597 } 598 appendModificationWhereClause(sb, rgj, rgj.table.colocatedTable); 599 return sb.toString(); 600 } 601 602 public String getUpdateNullStatement(RdbGenTable rgt) throws PException { 603 if (rgt.joins != null && rgt.joins.size() > 0) { 604 throw new PException("umanaged case, must use " + 605 "getUpdateNullStatement(RdbGenJoin) for each join."); 606 } 607 StringBuffer sb = new StringBuffer ("UPDATE "); 608 sb.append(rgt.tableName); 609 sb.append(" SET "); 610 String sep = ""; 611 for (int i = 0; i < rgt.columns.size(); i++) { 612 RdbGenColumn rgc = (RdbGenColumn) rgt.columns.get(i); 613 if (updatable(rgc)) { 614 if (rgt.colocatedColumn(rgc)) { 615 continue; 616 } 617 sb.append(sep); 618 sep = COLUMN; 619 sb.append(rgc.columnName); 620 sb.append(" = null"); 621 } 622 } 623 appendModificationWhereClause(sb, rgt); 624 return sb.toString(); 625 } 626 627 public String getUpdateNullStatement(RdbGenJoin rgj) throws PException { 628 StringBuffer sb = new StringBuffer ("UPDATE "); 629 sb.append(rgj.table.tableName); 630 sb.append(" SET "); 631 String sep = ""; 632 for (int i = 0; i < rgj.joinColumnsInExt.size(); i++) { 633 sb.append(sep); 634 sep = COLUMN; 635 sb.append(((RdbGenColumn) rgj.joinColumnsInExt.get(i)).columnName); 636 sb.append(" = null"); 637 } 638 appendModificationWhereClause(sb, rgj, false); 639 return sb.toString(); 640 } 641 642 648 public String getInsertStatement(RdbGenTable rgt) throws PException { 649 String sep = ""; 650 StringBuffer sb = new StringBuffer ("INSERT INTO "); 651 sb.append(rgt.tableName); 652 sb.append(" ("); 653 ArrayList columns = new ArrayList (); 654 columns.addAll(rgt.inheritedColumns); 655 columns.addAll(rgt.columns); 656 for (int i = 0; i < columns.size(); i++) { 657 sb.append(sep); 658 sep = COLUMN; 659 sb.append(((RdbGenColumn) columns.get(i)).columnName); 660 } 661 sb.append(") VALUES ("); 662 sep = ""; 663 for (int i = 0; i < columns.size(); i++) { 664 sb.append(sep); 665 sep = COLUMN; 666 sb.append("?"); 667 } 668 sb.append(")"); 669 return sb.toString(); 670 } 671 672 public String getInsertStatement(RdbGenJoin rgj) { 673 RdbGenTable rgt = rgj.getTable(); 674 int i = 0; 675 try { 676 String sep = ""; 677 StringBuffer sb = new StringBuffer ("INSERT INTO "); 678 sb.append(rgt.tableName); 679 sb.append(" ("); 680 for (i = 0; i < rgt.columns.size(); i++) { 681 RdbGenColumn col = (RdbGenColumn) rgt.columns.get(i); 682 if (col.isInJoin(rgj)) { 683 sb.append(sep); 684 sep = COLUMN; 685 sb.append(((RdbGenColumn) rgt.columns.get(i)).columnName); 686 } 687 } 688 sb.append(") VALUES ("); 689 sep = ""; 690 for (i = 0; i < rgt.columns.size(); i++) { 691 RdbGenColumn col = (RdbGenColumn) rgt.columns.get(i); 692 if (col.isInJoin(rgj)) { 693 sb.append(sep); 694 sep = COLUMN; 695 sb.append("?"); 696 } 697 } 698 sb.append(")"); 699 return sb.toString(); 700 } catch (RuntimeException e) { 701 System.out.println(rgt.tableName); 702 System.out.println(rgt.columns.get(i)); 703 System.out.println(((RdbGenColumn) rgt.columns.get(i)).joins); 704 e.printStackTrace(); 705 throw e; 706 } 707 } 708 709 715 public String getDeleteStatement(RdbGenTable rgt) throws PException { 716 StringBuffer sb = new StringBuffer ("DELETE FROM "); 717 sb.append(rgt.tableName); 718 appendModificationWhereClause(sb, rgt); 719 return sb.toString(); 720 } 721 722 public String getDeleteStatement(RdbGenJoin rgj) throws PException { 723 return getDeleteStatement(rgj.getTable()); 724 } 725 726 733 public String getExtDeleteStatement(RdbGenJoin rgj) throws PException { 734 StringBuffer sb = new StringBuffer ("DELETE FROM "); 735 sb.append(rgj.table.tableName); 736 appendModificationWhereClause(sb, rgj, rgj.table.colocatedTable); 737 return sb.toString(); 738 } 739 740 public String setIdPnParam(String cnfn, 741 int idx, 742 String adapter, 743 String pstmt, 744 boolean isSpecific) throws PException { 745 RdbGenColumn rgc = (RdbGenColumn) genId.cnFieldColumns.get(cnfn); 746 return rgc.getSqlSet(adapter, pstmt, setIdPNParameterOperand( cnfn, isSpecific), idx); 747 } 748 749 755 public String setIdPNParameterOperand(String cnfn, boolean isSpecific) throws PException { 756 RdbGenColumn rgc = (RdbGenColumn) genId.cnFieldColumns.get(cnfn); 757 StringBuffer value = new StringBuffer (); 758 if (isSpecific) { 759 value.append("(("); 760 value.append(genId.getCnFQName()); 761 value.append("PNG) _pngId).pnGet"); 762 value.append(commonHelper.upperFL(cnfn)); 763 value.append("(null)"); 764 } else { 765 value.append("((PNameGetter) _pngId)."); 766 value.append(commonHelper.getPNameGetterGetFunction(rgc.columnType)); 767 value.append("(\""); 768 value.append(cnfn); 769 value.append("\", null)"); 770 } 771 return value.toString(); 772 } 773 774 780 public RdbGenRef getGenRefOfColumn(RdbGenColumn rgc) { 781 return getGenRefOfColumn(rgc, null); 782 } 783 784 public RdbGenRef getGenRefOfColumn(RdbGenColumn rgc, RdbGenJoin rgj) { 785 if (rgc.joinCol != null) { 786 return getGenRefOfColumn(rgc.joinCol, rgj); 787 } 788 PrimitiveElement pe = null; 789 if (rgj != null && rgc.joins != null) { 790 int idx = rgc.joins.indexOf(rgj); 791 if (idx != -1) { 792 pe = (PrimitiveElement) rgc.pes.get(idx); 793 } 794 } 795 Iterator it = genRefs.values().iterator(); 796 while (it.hasNext()) { 797 RdbGenRef rgr = (RdbGenRef) it.next(); 798 if (rgr.refColumn != null) { 799 if (rgr.refColumn == rgc && (pe == null 800 || rgr.nd.getFieldName().equals(pe.getName()))) { 801 return rgr; 803 } 804 } else { 805 Iterator it2 = rgr.cnFieldColumns.values().iterator(); 806 Collection classfields = rgr.nd.getNameRef().getProjection().values(); 807 while (it2.hasNext()) { 808 RdbGenColumn currentRgc = (RdbGenColumn) it2.next(); 809 if (currentRgc == rgc && (pe == null 810 || classfields.contains(pe.getName()))) { 811 return rgr; 813 } 814 } 815 } 816 } 817 if (rgj !=null) System.out.println("getGenRefOfColumn(" + rgc.columnName + COLUMN + rgj.joinIdx + ") return null"); 818 return null; 819 } 820 821 827 public boolean colInGenId(RdbGenColumn genCol) { 828 if (genCol.joinCol != null) { 829 return colInGenId(genCol.joinCol); 834 } 835 if (genId.refColumn != null) { 836 if (genId.refColumn == genCol) { 837 return true; 838 } 839 } else { 840 Iterator itCol = genId.cnFieldColumns.values().iterator(); 841 while (itCol.hasNext()) { 842 RdbGenColumn genColOfComposite = (RdbGenColumn) itCol.next(); 843 if (genColOfComposite == genCol) { 844 return true; 845 } 846 } 847 } 848 return false; 849 } 850 851 856 public String getGenRefFieldName(RdbGenRef gr) { 857 Iterator it = genRefs.keySet().iterator(); 858 while (it.hasNext()) { 859 String res = (String ) it.next(); 860 if (genRefs.get(res) == gr) { 861 return res; 862 } 863 } 864 return null; 865 } 866 867 873 public String getPnFieldName(RdbGenColumn rgc) { 874 Iterator it = genId.cnFieldColumns.keySet().iterator(); 875 while (it.hasNext()) { 876 String res = (String ) it.next(); 877 if (rgc == genId.cnFieldColumns.get(res)) { 878 return res; 879 } 880 } 881 return null; 882 } 883 884 886 892 private void appendFQColName(StringBuffer sb, RdbGenColumn rgc) { 893 sb.append(rgc.table.tableName); 894 sb.append("."); 895 sb.append(rgc.columnName); 896 } 897 898 private String getFQColName(RdbGenColumn rgc, String tableSuffix) { 899 return rgc.table.tableName + tableSuffix + "." + rgc.columnName; 900 } 901 902 908 private void appendModificationWhereClause(StringBuffer sb, 909 RdbGenTable rgt) 910 throws PException { 911 String sep = ""; 912 sb.append(" WHERE "); 913 if (rgt != mainTable) { 914 throw new PException("umanaged case, the table " 915 + rgt.tableName + " is an external table"); 916 } 917 if (genId.refColumn != null) { 918 sb.append(genId.refColumn.columnName); 919 sb.append(" = ?"); 920 } else { 921 Iterator it = genId.cnFieldColumns.values().iterator(); 922 while (it.hasNext()) { 923 RdbGenColumn cur = (RdbGenColumn) it.next(); 924 sb.append(sep); 925 sep = " AND "; 926 sb.append(cur.columnName); 927 sb.append(" = ?"); 928 } 929 } 930 } 931 932 938 private void appendModificationWhereClause(StringBuffer sb, 939 RdbGenJoin rgj, 940 boolean isNotJoinColumn) { 941 String sep = ""; 942 sb.append(" WHERE "); 943 sep = ""; 944 if (isNotJoinColumn) { 945 for (int i = 0; i < rgj.table.columns.size(); i++) { 946 RdbGenColumn rgc = (RdbGenColumn) rgj.table.columns.get(i); 947 if (rgc.joinCol == null) { 948 sb.append(sep); 949 sep = " AND "; 950 sb.append(rgc.columnName); 951 sb.append(" = ?"); 952 } 953 } 954 } else { 955 for (int i = 0; i < rgj.joinColumnsInExt.size(); i++) { 956 sb.append(sep); 957 sep = " AND "; 958 sb.append(((RdbGenColumn) rgj.joinColumnsInExt.get(i)).columnName); 959 sb.append(" = ?"); 960 } 961 } 962 } 963 964 public boolean updatable(RdbGenColumn rgc) { 965 return (!rgc.hiddenField || rgc.joinCol != null || !colInGenId(rgc)) 966 && rgc.constant == null; 967 } 968 969 public boolean updatable(RdbGenColumn rgc, RdbGenJoin rgj) { 970 if (rgj == null) { 971 return updatable(rgc); 972 } 973 if (rgj.table.colocatedTable) { 974 return rgc.joinCol != null && rgc.isInJoin(rgj); 975 } else { 976 return rgc.joinCol == null; 977 } 978 } 979 980 public boolean hasFilter() { 981 return filterExpr!= null && !"".equals(filterExpr); 982 } 983 984 985 986 class JormExpression2WhereClause extends Expression2WhereClauseImpl { 987 988 public void convertExp2WhereClauseBuilder(Expression expression, 989 String rdbAdapterVarName, 990 StringBuffer sb) throws ExpressionException { 991 if (expression instanceof ParameterOperand) { 992 String fieldName = ((ParameterOperand) expression).getName(); 995 RdbPrimitiveElementMapping rpem = 996 getRPEM(fieldName, mapping.getClassMapping()); 997 if (rpem == null) { 998 throw new ExpressionException( 999 "No mapping found for the field '" + fieldName 1000 + "' into the mapping filter"); 1001 } 1002 RdbGenColumn rgc = mainTable.getColumn(rpem.getName()); 1003 if (rgc == null) { 1004 throw new ExpressionException( 1005 "No column found for the field '" + fieldName 1006 + "' into the mainb table '" 1007 + mainTable.tableName 1008 + "' in the mapping filter"); 1009 } 1010 appendFQColName(sb, rgc); 1011 } else if (expression instanceof VariableOperand) { 1012 sb.append("\" + "); 1013 sb.append(rdbAdapterVarName); 1014 sb.append(".getValueAsSQLString(getNamingFilterKey(), "); 1015 sb.append(expression.getType().getProgName()); 1016 sb.append(".getTypeCode()) + \""); 1017 } else { 1018 super.convertExp2WhereClauseBuilder(expression, rdbAdapterVarName, sb); 1019 } 1020 } 1021 private RdbPrimitiveElementMapping getRPEM(String fieldName, 1022 ClassMapping cm) 1023 throws ExpressionException { 1024 if (debug) { 1025 logger.log(BasicLevel.DEBUG, "Try to find the field " + fieldName 1026 + " on the class mapping of the class " 1027 + ((Class ) cm.getLinkedMO()).getFQName()); 1028 } 1029 RdbPrimitiveElementMapping rpem = (RdbPrimitiveElementMapping) 1030 cm.getPrimitiveElementMapping(fieldName); 1031 if (rpem == null) { 1032 for(Iterator it = cm.getParentClassMappings().iterator(); 1033 rpem == null && it.hasNext();) { 1034 ParentClassMapping pcm = (ParentClassMapping) it.next(); 1035 ClassMapping _cm = pcm.getMOClass() 1036 .getClassProject(pcm.getProjectName()) 1037 .getMapping(pcm.getMapperName()).getClassMapping(); 1038 rpem = getRPEM(fieldName, _cm); 1039 } 1040 1041 } 1042 return rpem; 1043 } 1044 } 1045 1046} 1047 | Popular Tags |