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

/* embedded SQL headers */
EXEC SQL INCLUDE SQLCA;
#if defined(DB2)
#include <sql.h>
#endif

#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 sql_exit(char *sql)
{
    printf("SQL error in %s: %s\n", sql, sqlca.sqlerrm.sqlerrmc);
    exit(1);
}

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

/* get f1 given f2 */
int t1_get_one(char *f2)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char inf2[51];
    long int outf1;
    EXEC SQL END DECLARE SECTION;
    /* open cursor */
    strcpy(inf2, f2);
    EXEC SQL DECLARE cone CURSOR FOR SELECT f1 FROM t1 WHERE f2 = :inf2;
    if(sqlca.sqlcode != 0) sql_exit("declare cursor");
    EXEC SQL OPEN cone;
    if(sqlca.sqlcode != 0) sql_exit("open cursor");
    /* read from cursor */
    EXEC SQL FETCH cone INTO :outf1;
    if(sqlca.sqlcode != 0)
    {
        other_exit("Row not found");
    }
    /* close cursor */
    EXEC SQL CLOSE cone;
    return outf1;
}

/* get all records */
int t1_get_all(struct t1 *buf, int bufsiz)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char f2[51];
    long int f1;
    EXEC SQL END DECLARE SECTION;
    int count;
    /* open cursor */
    EXEC SQL DECLARE call CURSOR FOR SELECT f1,f2 FROM t1;
    if(sqlca.sqlcode != 0) sql_exit("declare cursor");
    EXEC SQL OPEN call;
    if(sqlca.sqlcode != 0) sql_exit("open cursor");
    /* read from cursor */
    count = 0;
    for(;;)
    {
        if(count >= bufsiz) break;
        EXEC SQL FETCH call INTO :f1, :f2;
        if(sqlca.sqlcode != 0) break;
        buf[count].f1 = f1;
        strcpy(buf[count].f2, f2);
        count++;
    }
    /* close cursor */
    EXEC SQL CLOSE call;
    return count;
}

/* add record */
void t1_put(int f1, char *f2)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char insf2[51];
    long int insf1;
    EXEC SQL END DECLARE SECTION;
    /* execute */
    insf1 = f1;
    strcpy(insf2, f2);
    EXEC SQL INSERT INTO t1 VALUES(:insf1, :insf2);
    if(sqlca.sqlcode != 0) sql_exit("insert");
    /* check if OK */
    if(sqlca.sqlerrd[2] != 1)
    {
        other_exit("INSERT did not insert 1 row");
    }
}

/* delete record */
void t1_remove(int f1)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long int delf1;
    EXEC SQL END DECLARE SECTION;
    /* execute */
    delf1 = f1;
    EXEC SQL DELETE FROM t1 WHERE f1 = :delf1;
    if(sqlca.sqlcode != 0) sql_exit("delete");
    /* check if OK */
    if(sqlca.sqlerrd[2] != 1)
    {
        other_exit("DELETE did not delete 1 row");
    }
}

/* print all records */
void t1_dump()
{
    struct t1 buf[MAX_REC];
    int i, n;
    n = t1_get_all(buf, MAX_REC);
    for(i = 0; i < n; i++)
    {
        printf("%d \"%s\"\n", buf[i].f1, buf[i].f2);
    }
}

int main()
{
    EXEC SQL BEGIN DECLARE SECTION;
    char srv[50];
    char un[50];
    char pw[50];
    EXEC SQL END DECLARE SECTION;
    int f1;
    /* get connection */
    strcpy(srv, "arnepc4:9004");
    strcpy(un, "arne");
    strcpy(pw, "topsecret");
    EXEC SQL CONNECT :un IDENTIFIED BY :pw USING :srv;
    if(sqlca.sqlcode != 0) sql_exit("connect");
    /* test t1_get_one */
    f1 = t1_get_one("BB");
    printf("%d\n", f1);
    /* test t1_get_all */
    t1_dump();
    /* test t1_put */
    t1_put(999, "XXX");
    t1_dump();
    /* test t1_remove */
    t1_remove(999);
    t1_dump();
    return 0;
}
