📄 databasedataio.java
字号:
* @param tables The tables
* @return The sorted tables
*/
private List sortTables(Table[] tables)
{
ArrayList result = new ArrayList();
HashSet processed = new HashSet();
ListOrderedMap pending = new ListOrderedMap();
for (int idx = 0; idx < tables.length; idx++)
{
Table table = tables[idx];
if (table.getForeignKeyCount() == 0)
{
result.add(table);
processed.add(table);
}
else
{
HashSet waitedFor = new HashSet();
for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++)
{
Table waitedForTable = table.getForeignKey(fkIdx).getForeignTable();
if (!table.equals(waitedForTable))
{
waitedFor.add(waitedForTable);
}
}
pending.put(table, waitedFor);
}
}
HashSet newProcessed = new HashSet();
while (!processed.isEmpty() && !pending.isEmpty())
{
newProcessed.clear();
for (Iterator it = pending.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
Table table = (Table)entry.getKey();
HashSet waitedFor = (HashSet)entry.getValue();
waitedFor.removeAll(processed);
if (waitedFor.isEmpty())
{
it.remove();
result.add(table);
newProcessed.add(table);
}
}
processed.clear();
HashSet tmp = processed;
processed = newProcessed;
newProcessed = tmp;
}
// the remaining are within circular dependencies
for (Iterator it = pending.keySet().iterator(); it.hasNext();)
{
result.add(it.next());
}
return result;
}
/**
* Writes the data contained in a single table to XML.
*
* @param platform The platform
* @param model The database model
* @param writer The data writer
* @param tableIdx
*/
private void writeDataForTableToXML(Platform platform, Database model, Table table, DataWriter writer)
{
Table[] tables = { table };
StringBuffer query = new StringBuffer();
query.append("SELECT ");
Connection connection = null;
String schema = null;
if (_determineSchema)
{
try
{
// TODO: Remove this once we have full support for schemas
connection = platform.borrowConnection();
schema = platform.getModelReader().determineSchemaOf(connection, _schemaPattern, tables[0]);
}
catch (SQLException ex)
{
// ignored
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException ex)
{
// ignored
}
}
}
}
Column[] columns = tables[0].getColumns();
for (int columnIdx = 0; columnIdx < columns.length; columnIdx++)
{
if (columnIdx > 0)
{
query.append(",");
}
if (platform.isDelimitedIdentifierModeOn())
{
query.append(platform.getPlatformInfo().getDelimiterToken());
}
query.append(columns[columnIdx].getName());
if (platform.isDelimitedIdentifierModeOn())
{
query.append(platform.getPlatformInfo().getDelimiterToken());
}
}
query.append(" FROM ");
if (platform.isDelimitedIdentifierModeOn())
{
query.append(platform.getPlatformInfo().getDelimiterToken());
}
if (schema != null)
{
query.append(schema);
query.append(".");
}
query.append(tables[0].getName());
if (platform.isDelimitedIdentifierModeOn())
{
query.append(platform.getPlatformInfo().getDelimiterToken());
}
writer.write(platform.query(model, query.toString(), tables));
}
/**
* Returns a data reader application configured for the given platform (which needs to
* be connected to a live database) and model.
*
* @param platform The database
* @param model The model
* @return The data reader
*/
public DataReader getConfiguredDataReader(Platform platform, Database model) throws DdlUtilsException
{
DataToDatabaseSink sink = new DataToDatabaseSink(platform, model);
DataReader reader = new DataReader();
sink.setHaltOnErrors(_failOnError);
sink.setEnsureForeignKeyOrder(_ensureFKOrder);
sink.setUseBatchMode(_useBatchMode);
if (_batchSize != null)
{
sink.setBatchSize(_batchSize.intValue());
}
reader.setModel(model);
reader.setSink(sink);
registerConverters(reader.getConverterConfiguration());
return reader;
}
/**
* Reads the data from the specified files and writes it to the database to which the given
* platform is connected.
*
* @param platform The platform, must be connected to a live database
* @param files The XML data files
*/
public void writeDataToDatabase(Platform platform, String[] files) throws DdlUtilsException
{
writeDataToDatabase(platform, platform.readModelFromDatabase("unnamed"), files);
}
/**
* Reads the data from the given input streams and writes it to the database to which the given
* platform is connected.
*
* @param platform The platform, must be connected to a live database
* @param inputs The input streams for the XML data
*/
public void writeDataToDatabase(Platform platform, InputStream[] inputs) throws DdlUtilsException
{
writeDataToDatabase(platform, platform.readModelFromDatabase("unnamed"), inputs);
}
/**
* Reads the data from the given input readers and writes it to the database to which the given
* platform is connected.
*
* @param platform The platform, must be connected to a live database
* @param inputs The input readers for the XML data
*/
public void writeDataToDatabase(Platform platform, Reader[] inputs) throws DdlUtilsException
{
writeDataToDatabase(platform, platform.readModelFromDatabase("unnamed"), inputs);
}
/**
* Reads the data from the indicated files and writes it to the database to which the given
* platform is connected. Only data that matches the given model will be written.
*
* @param platform The platform, must be connected to a live database
* @param model The model to which to constrain the written data
* @param files The XML data files
*/
public void writeDataToDatabase(Platform platform, Database model, String[] files) throws DdlUtilsException
{
DataReader dataReader = getConfiguredDataReader(platform, model);
dataReader.getSink().start();
for (int idx = 0; (files != null) && (idx < files.length); idx++)
{
writeDataToDatabase(dataReader, files[idx]);
}
dataReader.getSink().end();
}
/**
* Reads the data from the given input streams and writes it to the database to which the given
* platform is connected. Only data that matches the given model will be written.
*
* @param platform The platform, must be connected to a live database
* @param model The model to which to constrain the written data
* @param inputs The input streams for the XML data
*/
public void writeDataToDatabase(Platform platform, Database model, InputStream[] inputs) throws DdlUtilsException
{
DataReader dataReader = getConfiguredDataReader(platform, model);
dataReader.getSink().start();
for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++)
{
writeDataToDatabase(dataReader, inputs[idx]);
}
dataReader.getSink().end();
}
/**
* Reads the data from the given input readers and writes it to the database to which the given
* platform is connected. Only data that matches the given model will be written.
*
* @param platform The platform, must be connected to a live database
* @param model The model to which to constrain the written data
* @param inputs The input readers for the XML data
*/
public void writeDataToDatabase(Platform platform, Database model, Reader[] inputs) throws DdlUtilsException
{
DataReader dataReader = getConfiguredDataReader(platform, model);
dataReader.getSink().start();
for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++)
{
writeDataToDatabase(dataReader, inputs[idx]);
}
dataReader.getSink().end();
}
/**
* Reads the data from the specified files and writes it to the database via the given data reader.
* Note that the sink that the data reader is configured with, won't be started or ended by
* this method. This has to be done by the code using this method.
*
* @param dataReader The data reader
* @param files The XML data files
*/
public void writeDataToDatabase(DataReader dataReader, String[] files) throws DdlUtilsException
{
for (int idx = 0; (files != null) && (idx < files.length); idx++)
{
writeDataToDatabase(dataReader, files[idx]);
}
}
/**
* Reads the data from the given input stream and writes it to the database via the given data reader.
* Note that the input stream won't be closed by this method. Note also that the sink that the data
* reader is configured with, won't be started or ended by this method. This has to be done by the
* code using this method.
*
* @param dataReader The data reader
* @param inputs The input streams for the XML data
*/
public void writeDataToDatabase(DataReader dataReader, InputStream[] inputs) throws DdlUtilsException
{
for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++)
{
writeDataToDatabase(dataReader, inputs[idx]);
}
}
/**
* Reads the data from the given input stream and writes it to the database via the given data reader.
* Note that the input stream won't be closed by this method. Note also that the sink that the data
* reader is configured with, won't be started or ended by this method. This has to be done by the
* code using this method.
*
* @param dataReader The data reader
* @param inputs The input readers for the XML data
*/
public void writeDataToDatabase(DataReader dataReader, Reader[] inputs) throws DdlUtilsException
{
for (int idx = 0; (inputs != null) && (idx < inputs.length); idx++)
{
writeDataToDatabase(dataReader, inputs[idx]);
}
}
/**
* Reads the data from the indicated XML file and writes it to the database via the given data reader.
* Note that the sink that the data reader is configured with, won't be started or ended by this method.
* This has to be done by the code using this method.
*
* @param dataReader The data reader
* @param path The path to the XML data file
*/
public void writeDataToDatabase(DataReader dataReader, String path) throws DdlUtilsException
{
try
{
dataReader.parse(path);
}
catch (Exception ex)
{
throw new DdlUtilsException(ex);
}
}
/**
* Reads the data from the given input stream and writes it to the database via the given data reader.
* Note that the input stream won't be closed by this method. Note also that the sink that the data
* reader is configured with, won't be started or ended by this method. This has to be done by the
* code using this method.
*
* @param dataReader The data reader
* @param input The input stream for the XML data
*/
public void writeDataToDatabase(DataReader dataReader, InputStream input) throws DdlUtilsException
{
try
{
dataReader.parse(input);
}
catch (Exception ex)
{
throw new DdlUtilsException(ex);
}
}
/**
* Reads the data from the given input stream and writes it to the database via the given data reader.
* Note that the input stream won't be closed by this method. Note also that the sink that the data
* reader is configured with, won't be started or ended by this method. This has to be done by the
* code using this method.
*
* @param dataReader The data reader
* @param input The input reader for the XML data
*/
public void writeDataToDatabase(DataReader dataReader, Reader input) throws DdlUtilsException
{
try
{
dataReader.parse(input);
}
catch (Exception ex)
{
throw new DdlUtilsException(ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -