NetezzaMetadataHandler.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.ext.netezza;

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

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

  29. /**
  30.  * Special metadata handler for Netezza.
  31.  *
  32.  * @author Ameet (amit3011 AT users.sourceforge.net)
  33.  * @author Last changed by: $Author$
  34.  * @version $Revision$ $Date$
  35.  * @since 2.4.6
  36.  */
  37. public class NetezzaMetadataHandler implements IMetadataHandler
  38. {

  39.     /**
  40.      * Logger for this class
  41.      */
  42.     private static final Logger logger = LoggerFactory.getLogger(NetezzaMetadataHandler.class);

  43.     public NetezzaMetadataHandler()
  44.     {
  45.         logger.debug("Created object of metadatahandler");
  46.     }

  47.     public ResultSet getColumns(DatabaseMetaData databaseMetaData, String schemaName, String tableName) throws SQLException
  48.     {
  49.         // Note that Netezza uses the catalogName instead of the schemaName, so
  50.         // pass in the given schema name as catalog name (first argument).
  51.         ResultSet resultSet = databaseMetaData.getColumns(schemaName, null, tableName, "%");
  52.         return resultSet;
  53.     }

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

  58.     public boolean matches(ResultSet columnsResultSet, String catalog, String schema, String table, String column, boolean caseSensitive) throws SQLException
  59.     {
  60.         String catalogName = columnsResultSet.getString(1);
  61.         String schemaName = columnsResultSet.getString(2);
  62.         String tableName = columnsResultSet.getString(3);
  63.         String columnName = columnsResultSet.getString(4);

  64.         logger.debug("inputCatalog="+catalog+" inputSchema="+schema+" inputTable="+table+" inputColumn="+column);
  65.         logger.debug("catalogName=" + catalogName + " schemaName=" + schemaName+"tableName=" + tableName+" columnName=" + columnName);
  66.        
  67.         // Netezza provides only a catalog but no schema
  68.         //if (schema != null && schemaName == null && catalog == null && catalogName != null)
  69.         if(catalog==null && catalogName!=null && schemaName !=null)
  70.         {
  71.             logger.debug("Netezza uses catalogs");
  72.             schema = schemaName;
  73.             catalog = catalogName;
  74.         }

  75.         boolean areEqual = areEqualIgnoreNull(catalog, catalogName, caseSensitive) && areEqualIgnoreNull(schema, schemaName, caseSensitive) && areEqualIgnoreNull(table, tableName, caseSensitive) && areEqualIgnoreNull(column, columnName, caseSensitive);
  76.         return areEqual;
  77.     }

  78.     private boolean areEqualIgnoreNull(String value1, String value2, boolean caseSensitive)
  79.     {
  80.         return SQLHelper.areEqualIgnoreNull(value1, value2, caseSensitive);
  81.     }

  82.     public String getSchema(ResultSet resultSet) throws SQLException
  83.     {
  84.         String catalogName = resultSet.getString(1);
  85.         String schemaName = resultSet.getString(2);

  86.         // Fix schema/catalog for netezza. Normally the schema is not set but only the catalog is set
  87.         if (schemaName == null && catalogName != null)
  88.         {
  89.             logger.debug("Using catalogName '" + catalogName + "' as schema since the schema is null but the catalog is set (probably in Netezza environment).");
  90.             schemaName = catalogName;
  91.         }
  92.         return schemaName;
  93.     }

  94.     public boolean tableExists(DatabaseMetaData metaData, String schema, String tableName) throws SQLException
  95.     {
  96.         ResultSet tableRs = metaData.getTables(schema, null, tableName, null);
  97.         try
  98.         {
  99.             return tableRs.next();
  100.         }
  101.         finally
  102.         {
  103.             SQLHelper.close(tableRs);
  104.         }
  105.     }

  106.     public ResultSet getTables(DatabaseMetaData metaData, String schemaName, String[] tableType) throws SQLException
  107.     {
  108.         if (logger.isTraceEnabled())
  109.             logger.trace("tableExists(metaData={}, schemaName={}, tableType={}) - start", new Object[] { metaData, schemaName, tableType });

  110.         return metaData.getTables(schemaName, null, "%", tableType);
  111.     }

  112.     public ResultSet getPrimaryKeys(DatabaseMetaData metaData, String schemaName, String tableName) throws SQLException
  113.     {
  114.         if (logger.isTraceEnabled())
  115.             logger.trace("getPrimaryKeys(metaData={}, schemaName={}, tableName={}) - start", new Object[] { metaData, schemaName, tableName });
  116.         ResultSet resultSet = metaData.getPrimaryKeys(schemaName, null, tableName);
  117.         return resultSet;
  118.     }
  119. }

  120.