/** * Adds the tables of the DbUnit dataset to the given schema. * * @param dbUnitDataSet The DbUnit dataset containing the tables, not null * @param schema The schema to add the tables to, not null */ protected void addTables(IDataSet dbUnitDataSet, Schema schema) throws DataSetException { ITableIterator dbUnitTableIterator = dbUnitDataSet.iterator(); while (dbUnitTableIterator.next()) { ITable dbUnitTable = dbUnitTableIterator.getTable(); String tableName = dbUnitTable.getTableMetaData().getTableName(); List<String> primaryKeyColumnNames = getPrimaryKeyColumnNames(dbUnitTable); Table table = schema.getTable(tableName); if (table == null) { table = new Table(tableName); schema.addTable(table); } addRows(dbUnitTable, table, primaryKeyColumnNames); } }
/** * Gets the primary key column names for the given DbUnit table. * * @param dbUnitTable The DbUnit table, not null * @return The pk column names, empty if none found */ protected List<String> getPrimaryKeyColumnNames(ITable dbUnitTable) throws DataSetException { List<String> result = new ArrayList<String>(); for (org.dbunit.dataset.Column column : dbUnitTable.getTableMetaData().getPrimaryKeys()) { result.add(column.getColumnName()); } return result; }
/** * Initializes the tables of this dataset * * @throws DataSetException * @since 2.4 */ private void initialize() throws DataSetException { logger.debug("initialize() - start"); if (_orderedTableNameMap != null) { logger.debug("The table name map has already been initialized."); // already initialized return; } // Gather all tables in the OrderedTableNameMap which also makes the duplicate check _orderedTableNameMap = this.createTableNameMap(); ITableIterator iterator = createIterator(false); while (iterator.next()) { ITable table = iterator.getTable(); _orderedTableNameMap.add(table.getTableMetaData().getTableName(), table); } }
/** * Adds the rows of the DbUnit table to the given table. * * @param dbUnitTable The DbUnit table containing the rows, not null * @param table The table to add the rows to, not null * @param primaryKeyColumnNames The names of the pk columns, empty if there are none */ protected void addRows(ITable dbUnitTable, Table table, List<String> primaryKeyColumnNames) throws DataSetException { org.dbunit.dataset.Column[] columns = dbUnitTable.getTableMetaData().getColumns(); int rowCount = dbUnitTable.getRowCount(); for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { Row row = new Row(); table.addRow(row); for (org.dbunit.dataset.Column dbUnitColumn : columns) { String columnName = dbUnitColumn.getColumnName(); DataType columnType = dbUnitColumn.getDataType(); Object value = dbUnitTable.getValue(rowIndex, columnName); Column column = new Column(columnName, columnType, value); if (primaryKeyColumnNames.contains(columnName)) { row.addPrimaryKeyColumn(column); } else { row.addColumn(column); } } } }