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.data.geometryless;
018:
019: import java.util.logging.Logger;
020:
021: import org.geotools.data.geometryless.filter.SQLEncoderLocationsXY; // import org.geotools.data.jdbc.DefaultSQLBuilder;
022: import org.geotools.data.jdbc.GeoAPISQLBuilder;
023: import org.geotools.data.jdbc.fidmapper.FIDMapper;
024: import org.geotools.data.sql.BypassSqlFeatureTypeHandler;
025: import org.geotools.data.sql.BypassSqlSQLBuilder;
026: import org.geotools.feature.AttributeType;
027: import org.geotools.feature.GeometryAttributeType;
028: import org.opengis.feature.type.Name;
029:
030: // import org.geotools.filter.SQLEncoder;
031:
032: /**
033: * A an extension of DefaultSQLBuilder, which supports point geometries that are
034: * specified with x,y columns
035: *
036: * @author Chris Holmes, TOPP
037: * @source $URL:
038: * http://svn.geotools.org/geotools/trunk/gt/modules/unsupported/geometryless/src/main/java/org/geotools/data/geometryless/LocationsXYSQLBuilder.java $
039: * @version $Id: LocationsXYSQLBuilder.java 25031 2007-04-05 09:52:31Z
040: * robatkinson $
041: */
042: public class LocationsXYSQLBuilder extends BypassSqlSQLBuilder {
043: /** The logger for the mysql module. */
044: private static final Logger LOGGER = org.geotools.util.logging.Logging
045: .getLogger("org.geotools.data.geometryless");
046:
047: private String xCoordColumnName = null;
048:
049: private String yCoordColumnName = null;
050:
051: private String geomName;
052:
053: public LocationsXYSQLBuilder(SQLEncoderLocationsXY encoder,
054: String geomName, String x, String y,
055: BypassSqlFeatureTypeHandler typeHandler) {
056: super (encoder, typeHandler);
057: this .geomName = geomName;
058: this .xCoordColumnName = x;
059: this .yCoordColumnName = y;
060: }
061:
062: /**
063: * Produces the select information required.
064: *
065: * <p>
066: * The featureType, if known, is always requested.
067: * </p>
068: *
069: * <p>
070: * sql: <code>featureID (,attributeColumn)</code>
071: * </p>
072: *
073: * <p>
074: * We may need to provide AttributeReaders with a hook so they can request a
075: * wrapper function.
076: * </p>
077: *
078: * @param sql
079: * @param mapper
080: * @param attributes
081: */
082: public void sqlColumns(StringBuffer sql, FIDMapper mapper,
083: AttributeType[] attributes) {
084: for (int i = 0; i < mapper.getColumnCount(); i++) {
085: LOGGER.finest(mapper.getColumnName(i));
086: sql.append(mapper.getColumnName(i));
087:
088: if ((attributes.length > 0)
089: || (i < (mapper.getColumnCount() - 1))) {
090: sql.append(", ");
091: }
092: }
093:
094: for (int i = 0; i < attributes.length; i++) {
095: String colName = attributes[i].getName();
096:
097: LOGGER.finest(attributes[i].getName() + " isGeom: "
098: + (attributes[i] instanceof GeometryAttributeType));
099:
100: // Here we want the x and y columns to be requested.
101: if (attributes[i] instanceof GeometryAttributeType) {
102:
103: sql.append(xCoordColumnName + ", " + yCoordColumnName);
104:
105: // "AsText(" + attributes[i].getName() + ") AS " +
106: // attributes[i].getName());
107: } else {
108: sql.append(colName);
109: }
110:
111: if (i < (attributes.length - 1)) {
112: sql.append(", ");
113: }
114: }
115: }
116:
117: public void sqlGeometryColumn(StringBuffer sql,
118: AttributeType geomAttribute) {
119: if (null == super .fieldAliases) {
120: sql.append(xCoordColumnName + ", " + yCoordColumnName);
121: } else {
122: String xSqlExpression = (String) fieldAliases
123: .get(xCoordColumnName);
124: String ySqlExpression = (String) fieldAliases
125: .get(yCoordColumnName);
126:
127: String xfieldName = xSqlExpression;
128: String yfieldName = ySqlExpression;
129:
130: if (!xCoordColumnName.equalsIgnoreCase(xSqlExpression)) {
131: xfieldName += " AS " + xCoordColumnName;
132: }
133: if (!yCoordColumnName.equalsIgnoreCase(ySqlExpression)) {
134: yfieldName += " AS " + yCoordColumnName;
135: }
136: sql.append(xfieldName);
137: sql.append(", ");
138: sql.append(yfieldName);
139: }
140: }
141: }
|