DefaultMetadataHandler.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2009, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  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.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit.database;

  22. import java.sql.DatabaseMetaData;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;

  25. import org.dbunit.util.SQLHelper;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;

  28. /**
  29.  * Default implementation of {@link IMetadataHandler} which works for the most databases.
  30.  * @author gommma (gommma AT users.sourceforge.net)
  31.  * @author Last changed by: $Author$
  32.  * @version $Revision$ $Date$
  33.  * @since 2.4.4
  34.  */
  35. public class DefaultMetadataHandler implements IMetadataHandler {

  36.     /**
  37.      * Logger for this class
  38.      */
  39.     private static final Logger logger = LoggerFactory.getLogger(DefaultMetadataHandler.class);

  40.     public ResultSet getColumns(DatabaseMetaData databaseMetaData, String schemaName, String tableName)
  41.     throws SQLException
  42.     {
  43.         if(logger.isTraceEnabled())
  44.             logger.trace("getColumns(databaseMetaData={}, schemaName={}, tableName={}) - start",
  45.                     new Object[] {databaseMetaData, schemaName, tableName} );
  46.        
  47.         ResultSet resultSet = databaseMetaData.getColumns(
  48.                 null, schemaName, tableName, "%");
  49.         return resultSet;
  50.     }

  51.     public boolean matches(ResultSet resultSet,
  52.             String schema, String table, boolean caseSensitive)
  53.     throws SQLException
  54.     {
  55.         return matches(resultSet, null, schema, table, null, caseSensitive);
  56.     }

  57.     public boolean matches(ResultSet columnsResultSet, String catalog,
  58.             String schema, String table, String column,
  59.             boolean caseSensitive) throws SQLException
  60.     {
  61.         if(logger.isTraceEnabled())
  62.             logger.trace("matches(columnsResultSet={}, catalog={}, schema={}," +
  63.                     " table={}, column={}, caseSensitive={}) - start",
  64.                     new Object[] {columnsResultSet, catalog, schema,
  65.                             table, column, Boolean.valueOf(caseSensitive)});
  66.        
  67.         String catalogName = columnsResultSet.getString(1);
  68.         String schemaName = columnsResultSet.getString(2);
  69.         String tableName = columnsResultSet.getString(3);
  70.         String columnName = columnsResultSet.getString(4);

  71.         if(logger.isDebugEnabled()){
  72.             logger.debug("Comparing the following values using caseSensitive={} (searched<=>actual): " +
  73.                     "catalog: {}<=>{} schema: {}<=>{} table: {}<=>{} column: {}<=>{}",
  74.                     new Object[] {
  75.                         Boolean.valueOf(caseSensitive),
  76.                         catalog, catalogName,
  77.                         schema, schemaName,
  78.                         table, tableName,
  79.                         column, columnName
  80.                     });
  81.         }
  82.        
  83.         boolean areEqual =
  84.                 areEqualIgnoreNull(catalog, catalogName, caseSensitive) &&
  85.                 areEqualIgnoreNull(schema, schemaName, caseSensitive) &&
  86.                 areEqualIgnoreNull(table, tableName, caseSensitive) &&
  87.                 areEqualIgnoreNull(column, columnName, caseSensitive);
  88.         return areEqual;
  89.     }

  90.     private boolean areEqualIgnoreNull(String value1, String value2,
  91.             boolean caseSensitive) {
  92.         return SQLHelper.areEqualIgnoreNull(value1, value2, caseSensitive);
  93.     }

  94.     public String getSchema(ResultSet resultSet) throws SQLException {
  95.         if(logger.isTraceEnabled())
  96.             logger.trace("getColumns(resultSet={}) - start", resultSet);

  97.         String schemaName = resultSet.getString(2);
  98.         return schemaName;
  99.     }
  100.    
  101.     public boolean tableExists(DatabaseMetaData metaData, String schemaName, String tableName)
  102.     throws SQLException
  103.     {
  104.         if(logger.isTraceEnabled())
  105.             logger.trace("tableExists(metaData={}, schemaName={}, tableName={}) - start",
  106.                     new Object[] {metaData, schemaName, tableName} );
  107.        
  108.         ResultSet tableRs = metaData.getTables(null, schemaName, tableName, null);
  109.         try
  110.         {
  111.             return tableRs.next();
  112.         }
  113.         finally
  114.         {
  115.             SQLHelper.close(tableRs);
  116.         }
  117.     }

  118.     public ResultSet getTables(DatabaseMetaData metaData, String schemaName, String[] tableType)
  119.     throws SQLException
  120.     {
  121.         if(logger.isTraceEnabled())
  122.             logger.trace("getTables(metaData={}, schemaName={}, tableType={}) - start",
  123.                     new Object[] {metaData, schemaName, tableType} );

  124.         return metaData.getTables(null, schemaName, "%", tableType);
  125.     }

  126.     public ResultSet getPrimaryKeys(DatabaseMetaData metaData, String schemaName, String tableName)
  127.     throws SQLException
  128.     {
  129.         if(logger.isTraceEnabled())
  130.             logger.trace("getPrimaryKeys(metaData={}, schemaName={}, tableName={}) - start",
  131.                     new Object[] {metaData, schemaName, tableName} );

  132.         ResultSet resultSet = metaData.getPrimaryKeys(
  133.                 null, schemaName, tableName);
  134.         return resultSet;
  135.     }

  136. }