13.5. Follower queries #

13.5.1. Data
13.5.2. C program code
13.5.3. PHP program code
13.5.4. RLIB compatible PHP program code
13.5.5. Report description
13.5.6. Report PDF result

This example below exercises a basic follower query along with the main query. For more information, see Follower queries.

13.5.1. Data #

Data is created as follows in the same database using the same user as the first example.

create table flintstones4 (id serial, firstname text);
create table flintstones5 (id serial, lastname text);

insert into flintstones4 (firstname)
values
('Fred'),
('Wilma'),
('Pebbles'),
('Barney'),
('Betty'),
('Bamm-Bamm'),
('The Great');

insert into flintstones5 (lastname)
values
('Flintstone'),
('Flintstone'),
('Flintstone'),
('Rubble'),
('Rubble'),
('Rubble'),
('Gazoo');

The data looks like this when queried:

ocrpttest=> select * from flintstones4;
 id | firstname 
----+-----------
  1 | Fred
  2 | Wilma
  3 | Pebbles
  4 | Barney
  5 | Betty
  6 | Bamm-Bamm
  7 | The Great
(7 rows)

ocrpttest=> select * from flintstones5;
 id |  lastname  
----+------------
  1 | Flintstone
  2 | Flintstone
  3 | Flintstone
  4 | Rubble
  5 | Rubble
  6 | Rubble
  7 | Gazoo
(7 rows)

13.5.2. C program code #

The program code adds the two queries and establishes the follower link between them.

#include <stdio.h>
#include <opencreport.h>

int main(int argc, char **argv) {
    opencreport *o = ocrpt_init();

    struct ocrpt_input_connect_parameter conn_params[] = {
        { .param_name = "dbname", .param_value = "ocrpttest" },
        { .param_name = "user", .param_value = "ocrpt" },
        { NULL }
    };

    ocrpt_datasource *ds = ocrpt_datasource_add(o, "pgsql", "postgresql", conn_params);
    ocrpt_query *q1 = ocrpt_query_add_sql(ds, "q1", "select * from flintstones4;");
    ocrpt_query *q2 = ocrpt_query_add_sql(ds, "q2", "select * from flintstones5;");

    ocrpt_query_add_follower(q1, q2);

    if (!ocrpt_parse_xml(o, "example5.xml")) {
        printf("XML parse error\n");
        ocrpt_free(o);
        return 0;
    }

    ocrpt_set_output_format(o, OCRPT_OUTPUT_PDF);
    ocrpt_execute(o);
    ocrpt_spool(o);
    ocrpt_free(o);

    return 0;
}

13.5.3. PHP program code #

Here's the equivalent program code in PHP.

<?php
$o = new OpenCReport();

$conn_params = [
    "dbname" => "ocrpttest",
    "user" => "ocrpt"
];

$ds = $o->datasource_add("pgsql", "postgresql", $conn_params);
$q1 = $ds->query_add("q1", "select * from flintstones4;");
$q2 = $ds->query_add("q2", "select * from flintstones5;");

$q1->add_follower($q2);

if (!$o->parse_xml("example5.xml")) {
    echo "XML parse error" . PHP_EOL;
    exit(0);
}

$o->execute();
$o->spool();

13.5.4. RLIB compatible PHP program code #

Here's the equivalent program code in PHP, using the RLIB compatibility functions.

<?php
$r = rlib_init();

rlib_add_datasource_postgres($r, "pgsql", "dbname=ocrpttest user=ocrpt");

rlib_add_query_as($r, "pgsql", "select * from flintstones4;", "q1");
rlib_add_query_as($r, "pgsql", "select * from flintstones5;", "q2");

rlib_add_resultset_follower($r, "q1", "q2");

if (!rlib_add_report($r, "example5.xml")) {
    echo "XML parse error" . PHP_EOL;
    exit(0);
}

rlib_execute($r);
rlib_spool($r);

13.5.5. Report description #

The program code uses this file contents from example5.xml.

Note that when using multiple queries in the same report, column names may be identical. Because of this, using queryname.columnname will indicate which one is needed. When using columnname then it will mean the first query's column.

<?xml version="1.0"?>
<!DOCTYPE OpenCReport SYSTEM "opencreport.dtd">
<OpenCReport>
    <Report query="q1">
        <Detail>
            <FieldHeaders>
                <Output>
                    <HorizontalLine size="2" color="'black'" />
                    <HorizontalLine size="2" color="'white'" />
                    <Line bold="yes">
                        <literal width="20">First name</literal>
                        <literal width="20">Last name</literal>
                    </Line>
                    <HorizontalLine size="2" color="'white'" />
                    <HorizontalLine size="2" color="'black'" />
                    <HorizontalLine size="2" color="'white'" />
                </Output>
            </FieldHeaders>

            <FieldDetails>
                <Output>
                    <Line>
                        <field width="20" value="q1.firstname" />
                        <field width="20" value="q2.lastname" />
                    </Line>
                </Output>
            </FieldDetails>
        </Detail>
    </Report>
</OpenCReport>

13.5.6. Report PDF result #

Note that compared to RLIB, OpenCReports may or may not produce the same output. This is due to the incomplete and faulty implementation of follower queries in RLIB.