// 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>
using namespace std;

// MySQL headers
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
using namespace sql;

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 OOMysql : public DB
{
private:
    Connection *con;
    void ExceptionExit(SQLException ex);
    void OtherExit(const char *msg);
public:
    // construct instance
    OOMysql(const char *conurl, const char *un, const char *pw);
    // destruct instance
    virtual ~OOMysql();
    // 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 OOMysql::ExceptionExit(SQLException ex)
{
    cout << "MySQL exception: " << ex.what() << endl;
    exit(1);
}

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

// construct instance
OOMysql::OOMysql(const char *conurl, const char *un, const char *pw)
{
    try
    {
        Driver *driver = get_driver_instance();
        con = driver->connect(conurl, un, pw);
    }
    catch(SQLException &ex)
    {
        ExceptionExit(ex);
    }
}

// destruct instance
OOMysql::~OOMysql()
{
    delete con;
}

// get f1 given f2
int OOMysql::T1GetOne(const char *f2)
{
    try
    {
        // prepare statement
        const char *sqlstr = "SELECT f1 FROM t1 WHERE f2 = ?";
        PreparedStatement *pstmt = con->prepareStatement(sqlstr);
        // bind parameters and execute query
        pstmt->setString(1, f2);
        ResultSet *rs = pstmt->executeQuery();
        // process result
        int f1;
        if(rs->next())
        {
            f1 = rs->getInt(1);
        }
        else
        {
            OtherExit("Row not found");
            return 0; // will never be reached
        }
        delete rs;
        delete pstmt;
        return f1;
    }
    catch(SQLException &ex)
    {
        ExceptionExit(ex);
        return 0; // will never be reached
    }
}

// get all records
int OOMysql::T1GetAll(struct t1 *buf, int bufsiz)
{
    try
    {
        // get statement and execute query
        const char *sqlstr = "SELECT f1,f2 FROM t1";
        Statement *stmt = con->createStatement();
        ResultSet *rs = stmt->executeQuery(sqlstr);
        // process result
        int count = 0;
        while(rs->next()  && (count < bufsiz))
        {
            buf[count].f1 = rs->getInt(1);
            strcpy(buf[count].f2, rs->getString(2).c_str()); 
            count++;
        }
        delete rs;
        delete stmt;
        return count;
    }
    catch(SQLException &ex)
    {
        ExceptionExit(ex);
        return 0; // will never be reached
    }
}

// add record
void OOMysql::T1Put(int f1, const char *f2)
{
    try
    {
        // prepare statement
        const char *sqlstr = "INSERT INTO t1 VALUES(?, ?)";
        PreparedStatement *pstmt = con->prepareStatement(sqlstr);
        // bind parameters and execute query
        pstmt->setInt(1, f1);
        pstmt->setString(2, f2);
        int n = pstmt->executeUpdate();
        // check if OK
        if(n != 1)
        {
            OtherExit("INSERT did not insert 1 row");
        }
        delete pstmt;
    }
    catch(SQLException &ex)
    {
        ExceptionExit(ex);
    }
}

// delete record
void OOMysql::T1Remove(int f1)
{
    try
    {
        // prepare statement
        const char *sqlstr = "DELETE FROM t1 WHERE f1 = ?";
        PreparedStatement *pstmt = con->prepareStatement(sqlstr);
        // bind parameters and execute query
        pstmt->setInt(1, f1);
        int n = pstmt->executeUpdate();
        // check if OK
        if(n != 1)
        {
            OtherExit("DELETE did not delete 1 row");
        }
        delete pstmt;
    }
    catch(SQLException &ex)
    {
        ExceptionExit(ex);
    }
}

int main()
{
    // get database connection
    DB *db = new OOMysql("tcp://localhost:3306/Test", "root", "");
    // 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();
    // release database connection
    delete db;
    return 0;
}
