diff options
author | louie <lshprung@tutanota.com> | 2023-07-04 09:53:43 -0700 |
---|---|---|
committer | louie <lshprung@tutanota.com> | 2023-07-04 09:53:43 -0700 |
commit | a3998565d27e5ce835d521dd58a9c30ff2fd83ed (patch) | |
tree | 60ebb124a7ded3a612303fe216adb8794067284c /src | |
parent | 89906af1a08aee8fd9f49fff30a655240d041c30 (diff) |
Progress on db, but still broken
Diffstat (limited to 'src')
-rw-r--r-- | src/db/sqlite3/db.c | 33 | ||||
-rw-r--r-- | src/db/sqlite3/db.h | 4 | ||||
-rw-r--r-- | src/draw.c | 9 | ||||
-rw-r--r-- | src/draw.h | 5 | ||||
-rw-r--r-- | 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 @@ -1,9 +1,10 @@ #include <curses.h> #include <signal.h> +#include <stdlib.h> #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() { @@ -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(); @@ -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; } |