AbstractOperation.java

  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. import java.sql.SQLException;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import org.dbunit.DatabaseUnitException;
  26. import org.dbunit.database.DatabaseConfig;
  27. import org.dbunit.database.IDatabaseConnection;
  28. import org.dbunit.dataset.Column;
  29. import org.dbunit.dataset.DefaultTableMetaData;
  30. import org.dbunit.dataset.IDataSet;
  31. import org.dbunit.dataset.ITableMetaData;
  32. import org.dbunit.util.QualifiedTableName;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;

  35. /**
  36.  * @author Manuel Laflamme
  37.  * @since Jan 17, 2004
  38.  * @version $Revision$
  39.  */
  40. public abstract class AbstractOperation extends DatabaseOperation
  41. {

  42.     /**
  43.      * Logger for this class
  44.      */
  45.     private static final Logger logger = LoggerFactory.getLogger(AbstractOperation.class);

  46.     protected String getQualifiedName(String prefix, String name, IDatabaseConnection connection)
  47.     {
  48.         if (logger.isDebugEnabled())    
  49.         {
  50.             logger.debug("getQualifiedName(prefix={}, name={}, connection={}) - start",
  51.                     new Object[] {prefix, name, connection});
  52.         }

  53.         String escapePattern = (String)connection.getConfig().getProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
  54.         QualifiedTableName qualifiedTbleName = new QualifiedTableName(name, prefix, escapePattern);
  55.         return qualifiedTbleName.getQualifiedName();
  56.     }

  57.     /**
  58.      * Returns the metadata to use in this operation. It is retrieved
  59.      * from the database connection using the information from the physical
  60.      * database table.
  61.      *
  62.      * @param connection the database connection
  63.      * @param metaData the XML table metadata
  64.      */
  65.     static ITableMetaData getOperationMetaData(IDatabaseConnection connection,
  66.             ITableMetaData metaData) throws DatabaseUnitException, SQLException
  67.     {
  68.         logger.debug("getOperationMetaData(connection={}, metaData={}) - start", connection, metaData);

  69.         IDataSet databaseDataSet = connection.createDataSet();
  70.         String tableName = metaData.getTableName();

  71.         ITableMetaData tableMetaData = databaseDataSet.getTableMetaData(tableName);
  72.         Column[] columns = metaData.getColumns();

  73.         List columnList = new ArrayList();
  74.         for (int j = 0; j < columns.length; j++)
  75.         {
  76.             String columnName = columns[j].getColumnName();
  77.             // Check if column exists in database
  78.             // method "getColumnIndex()" throws NoSuchColumnsException when columns have not been found
  79.             int dbColIndex = tableMetaData.getColumnIndex(columnName);
  80.             // If we get here the column exists in the database
  81.             Column dbColumn = tableMetaData.getColumns()[dbColIndex];
  82.             columnList.add(dbColumn);
  83.         }

  84.         return new DefaultTableMetaData(tableMetaData.getTableName(),
  85.                 (Column[])columnList.toArray(new Column[0]),
  86.                 tableMetaData.getPrimaryKeys());
  87.     }
  88. }