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.database;
22  
23  import java.sql.DatabaseMetaData;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  
27  import org.dbunit.util.SQLHelper;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Default implementation of {@link IMetadataHandler} which works for the most databases.
33   * @author gommma (gommma AT users.sourceforge.net)
34   * @author Last changed by: $Author$
35   * @version $Revision$ $Date$
36   * @since 2.4.4
37   */
38  public class DefaultMetadataHandler implements IMetadataHandler {
39  
40      /**
41       * Logger for this class
42       */
43      private static final Logger logger = LoggerFactory.getLogger(DefaultMetadataHandler.class);
44  
45      public ResultSet getColumns(DatabaseMetaData databaseMetaData, String schemaName, String tableName) 
46      throws SQLException 
47      {
48          if(logger.isTraceEnabled())
49              logger.trace("getColumns(databaseMetaData={}, schemaName={}, tableName={}) - start", 
50                      new Object[] {databaseMetaData, schemaName, tableName} );
51          
52          ResultSet resultSet = databaseMetaData.getColumns(
53                  null, schemaName, tableName, "%");
54          return resultSet;
55      }
56  
57      public boolean matches(ResultSet resultSet,
58              String schema, String table, boolean caseSensitive) 
59      throws SQLException 
60      {
61          return matches(resultSet, null, schema, table, null, caseSensitive);
62      }
63  
64      public boolean matches(ResultSet columnsResultSet, String catalog,
65              String schema, String table, String column,
66              boolean caseSensitive) throws SQLException 
67      {
68          if(logger.isTraceEnabled())
69              logger.trace("matches(columnsResultSet={}, catalog={}, schema={}," +
70              		" table={}, column={}, caseSensitive={}) - start", 
71                      new Object[] {columnsResultSet, catalog, schema, 
72                              table, column, Boolean.valueOf(caseSensitive)});
73          
74          String catalogName = columnsResultSet.getString(1);
75          String schemaName = columnsResultSet.getString(2);
76          String tableName = columnsResultSet.getString(3);
77          String columnName = columnsResultSet.getString(4);
78  
79          if(logger.isDebugEnabled()){
80              logger.debug("Comparing the following values using caseSensitive={} (searched<=>actual): " +
81                      "catalog: {}<=>{} schema: {}<=>{} table: {}<=>{} column: {}<=>{}", 
82                      new Object[] {
83                          Boolean.valueOf(caseSensitive),
84                          catalog, catalogName,
85                          schema, schemaName,
86                          table, tableName,
87                          column, columnName
88                      });
89          }
90          
91          boolean areEqual = 
92                  areEqualIgnoreNull(catalog, catalogName, caseSensitive) &&
93                  areEqualIgnoreNull(schema, schemaName, caseSensitive) &&
94                  areEqualIgnoreNull(table, tableName, caseSensitive) &&
95                  areEqualIgnoreNull(column, columnName, caseSensitive);
96          return areEqual;
97      }
98  
99      private boolean areEqualIgnoreNull(String value1, String value2,
100             boolean caseSensitive) {
101         return SQLHelper.areEqualIgnoreNull(value1, value2, caseSensitive);
102     }
103 
104     public String getSchema(ResultSet resultSet) throws SQLException {
105         if(logger.isTraceEnabled())
106             logger.trace("getColumns(resultSet={}) - start", resultSet);
107 
108         String schemaName = resultSet.getString(2);
109         return schemaName;
110     }
111     
112     public boolean tableExists(DatabaseMetaData metaData, String schemaName, String tableName) 
113     throws SQLException 
114     {
115         if(logger.isTraceEnabled())
116             logger.trace("tableExists(metaData={}, schemaName={}, tableName={}) - start", 
117                     new Object[] {metaData, schemaName, tableName} );
118         
119         ResultSet tableRs = metaData.getTables(null, schemaName, tableName, null);
120         try 
121         {
122             return tableRs.next();
123         }
124         finally
125         {
126             SQLHelper.close(tableRs);
127         }
128     }
129 
130     public ResultSet getTables(DatabaseMetaData metaData, String schemaName, String[] tableType) 
131     throws SQLException
132     {
133         if(logger.isTraceEnabled())
134             logger.trace("getTables(metaData={}, schemaName={}, tableType={}) - start", 
135                     new Object[] {metaData, schemaName, tableType} );
136 
137         return metaData.getTables(null, schemaName, "%", tableType);
138     }
139 
140     public ResultSet getPrimaryKeys(DatabaseMetaData metaData, String schemaName, String tableName) 
141     throws SQLException
142     {
143         if(logger.isTraceEnabled())
144             logger.trace("getPrimaryKeys(metaData={}, schemaName={}, tableName={}) - start", 
145                     new Object[] {metaData, schemaName, tableName} );
146 
147         ResultSet resultSet = metaData.getPrimaryKeys(
148                 null, schemaName, tableName);
149         return resultSet;
150     }
151 
152 }