1 package org.hibernate.sql; 3 4 import java.util.ArrayList ; 5 import java.util.Arrays ; 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.hibernate.util.StringHelper; 11 12 17 public class SelectFragment { 18 private String suffix; 19 private List columns = new ArrayList (); 20 private List columnAliases = new ArrayList (); 22 private String extraSelectList; 23 private String [] usedAliases; 24 25 public SelectFragment() {} 26 27 public SelectFragment setUsedAliases(String [] aliases) { 28 usedAliases = aliases; 29 return this; 30 } 31 32 public SelectFragment setExtraSelectList(String extraSelectList) { 33 this.extraSelectList = extraSelectList; 34 return this; 35 } 36 37 public SelectFragment setExtraSelectList(CaseFragment caseFragment, String fragmentAlias) { 38 setExtraSelectList( caseFragment.setReturnColumnName(fragmentAlias, suffix).toFragmentString() ); 39 return this; 40 } 41 42 public SelectFragment setSuffix(String suffix) { 43 this.suffix = suffix; 44 return this; 45 } 46 47 public SelectFragment addColumn(String columnName) { 48 addColumn(null, columnName); 49 return this; 50 } 51 52 public SelectFragment addColumns(String [] columnNames) { 53 for (int i=0; i<columnNames.length; i++) addColumn( columnNames[i] ); 54 return this; 55 } 56 57 public SelectFragment addColumn(String tableAlias, String columnName) { 58 return addColumn(tableAlias, columnName, columnName); 59 } 60 61 public SelectFragment addColumn(String tableAlias, String columnName, String columnAlias) { 62 columns.add( StringHelper.qualify(tableAlias, columnName) ); 63 columnAliases.add(columnAlias); 66 return this; 67 } 68 69 public SelectFragment addColumns(String tableAlias, String [] columnNames) { 70 for (int i=0; i<columnNames.length; i++) addColumn( tableAlias, columnNames[i] ); 71 return this; 72 } 73 74 public SelectFragment addColumns(String tableAlias, String [] columnNames, String [] 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 tableAlias, String [] formulas, String [] 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 tableAlias, String formula, String formulaAlias) { 89 columns.add( StringHelper.replace(formula, Template.TEMPLATE, tableAlias) ); 90 columnAliases.add(formulaAlias); 91 return this; 92 } 93 94 public String toFragmentString() { 95 StringBuffer buf = new StringBuffer ( columns.size() * 10 ); 96 Iterator iter = columns.iterator(); 97 Iterator columnAliasIter = columnAliases.iterator(); 98 HashSet columnsUnique = new HashSet (); 100 if (usedAliases!=null) columnsUnique.addAll( Arrays.asList(usedAliases) ); 101 while ( iter.hasNext() ) { 102 String column = (String ) iter.next(); 103 String columnAlias = (String ) columnAliasIter.next(); 104 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 |