Class QuerySet

java.lang.Object
org.apache.tools.ant.ProjectComponent
org.dbunit.ant.QuerySet
All Implemented Interfaces:
Cloneable

public class QuerySet extends org.apache.tools.ant.ProjectComponent
This element is a container for Queries. It facilitates reuse through references. Using Ant 1.6 and greater, references can be defined in a single build file and imported into many others. An example of where this is useful follows:

In our database we have INDIVIDUALS which must have an associated NAME_INFO and at least one IND_ADDRESS. The developer creating a dataset for his/her tests probably won't know all the details of what relationships are expected, and if he did, its an error prone and repetitive task to create the correct SQL for entities in each dataset. Missing a related table, not only creates invalid data for your tests, but also is likely to cause DBUnit setUp() failures from foreign key constraint violation errors. (example: If a previous test had inserted INDIVIDUALS and NAME_INFO and my test tries to delete only the INDIVIDUALS, the NAME_INFO.IND_ID constraint would be violated)

Each queryset is internally converted to a QueryDataSet and then combined using a CompositeDataSet. This means that you can use more than one query element for any given table provided they are nested within separate querysets.

Usage:

 <!-- ======== Define the reusable reference ========== -->

 <queryset id="individuals">
    <query name="INDIVIDUALS" sql="
      SELECT * FROM INDIVIDUALS WHERE IND_ID IN (@subQuery@)"/>

    <query name="NAME_INFO" sql="
      SELECT B.* FROM INDIVIDUALS A, NAME_INFO B
      WHERE A.IND_ID IN (@subQuery@)
      AND B.IND_ID = A.IND_ID"/>

    <query name="IND_ADDRESSES" sql="
      SELECT B.* FROM INDIVIDUALS A, IND_ADDRESSES B
      WHERE A.IND_ID IN (@subQuery@)
      AND B.IND_ID = A.IND_ID"/>
 </queryset>

 <!-- ========= Use the reference ====================== -->

 <dbunit driver="${jdbcDriver}"
     url="${jdbcURL}" userid="${jdbcUser}" password="${jdbcPassword}">
   <export dest="${dest}">
   <queryset refid="individuals">
      <filterset>
        <filter token="subQuery" value="
          SELECT IND_ID FROM INDIVIDUALS WHERE USER_NAME = 'UNKNOWN'"/>
      </filterset>
   </queryset>

   </export>
 </dbunit>

 
Since:
2.2.0 (Sep. 13 2004)
Version:
$Revision$ $Date$
Author:
Lenny Marks lenny@aps.org, Last changed by: $Author$
  • Field Summary

    Fields inherited from class org.apache.tools.ant.ProjectComponent

    description, location, project
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addFilterSet(org.apache.tools.ant.types.FilterSet filterSet)
    Adds a filterset whose tokens are substituted into this queryset's query SQL.
    void
    addQuery(Query query)
    Adds a query to this queryset.
    void
    Copies the queries from the given queryset into this one.
    Returns the id under which this queryset can be referenced.
    Returns this queryset's queries, with filterset tokens substituted.
    Builds a QueryDataSet from this queryset's queries, resolving a referenced queryset first if getRefid() is set.
    Returns the id of the queryset this queryset references.
    void
    setId(String string)
    Sets the id under which this queryset can be referenced.
    void
    setRefid(String string)
    Sets the id of the queryset this queryset references.

    Methods inherited from class org.apache.tools.ant.ProjectComponent

    clone, getDescription, getLocation, getProject, log, log, setDescription, setLocation, setProject

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • QuerySet

      public QuerySet()
      Default constructor.
  • Method Details

    • addQuery

      public void addQuery(Query query)
      Adds a query to this queryset.
      Parameters:
      query - the query to add.
    • addFilterSet

      public void addFilterSet(org.apache.tools.ant.types.FilterSet filterSet)
      Adds a filterset whose tokens are substituted into this queryset's query SQL.
      Parameters:
      filterSet - the filterset to add.
    • getId

      public String getId()
      Returns the id under which this queryset can be referenced.
      Returns:
      the id under which this queryset can be referenced.
    • getRefid

      public String getRefid()
      Returns the id of the queryset this queryset references.
      Returns:
      the id of the queryset this queryset references.
    • setId

      public void setId(String string) throws org.apache.tools.ant.BuildException
      Sets the id under which this queryset can be referenced.
      Parameters:
      string - the id under which this queryset can be referenced.
      Throws:
      org.apache.tools.ant.BuildException - if refid is already set.
    • setRefid

      public void setRefid(String string) throws org.apache.tools.ant.BuildException
      Sets the id of the queryset this queryset references.
      Parameters:
      string - the id of the queryset this queryset references.
      Throws:
      org.apache.tools.ant.BuildException - if id is already set.
    • getQueries

      public List getQueries()
      Returns this queryset's queries, with filterset tokens substituted.
      Returns:
      this queryset's queries, with filterset tokens substituted.
    • copyQueriesFrom

      public void copyQueriesFrom(QuerySet referenced)
      Copies the queries from the given queryset into this one.
      Parameters:
      referenced - the queryset to copy queries from.
    • getQueryDataSet

      public QueryDataSet getQueryDataSet(IDatabaseConnection connection) throws SQLException, AmbiguousTableNameException
      Builds a QueryDataSet from this queryset's queries, resolving a referenced queryset first if getRefid() is set.
      Parameters:
      connection - the database connection needed to load data.
      Returns:
      the resulting dataset.
      Throws:
      SQLException - if a database access error occurs.
      AmbiguousTableNameException - if a table name is added more than once.