1 21 22 package org.dbunit.database; 23 24 import org.dbunit.dataset.*; 25 26 import java.sql.Connection ; 27 import java.sql.DatabaseMetaData ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.util.*; 31 32 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 47 { 48 _connection = connection; 49 } 50 51 static String getSelectStatement(String schema, ITableMetaData metaData, String escapePattern) 52 throws DataSetException 53 { 54 Column[] columns = metaData.getColumns(); 55 Column[] primaryKeys = metaData.getPrimaryKeys(); 56 57 StringBuffer sqlBuffer = new StringBuffer (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 columnName = DataSetUtils.getQualifiedName(null, 67 columns[i].getColumnName(), escapePattern); 68 sqlBuffer.append(columnName); 69 } 70 71 sqlBuffer.append(" from "); 73 sqlBuffer.append(DataSetUtils.getQualifiedName(schema, 74 metaData.getTableName(), escapePattern)); 75 76 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 getQualifiedName(String prefix, String 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 108 private void initialize() throws DataSetException 109 { 110 if (_nameList != null) 111 { 112 return; 113 } 114 115 try 116 { 117 Connection jdbcConnection = _connection.getConnection(); 118 String schema = _connection.getSchema(); 119 String [] tableType = (String [])_connection.getConfig().getProperty( 120 DatabaseConfig.PROPERTY_TABLE_TYPE); 121 122 DatabaseMetaData databaseMetaData = jdbcConnection.getMetaData(); 123 ResultSet resultSet = databaseMetaData.getTables( 124 null, schema, "%", tableType); 125 126 try 127 { 128 List nameList = new ArrayList(); 129 while (resultSet.next()) 130 { 131 String schemaName = resultSet.getString(2); 132 String tableName = resultSet.getString(3); 133 tableName = getQualifiedName(schemaName, tableName); 136 137 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 e) 154 { 155 throw new DataSetException(e); 156 } 157 } 158 159 162 protected ITableIterator createIterator(boolean reversed) 163 throws DataSetException 164 { 165 String [] names = getTableNames(); 166 if (reversed) 167 { 168 names = DataSetUtils.reverseStringArray(names); 169 } 170 171 return new DatabaseTableIterator(names, this); 172 } 173 174 177 public String [] getTableNames() throws DataSetException 178 { 179 initialize(); 180 181 return (String [])_nameList.toArray(new String [0]); 182 } 183 184 public ITableMetaData getTableMetaData(String tableName) throws DataSetException 185 { 186 initialize(); 187 188 String upperTableName = tableName.toUpperCase(); 190 if (!_tableMap.containsKey(upperTableName)) 191 { 192 throw new NoSuchTableException(tableName); 193 } 194 195 ITableMetaData metaData = (ITableMetaData)_tableMap.get(upperTableName); 197 if (metaData != null) 198 { 199 return metaData; 200 } 201 202 for (Iterator it = _nameList.iterator(); it.hasNext();) 204 { 205 String databaseTableName = (String )it.next(); 206 if (databaseTableName.equalsIgnoreCase(tableName)) 207 { 208 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 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 e) 233 { 234 throw new DataSetException(e); 235 } 236 } 237 } 238 239 240 241 242 243 244 245 246 247 248 249 | Popular Tags |