Datasources in OpenCReports are either database connections, or accessors (mini-drivers) for data files in certain formats.
Datasource descriptions are in the following format:
<Datasources> <Datasource name="mysource" type="..." ... /> </Datasources>
A report may have multiple datasources, i.e.
the description may list multiple
<Datasource>
lines.
Datasources must have unique names in a report and
their type may be:
mariadb
(or mysql
),
postgresql
, odbc
,
csv
, json
,
xml
or array
.
A MariaDB database connection may be declared in three ways. Either by using the database host and port, the database name, user name and password directly:
<Datasource name="mysource" type="mariadb" host="..." port="..." dbname="..." user="..." password="..." />
or alternatively, instead of the host and port, specifying the UNIX Domain Socket file for a local connection if it's not in the standard location:
<Datasource name="mysource" type="mariadb" unix_socket="..." dbname="..." user="..." password="..." />
or moving these details out to an external configuration file in an INI file format:
<Datasource name="mysource" type="mariadb" optionfile="myconn.cnf" group="myconn" />
In the last case, the configuration file myconn.cnf
would contain something like this:
[myconn] !include /etc/my.cnf database=mydb user=myuser #password= #host= #port= #unix_socket=
Please note that the INI group name [myconn]
matches group="myconn"
in the above
datasource declaration.
The database name and user name are mandatory. The user password is optional, depending on the database security authentication setup.
The database host and port, or the socket file location are all optional. Without these, a local connection is attempted using the default settings. If the host name is specified but the port isn't, the remote host is used on the default port (as known by the local MariaDB database client library).
A PostgreSQL database connection may be declared in three ways. Either by using the database host and port, the database name, user name and password directly:
<Datasource name="mysource" type="postgresql" host="..." port="..." dbname="..." user="..." password="..." />
or alternatively, instead of the host and port, specifying the UNIX Domain Socket file for a local connection if it's not in the standard location:
<Datasource name="mysource" type="postgresql" unix_socket="..." dbname="..." user="..." password="..." />
or using a so called connection string:
<Datasource name="mysource" type="postgresql" connstr="..." />
For the connection string format, see the PostgreSQL documentation.
The database name and user name are mandatory. The user password is optional, depending on the database security authentication setup.
The database host and port, or the socket file location are all optional. Without these, a local connection is attempted using the default settings. If the host name is specified but the port isn't, the remote host is used on the default port (as known by the local PostgreSQL database client library).
There are also two optional parameters that control the behaviour of the PostgreSQL driver in OpenCReports, rather than being actual connection parameters to a PostgreSQL server. These parameters may be used with any of the above connection methods.
The parameter
usecursor
may have a boolean value:
true
,
false
,
yes
,
no
, or
a numeric value interpreted
as a boolean value: non-zero
values mean true
,
zero means false
.
When usecursor
is enabled,
the SQL query will be wrapped in
a cursor, and the result is retrieved
in parts. Otherwise, the SQL query is
executed as is and the result is
retrieved in whole.
The default value is usually true
but this can be controlled when OpenCReports is built.
When usecursor
is enabled,
the parameter fetchsize
controls the number of rows retrieved
at once. Default value is 1024.
Examples (add the necessary connection parameters from the above):
<Datasource name="mysource" type="postgresql" ... usecursor="false" />
or
<Datasource name="mysource" type="postgresql" ... usecursor="true" fetchsize="4096" />
SQL queries added to the same PostgreSQL datasource (connection) will behave the same way. Either all of them are executed as is, or all of them will use a cursor.
The above described MariaDB and PostgreSQL database connection types are using their respective client libraries. There is a more generic way, i.e. ODBC. ODBC was invented by Microsoft in the 1990s for Windows. See Microsoft Open Database Connectivity (ODBC) In their solution, there's an abstract client library and individual database drivers adhere to the APIs offered by ODBC toplevel library. Since then, UNIX and UNIX-like systems also gained their ODBC client libraries in two different implementations, both of which are supported by OpenCReports: unixODBC and iODBC.
An ODBC database setup is done a differently. There are
two system-wide configuration files. The first one is
odbcinst.ini
that lists the database
drivers installed into the system. The second one is
odbc.ini
which references the first
one and lists pre-defined database connections. These
database connections are named. In ODBC speak, these
are called Data Source Names or DSNs. The DSNs specify
the low level connection parameters, like the database
host and port, and optionally the user name and
password, too.
Thus, an ODBC database connection may be declared in two ways. The first way is by using the DSN name, and optionally the user name and password:
<Datasource name="mysource" type="odbc" dbname="..." user="..." password="..." />
In this case, the dbname
attribute
is not the low level database name, but the ODBC
abstract DSN name.
There's also a way to use the so called connection string which contain the same connection information:
<Datasource name="mysource" type="odbc" connstr="..." />
For the connection string format, see the public examples.
For a generic description of the CSV file format, see CSV file type.
A CSV file datasource is declared very simply:
<Datasource name="mysource" type="'csv'" />
In this case, the actual CSV file is not declared,
only that a "query" using a CSV file will be listed
later under <Queries>
.
For a generic description of the expected JSON file format, see JSON file type.
Similarly to CSV, the JSON file datasource is also declared very simply:
<Datasource name="mysource" type="'json'" />
In this case, the actual JSON file is not declared,
only that a "query" using a JSON file will be listed
later under <Queries>
.
Similarly to CSV and JSON, the XML file datasource is also declared very simply:
<Datasource name="mysource" type="'xml'" />
In this case, the actual XML file is not declared,
only that a "query" using an XML file will be listed
later under <Queries>
.
Declaring the spreadsheet based file datasource is also very simple:
<Datasource name="mysource" type="'spreadsheet'" filename="'myfile.xlsx'" />
or
<Datasource name="mysource" type="'pandas'" filename="'myfile.xlsx'" />
Since the spreadsheet file may contain multiple
sheets, the datasource declaration must specify the
file name, and the query will need to specify the
sheet label. An example can be seen under
<Queries>
.
If the sheets that the report uses are in different files, multiple spreadsheet datasources must be declared, one for each file. If the sheets are in the same file, then the same datasource can be used for multiple queries, one query for every sheet.
Arrays are global in-memory structures in the
application that should be accessible to the
OpenCReports library. For example, when using
the C programming language, global non-static
symbols are visible to libraries if the application
is compiled with -rdynamic
.
Similarly to file based datasources, the array datasource is declared very simply:
<Datasource name="mysource" type="'array'" />
In this case, the actual array is not declared,
only that a "query" using an array will be listed
later under <Queries>
.
A C array is declared in this format:
const char *array[ROWS + 1][COLUMNS] = { { "column1", ... }, { "value1", ... }, ... };
The array is declared as a two-dimensional array of
C strings. The first row of the array is the column
names, [ROWS + 1]
in the array
declaration accounts for the title row.
All rows have the same number of columns. Column values may be NULL, in which case they will be treated the same as SQL NULLs in SQL query results.
Optionally, a column types array is declared separately:
#include <opencreport.h> const enum ocrpt_result_type coltypes[COLUMNS] = { ... };
If this array is present, it must have the same number
of COLUMNS
as the matching data
array. The enum ocrpt_result_type
usable in data array type declaration are
OCRPT_RESULT_STRING
,
OCRPT_RESULT_NUMBER
and
OCRPT_RESULT_DATETIME
.
OpenCReports expects strings in UTF-8 encoding. However, some datasources may use a different encoding. To use and display strings from the datasource, an internal conversion to UTF-8 is needed. To perform this correctly, the datasource encoding must be set.
<Datasource name="mysource" ... encoding="ISO-8859-2" />