From a3998565d27e5ce835d521dd58a9c30ff2fd83ed Mon Sep 17 00:00:00 2001 From: louie Date: Tue, 4 Jul 2023 09:53:43 -0700 Subject: Progress on db, but still broken --- src/db/sqlite3/db.c | 33 +++++++++++++++++++++++++++++---- src/db/sqlite3/db.h | 4 ++-- src/draw.c | 9 ++++++++- src/draw.h | 5 ++++- src/main.c | 20 +++++++++++++++++--- 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/db/sqlite3/db.c b/src/db/sqlite3/db.c index 91ac0eb..796d924 100644 --- a/src/db/sqlite3/db.c +++ b/src/db/sqlite3/db.c @@ -18,6 +18,7 @@ int create_tables(const char *path); int group_count_callback(void *first_arg, int argc, char **argv, char **azColName); int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName); int load_groups_callback(void *first_arg, int argc, char **argv, char **azColName); +int load_entries_callback(void *first_arg, int argc, char **argv, char **azColName); // globals static int callback_index; // keep track of index when filling arrays using callback functions @@ -41,7 +42,7 @@ int db_init() { return 0; } -Group *db_load_groups() { +Group *db_load_groups(int *group_count) { Group *output; sqlite3 *db; const char *count_query = "SELECT COUNT(*) FROM 'Group'"; @@ -72,12 +73,13 @@ Group *db_load_groups() { sqlite3_free(zErrMsg); return NULL; } + *group_count = callback_index; sqlite3_close(db); return output; } -Entry *db_load_entries() { +Entry *db_load_entries(int *entry_count) { Entry *output; sqlite3 *db; const char *count_query = "SELECT COUNT(*) FROM 'Entry'"; @@ -102,12 +104,13 @@ Entry *db_load_entries() { // do select query callback_index = 0; - res = sqlite3_exec(db, query, load_groups_callback, &output, &zErrMsg); + res = sqlite3_exec(db, query, load_entries_callback, &output, &zErrMsg); if(res != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); return NULL; } + *entry_count = callback_index; sqlite3_close(db); return output; @@ -193,7 +196,29 @@ int load_groups_callback(void *first_arg, int argc, char **argv, char **azColNam group_set_name(&((*groups)[callback_index]), (argv[1] ? argv[1] : "")); group_set_desc(&((*groups)[callback_index]), (argv[2] ? argv[2] : "")); group_set_url(&((*groups)[callback_index]), (argv[3] ? argv[3] : "")); - printf("%d\n", argc); + + ++callback_index; + return 0; +} + +int load_entries_callback(void *first_arg, int argc, char **argv, char **azColName) { + Entry **entries = (Entry **) first_arg; + + // check that enough arguments were passed + if(argc < 7) { + fprintf(stderr, "Error: not enough rows returned in 'Entry' table\n"); + return 1; + } + + entry_set_id(&((*entries)[callback_index]), (argv[0] ? atoi(argv[0]) : 0)); + fprintf(stderr, "time format: %s\n", (argv[1] ? argv[1] : "")); + //entry_set_due_date(&((*entries)[callback_index]), (argv[1] ? argv[1] : "")); // TODO + entry_set_alt_due_date(&((*entries)[callback_index]), (argv[2] ? argv[2] : "")); + entry_set_title(&((*entries)[callback_index]), (argv[3] ? argv[3] : "")); + entry_set_color(&((*entries)[callback_index]), (argv[4] ? argv[4] : "")); + entry_set_highlight(&((*entries)[callback_index]), (argv[5] ? argv[5] : "")); + entry_set_done(&((*entries)[callback_index]), (argv[6] ? argv[6] : false)); + entry_set_url(&((*entries)[callback_index]), (argv[7] ? argv[7] : "")); ++callback_index; return 0; diff --git a/src/db/sqlite3/db.h b/src/db/sqlite3/db.h index 44f61b7..78353fb 100644 --- a/src/db/sqlite3/db.h +++ b/src/db/sqlite3/db.h @@ -13,8 +13,8 @@ int db_init(); -Group *db_load_groups(); +Group *db_load_groups(int *group_count); -Entry *db_load_entries(); +Entry *db_load_entries(int *entry_count); #endif diff --git a/src/draw.c b/src/draw.c index 0e87e3e..04e02e2 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,9 +1,10 @@ #include #include +#include #include "draw.h" -void tui_init() { +void tui_init(Group **groups, Entry **entries) { int input; // capture user input WINDOW *w_main; @@ -22,8 +23,14 @@ void tui_init() { refresh(); wrefresh(w_main); + // draw groups and entries + fprintf(stderr, "groups: %d\n", sizeof(*groups) / sizeof(Group)); + fprintf(stderr, "entries: %d\n", sizeof(*entries) / sizeof(Entry)); + input = getch(); tui_end(); + free(*groups); + free(*entries); } void tui_end() { diff --git a/src/draw.h b/src/draw.h index 8a39605..5a44f55 100644 --- a/src/draw.h +++ b/src/draw.h @@ -1,8 +1,11 @@ #ifndef DRAW_H #define DRAW_H +#include "entry.h" +#include "group.h" + // initialize the tui -void tui_init(); +void tui_init(Group **groups, Entry **entries); // destroy the tui void tui_end(); diff --git a/src/main.c b/src/main.c index 18cf5c5..c658cb7 100644 --- a/src/main.c +++ b/src/main.c @@ -7,13 +7,27 @@ int main() { Group *groups; + int group_count; Entry *entries; + int entry_count; db_init(); - groups = db_load_groups(); - entries = db_load_entries(); + groups = db_load_groups(&group_count); - tui_init(); + for(int i = 0; i < group_count; ++i) { + fprintf(stderr, "group %d:\n", i+1); + fprintf(stderr, "\t ID: %d\n", group_get_id(&(groups[i]))); + fprintf(stderr, "\tName: %s\n", group_get_name(&(groups[i]))); + fprintf(stderr, "\tDesc: %s\n", group_get_desc(&(groups[i]))); + fprintf(stderr, "\t Url: %s\n", group_get_url(&(groups[i]))); + } + + entries = db_load_entries(&entry_count); + + if(groups == NULL) perror("could not get groups"); + if(entries == NULL) perror("could not get entries"); + + tui_init(&groups, &entries); return 0; } -- cgit