1 package org.hibernate.sql; 3 4 import org.hibernate.AssertionFailure; 5 6 11 public class ANSIJoinFragment extends JoinFragment { 12 13 private StringBuffer buffer = new StringBuffer (); 14 private StringBuffer conditions = new StringBuffer (); 15 16 public void addJoin(String tableName, String alias, String [] fkColumns, String [] pkColumns, int joinType) { 17 addJoin(tableName, alias, fkColumns, pkColumns, joinType, null); 18 } 19 20 public void addJoin(String tableName, String alias, String [] fkColumns, String [] pkColumns, int joinType, String on) { 21 String joinString; 22 switch (joinType) { 23 case INNER_JOIN: 24 joinString = " inner join "; 25 break; 26 case LEFT_OUTER_JOIN: 27 joinString = " left outer join "; 28 break; 29 case RIGHT_OUTER_JOIN: 30 joinString = " right outer join "; 31 break; 32 case FULL_JOIN: 33 joinString = " full outer join "; 34 break; 35 default: 36 throw new AssertionFailure("undefined join type"); 37 } 38 39 buffer.append(joinString) 40 .append(tableName) 41 .append(' ') 42 .append(alias) 43 .append(" on "); 44 45 46 for ( int j=0; j<fkColumns.length; j++) { 47 50 buffer.append( fkColumns[j] ) 51 .append('=') 52 .append(alias) 53 .append('.') 54 .append( pkColumns[j] ); 55 if ( j<fkColumns.length-1 ) buffer.append(" and "); 56 } 57 58 addCondition(buffer, on); 59 60 } 61 62 public String toFromFragmentString() { 63 return buffer.toString(); 64 } 65 66 public String toWhereFragmentString() { 67 return conditions.toString(); 68 } 69 70 public void addJoins(String fromFragment, String whereFragment) { 71 buffer.append(fromFragment); 72 } 74 75 public JoinFragment copy() { 76 ANSIJoinFragment copy = new ANSIJoinFragment(); 77 copy.buffer = new StringBuffer ( buffer.toString() ); 78 return copy; 79 } 80 81 public void addCondition(String alias, String [] columns, String condition) { 82 for ( int i=0; i<columns.length; i++ ) { 83 conditions.append(" and ") 84 .append(alias) 85 .append('.') 86 .append( columns[i] ) 87 .append(condition); 88 } 89 } 90 91 public void addCrossJoin(String tableName, String alias) { 92 buffer.append(", ") 93 .append(tableName) 94 .append(' ') 95 .append(alias); 96 } 97 98 public void addCondition(String alias, String [] fkColumns, String [] pkColumns) { 99 throw new UnsupportedOperationException (); 100 101 } 102 103 public boolean addCondition(String condition) { 104 return addCondition(conditions, condition); 105 } 106 107 public void addFromFragmentString(String fromFragmentString) { 108 buffer.append(fromFragmentString); 109 } 110 111 } 112 113 114 115 116 117 118 | Popular Tags |