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.database;
22  
23  import java.sql.PreparedStatement;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  
27  import org.dbunit.dataset.DataSetException;
28  import org.dbunit.dataset.ITable;
29  import org.dbunit.dataset.ITableMetaData;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * @author manuel.laflamme
35   * @author Last changed by: $Author$
36   * @version $Revision$ $Date$
37   * @since 2.0 (Jul 31, 2003)
38   */
39  public class ForwardOnlyResultSetTableFactory implements IResultSetTableFactory
40  {
41  
42      /**
43       * Logger for this class
44       */
45      private static final Logger logger = LoggerFactory.getLogger(ForwardOnlyResultSetTableFactory.class);
46  
47      public IResultSetTable createTable(String tableName, String selectStatement,
48              IDatabaseConnection connection) throws SQLException, DataSetException
49      {
50      	if (logger.isTraceEnabled())
51      		logger.trace("createTable(tableName={}, selectStatement={}, connection={}) - start",
52                  new Object[]{ tableName, selectStatement, connection});
53  
54          return new ForwardOnlyResultSetTable(tableName, selectStatement, connection);
55      }
56  
57      public IResultSetTable createTable(ITableMetaData metaData,
58              IDatabaseConnection connection) throws SQLException, DataSetException
59      {
60          logger.trace("createTable(metaData={}, connection={}) - start", metaData, connection);
61  
62          return new ForwardOnlyResultSetTable(metaData, connection);
63      }
64  
65      public IResultSetTable createTable(String tableName,
66              PreparedStatement preparedStatement, IDatabaseConnection connection) 
67      throws SQLException, DataSetException
68      {
69          if (logger.isTraceEnabled())
70              logger.trace("createTable(tableName={}, preparedStatement={}, connection={}) - start",
71                  new Object[]{ tableName, preparedStatement, connection});
72          
73          return createForwardOnlyResultSetTable(tableName, preparedStatement, connection);
74      }
75  
76      
77      /**
78       * Creates a new {@link ForwardOnlyResultSetTable} using the given {@link PreparedStatement} to
79       * retrieve the data.
80       * @param tableName The name the {@link ITable} will have
81       * @param preparedStatement The statement which provides the data
82       * @param connection The database connection which also holds dbunit configuration information
83       * @return The new table object
84       * @throws SQLException
85       * @throws DataSetException
86       */
87      ForwardOnlyResultSetTable createForwardOnlyResultSetTable(String tableName,
88              PreparedStatement preparedStatement, IDatabaseConnection connection) 
89      throws SQLException, DataSetException 
90      {
91          if (logger.isTraceEnabled())
92              logger.trace("createForwardOnlyResultSetTable(tableName={}, preparedStatement={}, connection={}) - start",
93                  new Object[]{ tableName, preparedStatement, connection});
94  
95          connection.getConfig().getConfigurator().configureStatement(preparedStatement);
96  
97          ResultSet rs = preparedStatement.executeQuery();
98          
99          boolean caseSensitiveTableNames = connection.getConfig().getFeature(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES);
100         ITableMetaData metaData = new ResultSetTableMetaData(tableName, rs, connection, caseSensitiveTableNames);
101         ForwardOnlyResultSetTable table = new ForwardOnlyResultSetTable(metaData, rs);
102         return table;
103     }
104     
105 
106 }