KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ANSIJoinFragment.java,v 1.6 2004/12/05 15:04:20 pgmjsd Exp $
2
package org.hibernate.sql;
3
4 import org.hibernate.AssertionFailure;
5
6 /**
7  * An ANSI-style join
8  *
9  * @author Gavin King
10  */

11 public class ANSIJoinFragment extends JoinFragment {
12
13     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
14     private StringBuffer JavaDoc conditions = new StringBuffer JavaDoc();
15
16     public void addJoin(String JavaDoc tableName, String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns, int joinType) {
17         addJoin(tableName, alias, fkColumns, pkColumns, joinType, null);
18     }
19
20     public void addJoin(String JavaDoc tableName, String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns, int joinType, String JavaDoc on) {
21         String JavaDoc 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             /*if ( fkColumns[j].indexOf('.')<1 ) {
48                 throw new AssertionFailure("missing alias");
49             }*/

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 JavaDoc toFromFragmentString() {
63         return buffer.toString();
64     }
65
66     public String JavaDoc toWhereFragmentString() {
67         return conditions.toString();
68     }
69
70     public void addJoins(String JavaDoc fromFragment, String JavaDoc whereFragment) {
71         buffer.append(fromFragment);
72         //where fragment must be empty!
73
}
74
75     public JoinFragment copy() {
76         ANSIJoinFragment copy = new ANSIJoinFragment();
77         copy.buffer = new StringBuffer JavaDoc( buffer.toString() );
78         return copy;
79     }
80
81     public void addCondition(String JavaDoc alias, String JavaDoc[] columns, String JavaDoc 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 JavaDoc tableName, String JavaDoc alias) {
92         buffer.append(", ")
93             .append(tableName)
94             .append(' ')
95             .append(alias);
96     }
97
98     public void addCondition(String JavaDoc alias, String JavaDoc[] fkColumns, String JavaDoc[] pkColumns) {
99         throw new UnsupportedOperationException JavaDoc();
100
101     }
102
103     public boolean addCondition(String JavaDoc condition) {
104         return addCondition(conditions, condition);
105     }
106
107     public void addFromFragmentString(String JavaDoc fromFragmentString) {
108         buffer.append(fromFragmentString);
109     }
110
111 }
112
113
114
115
116
117
118
Popular Tags