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.Connection;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  import java.sql.Statement;
27  
28  import org.dbunit.dataset.AbstractTable;
29  import org.dbunit.dataset.DataSetException;
30  import org.dbunit.dataset.ITableMetaData;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * @author Manuel Laflamme
36   * @since Apr 10, 2003
37   * @version $Revision$
38   */
39  public abstract class AbstractResultSetTable extends AbstractTable
40          implements IResultSetTable
41  {
42  
43      /**
44       * Logger for this class
45       */
46      private static final Logger logger = LoggerFactory.getLogger(AbstractResultSetTable.class);
47  
48      protected ITableMetaData _metaData;
49      private Statement _statement;
50      protected ResultSet _resultSet;
51  
52      public AbstractResultSetTable(ITableMetaData metaData, ResultSet resultSet)
53              throws SQLException, DataSetException
54      {
55          _metaData = metaData;
56          _resultSet = resultSet;
57      }
58  
59      public AbstractResultSetTable(String tableName, String selectStatement,
60              IDatabaseConnection connection)
61              throws DataSetException, SQLException
62      {
63          this(tableName, selectStatement, connection, false);
64      }
65      
66      /**
67       * @param tableName
68       * @param selectStatement
69       * @param connection
70       * @param caseSensitiveTableNames
71       * @throws DataSetException
72       * @throws SQLException
73       * @since 2.4.1
74       */
75      public AbstractResultSetTable(String tableName, String selectStatement,
76              IDatabaseConnection connection, boolean caseSensitiveTableNames)
77              throws DataSetException, SQLException
78      {
79      	_statement = createStatement(connection);
80  
81          try
82          {
83              _resultSet = _statement.executeQuery(selectStatement);
84              _metaData = new ResultSetTableMetaData(tableName, _resultSet, connection, caseSensitiveTableNames);
85          }
86          catch (SQLException e)
87          {
88              _statement.close();
89              _statement = null;
90              throw e;
91          }
92      }
93  
94  	public AbstractResultSetTable(ITableMetaData metaData,
95              IDatabaseConnection connection) throws DataSetException, SQLException
96      {
97  		_statement = createStatement(connection);
98  		
99          String escapePattern = (String)connection.getConfig().getProperty(
100                 DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
101 
102         try
103         {
104             String schema = connection.getSchema();
105             String selectStatement = getSelectStatement(schema, metaData, escapePattern);
106 
107             if(logger.isDebugEnabled())
108                 logger.debug("Query: {}", selectStatement);
109             
110             _resultSet = _statement.executeQuery(selectStatement);
111             _metaData = metaData;
112         }
113         catch (SQLException e)
114         {
115             _statement.close();
116             _statement = null;
117             throw e;
118         }
119     }
120 
121     private Statement createStatement(IDatabaseConnection connection) throws SQLException 
122     {
123         logger.trace("createStatement() - start");
124 
125         Connection jdbcConnection = connection.getConnection();
126         Statement stmt = jdbcConnection.createStatement();
127         connection.getConfig().getConfigurator().configureStatement(stmt);
128         return stmt;
129     }
130 
131     static String getSelectStatement(String schema, ITableMetaData metaData, String escapePattern)
132             throws DataSetException
133     {
134         return DatabaseDataSet.getSelectStatement(schema, metaData, escapePattern);
135     }
136 
137     ////////////////////////////////////////////////////////////////////////////
138     // ITable interface
139 
140     public ITableMetaData getTableMetaData()
141     {
142         return _metaData;
143     }
144 
145     ////////////////////////////////////////////////////////////////////////////
146     // IResultSetTable interface
147 
148     public void close() throws DataSetException
149     {
150         logger.trace("close() - start");
151 
152         try
153         {
154             if (_resultSet != null)
155             {
156                 _resultSet.close();
157                 _resultSet = null;
158             }
159 
160             if (_statement != null)
161             {
162                 _statement.close();
163                 _statement = null;
164             }
165         }
166         catch (SQLException e)
167         {
168             throw new DataSetException(e);
169         }
170     }
171 
172     /**
173      * {@inheritDoc}
174      */
175     public String toString()
176     {
177         StringBuilder sb = new StringBuilder(2000);
178 
179         sb.append(getClass().getName()).append("[");
180         sb.append("_metaData=[").append(_metaData).append("], ");
181         sb.append("_resultSet=[").append(_resultSet).append("], ");
182         sb.append("_statement=[").append(_statement).append("]");
183         sb.append("]");
184 
185         return sb.toString();
186     }
187 }