01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) Copyright IBM Corporation, 2005-2007. All rights reserved.
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package org.geotools.data.db2;
18:
19: import java.io.IOException;
20: import java.sql.Connection;
21: import java.sql.ResultSet;
22: import java.sql.SQLException;
23: import java.sql.Statement;
24:
25: import org.geotools.data.jdbc.fidmapper.AutoIncrementFIDMapper;
26: import org.geotools.feature.Feature;
27:
28: /**
29: * Overrides AutoIncrementFIDMapper methods for DB2-specific handling.
30: *
31: * @author David Adler - IBM Corporation
32: */
33: public class DB2AutoIncrementFIDMapper extends AutoIncrementFIDMapper {
34:
35: /**
36: * Default constructor.
37: */
38: public DB2AutoIncrementFIDMapper(String databaseSchemaName,
39: String tableName, String colName, int dataType) {
40: super (databaseSchemaName, tableName, colName, dataType);
41:
42: }
43:
44: /**
45: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#createID(java.sql.Connection,
46: * org.geotools.feature.Feature, Statement)
47: */
48: public String createID(Connection conn, Feature feature,
49: Statement statement) throws IOException {
50: if (tableName == null || getColumnName() == null)
51: return null;
52: try {
53: String sql = "SELECT MAX(\"" + getColumnName()
54: + "\") FROM \"" + this .tableSchemaName + "\"."
55: + "\"" + tableName + "\"";
56: statement.execute(sql);
57: ResultSet resultSet = statement.getResultSet();
58: if (resultSet.next())
59: return resultSet.getString(1);
60: else
61: return null;
62: } catch (SQLException e) {
63: throw (IOException) new IOException().initCause(e);
64: }
65: }
66: }
|