From 22e71dd40a788e4dc1de3cfa20d752f49d1eb1a5 Mon Sep 17 00:00:00 2001 From: xavi Date: Tue, 3 Sep 2024 21:21:34 -0700 Subject: [PATCH] Added error checking but broke other things TODO: fix gen_sql_stmt --- src/dodo.c | 88 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/src/dodo.c b/src/dodo.c index f02c156..2d5481a 100644 --- a/src/dodo.c +++ b/src/dodo.c @@ -5,6 +5,10 @@ #include "x_curses.h" #define SQLQUERY_MAX 100 +#define TODAY_COL_START -1 +#define BACKLOG_COL_START 33 +#define BLOCKED_COL_START 66 +#define FIXED_WIDTH 19 int checksqlerr(int rc, char *errmsg){ if( rc!=SQLITE_OK ){ @@ -72,14 +76,19 @@ void view_heading(){ printf("\n"); } -sqlite3_stmt *print_select_test(sqlite3 *db, char* colnames, char* table, char* status){ +// Generate the sql statement by giving the columns, table, and current task list wanted +// TODO make the stmt an arg pass and return an int +// TODO ME THINKS THIS IS THE NEXT THING TO DO BECAUSE BREAKING EVERYTHING +sqlite3_stmt *gen_sql_stmt(sqlite3 *db, char* colnames, char* table, char* status){ char sql_query[SQLQUERY_MAX]; int rc = 0; sqlite3_stmt *out_stmt; snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s'", colnames, table, status); rc = sqlite3_prepare_v2(db, sql_query, -1, &out_stmt, NULL); - checksqlerr(rc, "prepare broken"); + if (checksqlerr(rc, "prepare broken in gen_sql_stmt")){ + return NULL; + } return out_stmt; } @@ -90,12 +99,17 @@ int get_num_rows(sqlite3 *db, char* table, char* status){ char *sql_query = "SELECT COUNT(*) FROM tasks WHERE status = ?"; sqlite3_stmt *out_stmt; int rc = 0; + char *col_name = "COUNT(*)"; - rc = sqlite3_prepare_v2(db, sql_query, -1, &out_stmt, NULL); - checksqlerr(rc, "prepare broken"); + if ( ( out_stmt = gen_sql_stmt(db, col_name, table, status) ) == NULL ){ + return 1; + } + + //rc = sqlite3_prepare_v2(db, sql_query, -1, &out_stmt, NULL); + //checksqlerr(rc, "prepare broken in get_num_rows"); rc = sqlite3_bind_text(out_stmt, 1, status, -1, SQLITE_STATIC); - checksqlerr(rc, "bind broken"); + checksqlerr(rc, "bind broken in get_num_rows"); while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){ return sqlite3_column_int(out_stmt, 0); @@ -107,34 +121,65 @@ int get_num_rows(sqlite3 *db, char* table, char* status){ // Print columns int print_fixed_width(const unsigned char* str){ if (str){ - printf("%-19.19s", str); + printf("%-*.*s", FIXED_WIDTH, FIXED_WIDTH, str); }else{ - printf("%-19.19s",""); + printf("%-*.*s", FIXED_WIDTH, FIXED_WIDTH, ""); } } -void display_status_list(int start_col, sqlite3 *db, char* colnames, char* table, char* status){ +int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, char* status){ + static int max_rows = 0; int rc = 0; int i = 0; - int numrows = 0; + int num_rows = 0; int num_cols = 0; char* errmsg; const unsigned char* col_val; sqlite3_stmt* out_stmt; + // Start col for the current task list X_goright(start_col); - numrows = get_num_rows(db, table, status); - out_stmt = print_select_test(db, colnames, table, status); + + // Get the number of rows in current task list + num_rows = get_num_rows(db, table, status); + // Then keep track of the furthest down we go + if (num_rows > max_rows){ + max_rows = num_rows; + } + + // Generate the sql statement by giving the columns, table, and current task list wanted + if ( ( out_stmt = gen_sql_stmt(db, colnames, table, status) ) == NULL ){ + return 1; + } + // while there is still rows available while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){ + // for each column print the column num_cols = sqlite3_column_count(out_stmt); for (i = 0; i < num_cols; i++){ col_val = sqlite3_column_text(out_stmt, i); print_fixed_width(col_val); } + // move down one and over to the start of the current task column printf("\n"); X_goright(start_col); } - X_goup(numrows); + + // if while loop broke and rc returned an error + if ( rc == SQLITE_ERROR ){ + X_godown(max_rows); + checksqlerr(rc, "step broken in display_task_list"); + return 1; + } + + // Once the task list is completely printed + // reset to the top of the task lists + X_goup(num_rows); + + // if it is the last task list move down one past the longest list + if ( status == "blocked"){ + X_godown(max_rows); + } + return 0; } // Print kanban table @@ -149,17 +194,24 @@ int view(sqlite3 *db){ // Print Heading view_heading(); - // Print "today" tasks - display_status_list(-1, db, colnames, table, status); + if ( display_task_list(TODAY_COL_START, db, colnames, table, status) ){ + return 1; + } // Print "backlog" tasks status = "backlog"; - display_status_list(33, db, colnames, table, status); + if ( display_task_list(BACKLOG_COL_START, db, colnames, table, status) ){ + return 1; + } // Print "blocked" tasks status = "blocked"; - display_status_list(66, db, colnames, table, status); + if ( display_task_list(BLOCKED_COL_START, db, colnames, table, status) ){ + return 1; + } + + return 0; } int main( int argc, char **argv ){ @@ -168,10 +220,6 @@ int main( int argc, char **argv ){ char* home_dir = getenv("HOME"); char* filename = x_strconcat(home_dir, DB_PATH); - //puts(filename); - - //puts(filename); - rc = opendb(&db, filename); rc = view(db);