KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > impl > SelectStatementImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.sqls.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
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 /**
32  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
33  */

34 public class SelectStatementImpl extends ConstrainedStatementImpl implements SelectStatement {
35    public static class OrderColumnImpl implements SelectStatement.OrderColumn {
36       private final Object JavaDoc columnReference;
37       private final boolean descending;
38       public OrderColumnImpl(Object JavaDoc pColumnReference, boolean pDescending) {
39          columnReference = pColumnReference;
40          descending = pDescending;
41       }
42       public Object JavaDoc getColumn() {
43          return columnReference;
44       }
45       public boolean isDescending() {
46          return descending;
47       }
48    }
49
50    private List JavaDoc orderColumns = new ArrayList JavaDoc();
51    private List JavaDoc resultColumns = new ArrayList JavaDoc();
52    private boolean distinct;
53    private int maxRows, skippedRows;
54
55     /** <p>Creates a new SelectStatement and sets the creating
56    * {@link org.apache.ws.jaxme.sqls.SQLFactory}.</p>
57      */

58     public SelectStatementImpl(SQLFactory pFactory) {
59         super(pFactory);
60     }
61
62     public void addOrderColumn(Object JavaDoc pColumn) {
63       addOrderColumn(pColumn, false);
64     }
65
66     public void addOrderColumn(Object JavaDoc pColumn, boolean pDescending) {
67       addOrderColumn(new OrderColumnImpl(pColumn, pDescending));
68     }
69
70     public void addOrderColumn(SelectStatement.OrderColumn pColumn) {
71         Object JavaDoc o = pColumn.getColumn();
72         if (o instanceof ColumnReference) {
73             for (Iterator JavaDoc iter = orderColumns.iterator(); iter.hasNext(); ) {
74                 SelectStatement.OrderColumn column = (SelectStatement.OrderColumn) iter.next();
75                 Object JavaDoc p = column.getColumn();
76                 if (p instanceof ColumnReference) {
77                     if (o.equals(p)) {
78                         throw new NullPointerException JavaDoc("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 JavaDoc getOrderColumns() {
89         return orderColumns.iterator();
90     }
91
92     public void addResultColumn(ColumnReference pColumn) {
93       resultColumns.add(pColumn);
94     }
95
96    public Iterator JavaDoc 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 JavaDoc getSelectTableReferences() {
133       return new Iterator JavaDoc() {
134          SelectTableReference reference = getSelectTableReference();
135          public boolean hasNext() { return reference != null; }
136          public Object JavaDoc next() {
137             SelectTableReference result = reference;
138             reference = result.getRightJoinedTableReference();
139             return result;
140          }
141          public void remove() {
142             throw new UnsupportedOperationException JavaDoc();
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 JavaDoc pName) {
152         return createView(pName == null ? null : new TableImpl.NameImpl(pName));
153     }
154 }
155
Popular Tags