#include #include #include "stmt.h" #include "irccolors.h" #include "error.h" #include "irc.h" #define DBFILE "test.sqlite" #define DBERR(line) do { \ const int e = line; \ if(e != SQLITE_OK && e != SQLITE_ROW && e != SQLITE_DONE) \ { \ fprintf(stderr, \ "sqlite (%d): %s\n", \ sqlite3_errcode(connection), sqlite3_errmsg(connection)); \ exit(DB_ERROR); \ } \ } while (0) #define DBUSERERR(line) do { \ const int e = line; \ if(e != SQLITE_OK && e != SQLITE_ROW && e != SQLITE_DONE) { \ r = sqlite3_errmsg(connection); \ } \ } while(0) static sqlite3 * connection = NULL; DECL int api_init(void) { DBERR(sqlite3_open(DBFILE, &connection)); DBERR(stmt_prepare(remind_stmt)); DBERR(stmt_prepare(set_repo_stmt)); return 0; } DECL void api_rope(void) { DBERR(sqlite3_finalize(remind_stmt)); sqlite3_close(connection); } DECL void rope(void) { if (session) { irc_destroy_session(session); } api_rope(); } DECL char * remind(char * who) { char * r; char * title; char * desc; DBERR(sqlite3_bind_text(remind_stmt, 1, who, -1, SQLITE_STATIC)); const int i = sqlite3_step(remind_stmt); DBERR(i); if (i == SQLITE_ROW) { title = (char *) sqlite3_column_text(remind_stmt, 0); title = strdup(title); desc = (char *) sqlite3_column_text(remind_stmt, 1); desc = strdup(desc); asprintf(&r, IRC_COLOR_RED "%s: " IRC_COLOR_YELLOW "%s", title, desc); } else { r = strdup("No current assignment."); } return r; } DECL char * set_repo(char* who, char* link) { char * r; DBERR(sqlite3_bind_text(set_repo_stmt, 1, link, -1, SQLITE_STATIC)); DBERR(sqlite3_bind_text(set_repo_stmt, 2, who, -1, SQLITE_STATIC)); DBERR(sqlite3_step(set_repo_stmt)); return r; } DECL int rtos(void* data, int argc, char** argv, char** colname ){ (void) colname; char *const *const r = (char**)data; for(int i = 0; i < argc; i++){ strcat(*r, "|"); if(argv[i]){ strcat(*r, argv[i]); } else { strcat(*r, "NULL"); } } strcat(*r, "|"); return 0; } DECL char * raw(const char * const sql) { char* errmsg; char* r = (char*)malloc(10000); DBUSERERR(sqlite3_exec(connection, sql, rtos, &r, &errmsg)); if (errmsg){ free(r); r = errmsg; } else { strcat(r, "\00"); } return r; }