001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.arcsde.data.view;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import net.sf.jsqlparser.expression.Expression;
026: import net.sf.jsqlparser.schema.Table;
027: import net.sf.jsqlparser.statement.select.FromItem;
028: import net.sf.jsqlparser.statement.select.OrderByElement;
029: import net.sf.jsqlparser.statement.select.PlainSelect;
030: import net.sf.jsqlparser.statement.select.SelectItem;
031: import net.sf.jsqlparser.statement.select.Union;
032:
033: import com.esri.sde.sdk.client.SeConnection;
034:
035: /**
036: * Visitor on a PlainSelect that produces another one but with all the table
037: * names and field names fully qualified as expected by ArcSDE.
038: *
039: * <p>
040: * At any time may throw an IllegalArgumentException if a table or field name
041: * stated in the PlainSelect is not found on the arcsde instance.
042: * </p>
043: *
044: * <p>
045: * Usage:
046: * <pre>
047: * <code>
048: * PlainSelect unqualifiedSelect = ...
049: * SeConnection conn = ...
050: * SelectVisitor visitor = new SelectVisitor(conn);
051: * visitor.accept(unqualifiedSelect);
052: *
053: * PlainSelect qualifiedSelect = visitor.getQualifiedQuery();
054: * </code>
055: * </pre>
056: * </p>
057: *
058: * @author Gabriel Roldan, Axios Engineering
059: * @version $Id: SelectQualifier.java 29135 2008-02-07 19:49:09Z desruisseaux $
060: *
061: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/main/java/org/geotools/arcsde/data/view/SelectQualifier.java $
062: * @since 2.3.x
063: */
064: public class SelectQualifier implements
065: net.sf.jsqlparser.statement.select.SelectVisitor {
066: /** DOCUMENT ME! */
067: private SeConnection conn;
068:
069: /** DOCUMENT ME! */
070: private PlainSelect qualifiedSelect;
071:
072: /**
073: * Creates a new SelectQualifier object.
074: *
075: * @param conn DOCUMENT ME!
076: */
077: public SelectQualifier(SeConnection conn) {
078: this .conn = conn;
079: }
080:
081: public static PlainSelect qualify(SeConnection conn,
082: PlainSelect select) {
083: SelectQualifier q = new SelectQualifier(conn);
084: select.accept(q);
085: return q.qualifiedSelect;
086: }
087:
088: /**
089: * DOCUMENT ME!
090: *
091: * @return DOCUMENT ME!
092: *
093: * @throws IllegalStateException DOCUMENT ME!
094: */
095: public PlainSelect getQualifiedQuery() {
096: if (qualifiedSelect == null) {
097: throw new IllegalStateException("not created yet");
098: }
099:
100: return qualifiedSelect;
101: }
102:
103: /**
104: * DOCUMENT ME!
105: *
106: * @param plainSelect DOCUMENT ME!
107: *
108: * @throws IllegalStateException DOCUMENT ME!
109: */
110: public void visit(PlainSelect plainSelect)
111: throws IllegalStateException {
112: qualifiedSelect = new PlainSelect();
113:
114: List fromItems = qualifyFromItems(plainSelect.getFromItems());
115: qualifiedSelect.setFromItems(fromItems);
116:
117: Map /*<String,Table>*/aliases = extractTableAliases(fromItems);
118:
119: fromItems = removeTableAliases(fromItems);
120:
121: List selectItems = qualifySelectItems(aliases, plainSelect
122: .getSelectItems());
123: qualifiedSelect.setSelectItems(selectItems);
124:
125: Expression where = qualifyWhere(aliases, plainSelect.getWhere());
126: qualifiedSelect.setWhere(where);
127:
128: List orderByItems = qualifyOrderBy(aliases, plainSelect
129: .getOrderByElements());
130: qualifiedSelect.setOrderByElements(orderByItems);
131: }
132:
133: private Map extractTableAliases(List fromItems) {
134: Map aliases = new HashMap();
135: for (Iterator it = fromItems.iterator(); it.hasNext();) {
136: FromItem fromItem = (FromItem) it.next();
137: if (fromItem instanceof Table) {
138: Table table = (Table) fromItem;
139: String alias = table.getAlias();
140: if (alias != null) {
141: aliases.put(alias, table);
142: }
143: }
144: }
145: return aliases;
146: }
147:
148: private List removeTableAliases(List fromItems) {
149: fromItems = new ArrayList(fromItems);
150:
151: for (Iterator it = fromItems.iterator(); it.hasNext();) {
152:
153: FromItem fromItem = (FromItem) it.next();
154:
155: if (fromItem instanceof Table) {
156: Table table = (Table) fromItem;
157: table.setAlias(null);
158: }
159: }
160: return fromItems;
161: }
162:
163: /**
164: * DOCUMENT ME!
165: *
166: * @param where DOCUMENT ME!
167: *
168: * @return DOCUMENT ME!
169: */
170: private Expression qualifyWhere(Map tableAliases, Expression where) {
171: if (where == null) {
172: return null;
173: }
174:
175: Expression qualifiedWhere = ExpressionQualifier.qualify(conn,
176: tableAliases, where);
177:
178: return qualifiedWhere;
179: }
180:
181: /**
182: * DOCUMENT ME!
183: *
184: * @param orderByElements DOCUMENT ME!
185: *
186: * @return DOCUMENT ME!
187: */
188: private List qualifyOrderBy(Map tableAliases, List orderByElements) {
189: if (orderByElements == null) {
190: return null;
191: }
192:
193: List qualifiedOrderElems = new ArrayList();
194:
195: for (Iterator it = orderByElements.iterator(); it.hasNext();) {
196: OrderByElement orderByElem = (OrderByElement) it.next();
197:
198: OrderByElement qualified = OrderByElementQualifier.qualify(
199: conn, tableAliases, orderByElem);
200:
201: qualifiedOrderElems.add(qualified);
202: }
203:
204: return qualifiedOrderElems;
205: }
206:
207: /**
208: * DOCUMENT ME!
209: *
210: * @param selectItems List<{@link SelectItem}>
211: *
212: * @return DOCUMENT ME!
213: */
214: private List qualifySelectItems(Map tableAlias, List selectItems) {
215: if (selectItems == null) {
216: return null;
217: }
218:
219: List qualifiedItems = new ArrayList();
220:
221: for (Iterator it = selectItems.iterator(); it.hasNext();) {
222: SelectItem selectItem = (SelectItem) it.next();
223:
224: List items = SelectItemQualifier.qualify(conn, tableAlias,
225: selectItem);
226:
227: qualifiedItems.addAll(items);
228: }
229:
230: return qualifiedItems;
231: }
232:
233: /**
234: * DOCUMENT ME!
235: *
236: * @param fromItems List<{@link FromItem}>
237: *
238: * @return DOCUMENT ME!
239: */
240: private List qualifyFromItems(List fromItems) {
241: if (fromItems == null) {
242: return null;
243: }
244:
245: List qualifiedFromItems = new ArrayList();
246:
247: for (Iterator it = fromItems.iterator(); it.hasNext();) {
248: FromItem fromItem = (FromItem) it.next();
249:
250: FromItem qualifiedItem = FromItemQualifier.qualify(conn,
251: fromItem);
252:
253: qualifiedFromItems.add(qualifiedItem);
254: }
255:
256: return qualifiedFromItems;
257: }
258:
259: /**
260: * DOCUMENT ME!
261: *
262: * @param union DOCUMENT ME!
263: *
264: * @throws UnsupportedOperationException DOCUMENT ME!
265: */
266: public void visit(Union union) {
267: throw new UnsupportedOperationException();
268: }
269: }
|