01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
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: package org.geotools.data.hsql.fidmapper;
17:
18: import java.io.IOException;
19: import java.sql.Connection;
20:
21: import org.geotools.data.jdbc.fidmapper.DefaultFIDMapperFactory;
22: import org.geotools.data.jdbc.fidmapper.FIDMapper;
23:
24: /**
25: * This factory is only needed so it can be used as a hook to call
26: * the HsqlFIDMapper.
27: *
28: * @author Amr Alam, Refractions Research
29: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/hsql/src/main/java/org/geotools/data/hsql/fidmapper/HsqlFIDMapperFactory.java $
30: */
31: public class HsqlFIDMapperFactory extends DefaultFIDMapperFactory {
32:
33: /**
34: * Gets the appropriate FIDMapper for the specified table.
35: *
36: * @param catalog
37: * @param schema
38: * @param tableName
39: * @param connection the active database connection to get table key
40: * information
41: *
42: * @return the appropriate FIDMapper for the specified table.
43: *
44: * @throws IOException if any error occurs.
45: */
46: public FIDMapper getMapper(String catalog, String schema,
47: String tableName, Connection connection) throws IOException {
48: ColumnInfo[] colInfos = getPkColumnInfo(catalog, schema,
49: tableName, connection);
50: FIDMapper mapper = null;
51:
52: if (colInfos.length == 0) {
53: mapper = buildNoPKMapper(schema, tableName, connection);
54: } else if (colInfos.length > 1) {
55: mapper = buildMultiColumnFIDMapper(schema, tableName,
56: connection, colInfos);
57: } else {
58: ColumnInfo ci = colInfos[0];
59:
60: mapper = buildSingleColumnFidMapper(schema, tableName,
61: connection, ci);
62: }
63:
64: if (mapper == null) {
65: mapper = buildLastResortFidMapper(schema, tableName,
66: connection, colInfos);
67:
68: if (mapper == null) {
69: throw new IOException(
70: "Cannot map primary key to a FID mapper, primary key columns are:\n"
71: + getColumnInfoList(colInfos));
72: }
73: }
74: return new HsqlFIDMapper(mapper, tableName);
75: }
76: }
|