001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, 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: package org.geotools.data.mysql;
017:
018: import org.geotools.data.jdbc.DefaultSQLBuilder;
019: import org.geotools.data.jdbc.fidmapper.FIDMapper;
020: import org.geotools.feature.AttributeType;
021: import org.geotools.feature.FeatureType;
022: import org.geotools.feature.GeometryAttributeType;
023: import org.geotools.filter.SQLEncoder;
024:
025: /**
026: * A MySQL-specific instance of DefaultSQLBuilder, which supports MySQL 4.1's geometric
027: * datatypes.
028: * @author Gary Sheppard garysheppard@psu.edu
029: * @author Andrea Aime aaime@users.sourceforge.net
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mysql/src/main/java/org/geotools/data/mysql/MySQLSQLBuilder.java $
031: */
032: public class MySQLSQLBuilder extends DefaultSQLBuilder {
033: private boolean wkbEnabled;
034:
035: /**
036: * @deprecated please use MySQLSQLBuilder(encoder, ft)
037: * @param encoder
038: */
039: public MySQLSQLBuilder(SQLEncoder encoder) {
040: super (encoder);
041: }
042:
043: public MySQLSQLBuilder(SQLEncoder encoder, FeatureType ft) {
044: super (encoder, ft, null);
045: }
046:
047: /**
048: * Produces the select information required.
049: *
050: * <p>
051: * The featureType, if known, is always requested.
052: * </p>
053: *
054: * <p>
055: * sql: <code>featureID (,attributeColumn)</code>
056: * </p>
057: *
058: * <p>
059: * We may need to provide AttributeReaders with a hook so they can request
060: * a wrapper function.
061: * </p>
062: *
063: * @param sql
064: * @param mapper
065: * @param attributes
066: */
067: public void sqlColumns(StringBuffer sql, FIDMapper mapper,
068: AttributeType[] attributes) {
069: for (int i = 0; i < mapper.getColumnCount(); i++) {
070: sql.append(mapper.getColumnName(i));
071: if (attributes.length > 0
072: || i < (mapper.getColumnCount() - 1)) {
073: sql.append(", ");
074: }
075: }
076:
077: for (int i = 0; i < attributes.length; i++) {
078: String colName = attributes[i].getName();
079:
080: if (attributes[i] instanceof GeometryAttributeType) {
081: if (wkbEnabled)
082: sql.append("AsBinary(");
083: else
084: sql.append("AsText(");
085: sql.append(attributes[i].getLocalName() + ") AS "
086: + attributes[i].getLocalName());
087: } else {
088: sql.append(colName);
089: }
090:
091: if (i < (attributes.length - 1)) {
092: sql.append(", ");
093: }
094: }
095: }
096:
097: /**
098: * Returns true if the WKB format is used to transfer geometries, false
099: * otherwise
100: *
101: */
102: public boolean isWKBEnabled() {
103: return wkbEnabled;
104: }
105:
106: /**
107: * If turned on, WKB will be used to transfer geometry data instead of WKT
108: *
109: * @param enabled
110: */
111: public void setWKBEnabled(boolean enabled) {
112: wkbEnabled = enabled;
113: }
114: }
|