Building queries for defects and users

The following code fragments show how to build queries that fetch records from the database by using criteria about defects and users. The samples use the QueryDef and QueryFilterNode objects, as well as a Structured Query Language (SQL) query.

Note: You can use any of the following code fragments in a hook such as a field choice list hook or a field validation hook. However, you can also include this code in an external application if you manually create the session object and log on to the database (instead of getting the session object).

Perl


# ((planned_release = "beta") AND (state != resolved OR verified) AND 

(priority = 1 OR 2))
# OR
# (assigned_to = lihong OR gonzales OR nougareau OR akamoto)

$session = $entity->GetSession();

@users = ("lihong", "gonzales", "nougareau", "akamoto");
@priority_levels = ("1", "2");
@states = ("resolved", "verified");
@planned_release = ("beta");

$querydef = $session->BuildQuery("defect"); 
$querydef->BuildField("id"); 
$querydef->BuildField("component"); 
$querydef->BuildField("priority"); 
$querydef->BuildField("headline"); 

$operator = $querydef->BuildFilterOperator($CQPerlExt::CQ_BOOL_OP_OR);
$operator2 = $operator->BuildFilterOperator($CQPerlExt::CQ_BOOL_OP_AND);
$operator2->BuildFilter ("planned_release", $CQPerlExt::CQ_COMP_OP_EQ, 
\@planned_release);
$operator2->BuildFilter ("state", $CQPerlExt::CQ_COMP_OP_NOT_IN, \@states); 
$operator2->BuildFilter ("priority", $CQPerlExt::CQ_COMP_OP_IN, 
\@priority_levels); 

$operator->BuildFilter ("assigned_to",$CQPerlExt::CQ_COMP_OP_IN, \@users);

$resultset = $session->BuildResultSet(querydef);