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  
22  package org.dbunit.database;
23  
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.sql.Statement;
28  
29  import org.dbunit.DatabaseUnitRuntimeException;
30  import org.dbunit.database.statement.IStatementFactory;
31  import org.dbunit.dataset.DataSetException;
32  import org.dbunit.dataset.FilteredDataSet;
33  import org.dbunit.dataset.IDataSet;
34  import org.dbunit.dataset.ITable;
35  import org.dbunit.util.QualifiedTableName;
36  import org.dbunit.util.SQLHelper;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * @author Manuel Laflamme
42   * @version $Revision: 1186 $
43   * @since Mar 6, 2002
44   */
45  public abstract class AbstractDatabaseConnection implements IDatabaseConnection
46  {
47  
48      /**
49       * Logger for this class
50       */
51      private static final Logger logger = LoggerFactory.getLogger(AbstractDatabaseConnection.class);
52  
53      private IDataSet _dataSet = null;
54      private final DatabaseConfig _databaseConfig;
55  
56      public AbstractDatabaseConnection()
57      {
58          _databaseConfig = new DatabaseConfig();
59      }
60  
61      ////////////////////////////////////////////////////////////////////////////
62      // IDatabaseConnection interface
63  
64      public IDataSet createDataSet() throws SQLException
65      {
66          logger.debug("createDataSet() - start");
67  
68          if (_dataSet == null)
69          {
70              _dataSet = new DatabaseDataSet(this);
71          }
72  
73          return _dataSet;
74      }
75  
76      public IDataSet createDataSet(String[] tableNames)
77      throws DataSetException, SQLException
78      {
79          logger.debug("createDataSet(tableNames={}) - start", tableNames);
80  
81          return new FilteredDataSet(tableNames, createDataSet());
82      }
83  
84      public ITable createQueryTable(String resultName, String sql)
85      throws DataSetException, SQLException
86      {
87          logger.debug("createQueryTable(resultName={}, sql={}) - start", resultName, sql);
88  
89          IResultSetTableFactory tableFactory = getResultSetTableFactory();
90          IResultSetTable rsTable = tableFactory.createTable(resultName, sql, this);
91          logger.debug("createQueryTable: rowCount={}", Integer.valueOf(rsTable
92                  .getRowCount()));
93          return rsTable;
94      }
95  
96      public ITable createTable(String resultName, PreparedStatement preparedStatement)
97      throws DataSetException, SQLException
98      {
99          logger.debug("createQueryTable(resultName={}, preparedStatement={}) - start", resultName, preparedStatement);
100 
101         IResultSetTableFactory tableFactory = getResultSetTableFactory();
102         IResultSetTable rsTable = tableFactory.createTable(resultName, preparedStatement, this);
103         return rsTable;
104     }
105 
106 
107     public ITable createTable(String tableName) throws DataSetException,
108     SQLException
109     {
110         logger.debug("createTable(tableName={}) - start", tableName);
111 
112         if (tableName == null) {
113             throw new NullPointerException("The parameter 'tableName' must not be null");
114         }
115 
116         String sql = "select * from " + tableName; // TODO Think about QualifiedTableNames here - needed or not?
117         return this.createQueryTable(tableName, sql);
118     }
119 
120     public int getRowCount(String tableName) throws SQLException
121     {
122         logger.debug("getRowCount(tableName={}) - start", tableName);
123 
124         return getRowCount(tableName, null);
125     }
126 
127     public int getRowCount(String tableName, String whereClause) throws SQLException
128     {
129         logger.debug("getRowCount(tableName={}, whereClause={}) - start", tableName, whereClause);
130 
131         StringBuffer sqlBuffer = new StringBuffer(128);
132         sqlBuffer.append("select count(*) from ");
133 
134         //add table name and schema (schema only if available)
135         QualifiedTableName qualifiedTableName = new QualifiedTableName(tableName, this.getSchema());
136         String qualifiedName = qualifiedTableName.getQualifiedName();
137         sqlBuffer.append(qualifiedName);
138         if (whereClause != null)
139         {
140             sqlBuffer.append(" ");
141             sqlBuffer.append(whereClause);
142         }
143 
144         Statement statement = getConnection().createStatement();
145         ResultSet resultSet = null;
146         try
147         {
148             resultSet = statement.executeQuery(sqlBuffer.toString());
149             if(resultSet.next()) {
150                 return resultSet.getInt(1);
151             } else {
152                 throw new DatabaseUnitRuntimeException("Select count did not return any results for table '" +
153                         tableName + "'. Statement: " + sqlBuffer.toString());
154             }
155         }
156         finally
157         {
158             SQLHelper.close(resultSet, statement);
159         }
160     }
161 
162     public DatabaseConfig getConfig()
163     {
164         return _databaseConfig;
165     }
166 
167     /**
168      * @deprecated Use {@link #getConfig}
169      */
170     public IStatementFactory getStatementFactory()
171     {
172         return (IStatementFactory)_databaseConfig.getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
173     }
174 
175     private IResultSetTableFactory getResultSetTableFactory()
176     {
177         return (IResultSetTableFactory)_databaseConfig.getProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
178 
179     }
180 
181     public String toString()
182     {
183         StringBuffer sb = new StringBuffer();
184         sb.append("_databaseConfig=").append(_databaseConfig);
185         sb.append(", _dataSet=").append(_dataSet);
186         return sb.toString();
187     }
188 }