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:
022: package org.dbunit.database;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.database.statement.IStatementFactory;
028: import org.dbunit.dataset.*;
029: import org.dbunit.dataset.datatype.IDataTypeFactory;
030:
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033: import java.sql.Statement;
034:
035: /**
036: * @author Manuel Laflamme
037: * @version $Revision: 554 $
038: * @since Mar 6, 2002
039: */
040: public abstract class AbstractDatabaseConnection implements
041: IDatabaseConnection {
042:
043: /**
044: * Logger for this class
045: */
046: private static final Logger logger = LoggerFactory
047: .getLogger(AbstractDatabaseConnection.class);
048:
049: private IDataSet _dataSet = null;
050: private DatabaseConfig _databaseConfig;
051:
052: public AbstractDatabaseConnection() {
053: _databaseConfig = new DatabaseConfig();
054: }
055:
056: ////////////////////////////////////////////////////////////////////////////
057: // IDatabaseConnection interface
058:
059: public IDataSet createDataSet() throws SQLException {
060: logger.debug("createDataSet() - start");
061:
062: if (_dataSet == null) {
063: _dataSet = new DatabaseDataSet(this );
064: }
065:
066: return _dataSet;
067: }
068:
069: public IDataSet createDataSet(String[] tableNames)
070: throws SQLException {
071: logger.debug("createDataSet(tableNames=" + tableNames
072: + ") - start");
073:
074: return new FilteredDataSet(tableNames, createDataSet());
075: }
076:
077: public ITable createQueryTable(String resultName, String sql)
078: throws DataSetException, SQLException {
079: logger.debug("createQueryTable(resultName=" + resultName
080: + ", sql=" + sql + ") - start");
081:
082: Statement statement = getConnection().createStatement();
083: try {
084: ResultSet resultSet = statement.executeQuery(sql);
085:
086: try {
087: IDataTypeFactory typeFactory = (IDataTypeFactory) _databaseConfig
088: .getProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY);
089: ITableMetaData metaData = DatabaseTableMetaData
090: .createMetaData(resultName, resultSet,
091: typeFactory);
092: return new CachedResultSetTable(metaData, resultSet);
093: } finally {
094: resultSet.close();
095: }
096: } finally {
097: statement.close();
098: }
099: }
100:
101: public int getRowCount(String tableName) throws SQLException {
102: logger
103: .debug("getRowCount(tableName=" + tableName
104: + ") - start");
105:
106: return getRowCount(tableName, null);
107: }
108:
109: public int getRowCount(String tableName, String whereClause)
110: throws SQLException {
111: logger.debug("getRowCount(tableName=" + tableName
112: + ", whereClause=" + whereClause + ") - start");
113:
114: StringBuffer sqlBuffer = new StringBuffer(128);
115: sqlBuffer.append("select count(*) from ");
116: sqlBuffer.append(tableName);
117: if (whereClause != null) {
118: sqlBuffer.append(" ");
119: sqlBuffer.append(whereClause);
120: }
121:
122: Statement statement = getConnection().createStatement();
123: try {
124: ResultSet resultSet = statement.executeQuery(sqlBuffer
125: .toString());
126: try {
127: resultSet.next();
128: return resultSet.getInt(1);
129: } finally {
130: resultSet.close();
131: }
132: } finally {
133: statement.close();
134: }
135: }
136:
137: public DatabaseConfig getConfig() {
138: logger.debug("getConfig() - start");
139:
140: return _databaseConfig;
141: }
142:
143: public IStatementFactory getStatementFactory() {
144: logger.debug("getStatementFactory() - start");
145:
146: return (IStatementFactory) _databaseConfig
147: .getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
148: }
149:
150: }
|