KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractDataSet;
24 import org.dbunit.dataset.DataSetException;
25 import org.dbunit.dataset.ITableIterator;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * Holds collection of tables resulting from database query.
35  *
36  * @author Eric Pugh
37  * @since Dec 4, 2002
38  * @version $Revision: 1.8 $
39  */

40 public class QueryDataSet extends AbstractDataSet
41 {
42
43     private final IDatabaseConnection _connection;
44     private final List JavaDoc _tableEntries = new ArrayList JavaDoc();
45
46
47     /**
48      * Create a QueryDataSet by passing in the connection to the database to use.
49      *
50      * @param connection The connection object to the database.
51      * @exception java.sql.SQLException Description of the Exception
52      */

53     public QueryDataSet(IDatabaseConnection connection)
54             throws SQLException JavaDoc
55     {
56         _connection = connection;
57     }
58
59     /**
60      * Adds a table and it's associted query to this dataset.
61      *
62      * @param tableName The name of the table
63      * @param query The query to retrieve data with for this table
64      */

65     public void addTable(String JavaDoc tableName, String JavaDoc query)
66     {
67         _tableEntries.add(new TableEntry(tableName, query));
68     }
69
70     /**
71      * Adds a table with using 'SELECT * FROM <code>tableName</code>' as query.
72      *
73      * @param tableName The name of the table
74      */

75     public void addTable(String JavaDoc tableName)
76     {
77         _tableEntries.add(new TableEntry(tableName, null));
78     }
79
80     ////////////////////////////////////////////////////////////////////////////
81
// AbstractDataSet class
82

83     protected ITableIterator createIterator(boolean reversed) throws DataSetException
84     {
85         List JavaDoc tableEntries = new ArrayList JavaDoc(_tableEntries);
86         if (reversed)
87         {
88             Collections.reverse(tableEntries);
89         }
90
91         return new QueryTableIterator(tableEntries, _connection);
92     }
93
94     ////////////////////////////////////////////////////////////////////////////
95
// IDataSet interface
96

97     public String JavaDoc[] getTableNames() throws DataSetException
98     {
99         List JavaDoc names = new ArrayList JavaDoc();
100         for (Iterator it = _tableEntries.iterator(); it.hasNext();)
101         {
102             TableEntry entry = (TableEntry)it.next();
103             names.add(entry.getTableName());
104         }
105
106         return (String JavaDoc[])names.toArray(new String JavaDoc[0]);
107     }
108
109     static class TableEntry
110     {
111         private final String JavaDoc _tableName;
112         private final String JavaDoc _query;
113
114         public TableEntry(String JavaDoc tableName, String JavaDoc query)
115         {
116             _tableName = tableName;
117             _query = query;
118         }
119
120         public String JavaDoc getTableName()
121         {
122             return _tableName;
123         }
124
125         public String JavaDoc getQuery()
126         {
127             return _query;
128         }
129     }
130 }
131
132
133
Popular Tags