Statistics
| Revision:

root / trunk / libraries / geotools-2.1.1-epsg-hsql / src / org / geotools / referencing / factory / epsg / FactoryUsingHSQL.java @ 28854

History | View | Annotate | Download (3.71 KB)

1
/*
2
 * Geotools 2 - OpenSource mapping toolkit
3
 * (C) 2005, Geotools Project Managment Committee (PMC)
4
 *
5
 *    This library is free software; you can redistribute it and/or
6
 *    modify it under the terms of the GNU Lesser General Public
7
 *    License as published by the Free Software Foundation; either
8
 *    version 2.1 of the License, or (at your option) any later version.
9
 *
10
 *    This library is distributed in the hope that it will be useful,
11
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 *    Lesser General Public License for more details.
14
 *
15
 *    You should have received a copy of the GNU Lesser General Public
16
 *    License along with this library; if not, write to the Free Software
17
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package org.geotools.referencing.factory.epsg;
20

    
21
// J2SE dependencies
22
import java.sql.Statement;
23
import java.sql.Connection;
24
import java.sql.SQLException;
25
import java.util.regex.Pattern;
26
import java.util.regex.Matcher;
27

    
28
// Geotools dependencies
29
import org.geotools.factory.Hints;
30
import org.geotools.referencing.factory.FactoryGroup;
31

    
32

    
33
/**
34
 * Adapts SQL statements for HSQL. The HSQL database engine doesn't understand
35
 * the parenthesis in (INNER JOIN ... ON) statements for the "BursaWolfParameters"
36
 * query. Unfortunatly, those parenthesis are required by MS-Access. We need to
37
 * removes them programmatically here.
38
 *
39
 * @version $Id: FactoryUsingHSQL.java 14624 2005-06-29 02:19:08Z desruisseaux $
40
 * @author Martin Desruisseaux
41
 *
42
 * @since 2.2
43
 */
44
final class FactoryUsingHSQL extends FactoryUsingAnsiSQL {
45
    /**
46
     * The regular expression pattern for searching the "FROM (" clause.
47
     * This is the pattern for the opening parenthesis.
48
     */
49
    private static final Pattern OPENING_PATTERN =
50
            Pattern.compile("\\s+FROM\\s*\\(",
51
            Pattern.CASE_INSENSITIVE);
52

    
53
    /**
54
     * Constructs the factory for the given connection to the HSQL database.
55
     */
56
    public FactoryUsingHSQL(final FactoryGroup factories, final Connection connection) {
57
        super(factories, connection);
58
    }
59

    
60
    /**
61
     * If the query contains a "FROM (" expression, remove the parenthesis.
62
     */
63
    public String adaptSQL(String query) {
64
        query = super.adaptSQL(query);
65
        final Matcher matcher = OPENING_PATTERN.matcher(query);
66
        if (matcher.find()) {
67
            final int opening = matcher.end()-1;
68
            final int length  = query.length();
69
            int closing = opening;
70
            for (int count=0; ; closing++) {
71
                if (closing >= length) {
72
                    // Should never happen with well formed SQL statement.
73
                    // If it happen anyway, don't change anything and let
74
                    // the HSQL driver produces a "syntax error" message.
75
                    return query;
76
                }
77
                switch (query.charAt(closing)) {
78
                    case '(': count++; break;
79
                    case ')': count--; break;
80
                    default : continue;
81
                }
82
                if (count == 0) {
83
                    break;
84
                }
85
            }
86
            query = query.substring(0,         opening) +
87
                    query.substring(opening+1, closing) +
88
                    query.substring(closing+1);
89
        }
90
        return query;
91
    }
92

    
93
    /**
94
     * Shutdown the HSQL database engine.
95
     */
96
    void shutdown(final boolean active) throws SQLException {
97
        if (active) {
98
            final Statement statement = connection.createStatement();
99
            statement.execute("SHUTDOWN");
100
            statement.close();
101
        }
102
        super.shutdown(active);
103
    }
104
}