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.postgis.fidmapper;
017:
018: import java.io.IOException;
019: import java.sql.Connection;
020: import java.sql.SQLException;
021: import java.sql.Statement;
022: import java.sql.Types;
023:
024: import org.geotools.data.DataSourceException;
025: import org.geotools.data.jdbc.datasource.DataSourceFinder;
026: import org.geotools.data.jdbc.datasource.UnWrapper;
027: import org.geotools.data.jdbc.fidmapper.AbstractFIDMapper;
028: import org.geotools.feature.Feature;
029: import org.postgresql.PGStatement;
030:
031: /**
032: * Supports the creation of a Feature ID based on the Potgres row OID field.
033: * <p>
034: * This is <b>NOT</b> a stable approach for FID (as updates and so on will change the
035: * OID), but it will be our best guess in the case of read only access where an
036: * index is not present.
037: *
038: * @author wolf
039: */
040: public class OIDFidMapper extends AbstractFIDMapper {
041:
042: /** <code>serialVersionUID</code> field */
043: private static final long serialVersionUID = 3257569520561763632L;
044:
045: /**
046: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#initSupportStructures()
047: */
048: public void initSupportStructures() {
049: // nothing to do, oid are supported in the table or not depending on
050: // database configuration and table creation commands (not sure)
051: }
052:
053: /**
054: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getID(java.lang.Object[])
055: */
056: public String getID(Object[] attributes) {
057: return attributes[0].toString();
058: }
059:
060: /**
061: * Will always return an emtpy array since OIDs are not updatable,
062: * so we don't try to parse the Feature ID at all.
063: * Um - this causes failures in SQLEncoder - that may be the place
064: * to fix it, but I'm putting it in here for now. I believe that
065: * the oid will not try to get updated since auto increment
066: * is set to false.
067: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getPKAttributes(java.lang.String)
068: */
069: public Object[] getPKAttributes(String FID) throws IOException {
070: try {
071: return new Object[] { new Long(Long.parseLong(FID)) };
072: } catch (NumberFormatException nfe) {
073: //if we get a really bad featureid we want to return something
074: //that will not mess up the database and throw an exception,
075: //we just want to not match against it, so we return -1
076: return new Object[] { new Integer(-1) };
077: }
078: }
079:
080: /**
081: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#createID(java.sql.Connection, org.geotools.feature.Feature, Statement)
082: */
083: public String createID(Connection conn, Feature feature,
084: Statement statement) throws IOException {
085: try {
086: if (!(statement instanceof PGStatement)) {
087: UnWrapper uw = DataSourceFinder.getUnWrapper(statement);
088: if (uw != null)
089: statement = uw.unwrap(statement);
090: }
091: PGStatement pgStatement = (PGStatement) statement;
092: return String.valueOf(pgStatement.getLastOID());
093: } catch (SQLException e) {
094: throw new DataSourceException(
095: "Problems occurred while getting last generate oid from Postgresql statement",
096: e);
097: } catch (ClassCastException e) {
098: throw new DataSourceException(
099: "Statement is not a PGStatement. OIDFidMapper can be used only with Postgres!",
100: e);
101: }
102: }
103:
104: /**
105: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#returnFIDColumnsAsAttributes()
106: */
107: public boolean returnFIDColumnsAsAttributes() {
108: return false;
109: }
110:
111: /**
112: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnCount()
113: */
114: public int getColumnCount() {
115: return 1;
116: }
117:
118: /**
119: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnName(int)
120: */
121: public String getColumnName(int colIndex) {
122: return "oid";
123: }
124:
125: /**
126: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnType(int)
127: */
128: public int getColumnType(int colIndex) {
129: return Types.NUMERIC;
130: }
131:
132: /**
133: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnSize(int)
134: */
135: public int getColumnSize(int colIndex) {
136: return 8;
137: }
138:
139: /**
140: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnDecimalDigits(int)
141: */
142: public int getColumnDecimalDigits(int colIndex) {
143: return 0;
144: }
145:
146: /**
147: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#isAutoIncrement(int)
148: */
149: public boolean isAutoIncrement(int colIndex) {
150: return true;
151: }
152:
153: }
|