KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.dbunit.dataset.*;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.DatabaseMetaData JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.*;
31
32 /**
33  * provides access
34       to a database instance as a dataset.
35
36  * @author Manuel Laflamme
37  * @version $Revision: 1.28 $
38  * @since Feb 17, 2002
39  */

40 public class DatabaseDataSet extends AbstractDataSet
41 {
42     private final IDatabaseConnection _connection;
43     private final Map _tableMap = new HashMap();
44     private List _nameList = null;
45
46     DatabaseDataSet(IDatabaseConnection connection) throws SQLException JavaDoc
47     {
48         _connection = connection;
49     }
50
51     static String JavaDoc getSelectStatement(String JavaDoc schema, ITableMetaData metaData, String JavaDoc escapePattern)
52             throws DataSetException
53     {
54         Column[] columns = metaData.getColumns();
55         Column[] primaryKeys = metaData.getPrimaryKeys();
56
57         // select
58
StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc(128);
59         sqlBuffer.append("select ");
60         for (int i = 0; i < columns.length; i++)
61         {
62             if (i > 0)
63             {
64                 sqlBuffer.append(", ");
65             }
66             String JavaDoc columnName = DataSetUtils.getQualifiedName(null,
67                     columns[i].getColumnName(), escapePattern);
68             sqlBuffer.append(columnName);
69         }
70
71         // from
72
sqlBuffer.append(" from ");
73         sqlBuffer.append(DataSetUtils.getQualifiedName(schema,
74                 metaData.getTableName(), escapePattern));
75
76         // order by
77
for (int i = 0; i < primaryKeys.length; i++)
78         {
79             if (i == 0)
80             {
81                 sqlBuffer.append(" order by ");
82             }
83             else
84             {
85                 sqlBuffer.append(", ");
86             }
87             sqlBuffer.append(primaryKeys[i].getColumnName());
88
89         }
90
91         return sqlBuffer.toString();
92     }
93
94     private String JavaDoc getQualifiedName(String JavaDoc prefix, String JavaDoc name)
95     {
96         DatabaseConfig config = _connection.getConfig();
97         boolean feature = config.getFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES);
98         if (feature)
99         {
100             return DataSetUtils.getQualifiedName(prefix, name);
101         }
102         return name;
103     }
104
105     /**
106      * Get all the table names form the database that are not system tables.
107      */

108     private void initialize() throws DataSetException
109     {
110         if (_nameList != null)
111         {
112             return;
113         }
114
115         try
116         {
117             Connection JavaDoc jdbcConnection = _connection.getConnection();
118             String JavaDoc schema = _connection.getSchema();
119             String JavaDoc[] tableType = (String JavaDoc[])_connection.getConfig().getProperty(
120                     DatabaseConfig.PROPERTY_TABLE_TYPE);
121
122             DatabaseMetaData JavaDoc databaseMetaData = jdbcConnection.getMetaData();
123             ResultSet JavaDoc resultSet = databaseMetaData.getTables(
124                     null, schema, "%", tableType);
125
126             try
127             {
128                 List nameList = new ArrayList();
129                 while (resultSet.next())
130                 {
131                     String JavaDoc schemaName = resultSet.getString(2);
132                     String JavaDoc tableName = resultSet.getString(3);
133 // String tableType = resultSet.getString(4);
134
// System.out.println("schema=" + schemaName + ", table=" + tableName + ", type=" + tableType + "");
135
tableName = getQualifiedName(schemaName, tableName);
136
137                     // prevent table name conflict
138
if (_tableMap.containsKey(tableName.toUpperCase()))
139                     {
140                         throw new AmbiguousTableNameException(tableName);
141                     }
142                     nameList.add(tableName);
143                     _tableMap.put(tableName.toUpperCase(), null);
144                 }
145
146                 _nameList = nameList;
147             }
148             finally
149             {
150                 resultSet.close();
151             }
152         }
153         catch (SQLException JavaDoc e)
154         {
155             throw new DataSetException(e);
156         }
157     }
158
159     ////////////////////////////////////////////////////////////////////////////
160
// AbstractDataSet class
161

162     protected ITableIterator createIterator(boolean reversed)
163             throws DataSetException
164     {
165         String JavaDoc[] names = getTableNames();
166         if (reversed)
167         {
168             names = DataSetUtils.reverseStringArray(names);
169         }
170
171         return new DatabaseTableIterator(names, this);
172     }
173
174     ////////////////////////////////////////////////////////////////////////////
175
// IDataSet interface
176

177     public String JavaDoc[] getTableNames() throws DataSetException
178     {
179         initialize();
180
181         return (String JavaDoc[])_nameList.toArray(new String JavaDoc[0]);
182     }
183
184     public ITableMetaData getTableMetaData(String JavaDoc tableName) throws DataSetException
185     {
186         initialize();
187
188         // Verify if table exist in the database
189
String JavaDoc upperTableName = tableName.toUpperCase();
190         if (!_tableMap.containsKey(upperTableName))
191         {
192             throw new NoSuchTableException(tableName);
193         }
194
195         // Try to find cached metadata
196
ITableMetaData metaData = (ITableMetaData)_tableMap.get(upperTableName);
197         if (metaData != null)
198         {
199             return metaData;
200         }
201
202         // Search for original database table name
203
for (Iterator it = _nameList.iterator(); it.hasNext();)
204         {
205             String JavaDoc databaseTableName = (String JavaDoc)it.next();
206             if (databaseTableName.equalsIgnoreCase(tableName))
207             {
208                 // Create metadata and cache it
209
metaData = new DatabaseTableMetaData(
210                         databaseTableName, _connection);
211                 _tableMap.put(upperTableName, metaData);
212                 break;
213             }
214         }
215
216         return metaData;
217     }
218
219     public ITable getTable(String JavaDoc tableName) throws DataSetException
220     {
221         initialize();
222
223         try
224         {
225             ITableMetaData metaData = getTableMetaData(tableName);
226
227             DatabaseConfig config = _connection.getConfig();
228             IResultSetTableFactory factory = (IResultSetTableFactory)config.getProperty(
229                     DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
230             return factory.createTable(metaData, _connection);
231         }
232         catch (SQLException JavaDoc e)
233         {
234             throw new DataSetException(e);
235         }
236     }
237 }
238
239
240
241
242
243
244
245
246
247
248
249
Popular Tags