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

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

/* MySQL headers */
#include <mysql.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 con_exit(MYSQL *con)
{
    printf("MySQL error: %s\n", mysql_error(con));
    exit(1);
}

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

/* get connection */
MYSQL *get_connection(char *host, int port, char *un, char *pw, char *db)
{
    MYSQL *con;
    con = mysql_init(NULL);
    if(con == NULL) con_exit(con);
    con = mysql_real_connect(con, host, un, pw, db, port, NULL, 0);
    if(con == NULL) con_exit(con);
    return con;
}

/* get f1 given f2 */
int t1_get_one(MYSQL *con, char *f2)
{
    MYSQL_RES *result;
    MYSQL_ROW row;
    char f2esc[2 * F2_MAX_LEN], sqlstr[40 + 2 * F2_MAX_LEN];
    int stat, f1;
    /* construct SQL */
    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) con_exit(con);
    /* process result */
    result = mysql_store_result(con);
    if(result == NULL) con_exit(con);
    if((row = mysql_fetch_row(result)))
    {
        f1 = atoi(row[0]);
        mysql_free_result(result);
        return f1;
    }
    else
    {
        other_exit("Row not found");
        return 0; /* will never be reached */
    }
}

/* get all records */
int t1_get_all(MYSQL *con, struct t1 *buf, int bufsiz)
{
    MYSQL_RES *result;
    MYSQL_ROW row;
    int stat, count;
    /* execute */
    stat = mysql_query(con, "SELECT f1,f2 FROM t1");
    if(stat != 0) con_exit(con);
    /* process result */
    result = mysql_store_result(con);
    if(result == NULL) con_exit(con);
    count = 0;
    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 t1_put(MYSQL *con, int f1, char *f2)
{
    char f2esc[2 * F2_MAX_LEN + 1], sqlstr[40 + 2 * F2_MAX_LEN];
    int stat;
    /* construct SQL */
    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) con_exit(con);
    /* check if OK */
    if(mysql_affected_rows(con) != 1)
    {
        other_exit("INSERT did not insert 1 row");
    }
}

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

/* print all records */
void t1_dump(MYSQL *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);
    }
}

int main()
{
    MYSQL *con;
    int f1;
    /* get connection */
    con = get_connection("localhost", 3306, "root", "", "Test");
    /* 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);
    /* close connection */
    mysql_close(con);
    return 0;
}
