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

// required (can also be done by including OS specific header)
#define SOCKET int

// MySQL headers
#include <mysql.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 NewMysql : public DB
{
private:
    MYSQL *con;
    void ConExit();
    void StmtExit(MYSQL_STMT *stmt);
    void OtherExit(const char *msg);
public:
    // construct instance
    NewMysql(const char *host, int port, const char *un, const char *pw, const char *db);
    // destruct instance
    virtual ~NewMysql();
    // 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 NewMysql::ConExit()
{
    cout << "MySQL error: " << mysql_error(con) << endl;
    exit(1);
}

void NewMysql::StmtExit(MYSQL_STMT *stmt)
{
    cout << "MySQL Statement error: " << mysql_stmt_error(stmt) << endl;
    exit(1);
}

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

// construct instance
NewMysql::NewMysql(const char *host, int port, const char *un, const char *pw, const char *db)
{
    con = mysql_init(NULL);
    if(con == NULL) ConExit();
    con = mysql_real_connect(con, host, un, pw, db, port, NULL, 0);
    if(con == NULL) ConExit();
}

// destruct instance
NewMysql::~NewMysql()
{
    mysql_close(con);
}

// get f1 given f2
int NewMysql::T1GetOne(const char *f2)
{
    int stat;
    // prepare statement
    MYSQL_STMT *stmt = mysql_stmt_init(con);
    if(stmt == NULL) ConExit();
    const char *sqlstr = "SELECT f1 FROM t1 WHERE f2 = ?";
    stat = mysql_stmt_prepare(stmt, sqlstr, strlen(sqlstr));
    if(stat != 0) StmtExit(stmt);
    // bind parameters
    MYSQL_BIND in[1];
    memset(in, 0, sizeof(in));
    in[0].buffer_type = MYSQL_TYPE_STRING;
    in[0].buffer = (char *)f2;
    in[0].buffer_length = strlen(f2);
    in[0].is_null= 0;
    stat = mysql_stmt_bind_param(stmt, in);
    if(stat != 0) StmtExit(stmt);
    // execute
    stat = mysql_stmt_execute(stmt);
    if(stat != 0) StmtExit(stmt);
    // bind result
    int f1;
    MYSQL_BIND out[1];
    memset(out, 0, sizeof(out));
    out[0].buffer_type = MYSQL_TYPE_LONG;
    out[0].buffer = &f1;
    out[0].buffer_length = sizeof(f1);
    stat = mysql_stmt_bind_result(stmt, out);
    if(stat != 0) StmtExit(stmt);
    // process result
    stat = mysql_stmt_store_result(stmt);
    if(stat != 0) StmtExit(stmt);
    if(!mysql_stmt_fetch(stmt))
    {
        mysql_stmt_free_result(stmt);
        return f1;
    }
    else
    {
        OtherExit("Row not found");
        return 0; // will never be reached
    }
}

// get all records
int NewMysql::T1GetAll(struct t1 *buf, int bufsiz)
{
    int stat;
    // prepare statement
    MYSQL_STMT *stmt = mysql_stmt_init(con);
    if(stmt == NULL) ConExit();
    const char *sqlstr = "SELECT f1,f2 FROM t1";
    stat = mysql_stmt_prepare(stmt, sqlstr, strlen(sqlstr));
    if(stat != 0) StmtExit(stmt);
    // execute
    stat = mysql_stmt_execute(stmt);
    if(stat != 0) StmtExit(stmt);
    // bind result
    char f2[F2_MAX_LEN + 1];
    unsigned long int lenf2;
    int f1;
    MYSQL_BIND out[2];
    memset(out, 0, sizeof(out));
    out[0].buffer_type = MYSQL_TYPE_LONG;
    out[0].buffer = &f1;
    out[0].buffer_length = sizeof(f1);
    out[1].buffer_type = MYSQL_TYPE_STRING;
    out[1].buffer = f2;
    out[1].buffer_length = sizeof(f2);
    out[1].length = &lenf2;
    stat = mysql_stmt_bind_result(stmt, out);
    if(stat != 0) StmtExit(stmt);
    // process result
    stat = mysql_stmt_store_result(stmt);
    if(stat != 0) StmtExit(stmt);
    int count = 0;
    while(!mysql_stmt_fetch(stmt) && (count < bufsiz))
    {
        buf[count].f1 = f1;
        f2[lenf2] = 0;
        strcpy(buf[count].f2, f2);
        count++;
    }
    mysql_stmt_free_result(stmt);
    return count;
}

// add record
void NewMysql::T1Put(int f1, const char *f2)
{
    int stat;
    // prepare statement
    MYSQL_STMT *stmt = mysql_stmt_init(con);
    if(stmt == NULL) ConExit();
    const char *sqlstr = "INSERT INTO t1 VALUES(?, ?)";
    stat = mysql_stmt_prepare(stmt, sqlstr, strlen(sqlstr));
    if(stat != 0) StmtExit(stmt);
    // bind parameters
    MYSQL_BIND in[2];
    memset(in, 0, sizeof(in));
    in[0].buffer_type = MYSQL_TYPE_LONG;
    in[0].buffer = &f1;
    in[0].buffer_length = sizeof(f1);
    in[0].is_null= 0;
    in[1].buffer_type = MYSQL_TYPE_STRING;
    in[1].buffer = (char *)f2;
    in[1].buffer_length = sizeof(f2);
    stat = mysql_stmt_bind_param(stmt, in);
    if(stat != 0) StmtExit(stmt);
    // execute
    stat = mysql_stmt_execute(stmt);
    if(stat != 0) StmtExit(stmt);
    // check if OK
    if(mysql_stmt_affected_rows(stmt) != 1)
    {
        OtherExit("INSERT did not insert 1 row");
    }
}

// delete record
void NewMysql::T1Remove(int f1)
{
    int stat;
    // prepare statement
    MYSQL_STMT *stmt = mysql_stmt_init(con);
    if(stmt == NULL) ConExit();
    const char *sqlstr = "DELETE FROM t1 WHERE f1 = ?";
    stat = mysql_stmt_prepare(stmt, sqlstr, strlen(sqlstr));
    if(stat != 0) StmtExit(stmt);
    // bind parameters
    MYSQL_BIND in[1];
    memset(in, 0, sizeof(in));
    in[0].buffer_type = MYSQL_TYPE_LONG;
    in[0].buffer = &f1;
    in[0].buffer_length = sizeof(f1);
    in[0].is_null= 0;
    stat = mysql_stmt_bind_param(stmt, in);
    if(stat != 0) StmtExit(stmt);
    // execute
    stat = mysql_stmt_execute(stmt);
    if(stat != 0) StmtExit(stmt);
    // check if OK
    if(mysql_stmt_affected_rows(stmt) != 1)
    {
        OtherExit("DELETE did not delete 1 row");
    }
}

int main()
{
    // get database connection
    DB *db = new NewMysql("localhost", 3306, "root", "", "Test");
    // 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;
}
