1 21 package org.dbunit.database; 22 23 import org.dbunit.dataset.*; 24 25 import java.sql.SQLException ; 26 import java.util.List ; 27 28 33 public class QueryTableIterator implements ITableIterator 34 { 35 private final List _tableEntries; 36 private final IDatabaseConnection _connection; 37 private IResultSetTable _currentTable; 38 private int _index = -1; 39 40 public QueryTableIterator(List tableEntries, IDatabaseConnection connection) 41 { 42 _tableEntries = tableEntries; 43 _connection = connection; 44 _currentTable = null; 45 } 46 47 50 public boolean next() throws DataSetException 51 { 52 _index++; 53 54 if (_currentTable != null) 56 { 57 _currentTable.close(); 58 _currentTable = null; 59 } 60 61 return _index < _tableEntries.size(); 62 } 63 64 public ITableMetaData getTableMetaData() throws DataSetException 65 { 66 QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index); 67 68 if (entry.getQuery() == null) 70 { 71 try 72 { 73 IDataSet dataSet = _connection.createDataSet(); 74 return dataSet.getTableMetaData(entry.getTableName()); 75 } 76 catch (SQLException e) 77 { 78 throw new DataSetException(e); 79 } 80 } 81 else 82 { 83 return getTable().getTableMetaData(); 84 } 85 } 86 87 public ITable getTable() throws DataSetException 88 { 89 if (_currentTable == null) 90 { 91 try 92 { 93 QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index); 94 95 if (entry.getQuery() == null) 97 { 98 IDataSet dataSet = _connection.createDataSet(); 99 _currentTable = (IResultSetTable)dataSet.getTable(entry.getTableName()); 100 } 101 else 102 { 103 DatabaseConfig config = _connection.getConfig(); 104 IResultSetTableFactory factory = (IResultSetTableFactory)config.getProperty( 105 DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY); 106 107 _currentTable = factory.createTable(entry.getTableName(), entry.getQuery(), _connection); 108 } 109 } 110 catch (SQLException e) 111 { 112 throw new DataSetException(e); 113 } 114 } 115 return _currentTable; 116 } 117 } 118 | Popular Tags |