AbstractDatabaseConnection.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.database;

  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;

  26. import org.dbunit.DatabaseUnitRuntimeException;
  27. import org.dbunit.database.statement.IStatementFactory;
  28. import org.dbunit.dataset.DataSetException;
  29. import org.dbunit.dataset.FilteredDataSet;
  30. import org.dbunit.dataset.IDataSet;
  31. import org.dbunit.dataset.ITable;
  32. import org.dbunit.util.QualifiedTableName;
  33. import org.dbunit.util.SQLHelper;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;

  36. /**
  37.  * @author Manuel Laflamme
  38.  * @version $Revision$
  39.  * @since Mar 6, 2002
  40.  */
  41. public abstract class AbstractDatabaseConnection implements IDatabaseConnection
  42. {

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

  48.     private IDataSet _dataSet = null;
  49.     private final DatabaseConfig _databaseConfig;

  50.     public AbstractDatabaseConnection()
  51.     {
  52.         _databaseConfig = new DatabaseConfig();
  53.     }

  54.     ////////////////////////////////////////////////////////////////////////////
  55.     // IDatabaseConnection interface

  56.     public IDataSet createDataSet() throws SQLException
  57.     {
  58.         logger.debug("createDataSet() - start");

  59.         if (_dataSet == null)
  60.         {
  61.             _dataSet = new DatabaseDataSet(this);
  62.         }

  63.         return _dataSet;
  64.     }

  65.     public IDataSet createDataSet(String[] tableNames)
  66.             throws DataSetException, SQLException
  67.     {
  68.         logger.debug("createDataSet(tableNames={}) - start", tableNames);

  69.         return new FilteredDataSet(tableNames, createDataSet());
  70.     }

  71.     public ITable createQueryTable(String resultName, String sql)
  72.             throws DataSetException, SQLException
  73.     {
  74.         logger.debug("createQueryTable(resultName={}, sql={}) - start",
  75.                 resultName, sql);

  76.         IResultSetTableFactory tableFactory = getResultSetTableFactory();
  77.         IResultSetTable rsTable =
  78.                 tableFactory.createTable(resultName, sql, this);
  79.         if (logger.isDebugEnabled())
  80.         {
  81.             String rowCount = null;
  82.             try
  83.             {
  84.                 int rowCountInt = rsTable.getRowCount();
  85.                 rowCount = String.valueOf(rowCountInt);
  86.             } catch (Exception e)
  87.             {
  88.                 rowCount = "Unable to determine row count due to Exception: "
  89.                         + e.getLocalizedMessage();
  90.             }
  91.             logger.debug("createQueryTable: rowCount={}", rowCount);
  92.         }
  93.         return rsTable;
  94.     }

  95.     public ITable createTable(String resultName,
  96.             PreparedStatement preparedStatement)
  97.             throws DataSetException, SQLException
  98.     {
  99.         logger.debug(
  100.                 "createQueryTable(resultName={}, preparedStatement={}) - start",
  101.                 resultName, preparedStatement);

  102.         IResultSetTableFactory tableFactory = getResultSetTableFactory();
  103.         IResultSetTable rsTable =
  104.                 tableFactory.createTable(resultName, preparedStatement, this);
  105.         return rsTable;
  106.     }

  107.     public ITable createTable(String tableName)
  108.             throws DataSetException, SQLException
  109.     {
  110.         logger.debug("createTable(tableName={}) - start", tableName);

  111.         if (tableName == null)
  112.         {
  113.             throw new NullPointerException(
  114.                     "The parameter 'tableName' must not be null");
  115.         }

  116.         String escapePattern = (String) getConfig()
  117.                 .getProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN);

  118.         // qualify with schema if configured
  119.         QualifiedTableName qualifiedTableName = new QualifiedTableName(
  120.                 tableName, this.getSchema(), escapePattern);
  121.         String qualifiedName = qualifiedTableName.getQualifiedName();
  122.         String sql = "select * from " + qualifiedName;
  123.         return this.createQueryTable(tableName, sql);
  124.     }

  125.     public int getRowCount(String tableName) throws SQLException
  126.     {
  127.         logger.debug("getRowCount(tableName={}) - start", tableName);

  128.         return getRowCount(tableName, null);
  129.     }

  130.     public int getRowCount(String tableName, String whereClause)
  131.             throws SQLException
  132.     {
  133.         logger.debug("getRowCount(tableName={}, whereClause={}) - start",
  134.                 tableName, whereClause);

  135.         final StringBuilder sqlBuffer = new StringBuilder(128);
  136.         sqlBuffer.append("select count(*) from ");

  137.         // add table name and schema (schema only if available)
  138.         QualifiedTableName qualifiedTableName =
  139.                 new QualifiedTableName(tableName, this.getSchema());
  140.         String qualifiedName = qualifiedTableName.getQualifiedName();
  141.         sqlBuffer.append(qualifiedName);
  142.         if (whereClause != null)
  143.         {
  144.             sqlBuffer.append(" ");
  145.             sqlBuffer.append(whereClause);
  146.         }

  147.         Statement statement = getConnection().createStatement();
  148.         ResultSet resultSet = null;
  149.         try
  150.         {
  151.             resultSet = statement.executeQuery(sqlBuffer.toString());
  152.             if (resultSet.next())
  153.             {
  154.                 return resultSet.getInt(1);
  155.             } else
  156.             {
  157.                 throw new DatabaseUnitRuntimeException(
  158.                         "Select count did not return any results for table '"
  159.                                 + tableName + "'. Statement: "
  160.                                 + sqlBuffer.toString());
  161.             }
  162.         } finally
  163.         {
  164.             SQLHelper.close(resultSet, statement);
  165.         }
  166.     }

  167.     public DatabaseConfig getConfig()
  168.     {
  169.         return _databaseConfig;
  170.     }

  171.     /**
  172.      * @deprecated Use {@link #getConfig}
  173.      */
  174.     @Deprecated
  175.     public IStatementFactory getStatementFactory()
  176.     {
  177.         return (IStatementFactory) _databaseConfig
  178.                 .getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
  179.     }

  180.     private IResultSetTableFactory getResultSetTableFactory()
  181.     {
  182.         return (IResultSetTableFactory) _databaseConfig
  183.                 .getProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);

  184.     }

  185.     @Override
  186.     public String toString()
  187.     {
  188.         final StringBuilder sb = new StringBuilder();
  189.         sb.append("_databaseConfig=").append(_databaseConfig);
  190.         sb.append(", _dataSet=").append(_dataSet);
  191.         return sb.toString();
  192.     }
  193. }