/* 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 <stdlib.h>
#include <string.h>

/* SQLRelay headers */
#include <sqlrelay/sqlrclientwrapper.h>

#define F2_MAX_LEN 50
#define MAX_REC 100

typedef char *(*ss2s)(char *s1, char *s2);
typedef char *(*sss2s)(char *s1, char *s2, char *s3);

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

/* error handling functions */
void con_exit(sqlrcon con)
{
    printf("SQLR connection error: %s\n", sqlrcon_errorMessage(con));
    exit(1);
}

void curs_exit(sqlrcur curs)
{
    printf("SQLR cursor error: %s\n", sqlrcur_errorMessage(curs));
    exit(1);
}

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

/* get f1 given f2 */
int t1_get_one(sqlrcon con, char *f2, sss2s placeholder, ss2s bindname)
{
    sqlrcur curs;
    int stat, f1;
    char sqlstr[40 + 2 * F2_MAX_LEN];
    /* get cursor */
    curs = sqlrcur_alloc(con);
    /* prepare */
    sprintf(sqlstr, "SELECT f1 FROM t1 WHERE f2 = %s", placeholder("1", "f2", "text"));
    sqlrcur_prepareQuery(curs, sqlstr);
    /* bind parameters */
    sqlrcur_inputBindString(curs, bindname("1", "f2"), f2);
    /* execute */
    stat = sqlrcur_executeQuery(curs);
    if(!stat) curs_exit(curs);
    /* process result */
    if(sqlrcur_getFieldByIndex(curs, 0, 0) == NULL)
    {
        other_exit("Row not found");
        return 0; /* will never be reached */
    }
    f1 = sqlrcur_getFieldAsIntegerByIndex(curs, 0, 0);
    sqlrcon_endSession(con);
    /* close cursor */ 
    sqlrcur_free(curs);
    return f1;
}

/* get all records */
int t1_get_all(sqlrcon con, struct t1 *buf, int bufsiz)
{
    sqlrcur curs;
    int stat, row, count;
    /* get cursor */
    curs = sqlrcur_alloc(con);
    /* execute */
    stat = sqlrcur_sendQuery(curs, "SELECT f1,f2 FROM t1");
    if(!stat) curs_exit(curs);
    /* process result */
    sqlrcur_setResultSetBufferSize(curs, 100);
    row = 0;
    count = 0;
    for(;;)
    {
        if(count >= bufsiz) break;
        if(sqlrcur_getFieldByIndex(curs, row, 0) == NULL) break;
        buf[count].f1 = sqlrcur_getFieldAsIntegerByIndex(curs, row, 0);
        strcpy(buf[count].f2, sqlrcur_getFieldByIndex(curs, row, 1));
        count++;
        row++;
    }
    sqlrcon_endSession(con);
    /* close cursor */ 
    sqlrcur_free(curs);
    return count;
}

/* add record */
void t1_put(sqlrcon con, int f1, char *f2, sss2s placeholder, ss2s bindname)
{
    sqlrcur curs;
    int stat, n;
    char sqlstr[40 + 2 * F2_MAX_LEN];
    /* get cursor */
    curs = sqlrcur_alloc(con);
    /* prepare */
    sprintf(sqlstr, "INSERT INTO t1 VALUES(%s, %s)", placeholder("1", "f1", "int"), placeholder("2", "f2", "text"));
    sqlrcur_prepareQuery(curs, sqlstr);
    /* bind parameters */
    sqlrcur_inputBindLong(curs, bindname("1", "f1"), f1);
    sqlrcur_inputBindString(curs, bindname("2", "f2"), f2);
    /* execute */
    stat = sqlrcur_executeQuery(curs);
    if(!stat) curs_exit(curs);
    n = sqlrcur_affectedRows(curs);
    if(n != 1)
    {
        other_exit("INSERT did not insert 1 row");
    }
    sqlrcon_endSession(con);
    /* close cursor */ 
    sqlrcur_free(curs);
}

/* delete record */
void t1_remove(sqlrcon con, int f1, sss2s placeholder, ss2s bindname)
{
    sqlrcur curs;
    int stat, n;
    char sqlstr[40 + 2 * F2_MAX_LEN];
    /* get cursor */
    curs = sqlrcur_alloc(con);
    /* prepare */
    sprintf(sqlstr, "DELETE FROM t1 WHERE f1 = %s", placeholder("1", "f1", "int"));
    sqlrcur_prepareQuery(curs, sqlstr);
    /* bind parameters */
    sqlrcur_inputBindLong(curs, bindname("1", "f1"), f1);
    /* execute */
    stat = sqlrcur_executeQuery(curs);
    if(!stat) curs_exit(curs);
    n = sqlrcur_affectedRows(curs);
    if(n != 1)
    {
        other_exit("DELETE did not delete 1 row");
    }
    sqlrcon_endSession(con);
    /* close cursor */ 
    sqlrcur_free(curs);
}

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

void test(char *host, int port, char *un, char *pw, sss2s placeholder, ss2s bindname)
{
    sqlrcon con;
    int f1;
    printf("Connect host=%s port=%d:\n", host, port); 
    /* open */
    con = sqlrcon_alloc(host, port, NULL, un, pw, 0, 1);
    sqlrcon_autoCommitOn(con);
    /* test t1_get_one */
    f1 = t1_get_one(con, "BB", placeholder, bindname);
    printf("%d\n", f1);
    /* test t1_get_all */
    t1_dump(con);
    /* test t1_put */
    t1_put(con, 999, "XXX", placeholder, bindname);
    t1_dump(con);
    /* test t1_remove */
    t1_remove(con, 999, placeholder, bindname);
    t1_dump(con);
    /* close */
    sqlrcon_free(con);
}

#define MAXIX 100

static int phix = 0;
static char phbuf[MAXIX][16];
static int bnix = 0;
static char bnbuf[MAXIX][16];

/* standard (ODBC, DB2, MySQL) */
static char *std_ph(char *num, char *nam, char *typ)
{
    return "?";
}

static char *std_bn(char *num, char *nam)
{
    return num;
}

/* PostgreSQL */
static char *pgsql_ph(char *num, char *nam, char *typ)
{
    char *buf = phbuf[phix];
    sprintf(buf, "$%s::%s", num, typ);
    phix = (phix + 1) % MAXIX;
    return buf;
}

static char *pgsql_bn(char *num, char *nam)
{
    return num;
}

/* Oracle */
static char *ora_ph(char *num, char *nam, char *typ)
{
    char *buf = phbuf[phix];
    sprintf(buf, ":%s", nam);
    phix = (phix + 1) % MAXIX;
    return buf;
}

static char *ora_bn(char *num, char *nam)
{
    return nam;
}

int main()
{
    test("arnepc4", 9001, "arne", "topsecret", std_ph, std_bn); /* 9001 = localmysql */
    test("arnepc4", 9002, "arne", "topsecret", pgsql_ph, pgsql_bn); /* 9002 = localpgsql */
    test("arnepc4", 9003, "arne", "topsecret", std_ph, std_bn); /* 9003 = localdb2 */
    test("arnepc4", 9004, "arne", "topsecret", ora_ph, ora_bn); /* 9004 = localora */
    test("arnepc4", 9005, "arne", "topsecret", std_ph, std_bn); /* 9005 = odbcsqlsrv */
    test("arnepc4", 9006, "arne", "topsecret", std_ph, std_bn); /* 9006 = odbcmysql */
    test("arnepc4", 9007, "arne", "topsecret", std_ph, std_bn); /* 9007 = odbcpgsql */
    test("arnepc4", 9008, "arne", "topsecret", std_ph, std_bn); /* 9008 = odbcdb2 */
    //test("arnepc4", 9009, "arne", "topsecret", std_ph, std_bn); /* 9009 = odbcora */ /* NOTE: not working - INSERT and UPDATE fail */
    test("arnepc4", 9010, "arne", "topsecret", std_ph, std_bn); /* 9010 = vmsmysql */
    test("arnepc4", 9011, "arne", "topsecret", std_ph, std_bn); /* 9011 = vmsodbcrdb */
    //test("arnepc4", 9012, "arne", "topsecret", std_ph, std_bn); /* 9012 = vmsodbcmysql */  /* NOTE: not working - hanging */
    test("arnepc4", 9013, "arne", "topsecret", std_ph, std_bn); /* 9013 = vmsodbcmimer */
    return 0;
}
