Documentation Examples
From RLIB
Some RLIB examples are below.
Contents |
C example
This routine below is part of a larger GTK project, printing out a report. This routine demonstrates the following:
- Parametrized query constructed runtime to a static string (start_date, end_date parameters).
- It's needed because RLIB can only accept a static query.
- Choice on different output types depending on radiobuttons. The same query, report XML, etc. can be used to produce different output formats.
- Initialize RLIB
- Add a report XML definition
- Add an ODBC datasource
- Set report locale (for localized number formatting, etc)
- Set the database encoding (strings are coming as UTF-8 from the database)
- Set the output encoding: Latin2 for file output, system encoding (UTF-8 or WIN1250) for GUI display
- Add the query constructed previously
- Add report parameters for displaying on the report
- Set the output format
- Execute the report
- Get the finished report content and output somewhere (file or GUI)
void print_jk_fj_sum(void) {
GtkWidget *dialog;
gchar *filename, *query1,
*start_date, *end_date;
char *outbuf, *output_type, *output_encoding;
int fn, type;
size_t output_len, written_len, tmp_len;
rlib *r;
start_date = (gchar *)gtk_entry_get_text(
GTK_ENTRY(lookup_widget(window1, "entry1")));
end_date = (gchar *)gtk_entry_get_text(
GTK_ENTRY(lookup_widget(window1, "entry2")));
if (!strlen(start_date) || !strlen(end_date)) {
msgbox("Date interval was not specified!", window1);
return;
}
if (GTK_TOGGLE_BUTTON(lookup_widget(window1, "radiobutton3"))->active) {
filename = "jk_fj_sum.pdf";
type = RLIB_FORMAT_PDF;
output_encoding = "ISO-8859-2";
} else if (GTK_TOGGLE_BUTTON(lookup_widget(window1, "radiobutton4"))->active) {
filename = "jk_fj_sum_csv.txt";
type = RLIB_FORMAT_CSV;
output_encoding = "ISO-8859-2";
} else if (GTK_TOGGLE_BUTTON(lookup_widget(window1, "radiobutton5"))->active) {
filename = "jk_fj_sum.txt";
type = RLIB_FORMAT_TXT;
output_encoding = "ISO-8859-2";
} else /* if (GTK_TOGGLE_BUTTON(lookup_widget(window1, "radiobutton6"))->active) */ {
filename = NULL;
type = RLIB_FORMAT_TXT;
output_encoding = encoding; /* Global variable keeping the system encoding
UTF-8 on Linux, WIN1250 on Hungarian Windows XP */
}
if (filename) {
dialog = create_filechooserdialog1();
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(window1));
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
gtk_widget_destroy (dialog);
} else {
gtk_widget_destroy (dialog);
return;
}
}
query1 = g_strdup_printf(
"select jelleg1,sum(jkitem.osszeg) as sum1 "
"from jk,jkitem where jk.id=jkitem.jk and "
"jk.tipus=1 and jk.datum between '%s' and '%s' "
"group by jk.jelleg1;", start_date, end_date);
r = rlib_init();
rlib_add_report(r, "jk_fj_sum.xml");
rlib_add_datasource_odbc(
r, "mainsource", "DSNname", "username", "password");
/*
* Variable "locale" below is a global variable, keeping your system locale,
* so decimal separators, etc. work in your language.
*/
rlib_set_locale(r, locale);
rlib_set_datasource_encoding(r, "mainsource", "UTF-8");
rlib_set_output_encoding(r, output_encoding);
rlib_add_query_as(r, "mainsource", query1, "fjelleg");
rlib_add_parameter(r, "start_date", start_date);
rlib_add_parameter(r, "end_date", end_date);
rlib_set_output_format(r, type);
rlib_execute(r);
output_type = rlib_get_content_type_as_text(r);
outbuf = rlib_get_output(r);
output_len = rlib_get_output_length(r);
if (outbuf == NULL) {
rlib_free(r);
g_free(query1);
msgbox("Report is not executable!", window1);
return;
}
/* If we are outputting to a file */
if (filename) {
fn = open(filename, O_RDWR | O_CREAT | O_TRUNC
#if NEED_WINDOWS_H
| O_BINARY
#else
, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
#endif
);
if (fn >= 0) {
written_len = 0;
while (written_len < output_len) {
tmp_len = write(fn, outbuf + written_len, output_len - written_len);
if (tmp_len < 0) {
printf("write() error: %s\n", strerror(errno));
break;
}
written_len += tmp_len;
}
close(fn);
} else
printf("open() error: %s\n", strerror(errno));
g_free(filename);
} else {
/* We are outputting to the GUI (GTKTextView) */
GtkTextBuffer *textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(lookup_widget(window1, "textview1")));
gsize bytes_read = 0, bytes_written = 0;
gchar *tmp;
tmp = g_locale_to_utf8(outbuf, output_len, &bytes_read, &bytes_written, NULL);
gtk_text_buffer_set_text(textbuffer, tmp, -1);
g_free(tmp);
}
rlib_free(r);
g_free(query1);
}
The above code used this report definition:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE report >
<Report fontSize="9">
<PageHeader>
<Output>
<Image value="'firmlogo.jpg'" type="'jpeg'" width="200" height="37" />
<Line/>
<Line fontSize="16">
<literal align="center"> Income on drilling activities</literal>
</Line>
<Line fontSize="16">
<literal align="center"> From </literal>
<field value="m.start_date" width="10" align="left"/>
<literal align="center"> to </literal>
<field value="m.end_date" width="10" align="left"/>
</Line>
<Line/>
</Output>
</PageHeader>
<Variables>
<Variable name="ossz_sum1" value="val(fjelleg.sum1)" type="sum" resetonbreak=""/>
</Variables>
<Detail>
<FieldHeaders>
<Output>
<HorizontalLine length="105" size="1" bgcolor="'black'"/>
<HorizontalLine length="105" size="2" bgcolor="'0xe5e5e5'"/>
<Line>
<literal width="105" bgcolor="'0xe5e5e5'">Totals according to drilling activities</literal>
</Line>
<HorizontalLine length="105" size="2" bgcolor="'0xe5e5e5'"/>
<HorizontalLine length="105" size="1" bgcolor="'black'"/>
<HorizontalLine length="105" size="1" bgcolor="'white'"/>
<Line>
<literal width="84" col="1">Drilling activity</literal>
<literal width="1"/>
<literal width="20" col="1">Subtotal</literal>
</Line>
<HorizontalLine length="105" size="2" bgcolor="'white'"/>
<HorizontalLine length="105" size="1" bgcolor="'black'"/>
<HorizontalLine length="105" size="2" bgcolor="'white'"/>
</Output>
</FieldHeaders>
<FieldDetails>
<Output>
<Line>
<field value="fjelleg.jelleg1" width="84" col="1"/>
<literal width="1"/>
<field value="fjelleg.sum1" width="20" align="right" col="2"/>
</Line>
</Output>
</FieldDetails>
</Detail>
<ReportFooter>
<Output>
<HorizontalLine length="105" size="2" bgcolor="'white'"/>
<HorizontalLine length="105" size="1" bgcolor="'black'"/>
<HorizontalLine length="105" size="2" bgcolor="'0xe5e5e5'"/>
<Line>
<literal bgcolor="'0xe5e5e5'">Total:</literal>
<literal width="75" bgcolor="'0xe5e5e5'"/>
<literal width="1" bgcolor="'0xe5e5e5'"/>
<field value="str(v.ossz_sum1,17,2)" width="20" bgcolor="'0xe5e5e5'" align="right" col="2"/>
</Line>
<HorizontalLine length="105" size="2" bgcolor="'0xe5e5e5'"/>
<HorizontalLine length="105" size="1" bgcolor="'black'"/>
</Output>
</ReportFooter>
</Report>
PHP examples
Plain report
Here's a stock RLIB PHP example below. The code demonstrates the essentials of producing a report.
- Initialize RLIB
- Add a datasource (MySQL in this case)
- Add a query
- Add a report XML definition
- Set the output format (PDF)
- Execute the report
- Output the HTML content header
- Output the report content
- Free the report structure
<? dl ("rlib.so");
$hostname = "localhost";
$username = "rlib";
$password = "rlib";
$database = "rlib";
$rlib = rlib_init();
rlib_add_datasource_mysql($rlib, "local_mysql", $hostname, $username, $password, $database);
rlib_add_query_as($rlib, "local_mysql", "select * FROM products", "products");
rlib_add_report($rlib, "products.xml");
rlib_set_output_format_from_text($rlib, "pdf");
rlib_execute($rlib);
header(rlib_get_content_type($rlib));
rlib_spool($rlib);
rlib_free($rlib);
?>
The accompanying XML definition:
<?xml version="1.0"?>
<!DOCTYPE report >
<Report fontSize="9" orientation="landscape">
<ReportHeader>
<Output>
<Line/>
<Line fontSize="12">
<literal>Products Report</literal>
</Line>
<Line fontsize="4"/>
<HorizontalLine size="4" bgcolor="'white'"/>
<HorizontalLine size="2" bgcolor="'black'"/>
<HorizontalLine size="4" bgcolor="'white'"/>
</Output>
</ReportHeader>
<PageHeader>
<Output>
<Line fontSize="11">
<literal>Products Report (Page Header)</literal>
</Line>
<HorizontalLine size="4" bgcolor="'white'"/>
</Output>
</PageHeader>
<Detail>
<FieldHeaders>
<Output>
<HorizontalLine size="1" bgcolor="'black'"/>
<Line bgcolor="'0xe5e5e5'">
<literal width="15" col="1">Number</literal>
<literal width="1"/>
<literal width="20" col="2">Name</literal>
<literal width="1"/>
<literal width="10" col="3">Type</literal>
<literal width="1"/>
<literal width="10" col="4">Category</literal>
</Line>
<HorizontalLine size="1" bgcolor="'black'"/>
<HorizontalLine size="4" bgcolor="'white'"/>
</Output>
</FieldHeaders>
<FieldDetails>
<Output>
<Line bgcolor="iif(r.detailcnt%2,'0xe5e5e5','white')">
<field value="plunum" width="15" align="left" col="1"/>
<literal width="1"/>
<field value="name" width="20" align="left" col="2"/>
<literal width="1"/>
<field value="type" width="10" align="left" col="3"/>
<literal width="1"/>
<field value="category" width="10" align="left" col="4"/>
</Line>
</Output>
</FieldDetails>
</Detail>
<PageFooter>
<Output>
<Line>
<literal>Page: </literal>
<field value="r.pageno" width="3" align="right"/>
</Line>
</Output>
</PageFooter>
<ReportFooter>
</ReportFooter>
</Report>
Array report
Here is an array RLIB PHP example. The code demonstrates the essentials of producing an array report. The output array MUST be built from left to right and then from top to bottom. There may not be any holes in the array; each cell MUST have a value.
- Initialize RLIB
- Build the datasource ($data in this case)
- Add a query
- Add a report XML definition
- Set the output format (PDF)
- Execute the report
- Output the HTML content header
- Output the report content
- Free the report structure
<? dl ("rlib.so");
$data[0][0] = "first_name";
$data[0][1] = "last_name";
$data[0][2] = "color";
$data[0][3] = "group";
$data[0][4] = "breakfast";
$data[1][0] = "";
$data[1][1] = "Doan";
$data[1][2] = "blue";
$data[1][3] = "1";
$data[1][4] = "Green Eggs And Spam I Am I Am";
$rlib = rlib_init();
rlib_version();
rlib_add_datasource_array($rlib, "local_array");
rlib_add_query_as($rlib, "local_array", "data", "data");
rlib_add_report($rlib, "array.xml");
rlib_add_function($rlib, "bobdoan", "bobdoan", 1);
rlib_add_function($rlib, "mikeroth", "mike_roth", 2);
rlib_set_output_format_from_text($rlib, "csv");
rlib_set_output_parameter($rlib, "debugging", "yes");
rlib_set_output_parameter($rlib, "only_quote_strings", "yes");
rlib_set_locale($rlib, "en_US");
rlib_execute($rlib);
header(rlib_get_content_type($rlib));
rlib_spool($rlib);
rlib_free($rlib);
function mike_roth($a, $b) {
return "pancakes are yummier then $a and $b";
}
function bobdoan($a) {
return strtoupper($a);
}
?>
The array report xml:
<?xml version="1.0"?>
<!DOCTYPE report >
<Report fontSize="20" orientation="landscape">
<Alternate>
<NoData>
<Output>
<Line fontSize="12">
<literal>NO DATA</literal>
</Line>
</Output>
</NoData>
</Alternate>
<Detail>
<FieldDetails>
<Output>
<Line bgcolor="iif(r.detailcnt%2,'0xe5e5e5','white')">
<field value="'dude'" width="6" align="right" col="1" bgcolor="'red'"/>
<field value="'sweet'" width="6" align="right" col="2" bgcolor="'blue'"/>
<field value="'xxxx'" width="6" align="right" col="3" bgcolor="'yellow'"/>
<field value="'zzzzzz'" width="6" align="right" col="4" />
<field value="12345" width="6" align="right" col="5" />
</Line>
</Output>
</FieldDetails>
</Detail>
</Report>
Gantt chart
For the code, see Data Sources This is the XML definition used by that code:
<?xml version="1.0"?>
<!DOCTYPE report >
<Part layout="'flow'" fontSize="14" orientation="landscape">
<PageHeader>
<Output>
<Line fontSize="26" bgcolor="'yellow'">
<literal link="'http://rlib.sicompos.com'">RLIB IS Gantting </literal>
</Line>
<HorizontalLine size="4" bgcolor="'black'"/>
<HorizontalLine size="10" bgcolor="'green'"/>
</Output>
</PageHeader>
<pr>
<pd width="100">
<Report fontSize="12" orientation="landscape" query="'data'" leftMargin="0">
<ReportHeader>
<Output>
<HorizontalLine size="10" bgcolor="'red'"/>
<Line bgcolor="'0xe5e5e5'">
<literal col="1">THIS TEXT SHOULD NOT FLOW INTO THE GANTT CHART</literal>
</Line>
<Line bgcolor="'0xe5e5e5'">
<literal col="1">THIS TEXT SHOULD NOT FLOW INTO THE GANTT CHART</literal>
</Line>
<Line bgcolor="'0xe5e5e5'">
<literal col="1">THIS TEXT SHOULD NOT FLOW INTO THE GANTT CHART</literal>
</Line>
<Line bgcolor="'0xe5e5e5'">
<literal col="1">THIS TEXT SHOULD NOT FLOW INTO THE GANTT CHART</literal>
</Line>
<Line bgcolor="'0xe5e5e5'">
<literal col="1">THIS TEXT SHOULD NOT FLOW INTO THE GANTT CHART</literal>
</Line>
<Line bgcolor="'0xe5e5e5'">
<literal col="1">THIS TEXT SHOULD NOT FLOW INTO THE GANTT CHART</literal>
</Line>
<HorizontalLine size="10" bgcolor="'red'"/>
</Output>
</ReportHeader>
<Chart cols="24" rows="4" cell_width="20" cell_height="30" header_row="'yes'"
title="'Gantt Chart Example'" cell_width_padding="0" cell_height_padding="6">
<HeaderRow query="'header_data'" field="header_data.name" colspan="2"/>
<Row row="val(row_field)" bar_start="val(bar_start)" bar_end="val(bar_stop)"
label="label_field" bar_label="bar_label" bar_color="bar_color"
bar_label_color="bar_label_color"/>
</Chart>
</Report>
</pd>
</pr>
<PageFooter>
<Output>
<Line>
<literal>Page: </literal>
<field value="r.pageno" width="3" align="right"/>
<literal>/</literal>
<field value="r.totpages" width="3" align="right" delayed="yes"/>
</Line>
</Output>
</PageFooter>
</Part>
Perl example
The code below demonstrates a simple report written in Perl. As the PHP code above, it uses rlib_spool() to write the report to the standard output.
#!/usr/bin/perl -w
use rlib;
$r = rlib::rlib_init();
rlib::rlib_add_datasource_xml($r, "local_xml");
rlib::rlib_add_query_as($r, "local_xml", "data.xml", "data");
rlib::rlib_add_report($r, "array.xml");
rlib::rlib_set_output_format_from_text($r, "pdf");
rlib::rlib_execute($r);
rlib::rlib_spool($r);
rlib::rlib_free($r);
exit 0;
The datasource XML ("data.xml") used by rlib_add_datasource_xml() is below:
<?xml version="1.0"?>
<data>
<rows>
<row>
<col>Bob</col>
<col>Doan</col>
<col>blue</col>
<col>1</col>
<col>Green Eggs And Spam I Am I Am</col>
</row>
<row>
<col>Eric</col>
<col>Buruschkin</col>
<col>green</col>
<col>1</col>
<col>Green Eggs And Spam I Am I Am</col>
</row>
<row>
<col>Mike</col>
<col>Roth</col>
<col>yellow</col>
<col>2</col>
<col>Green Eggs And Spam I Am I Am</col>
</row>
<row>
<col>Bob</col>
<col>Kratz</col>
<col>pink</col>
<col>2</col>
<col>Green Eggs And Spam I Am I Am</col>
</row>
<row>
<col>Steve</col>
<col>Tilden</col>
<col>purple</col>
<col>2</col>
<col>Dude</col>
</row>
</rows>
<fields>
<field>first_name</field>
<field>last_name</field>
<field>color</field>
<field>group</field>
<field>breakfast</field>
</fields>
</data>
The report XML definition ("array.xml") is below:
<?xml version="1.0"?>
<!DOCTYPE report >
<Report fontSize="20" orientation="landscape">
<Alternate>
<NoData>
<Output>
<Line fontSize="12">
<literal>NO DATA</literal>
</Line>
</Output>
</NoData>
</Alternate>
<Detail>
<FieldDetails>
<Output>
<Line bgcolor="iif(r.detailcnt%2,'0xe5e5e5','white')">
<field value="'dude'" width="6" align="right" col="1" bgcolor="'red'"/>
<field value="'sweet'" width="6" align="right" col="2" bgcolor="'blue'"/>
<field value="'xxxx'" width="6" align="right" col="3" bgcolor="'yellow'"/>
<field value="'zzzzzz'" width="6" align="right" col="4" />
<field value="12345" width="6" align="right" col="5" />
</Line>
</Output>
</FieldDetails>
</Detail>
</Report>
Python example
This is essentially the same code as the Perl code above. It uses the same XML datasource and report XML definition above in the Perl example
The differences are:
- rlib_init() is embedded in rlib.Rlib()
- Instead of writing to the standard output, it writes to a file called "xml.pdf".
#!/usr/bin/python
# -*- coding: ISO-8859-15 -*-
import rlib
myreport = rlib.Rlib()
print rlib.version
myreport.add_datasource_xml("local_xml")
myreport.add_query_as("local_xml", "data.xml", "data")
myreport.add_report("array.xml")
myreport.set_output_format_from_text("pdf")
myreport.execute()
print myreport.get_content_type_as_text()
open('xml.pdf','wb').write(myreport.get_output())
C# example
Essentially the same code as the Perl example above.
public class Hello1
{
public static void Main()
{
SWIGTYPE_p_rlib r = rlib.rlib_init();
rlib.rlib_add_datasource_xml(r, "local_xml");
rlib.rlib_add_query_as(r, "local_xml", "data.xml", "data");
rlib.rlib_add_report(r, "array.xml");
rlib.rlib_set_output_format_from_text(r, "pdf");
rlib.rlib_execute(r);
rlib.rlib_spool(r);
rlib.rlib_free(r);
}
}
Java example
Essentially the same code as the first PHP example
class Example {
static {
System.loadLibrary("rlibjava");
}
public static void main(String[] args) {
SWIGTYPE_p_rlib rlib1;
String hostname = "localhost";
String username = "rlib";
String password = "rlib";
String database = "rlib";
rlib1 = rlib.rlib_init();
rlib.rlib_add_datasource_mysql(rlib1, "local_mysql", hostname, username, password, database);
rlib.rlib_add_query_as(rlib1, "local_mysql", "select * FROM products", "products");
rlib.rlib_add_report(rlib1, "products.xml");
rlib.rlib_set_output_format_from_text(rlib1, "pdf");
rlib.rlib_execute(rlib1);
rlib.rlib_spool(rlib1);
rlib.rlib_free(rlib1);
}
}
