/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
/* standard C headers */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* APR headers */
#include "apr.h"
#include "apr_pools.h"
#include "apr_dbd.h"

#define F2_MAX_LEN 50
#define MAX_REC 100

/* table structure */
struct t1
{
    int f1;
    char f2[F2_MAX_LEN + 1];
};

/* error handling functions */
void driverdb_exit(apr_dbd_driver_t *driver, apr_dbd_t *db, apr_status_t stat)
{
    printf("APR DBD error:%s\n", apr_dbd_error(driver, db, stat));
    exit(1);
}

void other_exit(char *msg)
{
    printf("%s\n", msg);
    exit(1);
}

/* get f1 given f2 */
int t1_get_one(apr_pool_t *pool, apr_dbd_driver_t *driver, apr_dbd_t *db, char *f2)
{
    apr_status_t stat;
    apr_dbd_prepared_t *stmt;
    apr_dbd_results_t *res;
    apr_dbd_row_t *row;
    int f1;
    /* prepare SQL */
    stat = apr_dbd_prepare(driver, pool, db, "SELECT f1 FROM t1 WHERE f2 = %s", NULL, &stmt);
    if(stat != APR_SUCCESS)
    {
        driverdb_exit(driver, db, stat);
    }
    /* execute */
    stat = apr_dbd_pvselect(driver, pool, db, &res, stmt, 0, f2);
    if(stat != APR_SUCCESS)
    {
        driverdb_exit(driver, db, stat);
    }
    /* process result */
    if(!apr_dbd_get_row(driver, pool, res, &row, -1))
    {
        f1 = atoi(apr_dbd_get_entry(driver, row, 0));
        return f1;
    }
    else
    {
        other_exit("Row not found");
        return 0; /* will never be reached */
    }
    return 0;
}

/* get all records */
int t1_get_all(apr_pool_t *pool, apr_dbd_driver_t *driver, apr_dbd_t *db, struct t1 *buf, int bufsiz)
{
    apr_status_t stat;
    apr_dbd_results_t *res;
    apr_dbd_row_t *row;
    int count;
    /* execute */
    stat = apr_dbd_select(driver, pool, db, &res, "SELECT f1,f2 FROM t1", 0);
    if(stat != APR_SUCCESS)
    {
        driverdb_exit(driver, db, stat);
    }
    /* process result */
    count = 0;
    while(!apr_dbd_get_row(driver, pool, res, &row, -1))
    {
        buf[count].f1 = atoi(apr_dbd_get_entry(driver, row, 0));
        strcpy(buf[count].f2, apr_dbd_get_entry(driver, row, 1));
        count++;
    }
    return count;
}

/* add record */
void t1_put(apr_pool_t *pool, apr_dbd_driver_t *driver, apr_dbd_t *db, int f1, char *f2)
{
    apr_status_t stat;
    apr_dbd_prepared_t *stmt;
    char f1s[20];
    int nrows;
    /* prepare SQL */
    stat = apr_dbd_prepare(driver, pool, db, "INSERT INTO t1 VALUES(%d, %s)", NULL, &stmt);
    if(stat != APR_SUCCESS)
    {
        driverdb_exit(driver, db, stat);
    }
    /* execute */
    sprintf(f1s, "%d", f1);
    stat = apr_dbd_pvquery(driver, pool, db, &nrows, stmt, f1s, f2);
    if(stat != APR_SUCCESS)
    {
        driverdb_exit(driver, db, stat);
    }
    /* check if OK */
    if(nrows != 1)
    {
        other_exit("INSERT did not insert 1 row");
    }
}

/* delete record */
void t1_remove(apr_pool_t *pool, apr_dbd_driver_t *driver, apr_dbd_t *db, int f1)
{
    apr_status_t stat;
    apr_dbd_prepared_t *stmt;
    char f1s[20];
    int nrows;
    /* prepare SQL */
    stat = apr_dbd_prepare(driver, pool, db, "DELETE FROM t1 WHERE f1 = %d", NULL, &stmt);
    if(stat != APR_SUCCESS)
    {
        driverdb_exit(driver, db, stat);
    }
    /* execute */
    sprintf(f1s, "%d", f1);
    stat = apr_dbd_pvquery(driver, pool, db, &nrows, stmt, f1s);
    if(stat != APR_SUCCESS)
    {
        driverdb_exit(driver, db, stat);
    }
    /* check if OK */
    if(nrows != 1)
    {
        other_exit("DELETE did not delete 1 row");
    }
}

/* print all records */
void t1_dump(apr_pool_t *pool, apr_dbd_driver_t *driver, apr_dbd_t *db)
{
    struct t1 buf[MAX_REC];
    int i, n;
    n = t1_get_all(pool, driver, db, buf, MAX_REC);
    for(i = 0; i < n; i++)
    {
        printf("%d \"%s\"\n", buf[i].f1, buf[i].f2);
    }
}

void test(char *drivername, char *params)
{
    printf("%s %s:\n", drivername, params);
    apr_status_t stat;
    apr_pool_t *pool;
    apr_dbd_driver_t *driver;
    apr_dbd_t *db;
    int f1;
    /* initialize */
    stat = apr_pool_initialize();
    if(stat != APR_SUCCESS)
    {
        other_exit("pool_initialize failed");
    }
    stat = apr_pool_create_core(&pool);
    if(stat != APR_SUCCESS)
    {
        other_exit("pool_create_core failed");
    }                                                          
    stat = apr_dbd_init(pool);
    if(stat != APR_SUCCESS)
    {
        other_exit("dbd_init failed");
    }
    /* get connection */
    stat = apr_dbd_get_driver(pool, drivername, &driver);
    if(stat != APR_SUCCESS)
    {
        other_exit("dbd_get_driver failed");
    }
    stat = apr_dbd_open(driver, pool, params, &db);
    if(stat != APR_SUCCESS)
    {
        other_exit("dbd_get_open failed");
    }
    /* test t1_get_one */
    f1 = t1_get_one(pool, driver, db, "BB");
    printf("%d\n", f1);
    /* test t1_get_all */
    t1_dump(pool, driver, db);
    /* test t1_put */
    t1_put(pool, driver, db, 999, "XXX");
    t1_dump(pool, driver, db);
    /* test t1_remove */
    t1_remove(pool, driver, db, 999);
    t1_dump(pool, driver, db);
    /* close */
    apr_dbd_close(driver, db);
    apr_pool_clear(pool);
    apr_pool_destroy(pool);
    apr_pool_terminate();
}

int main()
{
    test("odbc", "DataSource=ARNEPC4_SQLSRV");
    test("odbc", "DataSource=ARNEPC4_MYSQL,User=root");
    test("odbc", "DataSource=ARNEPC4_DB2");
    test("odbc", "DataSource=ARNEPC4_ORACLE,User=arne,Password=xxxxxx");
    test("odbc", "DataSource=ARNEPC4_PGSQL");
    test("odbc", "Connect='Driver={SQL Server Native Client 11.0};Server=localhost;Database=Test;Trusted_Connection=yes;'");
    test("odbc", "Connect='Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=Test;User=root;Password=;'");
    test("odbc", "Connect='Driver={IBM DB2 ODBC DRIVER};Hostname=localhost;Database=Test;Uid=arne;Pwd=xxxxxx'");
    test("odbc", "Connect='Driver={Oracle in instantclient_11_2};dbq=localhost/XE;Uid=arne;Pwd=xxxxxx;'");
    test("odbc", "Connect='Driver={PostgreSQL ANSI(x64)};Server=localhost;Port=5432;Database=Test;Uid=postgres;Pwd=xxxxxx;'");
    test("mysql", "Host=localhost,Dbname=Test,User=root,Pass=");
    test("pgsql", "host=localhost dbname=Test user=postgres password=xxxxxx");
    test("sqlite3", "test.db");
    return 0;
}
