dsatish
dsatish

Reputation: 1041

DbUnit - Delete rows based on non-primary key field value

It looks like DbUnit is using JDBC metadata to determine the primary key fields and constructing delete statement using those field:

delete from tbl_name where pk_field1=? and pk_field2=? and pk_field3=?

Is there a way to delete rows based on one field value of a composite key or value of a non-primary key field (e.g. delete rows where created_date = xyz)

Upvotes: 4

Views: 1788

Answers (1)

Guilherme Torres Castro
Guilherme Torres Castro

Reputation: 15350

You can create a QueryDataSet and set DatabaseOperation to DELETE.

For exemple, if you are extending DBTestCase:

protected IDataSet getDataSet() {
    QueryDataSet queryDataSet = null;
    String query = "SELECT * FROM tbl_name pk_field1=? and pk_field2=?";
    try {
        queryDataSet = new QueryDataSet(super.getConnection());
        queryDataSet.addTable("tbl_name",query);
    } catch (Exception e) {
        e.printStackTrace();            
    }
    return queryDataSet;
}

protected DatabaseOperation getSetUpOperation() throws Exception {
    return DatabaseOperation.DELETE;
}

Upvotes: 3

Related Questions