/* 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>

/* LIBOCI headers */
#define OCI_API __stdcall
#include <ocilib.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 lib_exit(OCI_Error *err)
{
    printf("SQL error: %d - %s (%s)\n", OCI_ErrorGetOCICode(err), OCI_ErrorGetString(err), OCI_GetSql(OCI_ErrorGetStatement(err)));
    exit(1);
}

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

/* get f1 given f2 */
int t1_get_one(OCI_Connection *con, char *f2)
{
    OCI_Statement *stmt;
    OCI_Resultset *rs;
    char *sqlstr;
    int f1;
    /* get statement */
    sqlstr = "SELECT f1,f2 FROM t1 WHERE f2 = :f2";
    stmt = OCI_StatementCreate(con);
    /* prepare */
    OCI_Prepare(stmt, sqlstr);
    /* bind parameters */
    OCI_BindString(stmt, ":f2", f2, strlen(f2));
    /* execute */
    OCI_Execute(stmt);
    /* process result */
    rs = OCI_GetResultset(stmt);
    if (OCI_FetchNext(rs)) 
    {
        f1 = OCI_GetInt(rs, 1);
    }
    else
    {
        other_exit("Row not found");
        return 0; /* will never be reached */
    }
    /* close statement */
    OCI_StatementFree(stmt);
    return f1;
}

/* get all records */
int t1_get_all(OCI_Connection *con, struct t1 *buf, int bufsiz)
{
    OCI_Statement *stmt;
    OCI_Resultset *rs;
    char *sqlstr;
    int count;
    /* get statement */
    sqlstr = "SELECT f1,f2 FROM t1";
    stmt = OCI_StatementCreate(con);
    /* execute */
    OCI_ExecuteStmt(stmt, sqlstr);
    /* process result */
    rs = OCI_GetResultset(stmt);
    count = 0;
    while (OCI_FetchNext(rs) && count < bufsiz)
    {
        buf[count].f1 = OCI_GetInt(rs, 1);
        strcpy(buf[count].f2, OCI_GetString(rs, 2));
        count++;
    }
    /* close statement */
    OCI_StatementFree(stmt);
    return count;
}

/* add record */
void t1_put(OCI_Connection *con, int f1, char *f2)
{
    OCI_Statement *stmt;
    char *sqlstr;
    /* get statement */
    sqlstr = "INSERT INTO t1 VALUES(:f1, :f2)";
    stmt = OCI_StatementCreate(con);
    /* prepare */
    OCI_Prepare(stmt, sqlstr);
    /* bind parameters */
    OCI_BindInt(stmt, ":f1", &f1);
    OCI_BindString(stmt, ":f2", f2, strlen(f2));
    /* execute */
    OCI_Execute(stmt);
    /* check if OK */
    if(OCI_GetAffectedRows(stmt) != 1)
    {
        other_exit("INSERT did not insert 1 row");
    }
    /* close statement */
    OCI_StatementFree(stmt);
}

/* delete record */
void t1_remove(OCI_Connection *con, int f1)
{
    OCI_Statement *stmt;
    char *sqlstr;
    /* get statement */
    sqlstr = "DELETE FROM t1 WHERE f1 = :f1";
    stmt = OCI_StatementCreate(con);
    /* prepare */
    OCI_Prepare(stmt, sqlstr);
    /* bind parameters */
    OCI_BindInt(stmt, ":f1", &f1);
    /* execute */
    OCI_Execute(stmt);
    /* check if OK */
    if(OCI_GetAffectedRows(stmt) != 1)
    {
        other_exit("DELETE did not delete 1 row");
    }
    /* close statement */
    OCI_StatementFree(stmt);
}

/* print all records */
void t1_dump(OCI_Connection *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(OCI_Connection *con)
{
    int f1;
    /* test t1_get_one */
    f1 = t1_get_one(con, "BB");
    printf("%d\n", f1);
    /* test t1_get_all */
    t1_dump(con);
    /* test t1_put */
    t1_put(con, 999, "XXX");
    t1_dump(con);
    /* test t1_remove */
    t1_remove(con, 999);
    t1_dump(con);
}

void test_oci(char *dbnam, char *usr, char *pw)
{
    OCI_Connection *con;
    /* get connection */
    con = OCI_ConnectionCreate(dbnam, usr, pw, OCI_SESSION_DEFAULT);
    /* test */
    test(con);
    /* close connection */
    OCI_ConnectionFree(con);
}

int main()
{
    OCI_Initialize(lib_exit, NULL, OCI_ENV_THREADED); 
    test_oci("XE", "arne", "xxxxxx");
    OCI_Cleanup();
    return 0;
}
