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.Collections;
021: import java.util.List;
022: import java.util.Map;
023:
024: import net.sf.jsqlparser.expression.Expression;
025: import net.sf.jsqlparser.schema.Column;
026: import net.sf.jsqlparser.schema.Table;
027: import net.sf.jsqlparser.statement.select.AllColumns;
028: import net.sf.jsqlparser.statement.select.AllTableColumns;
029: import net.sf.jsqlparser.statement.select.SelectExpressionItem;
030: import net.sf.jsqlparser.statement.select.SelectItem;
031:
032: import com.esri.sde.sdk.client.SeColumnDefinition;
033: import com.esri.sde.sdk.client.SeConnection;
034: import com.esri.sde.sdk.client.SeException;
035: import com.esri.sde.sdk.client.SeTable;
036:
037: /**
038: * Qualifies instances of {@link net.sf.jsqlparser.statement.select.SelectExpressionItem},
039: * and creates a list of qualified {@link net.sf.jsqlparser.statement.select.SelectExpressionItem}
040: * for each {@link net.sf.jsqlparser.statement.select.AllColumns} and
041: * {@link net.sf.jsqlparser.statement.select.AllTableColumns} instances. So, this visitor
042: * may produce more items than the visited.
043: *
044: * @author Gabriel Roldan, Axios Engineering
045: * @version $Id: SelectItemQualifier.java 29135 2008-02-07 19:49:09Z desruisseaux $
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/main/java/org/geotools/arcsde/data/view/SelectItemQualifier.java $
047: * @since 2.3.x
048: */
049: class SelectItemQualifier implements
050: net.sf.jsqlparser.statement.select.SelectItemVisitor {
051: /** DOCUMENT ME! */
052: private List /*<SelectExpressionItem>*/qualifiedItems = Collections.EMPTY_LIST;
053:
054: /** DOCUMENT ME! */
055: private SeConnection conn;
056:
057: private Map tableAliases;
058:
059: /**
060: * Creates a new SelectItemQualifier object.
061: *
062: * @param conn
063: * DOCUMENT ME!
064: */
065: private SelectItemQualifier(SeConnection conn, Map tableAliases) {
066: this .conn = conn;
067: this .tableAliases = tableAliases;
068: }
069:
070: /**
071: * DOCUMENT ME!
072: *
073: * @param conn
074: * DOCUMENT ME!
075: * @param item
076: * DOCUMENT ME!
077: *
078: * @return DOCUMENT ME!
079: */
080: public static List qualify(SeConnection conn, Map tableAliases,
081: SelectItem item) {
082: if (item == null) {
083: return null;
084: }
085:
086: SelectItemQualifier qualifier = new SelectItemQualifier(conn,
087: tableAliases);
088: item.accept(qualifier);
089:
090: return qualifier.qualifiedItems;
091: }
092:
093: /**
094: * DOCUMENT ME!
095: *
096: * @param allColumns
097: * DOCUMENT ME!
098: */
099: public void visit(AllColumns allColumns) {
100: this .qualifiedItems = Collections.singletonList(allColumns);
101: }
102:
103: /**
104: * DOCUMENT ME!
105: *
106: * @param allTableColumns
107: * DOCUMENT ME!
108: */
109: public void visit(AllTableColumns allTableColumns) {
110: AllTableColumns qualified = new AllTableColumns();
111:
112: Table qt = allTableColumns.getTable();
113: Table unaliasedTable = (Table) tableAliases.get(qt.getName());
114:
115: if (unaliasedTable == null) {
116: //not an aliased table, qualify it
117: qt = TableQualifier.qualify(conn, allTableColumns
118: .getTable());
119: } else {
120: //AllTableColumns is refering to an aliased table in the FROM clause,
121: //replace its table by the original one to get rid of the alias
122: qt = unaliasedTable;
123: }
124:
125: qualified.setTable(qt);
126:
127: String tableName = qt.getSchemaName() + "." + qt.getName();
128: SeTable table;
129: SeColumnDefinition[] cols;
130: try {
131: table = new SeTable(conn, tableName);
132: cols = table.describe();
133: } catch (SeException e) {
134: throw new RuntimeException(e.getMessage());
135: }
136:
137: qualifiedItems = new ArrayList(cols.length);
138:
139: for (int i = 0; i < cols.length; i++) {
140: String colName = cols[i].getName();
141:
142: Column column = new Column();
143: column.setTable(qt);
144: column.setColumnName(colName);
145:
146: SelectExpressionItem item = new SelectExpressionItem();
147: item.setExpression(column);
148:
149: qualifiedItems.add(item);
150: }
151: }
152:
153: /**
154: * DOCUMENT ME!
155: *
156: * @param selectExpressionItem
157: * DOCUMENT ME!
158: */
159: public void visit(SelectExpressionItem selectExpressionItem) {
160:
161: SelectExpressionItem qualifiedItem = new SelectExpressionItem();
162:
163: qualifiedItem.setAlias(selectExpressionItem.getAlias());
164:
165: Expression selectExpression = selectExpressionItem
166: .getExpression();
167:
168: Expression qualifiedExpression = ExpressionQualifier.qualify(
169: conn, tableAliases, selectExpression);
170:
171: qualifiedItem.setExpression(qualifiedExpression);
172:
173: this.qualifiedItems = Collections.singletonList(qualifiedItem);
174: }
175: }
|