ForwardOnlyResultSetTableFactory.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 org.dbunit.dataset.DataSetException;
  26. import org.dbunit.dataset.ITable;
  27. import org.dbunit.dataset.ITableMetaData;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;

  30. /**
  31.  * @author manuel.laflamme
  32.  * @author Last changed by: $Author$
  33.  * @version $Revision$ $Date$
  34.  * @since 2.0 (Jul 31, 2003)
  35.  */
  36. public class ForwardOnlyResultSetTableFactory implements IResultSetTableFactory
  37. {

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

  42.     public IResultSetTable createTable(String tableName, String selectStatement,
  43.             IDatabaseConnection connection) throws SQLException, DataSetException
  44.     {
  45.         if (logger.isTraceEnabled())
  46.             logger.trace("createTable(tableName={}, selectStatement={}, connection={}) - start",
  47.                 new Object[]{ tableName, selectStatement, connection});

  48.         return new ForwardOnlyResultSetTable(tableName, selectStatement, connection);
  49.     }

  50.     public IResultSetTable createTable(ITableMetaData metaData,
  51.             IDatabaseConnection connection) throws SQLException, DataSetException
  52.     {
  53.         logger.trace("createTable(metaData={}, connection={}) - start", metaData, connection);

  54.         return new ForwardOnlyResultSetTable(metaData, connection);
  55.     }

  56.     public IResultSetTable createTable(String tableName,
  57.             PreparedStatement preparedStatement, IDatabaseConnection connection)
  58.     throws SQLException, DataSetException
  59.     {
  60.         if (logger.isTraceEnabled())
  61.             logger.trace("createTable(tableName={}, preparedStatement={}, connection={}) - start",
  62.                 new Object[]{ tableName, preparedStatement, connection});
  63.        
  64.         return createForwardOnlyResultSetTable(tableName, preparedStatement, connection);
  65.     }

  66.    
  67.     /**
  68.      * Creates a new {@link ForwardOnlyResultSetTable} using the given {@link PreparedStatement} to
  69.      * retrieve the data.
  70.      * @param tableName The name the {@link ITable} will have
  71.      * @param preparedStatement The statement which provides the data
  72.      * @param connection The database connection which also holds dbunit configuration information
  73.      * @return The new table object
  74.      * @throws SQLException
  75.      * @throws DataSetException
  76.      */
  77.     ForwardOnlyResultSetTable createForwardOnlyResultSetTable(String tableName,
  78.             PreparedStatement preparedStatement, IDatabaseConnection connection)
  79.     throws SQLException, DataSetException
  80.     {
  81.         if (logger.isTraceEnabled())
  82.             logger.trace("createForwardOnlyResultSetTable(tableName={}, preparedStatement={}, connection={}) - start",
  83.                 new Object[]{ tableName, preparedStatement, connection});

  84.         connection.getConfig().getConfigurator().configureStatement(preparedStatement);

  85.         ResultSet rs = preparedStatement.executeQuery();
  86.        
  87.         boolean caseSensitiveTableNames = connection.getConfig().getFeature(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES);
  88.         ITableMetaData metaData = new ResultSetTableMetaData(tableName, rs, connection, caseSensitiveTableNames);
  89.         ForwardOnlyResultSetTable table = new ForwardOnlyResultSetTable(metaData, rs);
  90.         return table;
  91.     }
  92.    

  93. }