KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > AbstractResultSetTable


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 org.dbunit.dataset.AbstractTable;
24 import org.dbunit.dataset.DataSetException;
25 import org.dbunit.dataset.ITableMetaData;
26 import org.dbunit.dataset.datatype.IDataTypeFactory;
27
28 import java.sql.Connection JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.Statement JavaDoc;
32
33 /**
34  * @author Manuel Laflamme
35  * @since Apr 10, 2003
36  * @version $Revision: 1.7 $
37  */

38 public abstract class AbstractResultSetTable extends AbstractTable
39         implements IResultSetTable
40 {
41     protected ITableMetaData _metaData;
42     private Statement JavaDoc _statement;
43     protected ResultSet JavaDoc _resultSet;
44
45     public AbstractResultSetTable(ITableMetaData metaData, ResultSet JavaDoc resultSet)
46             throws SQLException JavaDoc, DataSetException
47     {
48         _metaData = metaData;
49         _resultSet = resultSet;
50     }
51
52     public AbstractResultSetTable(String JavaDoc tableName, String JavaDoc selectStatement,
53             IDatabaseConnection connection)
54             throws DataSetException, SQLException JavaDoc
55     {
56         Connection JavaDoc jdbcConnection = connection.getConnection();
57         _statement = jdbcConnection.createStatement();
58 // _statement.setFetchDirection(ResultSet.FETCH_FORWARD);
59

60         DatabaseConfig config = connection.getConfig();
61         IDataTypeFactory dataTypeFactory = (IDataTypeFactory)config.getProperty(
62                 DatabaseConfig.PROPERTY_DATATYPE_FACTORY);
63
64         try
65         {
66             _resultSet = _statement.executeQuery(selectStatement);
67             _metaData = DatabaseTableMetaData.createMetaData(tableName,
68                     _resultSet, dataTypeFactory);
69         }
70         catch (SQLException JavaDoc e)
71         {
72             _statement.close();
73             _statement = null;
74             throw e;
75         }
76     }
77
78     public AbstractResultSetTable(ITableMetaData metaData,
79             IDatabaseConnection connection) throws DataSetException, SQLException JavaDoc
80     {
81         Connection JavaDoc jdbcConnection = connection.getConnection();
82         String JavaDoc escapePattern = (String JavaDoc)connection.getConfig().getProperty(
83                 DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
84         _statement = jdbcConnection.createStatement();
85 // _statement.setFetchDirection(ResultSet.FETCH_FORWARD);
86

87         try
88         {
89             String JavaDoc schema = connection.getSchema();
90             String JavaDoc selectStatement = getSelectStatement(schema, metaData, escapePattern);
91             _resultSet = _statement.executeQuery(selectStatement);
92             _metaData = metaData;
93         }
94         catch (SQLException JavaDoc e)
95         {
96             _statement.close();
97             _statement = null;
98             throw e;
99         }
100     }
101
102     static String JavaDoc getSelectStatement(String JavaDoc schema, ITableMetaData metaData, String JavaDoc escapePattern)
103             throws DataSetException
104     {
105         return DatabaseDataSet.getSelectStatement(schema, metaData, escapePattern);
106     }
107
108     ////////////////////////////////////////////////////////////////////////////
109
// ITable interface
110

111     public ITableMetaData getTableMetaData()
112     {
113         return _metaData;
114     }
115
116     ////////////////////////////////////////////////////////////////////////////
117
// IResultSetTable interface
118

119     public void close() throws DataSetException
120     {
121         try
122         {
123             if (_resultSet != null)
124             {
125                 _resultSet.close();
126                 _resultSet = null;
127             }
128
129             if (_statement != null)
130             {
131                 _statement.close();
132                 _statement = null;
133             }
134         }
135         catch (SQLException JavaDoc e)
136         {
137             throw new DataSetException(e);
138         }
139     }
140 }
141
Popular Tags