KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > sql > OracleJoinFragment


1 //$Id: OracleJoinFragment.java,v 1.8 2005/05/11 15:26:04 oneovthafew Exp $
2
package org.hibernate.sql;
3
4 import java.util.HashSet JavaDoc;
5 import java.util.Set JavaDoc;
6
7 /**
8  * An Oracle-style (theta) join
9  *
10  * @author Jon Lipsky, Gavin King
11  */

12 public class OracleJoinFragment extends JoinFragment {
13
14     private StringBuffer JavaDoc afterFrom = new StringBuffer JavaDoc();
15     private StringBuffer JavaDoc afterWhere = new StringBuffer JavaDoc();
16
17     public void addJoin(String JavaDoc tableName, String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] 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 JavaDoc toFromFragmentString() {
36         return afterFrom.toString();
37     }
38
39     public String JavaDoc toWhereFragmentString() {
40         return afterWhere.toString();
41     }
42
43     public void addJoins(String JavaDoc fromFragment, String JavaDoc 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 JavaDoc( afterFrom.toString() );
51         copy.afterWhere = new StringBuffer JavaDoc( afterWhere.toString() );
52         return copy;
53     }
54
55     public void addCondition(String JavaDoc alias, String JavaDoc[] columns, String JavaDoc 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 JavaDoc tableName, String JavaDoc alias) {
66         afterFrom.append( ", " )
67                 .append( tableName )
68                 .append( ' ' )
69                 .append( alias );
70     }
71
72     public void addCondition(String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns) {
73         throw new UnsupportedOperationException JavaDoc();
74     }
75
76     public boolean addCondition(String JavaDoc condition) {
77         return addCondition( afterWhere, condition );
78     }
79
80     public void addFromFragmentString(String JavaDoc fromFragmentString) {
81         afterFrom.append( fromFragmentString );
82     }
83
84     public void addJoin(String JavaDoc tableName, String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns, int joinType, String JavaDoc on) {
85         //arbitrary on clause ignored!!
86
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 JavaDoc( "join type not supported by OracleJoinFragment (use Oracle9Dialect)" );
95         }
96     }
97
98     /**
99      * This method is a bit of a hack, and assumes
100      * that the column on the "right" side of the
101      * join appears on the "left" side of the
102      * operator, which is extremely wierd if this
103      * was a normal join condition, but is natural
104      * for a filter.
105      */

106     private void addLeftOuterJoinCondition(String JavaDoc on) {
107         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( on );
108         for ( int i = 0; i < buf.length(); i++ ) {
109             char character = buf.charAt( i );
110             boolean isInsertPoint = OPERATORS.contains( new Character JavaDoc( 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 JavaDoc OPERATORS = new HashSet JavaDoc();
121
122     static {
123         OPERATORS.add( new Character JavaDoc( '=' ) );
124         OPERATORS.add( new Character JavaDoc( '<' ) );
125         OPERATORS.add( new Character JavaDoc( '>' ) );
126     }
127 }
128
Popular Tags