1 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 ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 40 public class QueryDataSet extends AbstractDataSet 41 { 42 43 private final IDatabaseConnection _connection; 44 private final List _tableEntries = new ArrayList (); 45 46 47 53 public QueryDataSet(IDatabaseConnection connection) 54 throws SQLException 55 { 56 _connection = connection; 57 } 58 59 65 public void addTable(String tableName, String query) 66 { 67 _tableEntries.add(new TableEntry(tableName, query)); 68 } 69 70 75 public void addTable(String tableName) 76 { 77 _tableEntries.add(new TableEntry(tableName, null)); 78 } 79 80 83 protected ITableIterator createIterator(boolean reversed) throws DataSetException 84 { 85 List tableEntries = new ArrayList (_tableEntries); 86 if (reversed) 87 { 88 Collections.reverse(tableEntries); 89 } 90 91 return new QueryTableIterator(tableEntries, _connection); 92 } 93 94 97 public String [] getTableNames() throws DataSetException 98 { 99 List names = new ArrayList (); 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 [])names.toArray(new String [0]); 107 } 108 109 static class TableEntry 110 { 111 private final String _tableName; 112 private final String _query; 113 114 public TableEntry(String tableName, String query) 115 { 116 _tableName = tableName; 117 _query = query; 118 } 119 120 public String getTableName() 121 { 122 return _tableName; 123 } 124 125 public String getQuery() 126 { 127 return _query; 128 } 129 } 130 } 131 132 133 | Popular Tags |