View Javadoc
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  
23  import java.sql.DatabaseMetaData;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  
27  import org.dbunit.database.IMetadataHandler;
28  import org.dbunit.util.SQLHelper;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Special metadata handler for Netezza.
34   * 
35   * @author Ameet (amit3011 AT users.sourceforge.net)
36   * @author Last changed by: $Author$
37   * @version $Revision$ $Date$
38   * @since 2.4.6
39   */
40  public class NetezzaMetadataHandler implements IMetadataHandler
41  {
42  
43  	/**
44  	 * Logger for this class
45  	 */
46  	private static final Logger logger = LoggerFactory.getLogger(NetezzaMetadataHandler.class);
47  
48  	public NetezzaMetadataHandler()
49  	{
50  		logger.debug("Created object of metadatahandler");
51  	}
52  
53  	public ResultSet getColumns(DatabaseMetaData databaseMetaData, String schemaName, String tableName) throws SQLException
54  	{
55  		// Note that Netezza uses the catalogName instead of the schemaName, so
56  		// pass in the given schema name as catalog name (first argument).
57  		ResultSet resultSet = databaseMetaData.getColumns(schemaName, null, tableName, "%");
58  		return resultSet;
59  	}
60  
61  	public boolean matches(ResultSet resultSet, String schema, String table, boolean caseSensitive) throws SQLException
62  	{
63  		return matches(resultSet, null, schema, table, null, caseSensitive);
64  	}
65  
66  	public boolean matches(ResultSet columnsResultSet, String catalog, String schema, String table, String column, boolean caseSensitive) throws SQLException
67  	{
68  		String catalogName = columnsResultSet.getString(1);
69  		String schemaName = columnsResultSet.getString(2);
70  		String tableName = columnsResultSet.getString(3);
71  		String columnName = columnsResultSet.getString(4);
72  
73  		logger.debug("inputCatalog="+catalog+" inputSchema="+schema+" inputTable="+table+" inputColumn="+column);
74  		logger.debug("catalogName=" + catalogName + " schemaName=" + schemaName+"tableName=" + tableName+" columnName=" + columnName);
75  		
76  		// Netezza provides only a catalog but no schema
77  		//if (schema != null && schemaName == null && catalog == null && catalogName != null)
78  		if(catalog==null && catalogName!=null && schemaName !=null)
79  		{
80  			logger.debug("Netezza uses catalogs");
81  			schema = schemaName;
82  			catalog = catalogName;
83  		}
84  
85  		boolean areEqual = areEqualIgnoreNull(catalog, catalogName, caseSensitive) && areEqualIgnoreNull(schema, schemaName, caseSensitive) && areEqualIgnoreNull(table, tableName, caseSensitive) && areEqualIgnoreNull(column, columnName, caseSensitive);
86  		return areEqual;
87  	}
88  
89  	private boolean areEqualIgnoreNull(String value1, String value2, boolean caseSensitive)
90  	{
91  		return SQLHelper.areEqualIgnoreNull(value1, value2, caseSensitive);
92  	}
93  
94  	public String getSchema(ResultSet resultSet) throws SQLException
95  	{
96  		String catalogName = resultSet.getString(1);
97  		String schemaName = resultSet.getString(2);
98  
99  		// Fix schema/catalog for netezza. Normally the schema is not set but only the catalog is set
100 		if (schemaName == null && catalogName != null)
101 		{
102 			logger.debug("Using catalogName '" + catalogName + "' as schema since the schema is null but the catalog is set (probably in Netezza environment).");
103 			schemaName = catalogName;
104 		}
105 		return schemaName;
106 	}
107 
108 	public boolean tableExists(DatabaseMetaData metaData, String schema, String tableName) throws SQLException
109 	{
110 		ResultSet tableRs = metaData.getTables(schema, null, tableName, null);
111 		try
112 		{
113 			return tableRs.next();
114 		}
115 		finally
116 		{
117 			SQLHelper.close(tableRs);
118 		}
119 	}
120 
121 	public ResultSet getTables(DatabaseMetaData metaData, String schemaName, String[] tableType) throws SQLException
122 	{
123 		if (logger.isTraceEnabled())
124 			logger.trace("tableExists(metaData={}, schemaName={}, tableType={}) - start", new Object[] { metaData, schemaName, tableType });
125 
126 		return metaData.getTables(schemaName, null, "%", tableType);
127 	}
128 
129 	public ResultSet getPrimaryKeys(DatabaseMetaData metaData, String schemaName, String tableName) throws SQLException
130 	{
131 		if (logger.isTraceEnabled())
132 			logger.trace("getPrimaryKeys(metaData={}, schemaName={}, tableName={}) - start", new Object[] { metaData, schemaName, tableName });
133 		ResultSet resultSet = metaData.getPrimaryKeys(schemaName, null, tableName);
134 		return resultSet;
135 	}
136 }
137 
138