Running a query and reporting on its result set

The DevOps Plan application provides powerful reporting capability in a graphical user interface (GUI) environment. The DevOps Plan Schema API also supports programmatic reporting.

Sometimes all you need is the raw results rather than a highly formatted report. The following subroutine is an external application:

  • Uses an existing query object to run the query.
  • Prints out the name of the EntityDef (record type) that the query runs against.
  • Iterates through all the records in the result set to print the label and value of each field in each record. This subroutines makes use of two other routines: StdOut, which prints its arguments to a file, and ToStr(not included here), which converts its argument to a string.

Following the Perl code examples are additional Perl code examples that illustrate:

Perl

sub RunBasicQuery {
my($session)=@_[0];
my($querydef)=@_[1];
#  The parameters to this subroutine are a Session object 
#  and a QueryDef object. It is assumed that the QueryDef 
#  is valid (for example, BuildField has been used to select 
#  one or more fields to retrieve). 

my ($rsltset);      #  This is a ResultSet object 
my ($status);
my ($column);
my ($num_columns);
my ($num_records);

$rsltset = $session->BuildResultSet(querydef);
$rsltset->Execute;

print "primary entity def for query == ", 
$rsltset->LookupPrimaryEntityDefName;

   $num_columns = $rsltset->GetNumberOfColumns;
   $num_records = 0;
   $status = $rsltset->MoveNext;
   while ($status == $CQPerlExt::CQ_SUCCESS) {
      $num_records = $num_records + 1;
      print "Record #", $num_records;
      #  Note: result set indices are based 1..N, not the usual
      # 0..N-1 
      $column = 1;
      while ($column <= $num_columns) {
         print " ", $rsltset->GetColumnLabel($column), "=", 
         $rsltset->GetColumnValue($column);
         $column = $column + 1;
      }
      print "";
      $status = $rsltset->MoveNext;
   }
}