001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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.referencing.factory.epsg;
017:
018: // J2SE dependencies
019: import java.sql.Statement;
020: import java.sql.Connection;
021: import java.sql.SQLException;
022: import java.util.regex.Pattern;
023: import java.util.regex.Matcher;
024:
025: // Geotools dependencies
026: import org.geotools.factory.Hints;
027:
028: /**
029: * Adapts SQL statements for HSQL. The HSQL database engine doesn't understand
030: * the parenthesis in (INNER JOIN ... ON) statements for the "BursaWolfParameters"
031: * query. Unfortunatly, those parenthesis are required by MS-Access. We need to
032: * removes them programmatically here.
033: *
034: * @since 2.2
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/epsg-hsql/src/main/java/org/geotools/referencing/factory/epsg/FactoryUsingHSQL.java $
036: * @version $Id: FactoryUsingHSQL.java 26328 2007-07-24 16:57:19Z desruisseaux $
037: * @author Martin Desruisseaux
038: *
039: * @deprecated Will be renamed as {@code HsqlDialectEpsgFactory} in Geotools 2.5.
040: */
041: final class FactoryUsingHSQL extends FactoryUsingAnsiSQL {
042: /**
043: * The regular expression pattern for searching the "FROM (" clause.
044: * This is the pattern for the opening parenthesis.
045: */
046: private static final Pattern OPENING_PATTERN = Pattern.compile(
047: "\\s+FROM\\s*\\(", Pattern.CASE_INSENSITIVE);
048:
049: /**
050: * Constructs the factory for the given connection to the HSQL database.
051: */
052: public FactoryUsingHSQL(final Hints hints,
053: final Connection connection) {
054: super (hints, connection);
055: }
056:
057: /**
058: * If the query contains a "FROM (" expression, remove the parenthesis.
059: */
060: public String adaptSQL(String query) {
061: query = super .adaptSQL(query);
062: final Matcher matcher = OPENING_PATTERN.matcher(query);
063: if (matcher.find()) {
064: final int opening = matcher.end() - 1;
065: final int length = query.length();
066: int closing = opening;
067: for (int count = 0;; closing++) {
068: if (closing >= length) {
069: // Should never happen with well formed SQL statement.
070: // If it happen anyway, don't change anything and let
071: // the HSQL driver produces a "syntax error" message.
072: return query;
073: }
074: switch (query.charAt(closing)) {
075: case '(':
076: count++;
077: break;
078: case ')':
079: count--;
080: break;
081: default:
082: continue;
083: }
084: if (count == 0) {
085: break;
086: }
087: }
088: query = query.substring(0, opening)
089: + query.substring(opening + 1, closing)
090: + query.substring(closing + 1);
091: }
092: return query;
093: }
094:
095: /**
096: * Shutdown the HSQL database engine. This method is invoked automatically at JVM
097: * shutdown time just before to close the connection.
098: */
099: protected void shutdown(final boolean active) throws SQLException {
100: if (active) {
101: final Statement statement = connection.createStatement();
102: statement.execute("SHUTDOWN");
103: statement.close();
104: }
105: super.shutdown(active);
106: }
107: }
|