1 22 23 package org.xquark.extractor.sql; 24 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.xquark.extractor.algebra.Expression; 30 31 public class SqlSelect extends SqlExpression implements Executable 32 { 33 34 private static final String RCSRevision = "$Revision: 1.6 $"; 35 private static final String RCSName = "$Name: $"; 36 37 private boolean _selectDistinct; 38 private SqlExpression _whereClause; 39 private List _selectList; 40 private List _orderbyClause; 41 private List _groupbyClause; 42 private SqlExpression _havingClause; 43 private List _fromClause; 44 private Expression _originalExpression; 45 48 public SqlSelect(Expression originalExpression) { 49 _originalExpression = originalExpression; 50 } 51 52 57 public SqlExpression getWhereClause() 58 { 59 return _whereClause; 60 } 61 62 public void setWhereClause(SqlExpression aWhereClause) 63 { 64 _whereClause = aWhereClause; 66 } 67 68 73 public List getSelectList() 74 { 75 return _selectList; 76 } 77 78 83 public void setSelectList(List aSelectList) 84 { 85 _selectList = aSelectList; 86 } 87 88 93 public List getOrderbyClause() 94 { 95 return _orderbyClause; 96 } 97 98 103 public void setOrderbyClause(List aOrderbyClause) 104 { 105 _orderbyClause = aOrderbyClause; 107 } 108 109 114 public List getGroupbyClause() 115 { 116 return _groupbyClause ; 117 } 118 119 124 public void setGroupbyClause(List aGroupbyClause) 125 { 126 _groupbyClause = aGroupbyClause; 128 } 129 130 135 public SqlExpression getHavingClause() 136 { 137 return _havingClause; 138 } 139 140 145 public void setHavingClause(SqlExpression aHavingClause) 146 { 147 _havingClause = aHavingClause; 148 } 149 150 155 public List getFromClause() 156 { 157 return _fromClause; 158 } 159 160 165 public void setFromClause(List aFromClause) 166 { 167 _fromClause = aFromClause; 168 } 169 170 174 public boolean getSelectDistinct() 175 { 176 return _selectDistinct; 177 } 178 179 public void setSelectDistinct(boolean selectDistinct ) 180 { 181 _selectDistinct = selectDistinct ; 182 } 183 184 191 public void setSelectClause(boolean selectDistinct, List selectList) 192 { 193 _selectDistinct = selectDistinct ; 194 _selectList = selectList ; 195 } 196 197 198 214 215 216 221 public List getOrderbyClause(List sortSpecList) 222 { 223 return _orderbyClause ; 224 } 225 226 227 public void addFromClause(SqlExpression fromItem) 228 { 229 if ( null == _fromClause ) 230 _fromClause = new ArrayList () ; 231 232 _fromClause.add(fromItem); 233 } 234 235 public String toSql(Context context) 236 { 237 Context newContext = new Context(context); 238 239 StringBuffer retVal = new StringBuffer () ; 240 selectToSql(retVal, newContext); 241 fromToSql(retVal, newContext); 242 whereToSql(retVal, newContext); 243 groupbyToSql(retVal, newContext); 244 havingToSql(retVal, newContext); 245 orderbyToSql(retVal, newContext); 246 247 return retVal.toString() ; 248 } 249 250 protected StringBuffer selectToSql(StringBuffer statementBuffer, Context context) { 251 statementBuffer.append("SELECT") ; 252 if ( _selectDistinct ) 253 statementBuffer.append(" DISTINCT "); 254 else 255 statementBuffer.append(" ALL "); 256 257 String tmpStr = ""; 258 Iterator tmpItr ; 259 context.selectList = true; 260 if ( null != _selectList) 261 { 262 tmpItr = _selectList.iterator(); 263 while (tmpItr.hasNext()) { 264 tmpStr += ((SqlExpression)tmpItr.next()).toSql(context) + " , "; 265 } 266 tmpStr = tmpStr.substring(0 ,tmpStr.length()-3 ); 267 } 268 else 269 tmpStr += " * " ; 270 context.selectList = false; 271 statementBuffer.append(tmpStr); 272 273 return statementBuffer; 274 } 275 276 protected StringBuffer fromToSql(StringBuffer statementBuffer, Context context) { 277 Iterator tmpItr = null; 278 279 if ( null != _fromClause ) 280 { 281 statementBuffer.append("\nFROM "); 282 283 tmpItr = _fromClause.iterator(); 284 while (tmpItr.hasNext()) { 285 SqlExpression relation = (SqlExpression)tmpItr.next(); 286 287 if (relation instanceof SqlSelect) { 288 statementBuffer.append('('); 289 statementBuffer.append(relation.toSql(context)); 290 statementBuffer.append(')'); 291 } 292 else 293 statementBuffer.append(relation.toSql(context)); 294 295 statementBuffer.append(' '); } 297 } 298 return statementBuffer; 299 } 300 301 protected StringBuffer whereToSql(StringBuffer statementBuffer, Context context) { 302 if ( null != _whereClause ) 303 { 304 statementBuffer.append("\nWHERE "); 305 statementBuffer.append(_whereClause.toSql(context)); 306 } 307 return statementBuffer; 308 } 309 310 protected StringBuffer groupbyToSql(StringBuffer statementBuffer, Context context) { 311 String tmpStr = "" ; 313 Iterator tmpItr = null; 314 if ( null != _groupbyClause ) 315 { 316 tmpItr = _groupbyClause.iterator(); 317 while (tmpItr.hasNext()) { 318 tmpStr += ((SqlExpression)tmpItr.next()).toSql(context) + " , "; 319 } 320 tmpStr = tmpStr.substring(0 ,tmpStr.length()-3 ); 321 322 statementBuffer.append("\nGROUP BY "); 323 statementBuffer.append(tmpStr); 324 } 325 return statementBuffer; 326 } 327 328 protected StringBuffer havingToSql(StringBuffer statementBuffer, Context context) { 329 if ( null != _havingClause ) 331 { 332 statementBuffer.append("\nHAVING "); 333 statementBuffer.append(_havingClause.toSql(context)); 334 } 335 return statementBuffer; 336 } 337 338 protected StringBuffer orderbyToSql(StringBuffer statementBuffer, Context context) { 339 String tmpStr = "" ; 340 Iterator tmpItr = null; 341 if ( null != _orderbyClause && !_orderbyClause.isEmpty()) 342 { 343 tmpItr = _orderbyClause.iterator(); 344 while (tmpItr.hasNext()) { 345 tmpStr += ((SqlExpression)tmpItr.next()).toSql(context) + " , "; 346 } 347 tmpStr = tmpStr.substring(0 ,tmpStr.length()-3 ); 348 349 statementBuffer.append("\nORDER BY "); 350 statementBuffer.append(tmpStr); 351 } 352 return statementBuffer; 353 } 354 355 public Expression getOriginalExpression() { 356 return _originalExpression; 357 } 358 359 } 360 | Popular Tags |