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: * Created on 18-apr-2004
017: * TODO: 26-may-2005 D. Adler Make subclass of AbstractFIDFilter.
018: */
019: /*
020: * Created on 18-apr-2004
021: * 26-may-2005 D. Adler Make subclass of AbstractFIDFilter.
022: * 12-jul-2006 D. Adler GEOT-728 Refactor FIDMapper classes
023: */
024: package org.geotools.data.jdbc.fidmapper;
025:
026: import org.geotools.feature.Feature;
027: import java.io.IOException;
028: import java.math.BigInteger;
029: import java.sql.Connection;
030: import java.sql.Statement;
031: import java.sql.Types;
032:
033: /**
034: * Support primary key columns that are automatically generated by the
035: * database.
036: *
037: * @author wolf
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/fidmapper/AutoIncrementFIDMapper.java $
039: */
040: public class AutoIncrementFIDMapper extends AbstractFIDMapper {
041: private static final long serialVersionUID = 1L;
042:
043: /**
044: * Construct an AutoIncrementFIDMapper
045: *
046: * @param colName
047: * @param dataType
048: */
049: public AutoIncrementFIDMapper(String colName, int dataType) {
050: this (null, null, colName, dataType);
051: }
052:
053: /**
054: * Construct an AutoIncrementFIDMapper
055: *
056: * @param tableSchemaName
057: * @param tableName
058: * @param colName
059: * @param dataType
060: */
061: public AutoIncrementFIDMapper(String tableSchemaName,
062: String tableName, String colName, int dataType) {
063: super (tableSchemaName, tableName);
064: setInfo(colName, dataType, 0, 0, true);
065: }
066:
067: /**
068: * Construct an AutoIncrementFIDMapper
069: *
070: * @param tableName
071: * @param colName
072: * @param dataType
073: */
074: public AutoIncrementFIDMapper(String tableName, String colName,
075: int dataType) {
076: this (null, tableName, colName, dataType);
077: }
078:
079: /**
080: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getID(java.lang.Object[])
081: */
082: public String getID(Object[] attributes) {
083: if ((attributes != null) && (attributes.length == 1)
084: && (attributes[0] != null)) {
085: return attributes[0].toString();
086: } else {
087: return null;
088: }
089: }
090:
091: /**
092: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getPKAttributes(java.lang.String)
093: */
094: public Object[] getPKAttributes(String FID) throws IOException {
095: Object pk = null;
096:
097: switch (getColumnType(0)) {
098: case Types.INTEGER:
099: case Types.SMALLINT:
100: case Types.TINYINT:
101: try {
102: pk = new Integer(Integer.parseInt(FID));
103: } catch (NumberFormatException nfe) {
104: //if we get a really bad featureid we want to return something
105: //that will not mess up the database and throw an exception,
106: //we just want to not match against it, so we return -1
107: pk = new Integer(-1);
108: }
109: break;
110: case Types.BIGINT:
111: try {
112: pk = new BigInteger(FID);
113: } catch (NumberFormatException nfe) {
114: //if we get a really bad featureid we want to return something
115: //that will not mess up the database and throw an exception,
116: //we just want to not match against it, so we return -1
117: pk = new BigInteger("-1");
118: }
119: break;
120: case Types.NUMERIC:
121: try {
122: pk = new Long(Long.parseLong(FID));
123: } catch (NumberFormatException nfe) {
124: //if we get a really bad featureid we want to return something
125: //that will not mess up the database and throw an exception,
126: //we just want to not match against it, so we return -1
127: pk = new Integer(-1);
128: }
129: break;
130: case Types.VARCHAR: //unlikely to be used, but you never know...
131: case Types.LONGVARCHAR:
132: case Types.CHAR:
133: pk = new String(FID);
134: break;
135: }
136:
137: return new Object[] { pk };
138: }
139:
140: /**
141: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#createID(java.sql.Connection,
142: * org.geotools.feature.Feature, Statement)
143: */
144: public String createID(Connection conn, Feature feature,
145: Statement statement) throws IOException {
146: return null;
147: }
148: }
|