AbstractResultSetTable.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.Connection;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;

  26. import org.dbunit.dataset.AbstractTable;
  27. import org.dbunit.dataset.DataSetException;
  28. import org.dbunit.dataset.ITableMetaData;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * @author Manuel Laflamme
  33.  * @since Apr 10, 2003
  34.  * @version $Revision$
  35.  */
  36. public abstract class AbstractResultSetTable extends AbstractTable
  37.         implements IResultSetTable
  38. {

  39.     /**
  40.      * Logger for this class
  41.      */
  42.     private static final Logger logger = LoggerFactory.getLogger(AbstractResultSetTable.class);

  43.     protected ITableMetaData _metaData;
  44.     private Statement _statement;
  45.     protected ResultSet _resultSet;

  46.     public AbstractResultSetTable(ITableMetaData metaData, ResultSet resultSet)
  47.             throws SQLException, DataSetException
  48.     {
  49.         _metaData = metaData;
  50.         _resultSet = resultSet;
  51.     }

  52.     public AbstractResultSetTable(String tableName, String selectStatement,
  53.             IDatabaseConnection connection)
  54.             throws DataSetException, SQLException
  55.     {
  56.         this(tableName, selectStatement, connection, false);
  57.     }
  58.    
  59.     /**
  60.      * @param tableName
  61.      * @param selectStatement
  62.      * @param connection
  63.      * @param caseSensitiveTableNames
  64.      * @throws DataSetException
  65.      * @throws SQLException
  66.      * @since 2.4.1
  67.      */
  68.     public AbstractResultSetTable(String tableName, String selectStatement,
  69.             IDatabaseConnection connection, boolean caseSensitiveTableNames)
  70.             throws DataSetException, SQLException
  71.     {
  72.         _statement = createStatement(connection);

  73.         try
  74.         {
  75.             _resultSet = _statement.executeQuery(selectStatement);
  76.             _metaData = new ResultSetTableMetaData(tableName, _resultSet, connection, caseSensitiveTableNames);
  77.         }
  78.         catch (SQLException e)
  79.         {
  80.             _statement.close();
  81.             _statement = null;
  82.             throw e;
  83.         }
  84.     }

  85.     public AbstractResultSetTable(ITableMetaData metaData,
  86.             IDatabaseConnection connection) throws DataSetException, SQLException
  87.     {
  88.         _statement = createStatement(connection);
  89.        
  90.         String escapePattern = (String)connection.getConfig().getProperty(
  91.                 DatabaseConfig.PROPERTY_ESCAPE_PATTERN);

  92.         try
  93.         {
  94.             String schema = connection.getSchema();
  95.             String selectStatement = getSelectStatement(schema, metaData, escapePattern);

  96.             if(logger.isDebugEnabled())
  97.                 logger.debug("Query: {}", selectStatement);
  98.            
  99.             _resultSet = _statement.executeQuery(selectStatement);
  100.             _metaData = metaData;
  101.         }
  102.         catch (SQLException e)
  103.         {
  104.             _statement.close();
  105.             _statement = null;
  106.             throw e;
  107.         }
  108.     }

  109.     private Statement createStatement(IDatabaseConnection connection) throws SQLException
  110.     {
  111.         logger.trace("createStatement() - start");

  112.         Connection jdbcConnection = connection.getConnection();
  113.         Statement stmt = jdbcConnection.createStatement();
  114.         connection.getConfig().getConfigurator().configureStatement(stmt);
  115.         return stmt;
  116.     }

  117.     static String getSelectStatement(String schema, ITableMetaData metaData, String escapePattern)
  118.             throws DataSetException
  119.     {
  120.         return DatabaseDataSet.getSelectStatement(schema, metaData, escapePattern);
  121.     }

  122.     ////////////////////////////////////////////////////////////////////////////
  123.     // ITable interface

  124.     public ITableMetaData getTableMetaData()
  125.     {
  126.         return _metaData;
  127.     }

  128.     ////////////////////////////////////////////////////////////////////////////
  129.     // IResultSetTable interface

  130.     public void close() throws DataSetException
  131.     {
  132.         logger.trace("close() - start");

  133.         try
  134.         {
  135.             if (_resultSet != null)
  136.             {
  137.                 _resultSet.close();
  138.                 _resultSet = null;
  139.             }

  140.             if (_statement != null)
  141.             {
  142.                 _statement.close();
  143.                 _statement = null;
  144.             }
  145.         }
  146.         catch (SQLException e)
  147.         {
  148.             throw new DataSetException(e);
  149.         }
  150.     }

  151.     /**
  152.      * {@inheritDoc}
  153.      */
  154.     public String toString()
  155.     {
  156.         StringBuilder sb = new StringBuilder(2000);

  157.         sb.append(getClass().getName()).append("[");
  158.         sb.append("_metaData=[").append(_metaData).append("], ");
  159.         sb.append("_resultSet=[").append(_resultSet).append("], ");
  160.         sb.append("_statement=[").append(_statement).append("]");
  161.         sb.append("]");

  162.         return sb.toString();
  163.     }
  164. }