001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/IDGeneratorFactory.java $
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036 package org.deegree.io;
037
038 import java.sql.Connection;
039 import java.sql.DatabaseMetaData;
040 import java.sql.ResultSet;
041 import java.sql.ResultSetMetaData;
042 import java.sql.SQLException;
043 import java.sql.Statement;
044 import java.sql.Types;
045
046 import org.deegree.framework.log.ILogger;
047 import org.deegree.framework.log.LoggerFactory;
048 import org.deegree.framework.util.DataBaseIDGenerator;
049
050 /**
051 * Factory for <tt>IDGenerator</tt>-instances. The generated instance is suitable for the
052 * database used and for the configuration (i.e. table and field type).
053 * <p>
054 *
055 * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
056 * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
057 */
058 public class IDGeneratorFactory {
059
060 private static final ILogger LOG = LoggerFactory.getLogger( IDGeneratorFactory.class );
061
062 /**
063 * @param con
064 * @param tableName
065 * @param fieldName
066 * @return the id generator
067 * @throws SQLException
068 */
069 public static DataBaseIDGenerator createIDGenerator( Connection con, String tableName, String fieldName )
070 throws SQLException {
071
072 // retrieve type of field
073 Statement stmt = con.createStatement();
074 ResultSet rs = stmt.executeQuery( "SELECT " + fieldName + " FROM " + tableName );
075 ResultSetMetaData rsmd = rs.getMetaData();
076 int type = rsmd.getColumnType( 1 );
077 rs.close();
078 stmt.close();
079 boolean isNumeric = false;
080
081 switch ( type ) {
082 case Types.INTEGER:
083 case Types.BIGINT:
084 case Types.DECIMAL:
085 case Types.NUMERIC:
086 case Types.SMALLINT:
087 case Types.TINYINT: {
088 isNumeric = true;
089 break;
090 }
091 case Types.CHAR:
092 case Types.LONGVARCHAR:
093 case Types.VARCHAR: {
094 isNumeric = false;
095 break;
096 }
097 default: {
098 throw new SQLException( "Cannot create DataBaseIDGenerator for table '" + tableName + "' and field '"
099 + fieldName + "'. Only integer and alphanumeric " + "fields are supported" );
100 }
101 }
102
103 // find suitable instance of IDGenerator
104 DatabaseMetaData dbmd = con.getMetaData();
105 String dbName = dbmd.getDatabaseProductName();
106 String dbVersion = dbmd.getDatabaseProductVersion();
107 String driverName = dbmd.getDriverName();
108 String driverVersion = dbmd.getDriverVersion();
109
110 LOG.logDebug( "dbName : " + dbName );
111 LOG.logDebug( "dbVersion : " + dbVersion );
112 LOG.logDebug( "driverName : " + driverName );
113 LOG.logDebug( "driverVersion: " + driverVersion );
114
115 DataBaseIDGenerator idGenerator = new GenericSQLIDGenerator( con, tableName, fieldName, type, isNumeric );
116 return idGenerator;
117 }
118 }