Search in sources :

Example 6 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project ORCID-Source by ORCID.

the class DBUnitTest method cleanAll.

private static void cleanAll(IDatabaseConnection connection) throws DatabaseUnitException, SQLException {
    QueryDataSet dataSet = new QueryDataSet(connection);
    for (String table : tables) {
        dataSet.addTable(table);
    }
    DatabaseOperation.DELETE.execute(connection, dataSet);
}
Also used : QueryDataSet(org.dbunit.database.QueryDataSet)

Example 7 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project activityinfo by bedatadriven.

the class ExtractDbUnit method main.

/**
 * Utility to create a dbunit xml file from a local mysql database
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/activityinfo?zeroDateTimeBehavior=convertToNull", "root", "root");
    IDatabaseConnection connection = new MySqlConnection(jdbcConnection, null);
    // partial database export
    QueryDataSet partialDataSet = new QueryDataSet(connection);
    // 
    // partialDataSet.addTable("userlogin", "select * from userlogin where userid in " +
    // "(select owneruserid from userdatabase where databaseid=1100)");
    int databaseId = 4;
    partialDataSet.addTable("country", "select * from country where countryid in (select countryId from userdatabase where databaseid=" + databaseId + ")");
    partialDataSet.addTable("locationtype", "select * from locationtype where locationTypeId=1");
    partialDataSet.addTable("location", "select * from location where locationtypeid = 1");
    partialDataSet.addTable("userdatabase", "select * from userdatabase where databaseid=" + databaseId + "");
    partialDataSet.addTable("partnerindatabase", "select * from partnerindatabase where databaseid=" + databaseId + "");
    partialDataSet.addTable("partner", "select * from partner where partnerid in (select partnerid from partnerindatabase where databaseid=" + databaseId + ")");
    partialDataSet.addTable("activity", "select * from activity where activityId=33");
    partialDataSet.addTable("indicator", "select * from indicator where activityId=33");
    partialDataSet.addTable("attributegroupinactivity", "select * from attributegroupinactivity where activityid = 33");
    partialDataSet.addTable("attributegroup", "select * from attributegroup where attributegroupid in" + " (select attributegroupid from attributegroupinactivity where activityId=33)");
    partialDataSet.addTable("attribute", "select * from attribute where attributegroupid in" + " (select attributegroupid from attributegroupinactivity where activityId=33)");
    partialDataSet.addTable("userlogin", "select * from userlogin where userid in " + "(select userId from sitehistory where siteid in (302007470, 968196924, 1379807452, 1435486740))");
    partialDataSet.addTable("site", "select * from site where siteid in (302007470, 968196924, 1379807452, 1435486740)");
    partialDataSet.addTable("sitehistory", "select * from sitehistory where siteid in (302007470, 968196924, 1379807452, 1435486740)");
    FlatXmlDataSet.write(partialDataSet, new FileOutputStream("store/mysql/src/test/resources/org/activityinfo/store/mysql/history.db.xml"));
}
Also used : FileOutputStream(java.io.FileOutputStream) MySqlConnection(org.dbunit.ext.mysql.MySqlConnection) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) MySqlConnection(org.dbunit.ext.mysql.MySqlConnection) QueryDataSet(org.dbunit.database.QueryDataSet) IDatabaseConnection(org.dbunit.database.IDatabaseConnection)

Example 8 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project openmrs-core by openmrs.

the class TestUtil method printOutTableContents.

/**
 * Print the contents of the given tableName to system.out<br>
 * <br>
 * Call this from any {@link BaseContextSensitiveTest} child by:
 * TestUtil.printOutTableContents(getConnection(), "encounter");
 *
 * @param sqlConnection the connection to use
 * @param tableNames the name(s) of the table(s) to print out
 * @throws Exception
 */
public static void printOutTableContents(Connection sqlConnection, String... tableNames) throws Exception {
    for (String tableName : tableNames) {
        System.out.println("The contents of table: " + tableName);
        IDatabaseConnection connection = new DatabaseConnection(sqlConnection);
        DatabaseConfig config = connection.getConfig();
        config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
        QueryDataSet outputSet = new QueryDataSet(connection);
        outputSet.addTable(tableName);
        FlatXmlDataSet.write(outputSet, System.out);
    }
}
Also used : HsqldbDataTypeFactory(org.dbunit.ext.hsqldb.HsqldbDataTypeFactory) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) QueryDataSet(org.dbunit.database.QueryDataSet) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConfig(org.dbunit.database.DatabaseConfig)

Aggregations

QueryDataSet (org.dbunit.database.QueryDataSet)8 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)6 DatabaseConnection (org.dbunit.database.DatabaseConnection)5 FileOutputStream (java.io.FileOutputStream)4 Connection (java.sql.Connection)3 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 DatabaseConfig (org.dbunit.database.DatabaseConfig)1 HsqldbDataTypeFactory (org.dbunit.ext.hsqldb.HsqldbDataTypeFactory)1 MySqlConnection (org.dbunit.ext.mysql.MySqlConnection)1 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)1