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.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import net.sf.jsqlparser.expression.Expression;
027: import net.sf.jsqlparser.schema.Table;
028: import net.sf.jsqlparser.statement.select.AllColumns;
029: import net.sf.jsqlparser.statement.select.AllTableColumns;
030: import net.sf.jsqlparser.statement.select.FromItem;
031: import net.sf.jsqlparser.statement.select.PlainSelect;
032: import net.sf.jsqlparser.statement.select.SelectExpressionItem;
033: import net.sf.jsqlparser.statement.select.SelectItem;
034:
035: import com.esri.sde.sdk.client.SeColumnDefinition;
036: import com.esri.sde.sdk.client.SeConnection;
037: import com.esri.sde.sdk.client.SeException;
038: import com.esri.sde.sdk.client.SeQueryInfo;
039: import com.esri.sde.sdk.client.SeSqlConstruct;
040: import com.esri.sde.sdk.client.SeTable;
041:
042: /**
043: * Visits a {@link net.sf.jsqlparser.statement.select.PlainSelect} SQL SELECT construct
044: * to create the correspondent {@link com.esri.sde.sdk.client.SeQueryInfo} object, that
045: * can be used as an in process view definition of ArcSDE Java API.
046: *
047: * @author Gabriel Roldan, Axios Engineering
048: * @version $Id: QueryInfoParser.java 29135 2008-02-07 19:49:09Z desruisseaux $
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/main/java/org/geotools/arcsde/data/view/QueryInfoParser.java $
050: * @since 2.3.x
051: */
052: public class QueryInfoParser {
053: private static final Logger LOGGER = org.geotools.util.logging.Logging
054: .getLogger(QueryInfoParser.class.getPackage().getName());
055:
056: public static SeQueryInfo parse(SeConnection conn,
057: PlainSelect select) throws SeException, IOException {
058: String[] columns = null;
059: String[] tables = null;
060: String where = null;
061: String orderAndOrGroupByClause = null;
062:
063: if (LOGGER.isLoggable(Level.FINER)) {
064: LOGGER.finer("building SeQueryInfo to reflect " + select);
065: }
066:
067: //obtain needed SeQueryInfo components
068:
069: columns = getColumns(conn, select.getSelectItems());
070: tables = getTables(select.getFromItems());
071:
072: Expression whereClause = select.getWhere();
073: if (whereClause != null) {
074: where = whereClause.toString();
075: }
076:
077: if (select.getGroupByColumnReferences() != null
078: && select.getGroupByColumnReferences().size() > 0) {
079: String gb = PlainSelect.getFormatedList(select
080: .getGroupByColumnReferences(), " GROUP BY ");
081: orderAndOrGroupByClause = gb;
082: }
083: if (select.getOrderByElements() != null
084: && select.getOrderByElements().size() > 0) {
085: String ob = PlainSelect.orderByToString(select
086: .getOrderByElements());
087: if (orderAndOrGroupByClause == null) {
088: orderAndOrGroupByClause = "";
089: }
090: orderAndOrGroupByClause += " " + ob;
091: }
092:
093: //build SeQueryInfo
094: SeQueryInfo qinfo = new SeQueryInfo();
095: qinfo.setColumns(columns);
096:
097: SeSqlConstruct sqlConstruct = new SeSqlConstruct();
098: sqlConstruct.setTables(tables);
099: if (where != null) {
100: sqlConstruct.setWhere(where);
101: }
102:
103: qinfo.setConstruct(sqlConstruct);
104:
105: if (orderAndOrGroupByClause != null) {
106: qinfo.setByClause(orderAndOrGroupByClause);
107: }
108:
109: return qinfo;
110: }
111:
112: private static String[] getTables(List fromItems) {
113: if (fromItems == null) {
114: throw new NullPointerException("fromItems");
115: }
116:
117: List tableNames = new ArrayList(fromItems.size());
118:
119: for (Iterator it = fromItems.iterator(); it.hasNext();) {
120: FromItem fromItem = (FromItem) it.next();
121: String fromItemDef = fromItem.toString();
122: tableNames.add(fromItemDef);
123: }
124: return (String[]) tableNames.toArray(new String[tableNames
125: .size()]);
126: }
127:
128: /**
129: *
130: * @param selectItems
131: * @return <code>null</code> if <code>selectItems</code> is null or contains
132: * only an {@link net.sf.jsqlparser.statement.select.AllColumns}
133: */
134: private static String[] getColumns(SeConnection conn,
135: List selectItems) throws SeException {
136: if (selectItems == null || selectItems.size() == 0) {
137: return null;
138: }
139:
140: SelectItem item;
141: List colNames = new ArrayList(selectItems.size());
142: for (Iterator it = selectItems.iterator(); it.hasNext();) {
143: item = (SelectItem) it.next();
144: if (item instanceof AllColumns) {
145: continue;
146: } else if (item instanceof AllTableColumns) {
147: AllTableColumns allTableCols = (AllTableColumns) item;
148: Table table = allTableCols.getTable();
149: List tableColNames = getTableColumns(conn, table);
150: colNames.addAll(tableColNames);
151: } else if (item instanceof SelectExpressionItem) {
152: String stringItem = item.toString();
153: colNames.add(stringItem);
154: } else {
155: throw new RuntimeException("unknown select item type: "
156: + item);
157: }
158: }
159:
160: String[] columns = (String[]) colNames
161: .toArray(new String[colNames.size()]);
162: return columns;
163: }
164:
165: private static List getTableColumns(SeConnection conn, Table table)
166: throws SeException {
167: List colNames = new ArrayList();
168: String tableName = table.getSchemaName() + "."
169: + table.getName();
170: SeTable seTable = new SeTable(conn, tableName);
171: SeColumnDefinition[] cols = seTable.describe();
172: for (int i = 0; i < cols.length; i++) {
173: String colName = cols[i].getName();
174: colName = tableName + "." + colName;
175: colNames.add(colName);
176: }
177: return colNames;
178: }
179: }
|