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

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

// construct instance
OldMysql::OldMysql(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
OldMysql::~OldMysql()
{
    mysql_close(con);
}

// get f1 given f2
int OldMysql::T1GetOne(const char *f2)
{
    int stat;
    // construct SQL
    char f2esc[2 * F2_MAX_LEN], sqlstr[40 + 2 * F2_MAX_LEN];
    mysql_real_escape_string(con, f2esc, f2, strlen(f2));
    sprintf(sqlstr, "SELECT f1 FROM t1 WHERE f2 = '%s'", f2esc);
    // execute
    stat = mysql_query(con, sqlstr);
    if(stat != 0) ConExit();
    // process result
    MYSQL_RES *result = mysql_store_result(con);
    if(result == NULL) ConExit();
    MYSQL_ROW row;
    if((row = mysql_fetch_row(result)))
    {
        int f1 = atoi(row[0]);
        mysql_free_result(result);
        return f1;
    }
    else
    {
        OtherExit("Row not found");
        return 0; // will never be reached
    }
}

// get all records
int OldMysql::T1GetAll(struct t1 *buf, int bufsiz)
{
    int stat;
    // execute
    stat = mysql_query(con, "SELECT f1,f2 FROM t1");
    if(stat != 0) ConExit();
    // process result
    MYSQL_RES *result = mysql_store_result(con);
    if(result == NULL) ConExit();
    int count = 0;
    MYSQL_ROW row;
    while((row = mysql_fetch_row(result)) && (count < bufsiz))
    {
        buf[count].f1 = atoi(row[0]);
        strcpy(buf[count].f2, row[1]);
        count++;
    }
    mysql_free_result(result);
    return count;
}

// add record
void OldMysql::T1Put(int f1, const char *f2)
{
    int stat;
    // construct SQL
    char f2esc[2 * F2_MAX_LEN + 1], sqlstr[40 + 2 * F2_MAX_LEN];
    mysql_real_escape_string(con, f2esc, f2, strlen(f2));
    sprintf(sqlstr, "INSERT INTO t1 VALUES(%d, '%s')", f1, f2esc);
    // execute
    stat = mysql_query(con, sqlstr);
    if(stat != 0) ConExit();
    // check if OK
    if(mysql_affected_rows(con) != 1)
    {
        OtherExit("INSERT did not insert 1 row");
    }
}

// delete record
void OldMysql::T1Remove(int f1)
{
    int stat;
    // construct SQL
    char sqlstr[40 + 2 * F2_MAX_LEN];
    sprintf(sqlstr, "DELETE FROM t1 WHERE f1 = %d", f1);
    // execute
    stat = mysql_query(con, sqlstr);
    if(stat != 0) ConExit();
    // check if OK
    if(mysql_affected_rows(con) != 1)
    {
        OtherExit("DELETE did not delete 1 row");
    }
}

int main()
{
    // get database connection
    DB *db = new OldMysql("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;
}
