001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.database;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.AbstractTable;
027: import org.dbunit.dataset.DataSetException;
028: import org.dbunit.dataset.ITableMetaData;
029: import org.dbunit.dataset.datatype.IDataTypeFactory;
030:
031: import java.sql.Connection;
032: import java.sql.ResultSet;
033: import java.sql.SQLException;
034: import java.sql.Statement;
035:
036: /**
037: * @author Manuel Laflamme
038: * @since Apr 10, 2003
039: * @version $Revision: 554 $
040: */
041: public abstract class AbstractResultSetTable extends AbstractTable
042: implements IResultSetTable {
043:
044: /**
045: * Logger for this class
046: */
047: private static final Logger logger = LoggerFactory
048: .getLogger(AbstractResultSetTable.class);
049:
050: protected ITableMetaData _metaData;
051: private Statement _statement;
052: protected ResultSet _resultSet;
053:
054: public AbstractResultSetTable(ITableMetaData metaData,
055: ResultSet resultSet) throws SQLException, DataSetException {
056: _metaData = metaData;
057: _resultSet = resultSet;
058: }
059:
060: public AbstractResultSetTable(String tableName,
061: String selectStatement, IDatabaseConnection connection)
062: throws DataSetException, SQLException {
063: Connection jdbcConnection = connection.getConnection();
064: _statement = jdbcConnection.createStatement();
065: // _statement.setFetchDirection(ResultSet.FETCH_FORWARD);
066:
067: DatabaseConfig config = connection.getConfig();
068: IDataTypeFactory dataTypeFactory = (IDataTypeFactory) config
069: .getProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY);
070:
071: try {
072: _resultSet = _statement.executeQuery(selectStatement);
073: _metaData = DatabaseTableMetaData.createMetaData(tableName,
074: _resultSet, dataTypeFactory);
075: } catch (SQLException e) {
076: logger.error("AbstractResultSetTable()", e);
077:
078: _statement.close();
079: _statement = null;
080: throw e;
081: }
082: }
083:
084: public AbstractResultSetTable(ITableMetaData metaData,
085: IDatabaseConnection connection) throws DataSetException,
086: SQLException {
087: Connection jdbcConnection = connection.getConnection();
088: String escapePattern = (String) connection.getConfig()
089: .getProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
090: _statement = jdbcConnection.createStatement();
091: // _statement.setFetchDirection(ResultSet.FETCH_FORWARD);
092:
093: try {
094: String schema = connection.getSchema();
095: String selectStatement = getSelectStatement(schema,
096: metaData, escapePattern);
097: _resultSet = _statement.executeQuery(selectStatement);
098: _metaData = metaData;
099: } catch (SQLException e) {
100: logger.error("AbstractResultSetTable()", e);
101:
102: _statement.close();
103: _statement = null;
104: throw e;
105: }
106: }
107:
108: static String getSelectStatement(String schema,
109: ITableMetaData metaData, String escapePattern)
110: throws DataSetException {
111: logger.debug("getSelectStatement(schema=" + schema
112: + ", metaData=" + metaData + ", escapePattern="
113: + escapePattern + ") - start");
114:
115: return DatabaseDataSet.getSelectStatement(schema, metaData,
116: escapePattern);
117: }
118:
119: ////////////////////////////////////////////////////////////////////////////
120: // ITable interface
121:
122: public ITableMetaData getTableMetaData() {
123: logger.debug("getTableMetaData() - start");
124:
125: return _metaData;
126: }
127:
128: ////////////////////////////////////////////////////////////////////////////
129: // IResultSetTable interface
130:
131: public void close() throws DataSetException {
132: logger.debug("close() - start");
133:
134: try {
135: if (_resultSet != null) {
136: _resultSet.close();
137: _resultSet = null;
138: }
139:
140: if (_statement != null) {
141: _statement.close();
142: _statement = null;
143: }
144: } catch (SQLException e) {
145: logger.error("close()", e);
146:
147: throw new DataSetException(e);
148: }
149: }
150: }
|