KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SelectFragment.java,v 1.9 2005/07/14 23:55:02 oneovthafew Exp $
2
package org.hibernate.sql;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.hibernate.util.StringHelper;
11
12 /**
13  * A fragment of an SQL <tt>SELECT</tt> clause
14  *
15  * @author Gavin King
16  */

17 public class SelectFragment {
18     private String JavaDoc suffix;
19     private List JavaDoc columns = new ArrayList JavaDoc();
20     //private List aliases = new ArrayList();
21
private List JavaDoc columnAliases = new ArrayList JavaDoc();
22     private String JavaDoc extraSelectList;
23     private String JavaDoc[] usedAliases;
24     
25     public SelectFragment() {}
26     
27     public SelectFragment setUsedAliases(String JavaDoc[] aliases) {
28         usedAliases = aliases;
29         return this;
30     }
31     
32     public SelectFragment setExtraSelectList(String JavaDoc extraSelectList) {
33         this.extraSelectList = extraSelectList;
34         return this;
35     }
36     
37     public SelectFragment setExtraSelectList(CaseFragment caseFragment, String JavaDoc fragmentAlias) {
38         setExtraSelectList( caseFragment.setReturnColumnName(fragmentAlias, suffix).toFragmentString() );
39         return this;
40     }
41
42     public SelectFragment setSuffix(String JavaDoc suffix) {
43         this.suffix = suffix;
44         return this;
45     }
46
47     public SelectFragment addColumn(String JavaDoc columnName) {
48         addColumn(null, columnName);
49         return this;
50     }
51
52     public SelectFragment addColumns(String JavaDoc[] columnNames) {
53         for (int i=0; i<columnNames.length; i++) addColumn( columnNames[i] );
54         return this;
55     }
56
57     public SelectFragment addColumn(String JavaDoc tableAlias, String JavaDoc columnName) {
58         return addColumn(tableAlias, columnName, columnName);
59     }
60
61     public SelectFragment addColumn(String JavaDoc tableAlias, String JavaDoc columnName, String JavaDoc columnAlias) {
62         columns.add( StringHelper.qualify(tableAlias, columnName) );
63         //columns.add(columnName);
64
//aliases.add(tableAlias);
65
columnAliases.add(columnAlias);
66         return this;
67     }
68
69     public SelectFragment addColumns(String JavaDoc tableAlias, String JavaDoc[] columnNames) {
70         for (int i=0; i<columnNames.length; i++) addColumn( tableAlias, columnNames[i] );
71         return this;
72     }
73
74     public SelectFragment addColumns(String JavaDoc tableAlias, String JavaDoc[] columnNames, String JavaDoc[] columnAliases) {
75         for (int i=0; i<columnNames.length; i++) {
76             if ( columnNames[i]!=null ) addColumn( tableAlias, columnNames[i], columnAliases[i] );
77         }
78         return this;
79     }
80
81     public SelectFragment addFormulas(String JavaDoc tableAlias, String JavaDoc[] formulas, String JavaDoc[] formulaAliases) {
82         for ( int i=0; i<formulas.length; i++ ) {
83             if ( formulas[i]!=null ) addFormula( tableAlias, formulas[i], formulaAliases[i] );
84         }
85         return this;
86     }
87
88     public SelectFragment addFormula(String JavaDoc tableAlias, String JavaDoc formula, String JavaDoc formulaAlias) {
89         columns.add( StringHelper.replace(formula, Template.TEMPLATE, tableAlias) );
90         columnAliases.add(formulaAlias);
91         return this;
92     }
93
94     public String JavaDoc toFragmentString() {
95         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( columns.size() * 10 );
96         Iterator JavaDoc iter = columns.iterator();
97         Iterator JavaDoc columnAliasIter = columnAliases.iterator();
98         //HashMap columnsUnique = new HashMap();
99
HashSet JavaDoc columnsUnique = new HashSet JavaDoc();
100         if (usedAliases!=null) columnsUnique.addAll( Arrays.asList(usedAliases) );
101         while ( iter.hasNext() ) {
102             String JavaDoc column = (String JavaDoc) iter.next();
103             String JavaDoc columnAlias = (String JavaDoc) columnAliasIter.next();
104             //TODO: eventually put this back in, once we think all is fixed
105
//Object otherAlias = columnsUnique.put(qualifiedColumn, columnAlias);
106
/*if ( otherAlias!=null && !columnAlias.equals(otherAlias) ) {
107                 throw new AssertionFailure("bug in Hibernate SQL alias generation");
108             }*/

109             if ( columnsUnique.add(columnAlias) ) {
110                 buf.append(", ")
111                     .append(column)
112                     .append(" as ");
113                 if (suffix==null) {
114                     buf.append(columnAlias);
115                 }
116                 else {
117                     buf.append( new Alias(suffix).toAliasString(columnAlias) );
118                 }
119             }
120         }
121         if (extraSelectList!=null) {
122             buf.append(", ")
123                 .append(extraSelectList);
124         }
125         return buf.toString();
126     }
127
128 }
129
Popular Tags