From 7f15e44f29640a4b9bd449e3e9bbab3c9aff8e14 Mon Sep 17 00:00:00 2001 From: xavi Date: Mon, 16 Sep 2024 21:00:24 -0700 Subject: [PATCH] Modified view_tasks, display_task_list, and get_num_rows to use filtered_tasks struct --- src/dodo.c | 46 +++++++++++++++++++++++++++++----------------- src/dodo.h | 15 ++++++++++----- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/dodo.c b/src/dodo.c index bdd4605..cc1e2e7 100644 --- a/src/dodo.c +++ b/src/dodo.c @@ -101,11 +101,11 @@ int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* } // Generate the sql statement by giving the columns, table, and current task list wanted -int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* colnames, char* table, char* status){ +int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){ char sql_query[SQLQUERY_MAX]; int rc = 0; - snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s' AND active_id!='NULL' ORDER BY due_date NULLS LAST", colnames, table, status); + snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s' AND active_id!='NULL' ORDER BY due_date NULLS LAST", task->selected_columns, task->table, task->status); rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL); if (checksqlerr(rc, "prepare broken in gen_sql_select_stmt")){ return -1; @@ -229,19 +229,24 @@ int parse_args_v2(int argc, char** argv, char** title, char** new_title, char** // Get number of tasks from tasks table give status int get_num_rows(sqlite3 *db, char* table, char* status){ + filtered_tasks* task = malloc(sizeof(filtered_tasks)); + x_strcpy(task->table, table, ARG_MAX); + x_strcpy(task->status, status, ARG_MAX); + x_strcpy(task->selected_columns, "COUNT(*)", ARG_MAX); sqlite3_stmt *out_stmt; int rc = 0; - char *col_name = "COUNT(*)"; - if ( gen_sql_select_stmt(db, &out_stmt, col_name, table, status) == 1 ){ + if ( gen_sql_select_stmt(db, &out_stmt, task) == 1 ){ return -1; } + free(task); + while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){ return sqlite3_column_int(out_stmt, 0); } - return -1; + return rc; } // Print with FIXED_WIDTH @@ -254,7 +259,7 @@ int print_fixed_width(const unsigned char* str, int width){ } -int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, char* status){ +int display_task_list(int start_col, sqlite3 *db, filtered_tasks* task){ static int max_rows = -1; int rc = 0; int i = 0; @@ -268,14 +273,14 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c // Get the number of rows in current task list - num_rows = get_num_rows(db, table, status); + num_rows = get_num_rows(db, task->table, task->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 ( gen_sql_select_stmt(db, &out_stmt, colnames, table, status) ){ + if ( gen_sql_select_stmt(db, &out_stmt, task) ){ return -1; } @@ -321,8 +326,9 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c } // if it is the last task list move down one past the longest list - if ( status == "blocked"){ + if ( x_strcmp(task->status, "blocked") == 0){ X_godown(max_rows); + printf("\n"); } return 0; } @@ -330,30 +336,36 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c // Print kanban table // All lists with task name and due date int view_tasks(sqlite3 *db){ - char* table = "tasks"; - char* status = "today"; - char* colnames = "active_id,title,due_date"; + + // Set the table and cols to be printed + filtered_tasks* tasks = malloc(sizeof(filtered_tasks)); + x_strcpy(tasks->table, "tasks", ARG_MAX); + x_strcpy(tasks->selected_columns, "active_id, title, due_date", ARG_MAX); + // Print Heading display_heading(); // Print "today" tasks - if ( display_task_list(TODAY_COL_START, db, colnames, table, status) ){ + x_strcpy(tasks->status, "today", ARG_MAX); + if ( display_task_list(TODAY_COL_START, db, tasks) ){ return -1; } // Print "backlog" tasks - status = "backlog"; - if ( display_task_list(BACKLOG_COL_START, db, colnames, table, status) ){ + x_strcpy(tasks->status, "backlog", ARG_MAX); + if ( display_task_list(BACKLOG_COL_START, db, tasks) ){ return -1; } // Print "blocked" tasks - status = "blocked"; - if ( display_task_list(BLOCKED_COL_START, db, colnames, table, status) ){ + x_strcpy(tasks->status, "blocked", ARG_MAX); + if ( display_task_list(BLOCKED_COL_START, db, tasks) ){ return -1; } + free(tasks); + return 0; } diff --git a/src/dodo.h b/src/dodo.h index 0944699..2392936 100644 --- a/src/dodo.h +++ b/src/dodo.h @@ -20,14 +20,19 @@ typedef struct{ int day; }date; +// Filters for sql selection typedef struct { + char table[ARG_MAX]; int task_id; - char* title; + char selected_columns[ARG_MAX]; + char title[ARG_MAX]; int active_id; - char* status; + char status[ARG_MAX]; + char project_tag[ARG_MAX]; date creation_date; date due_date; -}task_entry; +}filtered_tasks; + // FOR DEBUG int callback(void *NotUsed, int argc, char **argv, char **azColName); @@ -37,7 +42,7 @@ int view_all(sqlite3 *db); int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* status, char* title); int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* title); int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* colnames, char* values); -int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* colnames, char* table, char* status); +int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task); // helpers int checksqlerr(int rc, char *errmsg); @@ -46,7 +51,7 @@ void display_heading(); int parse_args(int argc, char** argv, char** title, char** due_date); int get_num_rows(sqlite3 *db, char* table, char* status); int print_fixed_width(const unsigned char* str, int width); -int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, char* status); +int display_task_list(int start_col, sqlite3 *db, filtered_tasks*); // main functions int view_tasks(sqlite3 *db);