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