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.mysql;
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 MySQL.<br/>
34   * Was introduced to fix "[ 2545095 ] Mysql FEATURE_QUALIFIED_TABLE_NAMES column SQLHelper.matches".
35   * 
36   * @author gommma (gommma AT users.sourceforge.net)
37   * @author Last changed by: $Author$
38   * @version $Revision$ $Date$
39   * @since 2.4.4
40   */
41  public class MySqlMetadataHandler implements IMetadataHandler {
42  
43      /**
44       * Logger for this class
45       */
46      private static final Logger logger = LoggerFactory.getLogger(MySqlMetadataHandler.class);
47  
48      public ResultSet getColumns(DatabaseMetaData databaseMetaData, String schemaName, String tableName) 
49      throws SQLException {
50          // Note that MySQL uses the catalogName instead of the schemaName, so
51          // pass in the given schema name as catalog name (first argument).
52          ResultSet resultSet = databaseMetaData.getColumns(
53                  schemaName, null, 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          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          // MYSQL provides only a catalog but no schema
74          if(schema != null && schemaName == null && catalog==null && catalogName != null){
75              logger.debug("Switching catalog/schema because the are mutually null");
76              schemaName = catalogName;
77              catalogName = null;
78          }
79          
80          boolean areEqual = 
81              areEqualIgnoreNull(catalog, catalogName, caseSensitive) &&
82              areEqualIgnoreNull(schema, schemaName, caseSensitive) &&
83              areEqualIgnoreNull(table, tableName, caseSensitive) &&
84              areEqualIgnoreNull(column, columnName, caseSensitive);
85          return areEqual;
86      }
87  
88      private boolean areEqualIgnoreNull(String value1, String value2,
89              boolean caseSensitive) {
90          return SQLHelper.areEqualIgnoreNull(value1, value2, caseSensitive);
91      }
92  
93      public String getSchema(ResultSet resultSet) throws SQLException {
94          String catalogName = resultSet.getString(1);
95          String schemaName = resultSet.getString(2);
96          
97          // Fix schema/catalog for mysql. Normally the schema is not set but only the catalog is set
98          if(schemaName == null && catalogName != null) {
99              logger.debug("Using catalogName '" + catalogName + "' as schema since the schema is null but the catalog is set (probably in a MySQL environment).");
100             schemaName = catalogName;
101         }
102         return schemaName;
103     }
104 
105     public boolean tableExists(DatabaseMetaData metaData, String schema, String tableName) 
106     throws SQLException 
107     {
108         ResultSet tableRs = metaData.getTables(schema, null, tableName, null);
109         try 
110         {
111             return tableRs.next();
112         }
113         finally
114         {
115             SQLHelper.close(tableRs);
116         }
117     }
118 
119     public ResultSet getTables(DatabaseMetaData metaData, String schemaName, String[] tableType) 
120     throws SQLException
121     {
122         if(logger.isTraceEnabled())
123             logger.trace("tableExists(metaData={}, schemaName={}, tableType={}) - start", 
124                     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) 
130     throws SQLException
131     {
132         if(logger.isTraceEnabled())
133             logger.trace("getPrimaryKeys(metaData={}, schemaName={}, tableName={}) - start", 
134                     new Object[] {metaData, schemaName, tableName} );
135 
136         ResultSet resultSet = metaData.getPrimaryKeys(
137                 schemaName, null, tableName);
138         return resultSet;
139     }
140 
141 }