View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, 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.operation;
22  
23  import java.sql.SQLException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.dbunit.DatabaseUnitException;
28  import org.dbunit.database.DatabaseConfig;
29  import org.dbunit.database.IDatabaseConnection;
30  import org.dbunit.dataset.Column;
31  import org.dbunit.dataset.DefaultTableMetaData;
32  import org.dbunit.dataset.IDataSet;
33  import org.dbunit.dataset.ITableMetaData;
34  import org.dbunit.util.QualifiedTableName;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * @author Manuel Laflamme
40   * @since Jan 17, 2004
41   * @version $Revision$
42   */
43  public abstract class AbstractOperation extends DatabaseOperation
44  {
45  
46      /**
47       * Logger for this class
48       */
49      private static final Logger logger = LoggerFactory.getLogger(AbstractOperation.class);
50  
51      protected String getQualifiedName(String prefix, String name, IDatabaseConnection connection)
52      {
53      	if (logger.isDebugEnabled())	
54      	{
55      		logger.debug("getQualifiedName(prefix={}, name={}, connection={}) - start",
56      				new Object[] {prefix, name, connection});
57      	}
58  
59          String escapePattern = (String)connection.getConfig().getProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
60          QualifiedTableName qualifiedTbleName = new QualifiedTableName(name, prefix, escapePattern);
61          return qualifiedTbleName.getQualifiedName();
62      }
63  
64      /**
65       * Returns the metadata to use in this operation. It is retrieved
66       * from the database connection using the information from the physical
67       * database table.
68       *
69       * @param connection the database connection
70       * @param metaData the XML table metadata
71       */
72      static ITableMetaData getOperationMetaData(IDatabaseConnection connection,
73              ITableMetaData metaData) throws DatabaseUnitException, SQLException
74      {
75          logger.debug("getOperationMetaData(connection={}, metaData={}) - start", connection, metaData);
76  
77          IDataSet databaseDataSet = connection.createDataSet();
78          String tableName = metaData.getTableName();
79  
80          ITableMetaData tableMetaData = databaseDataSet.getTableMetaData(tableName);
81          Column[] columns = metaData.getColumns();
82  
83          List columnList = new ArrayList();
84          for (int j = 0; j < columns.length; j++)
85          {
86              String columnName = columns[j].getColumnName();
87          	// Check if column exists in database
88              // method "getColumnIndex()" throws NoSuchColumnsException when columns have not been found
89          	int dbColIndex = tableMetaData.getColumnIndex(columnName);
90          	// If we get here the column exists in the database
91          	Column dbColumn = tableMetaData.getColumns()[dbColIndex];
92              columnList.add(dbColumn);
93          }
94  
95          return new DefaultTableMetaData(tableMetaData.getTableName(),
96                  (Column[])columnList.toArray(new Column[0]),
97                  tableMetaData.getPrimaryKeys());
98      }
99  }