001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) Copyright IBM Corporation, 2005-2007. All rights reserved.
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.db2;
018:
019: import com.vividsolutions.jts.geom.Point;
020: import org.geotools.data.DefaultQuery;
021: import org.geotools.data.FeatureSource;
022:
023: import org.geotools.data.db2.filter.SQLEncoderDB2;
024: import org.geotools.factory.CommonFactoryFinder;
025: import org.geotools.feature.AttributeType;
026: import org.geotools.feature.AttributeTypeFactory;
027: import org.geotools.feature.FeatureType;
028: import org.geotools.filter.SQLEncoderException;
029:
030: import org.opengis.filter.Filter;
031: import org.opengis.filter.FilterFactory;
032: import org.opengis.filter.Id;
033: import org.opengis.filter.PropertyIsEqualTo;
034: import org.opengis.filter.PropertyIsLike;
035: import org.opengis.filter.expression.Literal;
036: import org.opengis.filter.expression.PropertyName;
037: import org.opengis.filter.identity.FeatureId;
038:
039: import java.io.IOException;
040: import java.util.HashSet;
041: import java.util.Set;
042:
043: /**
044: * Exercise DB2SQLBuilder.
045: *
046: * @author David Adler - IBM Corporation
047: * @source $URL:
048: * http://svn.geotools.org/geotools/trunk/gt/modules/unsupported/db2/src/test/java/org/geotools/data/db2/DB2SQLBuilderTest.java $
049: */
050: public class DB2SQLBuilderTest extends DB2TestCase {
051: private DB2SQLBuilder sqlBuilder = null;
052:
053: private DB2DataStore dataStore = null;
054:
055: /**
056: * Setup creates an encoder and SQLBuilder
057: *
058: * @throws Exception
059: */
060: public void setUp() throws Exception {
061: super .setUp();
062: this .dataStore = getDataStore();
063:
064: SQLEncoderDB2 encoder = new SQLEncoderDB2();
065: encoder.setSqlNameEscape("\"");
066: sqlBuilder = (DB2SQLBuilder) dataStore.getSqlBuilder("Places");
067: }
068:
069: public void testFidFilter() throws SQLEncoderException, IOException {
070: String typeName = "Places";
071: FeatureSource fs = dataStore.getFeatureSource("Places");
072: FeatureType ft = fs.getSchema();
073: FilterFactory ff2 = CommonFactoryFinder.getFilterFactory(null);
074:
075: Set ids = new HashSet();
076:
077: FeatureId fid1 = ff2.featureId("1");
078: ids.add(fid1);
079: Id ff3 = CommonFactoryFinder.getFilterFactory(null).id(ids);
080: sqlBuilder = (DB2SQLBuilder) dataStore.getSqlBuilder("Places");
081: DefaultQuery query = new DefaultQuery("Places", ff3);
082: Filter preFilter = sqlBuilder.getPreQueryFilter(query
083: .getFilter());
084: Filter postFilter = sqlBuilder.getPostQueryFilter(query
085: .getFilter());
086: String[] attrNames = new String[ft.getAttributeCount()];
087: AttributeType[] attrTypes = new AttributeType[ft
088: .getAttributeCount()];
089:
090: for (int i = 0; i < ft.getAttributeCount(); i++) {
091: attrNames[i] = ft.getAttributeType(i).getName();
092: attrTypes[i] = ft.getAttributeType(i);
093: }
094:
095: String fidQuery = this .sqlBuilder.buildSQLQuery("Places",
096: this .dataStore.getFIDMapper("Places"), attrTypes,
097: preFilter);
098: assertEquals(
099: "FID encoding failed",
100: "SELECT \"Id\", \"Name\", DB2GSE.ST_AsText(\"Geom\") FROM \"Test\".\"Places\" WHERE (\"Id\" = 1)",
101: fidQuery);
102:
103: FeatureId fid2 = ff2.featureId("2");
104: ids.add(fid2);
105: ff3 = CommonFactoryFinder.getFilterFactory(null).id(ids);
106:
107: query = new DefaultQuery("Places", ff3);
108: preFilter = sqlBuilder.getPreQueryFilter(query.getFilter());
109: fidQuery = this .sqlBuilder.buildSQLQuery("Places",
110: this .dataStore.getFIDMapper("Places"), attrTypes,
111: preFilter);
112: assertEquals(
113: "FID encoding failed",
114: "SELECT \"Id\", \"Name\", DB2GSE.ST_AsText(\"Geom\") FROM \"Test\".\"Places\" WHERE (\"Id\" = 2) OR (\"Id\" = 1)",
115: fidQuery);
116: }
117:
118: public void testCompareFilter() throws SQLEncoderException,
119: IOException {
120: String typeName = "Places";
121: FeatureSource fs = dataStore.getFeatureSource("Places");
122: FeatureType ft = fs.getSchema();
123: org.opengis.filter.FilterFactory ff = CommonFactoryFinder
124: .getFilterFactory(null);
125:
126: PropertyName column = ff.property("Name");
127: Literal compareValue = ff.literal("Zena");
128: PropertyIsEqualTo filter = ff.equals(column, compareValue);
129:
130: String[] attrNames = new String[ft.getAttributeCount()];
131: AttributeType[] attrTypes = new AttributeType[ft
132: .getAttributeCount()];
133:
134: for (int i = 0; i < ft.getAttributeCount(); i++) {
135: attrNames[i] = ft.getAttributeType(i).getName();
136: attrTypes[i] = ft.getAttributeType(i);
137: }
138: sqlBuilder = (DB2SQLBuilder) dataStore.getSqlBuilder("Places");
139: DefaultQuery query = new DefaultQuery("Places", filter);
140: Filter preFilter = sqlBuilder.getPreQueryFilter(query
141: .getFilter());
142: Filter postFilter = sqlBuilder.getPostQueryFilter(query
143: .getFilter());
144: String compareQuery = this .sqlBuilder.buildSQLQuery("Places",
145: this .dataStore.getFIDMapper("Places"), attrTypes,
146: preFilter);
147: assertEquals(
148: "compare encoding failed",
149: "SELECT \"Id\", \"Name\", DB2GSE.ST_AsText(\"Geom\") FROM \"Test\".\"Places\" WHERE \"Name\" = 'Zena'",
150: compareQuery);
151:
152: sqlBuilder = (DB2SQLBuilder) dataStore.getSqlBuilder("Roads");
153: column = ff.property("Length");
154: compareValue = ff.literal(2.5);
155: filter = ff.equals(column, compareValue);
156: query = new DefaultQuery("Roads", filter);
157: preFilter = sqlBuilder.getPreQueryFilter(query.getFilter());
158: postFilter = sqlBuilder.getPostQueryFilter(query.getFilter());
159: compareQuery = this .sqlBuilder.buildSQLQuery("Roads",
160: this .dataStore.getFIDMapper("Roads"), attrTypes,
161: preFilter);
162: assertEquals(
163: "compare encoding failed",
164: "SELECT \"ID\", \"Name\", DB2GSE.ST_AsText(\"Geom\") FROM \"Test\".\"Roads\" WHERE \"Length\" = 2.5",
165: compareQuery);
166: }
167:
168: public void testLikeFilter() throws SQLEncoderException,
169: IOException {
170: String typeName = "Places";
171: FeatureSource fs = dataStore.getFeatureSource("Places");
172: FeatureType ft = fs.getSchema();
173:
174: org.opengis.filter.FilterFactory ff = CommonFactoryFinder
175: .getFilterFactory(null);
176: PropertyName column = ff.property("Name");
177: Literal compareValue = ff.literal("Zena");
178: PropertyIsEqualTo filter = ff.equals(column, compareValue);
179:
180: String pattern = "s_met*s";
181: PropertyIsLike lf = ff.like(column, pattern);
182: sqlBuilder = (DB2SQLBuilder) dataStore.getSqlBuilder("Places");
183: DefaultQuery query = new DefaultQuery("Places", lf);
184: Filter preFilter = sqlBuilder.getPreQueryFilter(query
185: .getFilter());
186: Filter postFilter = sqlBuilder.getPostQueryFilter(query
187: .getFilter());
188: String[] attrNames = new String[ft.getAttributeCount()];
189: AttributeType[] attrTypes = new AttributeType[ft
190: .getAttributeCount()];
191:
192: for (int i = 0; i < ft.getAttributeCount(); i++) {
193: attrNames[i] = ft.getAttributeType(i).getName();
194: attrTypes[i] = ft.getAttributeType(i);
195: }
196:
197: String likeQuery = this .sqlBuilder.buildSQLQuery("Places",
198: this .dataStore.getFIDMapper("Places"), attrTypes,
199: preFilter);
200: assertEquals(
201: "LIKE encoding failed",
202: "SELECT \"Id\", \"Name\", DB2GSE.ST_AsText(\"Geom\") FROM \"Test\".\"Places\" WHERE \"Name\" LIKE 's_met%s' ",
203: likeQuery);
204: }
205:
206: public void testSqlFrom() {
207: StringBuffer sb;
208: sb = new StringBuffer();
209: sqlBuilder.sqlFrom(sb, "Test");
210: assertEquals("Encoding didn't match",
211: " FROM \"Test\".\"Test\"", sb.toString());
212: }
213:
214: public void testSqlGeometryColumn() {
215: String columnName = "Geom";
216: Class geomClass = Point.class;
217: AttributeType geomAttr = AttributeTypeFactory.newAttributeType(
218: columnName, geomClass);
219:
220: StringBuffer sb = new StringBuffer();
221: this .sqlBuilder.sqlGeometryColumn(sb, geomAttr);
222: assertEquals("Encoding didn't match",
223: "DB2GSE.ST_AsText(\"Geom\")", sb.toString());
224: }
225: }
|