// 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 <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

// LIBOCI headers
#define OCI_API __stdcall
#include <ocilib.h>

static const int F2_MAX_LEN = 50;
static const int MAX_REC = 100;

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

class DB
{
public:
    // destruct instance
    virtual ~DB() { };
    // get f1 given f2
    virtual int T1GetOne(const char *f2) = 0;
    // get all records
    virtual int T1GetAll(struct t1 *buf, int bufsiz) = 0;
    // add record
    virtual void T1Put(int f1, const char *f2) = 0;
    // delete record
    virtual void T1Remove(int f1) = 0;
    // print all records
    void T1Dump();
};

// print all records
void DB::T1Dump()
{
    struct t1 buf[MAX_REC];
    int i, n;
    n = T1GetAll(buf, MAX_REC);
    for(i = 0; i < n; i++)
    {
        cout << buf[i].f1 << " " << buf[i].f2 << endl;
    }
}

class LibOci : public DB
{
private:
    OCI_Connection *con;
public:
    // construct instance
    LibOci(const char *db, const char *usr, const char *pw);
    // destruct instance
    virtual ~LibOci();
    // get f1 given f2
    virtual int T1GetOne(const char *f2);
    // get all records
    virtual int T1GetAll(struct t1 *buf, int bufsiz);
    // add record
    virtual void T1Put(int f1, const char *f2);
    // delete record
    virtual void T1Remove(int f1);
};

// error handling functions
void LibExit(OCI_Error *err)
{
    cout << "SQL error: " << OCI_ErrorGetOCICode(err) << " - " << OCI_ErrorGetString(err) << " (" << OCI_GetSql(OCI_ErrorGetStatement(err)) << ")" << endl;
    exit(1);
}

void OtherExit(const char *msg)
{
    cout << msg << endl;
    exit(1);
}

// construct instance
LibOci::LibOci(const char *db, const char *usr, const char *pw)
{
    con = OCI_ConnectionCreate(db, usr, pw, OCI_SESSION_DEFAULT);
}

// destruct instance
LibOci::~LibOci()
{
    OCI_ConnectionFree(con);
}

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

// get all records
int LibOci::T1GetAll(struct t1 *buf, int bufsiz)
{
    // get statement
    const char *sqlstr = "SELECT f1,f2 FROM t1";
    OCI_Statement *stmt = OCI_StatementCreate(con);
    // execute
    OCI_ExecuteStmt(stmt, sqlstr);
    // process result
    OCI_Resultset *rs = OCI_GetResultset(stmt);
    int 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 LibOci::T1Put(int f1, const char *f2)
{
    // get statement
    const char *sqlstr = "INSERT INTO t1 VALUES(:f1, :f2)";
    OCI_Statement *stmt = OCI_StatementCreate(con);
    // prepare
    OCI_Prepare(stmt, sqlstr);
    // bind parameters
    OCI_BindInt(stmt, ":f1", &f1);
    OCI_BindString(stmt, ":f2", (char *)f2, strlen(f2));
    // execute
    OCI_Execute(stmt);
    // check if OK
    if(OCI_GetAffectedRows(stmt) != 1)
    {
        OtherExit("INSERT did not insert 1 row");
    }
    // close statement
    OCI_StatementFree(stmt);
}

// delete record
void LibOci::T1Remove(int f1)
{
    // get statement
    const char *sqlstr = "DELETE FROM t1 WHERE f1 = :f1";
    OCI_Statement *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)
    {
        OtherExit("DELETE did not delete 1 row");
    }
    // close statement
    OCI_StatementFree(stmt);
}

void test(DB *db)
{
    // test T1GetOne
    int f1 = db->T1GetOne("BB");
    cout << f1 << endl;
    // test T1GetAll
    db->T1Dump();
    // test t1_put
    db->T1Put(999, "XXX");
    db->T1Dump();
    // test t1_remove
    db->T1Remove(999);
    db->T1Dump();
}

void test_dsnless(const char *constr)
{
    cout << "Connection string=" << constr << endl;;
}

void test_oci(const char *dbnam, const char *usr, const char *pw)
{
    // get database connection
    DB *db = new LibOci(dbnam, usr, pw);
    // test
    test(db);
    // release database connection
    delete db;
}

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