1 17 package org.apache.ws.jaxme.sqls.impl; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.ws.jaxme.sqls.ColumnReference; 24 import org.apache.ws.jaxme.sqls.SQLFactory; 25 import org.apache.ws.jaxme.sqls.SelectStatement; 26 import org.apache.ws.jaxme.sqls.SelectTableReference; 27 import org.apache.ws.jaxme.sqls.Table; 28 import org.apache.ws.jaxme.sqls.TableReference; 29 30 31 34 public class SelectStatementImpl extends ConstrainedStatementImpl implements SelectStatement { 35 public static class OrderColumnImpl implements SelectStatement.OrderColumn { 36 private final Object columnReference; 37 private final boolean descending; 38 public OrderColumnImpl(Object pColumnReference, boolean pDescending) { 39 columnReference = pColumnReference; 40 descending = pDescending; 41 } 42 public Object getColumn() { 43 return columnReference; 44 } 45 public boolean isDescending() { 46 return descending; 47 } 48 } 49 50 private List orderColumns = new ArrayList (); 51 private List resultColumns = new ArrayList (); 52 private boolean distinct; 53 private int maxRows, skippedRows; 54 55 58 public SelectStatementImpl(SQLFactory pFactory) { 59 super(pFactory); 60 } 61 62 public void addOrderColumn(Object pColumn) { 63 addOrderColumn(pColumn, false); 64 } 65 66 public void addOrderColumn(Object pColumn, boolean pDescending) { 67 addOrderColumn(new OrderColumnImpl(pColumn, pDescending)); 68 } 69 70 public void addOrderColumn(SelectStatement.OrderColumn pColumn) { 71 Object o = pColumn.getColumn(); 72 if (o instanceof ColumnReference) { 73 for (Iterator iter = orderColumns.iterator(); iter.hasNext(); ) { 74 SelectStatement.OrderColumn column = (SelectStatement.OrderColumn) iter.next(); 75 Object p = column.getColumn(); 76 if (p instanceof ColumnReference) { 77 if (o.equals(p)) { 78 throw new NullPointerException ("The column " 79 + ((ColumnReference) o).getColumn().getName() 80 + " is already used for sorting."); 81 } 82 } 83 } 84 } 85 orderColumns.add(pColumn); 86 } 87 88 public Iterator getOrderColumns() { 89 return orderColumns.iterator(); 90 } 91 92 public void addResultColumn(ColumnReference pColumn) { 93 resultColumns.add(pColumn); 94 } 95 96 public Iterator getResultColumns() { 97 return resultColumns.iterator(); 98 } 99 100 public void setDistinct(boolean pDistinct) { 101 distinct = pDistinct; 102 } 103 104 public boolean isDistinct() { 105 return distinct; 106 } 107 108 public void setMaxRows(int pMaxRows) { 109 maxRows = pMaxRows; 110 } 111 112 public int getMaxRows() { 113 return maxRows; 114 } 115 116 public void setSkippedRows(int pSkippedRows) { 117 skippedRows = pSkippedRows; 118 } 119 120 public int getSkippedRows() { 121 return skippedRows; 122 } 123 124 protected TableReference newTableReference(Table pTable) { 125 return new SelectTableReferenceImpl(this, pTable); 126 } 127 128 public SelectTableReference getSelectTableReference() { 129 return (SelectTableReference) getTableReference(); 130 } 131 132 public Iterator getSelectTableReferences() { 133 return new Iterator () { 134 SelectTableReference reference = getSelectTableReference(); 135 public boolean hasNext() { return reference != null; } 136 public Object next() { 137 SelectTableReference result = reference; 138 reference = result.getRightJoinedTableReference(); 139 return result; 140 } 141 public void remove() { 142 throw new UnsupportedOperationException (); 143 } 144 }; 145 } 146 147 public Table createView(Table.Name pName) { 148 return getSQLFactory().getObjectFactory().newView(this, pName); 149 } 150 151 public Table createView(String pName) { 152 return createView(pName == null ? null : new TableImpl.NameImpl(pName)); 153 } 154 } 155 | Popular Tags |