Documentation DATASOURCE

From RLIB

Jump to: navigation, search

RLIB can use several datasources to feed the reports.

Contents

MYSQL

RLIB can use MySQL's native client driver as a datasource:

    int rlib_add_datasource_mysql(rlib *r, char *input_name,
                                  char *database_host,
                                  char *database_user,
                                  char *database_password,
                                  char *database_database);

The above call adds a native MySQL datasource to a report. It will make RLIB to connect to a certain MySQL database on the given host with the username and password.

POSTGRES

RLIB can use PostgreSQL's native client driver as a datasource:

    int rlib_add_datasource_postgre(rlib *r, char *input_name, char *conn);

The above call adds a native PostgreSQL datasource to a report. The conn string contains all the connection parameters for PostgreSQL, like host, database, username, password, and possibly other options, like port (if the database runs on a non-default port). The documentation for these connection parameters can be found here: http://www.postgresql.org/docs/8.3/static/libpq-connect.html

ODBC

RLIB can use any database that an ODBC driver has written for. ODBC stands for Open DataBase Connectivity, which is an industry-standard API for database programming.

    int rlib_add_datasource_odbc(rlib *r, char *input_name, char *source, char *user, char *password);

The above call adds an ODBC datasource to a report. source is the name of the datasource defined in the system. Also, a username and a password can be given.

XML

RLIB can use an XML file as a datasource.

    int rlib_add_datasource_xml(rlib *r, char *input_name);

The above call adds an XML datasource to a report. The XML file has to be formatted in a certain way:

    <?xml version="1.0"?>
    <data>
        <fields>
            <field>field1</field>
            <field>field2</field>
            <field>field3</field>
            ...
        </fields>
        <rows>
            <row>
                <col>value1 for row1</col>
                <col>value2 for row1</col>
                <col>value3 for row1</col>
                ...
            </row>
            <row>
                <col>value1 for row2</col>
                <col>value2 for row2</col>
                <col>value3 for row2</col>
                ...
            </row>
            ...
        </rows>
    </data>

CSV

RLIB can use a CSV file as a datasource. The CSV file is a simple text file, containing a header line listing the field names, and the data lines after them listing the field data. The values in the files are separated by commas.

    int rlib_add_datasource_csv(rlib *r, char *input_name);

The above call adds a CSV datasource to a report.

ARRAY DATA SOURCE (PHP only)

PHP binding in RLIB was extended in a way so it can also use an array as a datasource.

    rlib_add_datasource_array($rlib, "local_array");

The above call adds an array datasource to a report. Example:

    <? dl ("rlib.so");
       $header_data[0][0] = "name";
       
       for ($i = 1; $i <= 24; $i++)
               $header_data[$i][0] = $i;
       
       $data[0][0] = "row_field";
       $data[0][1] = "bar_start";
       $data[0][2] = "bar_stop";
       $data[0][3] = "label_field";
       $data[0][4] = "bar_label";
       $data[0][5] = "bar_color";
       $data[0][6] = "bar_label_color";
       
       $data[1][0] = "1";
       $data[1][1] = "3";
       $data[1][2] = "12";
       $data[1][3] = "Task A";
       $data[1][4] = "A";
       $data[1][5] = "blue";
       $data[1][6] = "white";
       
       $data[2][0] = "2";
       $data[2][1] = "6";
       $data[2][2] = "12";
       $data[2][3] = "Task B";
       $data[2][4] = "B";
       $data[2][5] = "blue";
       $data[2][6] = "white";
       
       $data[3][0] = "3";
       $data[3][1] = "9";
       $data[3][2] = "12";
       $data[3][3] = "Task C";
       $data[3][4] = "C";
       $data[3][5] = "blue";
       $data[3][6] = "white";
       
       $data[4][0] = "4";
       $data[4][1] = "11";
       $data[4][2] = "12";
       $data[4][3] = "Task D";
       $data[4][4] = "D";
       $data[4][5] = "blue";
       $data[4][6] = "white";
       
       $data[5][0] = "1";
       $data[5][1] = "13";
       $data[5][2] = "15";
       $data[5][3] = "Task A";
       $data[5][4] = "A'";
       $data[5][5] = "red";
       $data[5][6] = "white";
       
       $data[6][0] = "2";
       $data[6][1] = "13";
       $data[6][2] = "18";
       $data[6][3] = "Task B";
       $data[6][4] = "B'";
       $data[6][5] = "red";
       $data[6][6] = "white";
       
       $data[7][0] = "3";
       $data[7][1] = "13";
       $data[7][2] = "22";
       $data[7][3] = "Task C";
       $data[7][4] = "C'";
       $data[7][5] = "red";
       $data[7][6] = "white";
       
       $data[8][0] = "4";
       $data[8][1] = "13";
       $data[8][2] = "23";
       $data[8][3] = "Task D";
       $data[8][4] = "D'";
       $data[8][5] = "red";
       $data[8][6] = "white";
       
       $rlib = rlib_init();
       rlib_add_datasource_array($rlib, "local_array");
       rlib_add_query_as($rlib, "local_array", "header_data", "header_data");
       rlib_add_query_as($rlib, "local_array", "data", "data");
       rlib_set_output_parameter($rlib, "html_image_directory", "/tmp");
       rlib_set_output_parameter($rlib, "trim_links", "1");
       rlib_add_report($rlib, "gantt.xml");
       rlib_set_output_format_from_text($rlib, "html");
       rlib_execute($rlib);
       rlib_spool($rlib);
       rlib_free($rlib);
    ?>

Adding data using a datasource

The calls below adds a dataset to a report, using a datasource:

    int rlib_add_query_as(rlib *r, char *input_source, char *sql, char *name);
    int rlib_add_query_pointer_as(rlib *r, char *input_source, char *sql, char *name);

input_source is the name given in the rlib_add_datasource_*(..., input_name, ...) calls. The SQL datasources (MySQL, PostgreSQL, ODBC) expect the sql string to be a correct SQL statement. The file datasources (XML, CSV) expect the sql string to be a filename pointing to a correctly formatted data file. The difference between the above two calls are explained in the API documentation.

Personal tools