Search in sources :

Example 1 with FlatXmlWriter

use of org.dbunit.dataset.xml.FlatXmlWriter in project RecordManager2 by moravianlibrary.

the class DBUnitHelper method dump.

public void dump(String outputFile) throws Exception {
    try (Connection connection = dataSource.getConnection()) {
        IDatabaseConnection conn = new DatabaseConnection(connection);
        FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream(outputFile));
        datasetWriter.setDocType("dataset.dtd");
        datasetWriter.write(conn.createDataSet());
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) FlatXmlWriter(org.dbunit.dataset.xml.FlatXmlWriter) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection)

Example 2 with FlatXmlWriter

use of org.dbunit.dataset.xml.FlatXmlWriter in project openmrs-core by openmrs.

the class MigrateDataSet method doMigration.

/**
 * Recurse over the given file and convert all xml files there in
 *
 * @param fileOrDirectory
 * @throws Exception
 */
private static void doMigration(File fileOrDirectory) throws Exception {
    String filename = fileOrDirectory.getName();
    if (filename.startsWith(".svn") || filename.endsWith("TestingApplicationContext.xml")) {
    // skip .svn files
    } else if (fileOrDirectory.isDirectory()) {
        for (File innerFile : fileOrDirectory.listFiles()) {
            doMigration(innerFile);
        }
    } else if (filename.endsWith(".xml")) {
        System.out.println("Migrating " + fileOrDirectory.getAbsolutePath());
        System.out.println(execMysqlCmd("DROP DATABASE IF EXISTS " + tempDatabaseName, null, false));
        System.out.println(execMysqlCmd("CREATE DATABASE " + tempDatabaseName + " DEFAULT CHARACTER SET utf8", null, false));
        System.out.println(execMysqlCmd(null, OLD_SCHEMA_FILE, true));
        System.out.println(execMysqlCmd(null, OLD_UPDATE_FILE, true));
        // the straight-up database connection
        String url = "jdbc:mysql://localhost/" + tempDatabaseName;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, credentials[0], credentials[1]);
        // database connection for dbunit
        IDatabaseConnection dbunitConnection = new DatabaseConnection(con);
        try (InputStream fileOrDirectoryStream = new FileInputStream(fileOrDirectory)) {
            PreparedStatement ps = con.prepareStatement("SET FOREIGN_KEY_CHECKS=0;");
            ps.execute();
            ps.close();
            IDataSet dataset = new FlatXmlDataSet(fileOrDirectoryStream);
            DatabaseOperation.REFRESH.execute(dbunitConnection, dataset);
            // turn off foreign key checks here too.
            System.out.println(execMysqlCmd("SET FOREIGN_KEY_CHECKS=0", NEW_UPDATE_FILE, true));
            System.out.println("Dumping new xml file");
            // get a new connection so dbunit knows the right column headers
            dbunitConnection = new DatabaseConnection(con);
            // full database export that will ignore empty tables
            FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream(fileOrDirectory));
            datasetWriter.write(dbunitConnection.createDataSet());
        } catch (Exception e) {
            System.err.println("Unable to convert: " + filename + " Error: " + e.getMessage());
        } finally {
            dbunitConnection = null;
        }
        System.out.println("Finished!");
    }
}
Also used : FlatXmlDataSet(org.dbunit.dataset.xml.FlatXmlDataSet) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) PreparedStatement(java.sql.PreparedStatement) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) FlatXmlWriter(org.dbunit.dataset.xml.FlatXmlWriter) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IDataSet(org.dbunit.dataset.IDataSet) File(java.io.File)

Aggregations

FileOutputStream (java.io.FileOutputStream)2 Connection (java.sql.Connection)2 DatabaseConnection (org.dbunit.database.DatabaseConnection)2 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)2 FlatXmlWriter (org.dbunit.dataset.xml.FlatXmlWriter)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 PreparedStatement (java.sql.PreparedStatement)1 IDataSet (org.dbunit.dataset.IDataSet)1 FlatXmlDataSet (org.dbunit.dataset.xml.FlatXmlDataSet)1