001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.index.rtree.database;
017:
018: import java.sql.Connection;
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022:
023: /**
024: * DOCUMENT ME!
025: *
026: * @author Tommaso Nolli
027: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/index/rtree/database/AbstractDialect.java $
028: */
029: public abstract class AbstractDialect implements Dialect {
030: protected static final String CAT_TABLE = "rtrees_cat";
031: private static final String QUERY_CAT = "select * from "
032: + CAT_TABLE + " where rtree_name=?";
033: private static final String INSERT_CAT = "insert into " + CAT_TABLE
034: + " (rtree_name, min_entries, max_entries, split_alg)"
035: + " values (?, ?, ?, ?)";
036: private int current = -1;
037:
038: /**
039: * @see org.geotools.index.rtree.database.Dialect#getCatalogQuery()
040: */
041: public String getCatalogQuery() {
042: return QUERY_CAT;
043: }
044:
045: /**
046: * @see org.geotools.index.rtree.database.Dialect#getCatalogInsert()
047: */
048: public String getCatalogInsert() {
049: return INSERT_CAT;
050: }
051:
052: /**
053: * This implementation works only in one JVM
054: *
055: * @see org.geotools.index.rtree.database.Dialect#getNextPageId(java.sql.Connection,
056: * java.lang.String)
057: */
058: public synchronized int getNextPageId(Connection cnn,
059: String tableName) throws SQLException {
060: if (current == -1) {
061: PreparedStatement pst = null;
062: ResultSet rs = null;
063:
064: try {
065: pst = cnn.prepareStatement("select max(page_id) from "
066: + tableName);
067: rs = pst.executeQuery();
068: rs.next();
069:
070: this .current = rs.getInt(1);
071: } finally {
072: try {
073: rs.close();
074: } catch (Exception ee) {
075: }
076:
077: try {
078: pst.close();
079: } catch (Exception ee) {
080: }
081: }
082: }
083:
084: return ++this .current;
085: }
086:
087: /**
088: * @see org.geotools.index.rtree.database.Dialect#getSelectPage(java.lang.String)
089: */
090: public String getSelectPage(String tableName) {
091: return "select * from " + tableName + " where page_id=?";
092: }
093:
094: /**
095: * @see org.geotools.index.rtree.database.Dialect#getInsertPage(java.lang.String)
096: */
097: public String getInsertPage(String tableName) {
098: return "insert into " + tableName
099: + " (page_id, fl_leaf, blob_content)"
100: + " values (?,?,?)";
101: }
102:
103: /**
104: * @see org.geotools.index.rtree.database.Dialect#getUpdatePage(java.lang.String)
105: */
106: public String getUpdatePage(String tableName) {
107: return "update " + tableName
108: + " set fl_leaf=?, blob_content=? where page_id=?";
109: }
110: }
|