1 19 package org.openharmonise.commons.dsi.dml; 20 21 22 import java.util.*; 23 24 import org.openharmonise.commons.dsi.*; 25 26 33 public class SelectStatement extends AbstractDMLStatement { 34 35 38 static public String ORDER_ASCENDING = "ASC"; 39 40 43 static public String ORDER_DESCENDING = "DESC"; 44 45 48 private JoinConditions m_join = null; 49 50 53 private List m_SelectCols = null; 54 55 59 private Map m_orderColMap = null; 60 61 64 private int m_nLimit = -1; 65 66 70 private boolean m_distinct = false; 71 72 75 private List m_maxcols = null; 76 77 81 public SelectStatement() { 82 m_SelectCols = new Vector(); 83 } 84 85 90 public void addSelectColumn(int i) { 91 if (m_SelectCols == null) { 92 m_SelectCols = new Vector(16); 93 } 94 95 m_SelectCols.add(new Integer (i)); 96 } 97 98 104 public void addSelectColumns(List colrefs) { 105 if (m_SelectCols == null) { 106 m_SelectCols = new Vector(16); 107 } 108 109 m_SelectCols.addAll(colrefs); 110 } 111 112 118 public void addSelectColumn(ColumnRef colref) { 119 if (m_SelectCols == null) { 120 m_SelectCols = new Vector(16); 121 } 122 123 m_SelectCols.add(colref); 124 } 125 126 131 public List getSelectColumns() { 132 return m_SelectCols; 133 } 134 135 143 public int getResultSetIndex(ColumnRef colRef) { 144 return m_SelectCols.indexOf(colRef) + 1; 145 } 146 147 154 public boolean containsSelectColumn(ColumnRef colRef) { 155 return m_SelectCols.contains(colRef); 156 } 157 158 164 public void addSelectMaxColumn(ColumnRef colref) { 165 if (m_maxcols == null) { 166 m_maxcols = new Vector(16); 167 } 168 169 m_maxcols.add(new Integer (m_SelectCols.size())); 170 171 m_SelectCols.add(colref); 172 } 173 174 180 public List getSelectMaxColumns() { 181 return m_maxcols; 182 } 183 184 191 public void setOrderBy(ColumnRef colref) { 192 addOrderBy(colref); 193 } 194 195 200 public void setLimit(int num) { 201 m_nLimit = num; 202 } 203 204 210 public boolean isLimit() { 211 return (m_nLimit > 0); 212 } 213 214 219 public int getLimit() { 220 return m_nLimit; 221 } 222 223 229 public void addJoinConditions(JoinConditions join) 230 throws DataStoreException { 231 if(join != null) { 232 if (m_join == null) { 233 m_join = new JoinConditions(); 234 } 235 236 m_join.merge(join); 237 } 238 } 239 240 247 public void addJoinCondition(ColumnRef colref1, ColumnRef colref2) 248 throws DataStoreException { 249 if (m_join == null) { 250 m_join = new JoinConditions(); 251 } 252 253 m_join.addCondition(colref1, colref2); 254 } 255 256 263 public void addOuterJoinCondition(ColumnRef innerMember, ColumnRef outerMember) 264 throws DataStoreException { 265 if (m_join == null) { 266 m_join = new JoinConditions(); 267 } 268 269 m_join.addOuterJoin(innerMember, outerMember); 270 } 271 272 277 public JoinConditions getJoinConditions() { 278 return m_join; 279 } 280 281 286 public boolean hasJoinConditions() { 287 if ((m_join == null) || (m_join.size() == 0)) { 288 return false; 289 } else { 290 return true; 291 } 292 } 293 294 295 298 public void addWhereCondition(WhereCondition where) 299 throws DataStoreException { 300 super.addWhereCondition(where); 301 302 JoinConditions joins = where.getAssociatedJoinConditions(); 304 305 addJoinConditions(joins); 306 } 307 308 309 312 public void addWhereCondition(WhereConditionGroup where) 313 throws DataStoreException { 314 super.addWhereCondition(where); 315 316 for(int i=0;i<where.size();i++) { 318 Object cond = where.getCondition(i); 319 320 if(cond instanceof WhereCondition) { 321 JoinConditions joins = ((WhereCondition)cond).getAssociatedJoinConditions(); 322 323 addJoinConditions(joins); 324 } 325 } 326 } 327 328 333 public void setDistinct(boolean bIsDistinct) { 334 m_distinct = bIsDistinct; 335 } 336 337 343 public boolean isDistinct() { 344 return m_distinct; 345 } 346 347 350 public void clear() { 351 super.clear(); 352 353 if (m_join != null) { 354 m_join.empty(); 355 } 356 357 if (m_SelectCols != null) { 358 m_SelectCols.clear(); 359 } 360 361 m_orderColMap.clear(); 362 m_nLimit = -1; 363 } 364 365 370 public void addOrderBy(ColumnRef colref) { 371 addOrderBy(colref,ORDER_ASCENDING); 372 } 373 374 380 public void addOrderBy(ColumnRef colref,String orderDir) { 381 initialiseOrderByMap(); 382 383 m_orderColMap.put(colref, orderDir); 384 } 385 386 392 public Set getOrderByColumns() { 393 initialiseOrderByMap(); 394 395 return m_orderColMap.keySet(); 396 } 397 398 404 public String getOrderByDirection(ColumnRef colref) { 405 initialiseOrderByMap(); 406 407 return (String ) m_orderColMap.get(colref); 408 } 409 410 415 private void initialiseOrderByMap() { 416 if(m_orderColMap == null) { 417 m_orderColMap = new LinkedHashMap(); 419 } 420 } 421 422 428 public void addOrderBy(Map byColMap) { 429 if(m_orderColMap == null) { 430 m_orderColMap = new LinkedHashMap(byColMap); 431 } else { 432 Iterator iter = byColMap.keySet().iterator(); 433 434 while (iter.hasNext()) { 435 ColumnRef colref = (ColumnRef) iter.next(); 436 m_orderColMap.put(colref, byColMap.get(colref)); 437 } 438 } 439 440 } 441 } | Popular Tags |