1 package org.hibernate.sql; 3 4 import org.hibernate.dialect.Dialect; 5 6 11 public class QueryJoinFragment extends JoinFragment { 12 13 private StringBuffer afterFrom = new StringBuffer (); 14 private StringBuffer afterWhere = new StringBuffer (); 15 private Dialect dialect; 16 private boolean useThetaStyleInnerJoins; 17 18 public QueryJoinFragment(Dialect dialect, boolean useThetaStyleInnerJoins) { 19 this.dialect = dialect; 20 this.useThetaStyleInnerJoins = useThetaStyleInnerJoins; 21 } 22 23 public void addJoin(String tableName, String alias, String [] fkColumns, String [] pkColumns, int joinType) { 24 addJoin( tableName, alias, alias, fkColumns, pkColumns, joinType, null ); 25 } 26 27 public void addJoin(String tableName, String alias, String [] fkColumns, String [] pkColumns, int joinType, String on) { 28 addJoin( tableName, alias, alias, fkColumns, pkColumns, joinType, on ); 29 } 30 31 private void addJoin(String tableName, String alias, String concreteAlias, String [] fkColumns, String [] pkColumns, int joinType, String on) { 32 if ( !useThetaStyleInnerJoins || joinType != INNER_JOIN ) { 33 JoinFragment jf = dialect.createOuterJoinFragment(); 34 jf.addJoin( tableName, alias, fkColumns, pkColumns, joinType, on ); 35 addFragment( jf ); 36 } 37 else { 38 addCrossJoin( tableName, alias ); 39 addCondition( concreteAlias, fkColumns, pkColumns ); 40 addCondition( on ); 41 } 42 } 43 44 public String toFromFragmentString() { 45 return afterFrom.toString(); 46 } 47 48 public String toWhereFragmentString() { 49 return afterWhere.toString(); 50 } 51 52 public void addJoins(String fromFragment, String whereFragment) { 53 afterFrom.append( fromFragment ); 54 afterWhere.append( whereFragment ); 55 } 56 57 public JoinFragment copy() { 58 QueryJoinFragment copy = new QueryJoinFragment( dialect, useThetaStyleInnerJoins ); 59 copy.afterFrom = new StringBuffer ( afterFrom.toString() ); 60 copy.afterWhere = new StringBuffer ( afterWhere.toString() ); 61 return copy; 62 } 63 64 public void addCondition(String alias, String [] columns, String condition) { 65 for ( int i = 0; i < columns.length; i++ ) { 66 afterWhere.append( " and " ) 67 .append( alias ) 68 .append( '.' ) 69 .append( columns[i] ) 70 .append( condition ); 71 } 72 } 73 74 75 public void addCrossJoin(String tableName, String alias) { 76 afterFrom.append( ", " ) 77 .append( tableName ) 78 .append( ' ' ) 79 .append( alias ); 80 } 81 82 public void addCondition(String alias, String [] fkColumns, String [] pkColumns) { 83 for ( int j = 0; j < fkColumns.length; j++ ) { 84 afterWhere.append( " and " ) 85 .append( fkColumns[j] ) 86 .append( '=' ) 87 .append( alias ) 88 .append( '.' ) 89 .append( pkColumns[j] ); 90 } 91 } 92 93 99 public boolean addCondition(String condition) { 100 if ( 102 afterFrom.toString().indexOf( condition.trim() ) < 0 && 103 afterWhere.toString().indexOf( condition.trim() ) < 0 104 ) { 105 if ( !condition.startsWith( " and " ) ) { 106 afterWhere.append( " and " ); 107 } 108 afterWhere.append( condition ); 109 return true; 110 } 111 else { 112 return false; 113 } 114 } 115 116 public void addFromFragmentString(String fromFragmentString) { 117 afterFrom.append( fromFragmentString ); 118 } 119 120 public void clearWherePart() { 121 afterWhere.setLength( 0 ); 122 } 123 } 124 125 126 127 128 129 130 | Popular Tags |