1 package org.hibernate.sql; 3 4 import java.util.HashSet ; 5 import java.util.Set ; 6 7 12 public class OracleJoinFragment extends JoinFragment { 13 14 private StringBuffer afterFrom = new StringBuffer (); 15 private StringBuffer afterWhere = new StringBuffer (); 16 17 public void addJoin(String tableName, String alias, String [] fkColumns, String [] pkColumns, int joinType) { 18 19 addCrossJoin( tableName, alias ); 20 21 for ( int j = 0; j < fkColumns.length; j++ ) { 22 setHasThetaJoins( true ); 23 afterWhere.append( " and " ) 24 .append( fkColumns[j] ); 25 if ( joinType == RIGHT_OUTER_JOIN || joinType == FULL_JOIN ) afterWhere.append( "(+)" ); 26 afterWhere.append( '=' ) 27 .append( alias ) 28 .append( '.' ) 29 .append( pkColumns[j] ); 30 if ( joinType == LEFT_OUTER_JOIN || joinType == FULL_JOIN ) afterWhere.append( "(+)" ); 31 } 32 33 } 34 35 public String toFromFragmentString() { 36 return afterFrom.toString(); 37 } 38 39 public String toWhereFragmentString() { 40 return afterWhere.toString(); 41 } 42 43 public void addJoins(String fromFragment, String whereFragment) { 44 afterFrom.append( fromFragment ); 45 afterWhere.append( whereFragment ); 46 } 47 48 public JoinFragment copy() { 49 OracleJoinFragment copy = new OracleJoinFragment(); 50 copy.afterFrom = new StringBuffer ( afterFrom.toString() ); 51 copy.afterWhere = new StringBuffer ( afterWhere.toString() ); 52 return copy; 53 } 54 55 public void addCondition(String alias, String [] columns, String condition) { 56 for ( int i = 0; i < columns.length; i++ ) { 57 afterWhere.append( " and " ) 58 .append( alias ) 59 .append( '.' ) 60 .append( columns[i] ) 61 .append( condition ); 62 } 63 } 64 65 public void addCrossJoin(String tableName, String alias) { 66 afterFrom.append( ", " ) 67 .append( tableName ) 68 .append( ' ' ) 69 .append( alias ); 70 } 71 72 public void addCondition(String alias, String [] fkColumns, String [] pkColumns) { 73 throw new UnsupportedOperationException (); 74 } 75 76 public boolean addCondition(String condition) { 77 return addCondition( afterWhere, condition ); 78 } 79 80 public void addFromFragmentString(String fromFragmentString) { 81 afterFrom.append( fromFragmentString ); 82 } 83 84 public void addJoin(String tableName, String alias, String [] fkColumns, String [] pkColumns, int joinType, String on) { 85 addJoin( tableName, alias, fkColumns, pkColumns, joinType ); 87 if ( joinType == JoinFragment.INNER_JOIN ) { 88 addCondition( on ); 89 } 90 else if ( joinType == JoinFragment.LEFT_OUTER_JOIN ) { 91 addLeftOuterJoinCondition( on ); 92 } 93 else { 94 throw new UnsupportedOperationException ( "join type not supported by OracleJoinFragment (use Oracle9Dialect)" ); 95 } 96 } 97 98 106 private void addLeftOuterJoinCondition(String on) { 107 StringBuffer buf = new StringBuffer ( on ); 108 for ( int i = 0; i < buf.length(); i++ ) { 109 char character = buf.charAt( i ); 110 boolean isInsertPoint = OPERATORS.contains( new Character ( character ) ) || 111 ( character == ' ' && buf.length() > i + 3 && "is ".equals( buf.substring( i + 1, i + 4 ) ) ); 112 if ( isInsertPoint ) { 113 buf.insert( i, "(+)" ); 114 i += 3; 115 } 116 } 117 addCondition( buf.toString() ); 118 } 119 120 private static final Set OPERATORS = new HashSet (); 121 122 static { 123 OPERATORS.add( new Character ( '=' ) ); 124 OPERATORS.add( new Character ( '<' ) ); 125 OPERATORS.add( new Character ( '>' ) ); 126 } 127 } 128 | Popular Tags |