KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: QueryJoinFragment.java,v 1.4 2004/12/05 17:20:21 pgmjsd Exp $
2
package org.hibernate.sql;
3
4 import org.hibernate.dialect.Dialect;
5
6 /**
7  * A join that appears in a translated HQL query
8  *
9  * @author Gavin King
10  */

11 public class QueryJoinFragment extends JoinFragment {
12
13     private StringBuffer JavaDoc afterFrom = new StringBuffer JavaDoc();
14     private StringBuffer JavaDoc afterWhere = new StringBuffer JavaDoc();
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 JavaDoc tableName, String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns, int joinType) {
24         addJoin( tableName, alias, alias, fkColumns, pkColumns, joinType, null );
25     }
26
27     public void addJoin(String JavaDoc tableName, String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns, int joinType, String JavaDoc on) {
28         addJoin( tableName, alias, alias, fkColumns, pkColumns, joinType, on );
29     }
30
31     private void addJoin(String JavaDoc tableName, String JavaDoc alias, String JavaDoc concreteAlias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns, int joinType, String JavaDoc 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 JavaDoc toFromFragmentString() {
45         return afterFrom.toString();
46     }
47
48     public String JavaDoc toWhereFragmentString() {
49         return afterWhere.toString();
50     }
51
52     public void addJoins(String JavaDoc fromFragment, String JavaDoc 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 JavaDoc( afterFrom.toString() );
60         copy.afterWhere = new StringBuffer JavaDoc( afterWhere.toString() );
61         return copy;
62     }
63
64     public void addCondition(String JavaDoc alias, String JavaDoc[] columns, String JavaDoc 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 JavaDoc tableName, String JavaDoc alias) {
76         afterFrom.append( ", " )
77                 .append( tableName )
78                 .append( ' ' )
79                 .append( alias );
80     }
81
82     public void addCondition(String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] 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     /**
94      * Add the condition string to the join fragment.
95      *
96      * @param condition
97      * @return true if the condition was added, false if it was already in the fragment.
98      */

99     public boolean addCondition(String JavaDoc condition) {
100         // if the condition is not already there...
101
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 JavaDoc 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