Modified view_tasks, display_task_list, and get_num_rows to use filtered_tasks struct

This commit is contained in:
xavi 2024-09-16 21:00:24 -07:00
parent d12c968b4a
commit 7f15e44f29
2 changed files with 39 additions and 22 deletions

View File

@ -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 // 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]; char sql_query[SQLQUERY_MAX];
int rc = 0; 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); rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
if (checksqlerr(rc, "prepare broken in gen_sql_select_stmt")){ if (checksqlerr(rc, "prepare broken in gen_sql_select_stmt")){
return -1; 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 // Get number of tasks from tasks table give status
int get_num_rows(sqlite3 *db, char* table, char* 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; sqlite3_stmt *out_stmt;
int rc = 0; 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; return -1;
} }
free(task);
while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){ while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){
return sqlite3_column_int(out_stmt, 0); return sqlite3_column_int(out_stmt, 0);
} }
return -1; return rc;
} }
// Print with FIXED_WIDTH // 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; static int max_rows = -1;
int rc = 0; int rc = 0;
int i = 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 // 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 // Then keep track of the furthest down we go
if (num_rows > max_rows){ if (num_rows > max_rows){
max_rows = num_rows; max_rows = num_rows;
} }
// Generate the sql statement by giving the columns, table, and current task list wanted // 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; 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 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); X_godown(max_rows);
printf("\n");
} }
return 0; return 0;
} }
@ -330,30 +336,36 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c
// Print kanban table // Print kanban table
// All lists with task name and due date // All lists with task name and due date
int view_tasks(sqlite3 *db){ int view_tasks(sqlite3 *db){
char* table = "tasks";
char* status = "today"; // Set the table and cols to be printed
char* colnames = "active_id,title,due_date"; 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 // Print Heading
display_heading(); display_heading();
// Print "today" tasks // 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; return -1;
} }
// Print "backlog" tasks // Print "backlog" tasks
status = "backlog"; x_strcpy(tasks->status, "backlog", ARG_MAX);
if ( display_task_list(BACKLOG_COL_START, db, colnames, table, status) ){ if ( display_task_list(BACKLOG_COL_START, db, tasks) ){
return -1; return -1;
} }
// Print "blocked" tasks // Print "blocked" tasks
status = "blocked"; x_strcpy(tasks->status, "blocked", ARG_MAX);
if ( display_task_list(BLOCKED_COL_START, db, colnames, table, status) ){ if ( display_task_list(BLOCKED_COL_START, db, tasks) ){
return -1; return -1;
} }
free(tasks);
return 0; return 0;
} }

View File

@ -20,14 +20,19 @@ typedef struct{
int day; int day;
}date; }date;
// Filters for sql selection
typedef struct { typedef struct {
char table[ARG_MAX];
int task_id; int task_id;
char* title; char selected_columns[ARG_MAX];
char title[ARG_MAX];
int active_id; int active_id;
char* status; char status[ARG_MAX];
char project_tag[ARG_MAX];
date creation_date; date creation_date;
date due_date; date due_date;
}task_entry; }filtered_tasks;
// FOR DEBUG // FOR DEBUG
int callback(void *NotUsed, int argc, char **argv, char **azColName); 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_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_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_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 // helpers
int checksqlerr(int rc, char *errmsg); 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 parse_args(int argc, char** argv, char** title, char** due_date);
int get_num_rows(sqlite3 *db, char* table, char* status); int get_num_rows(sqlite3 *db, char* table, char* status);
int print_fixed_width(const unsigned char* str, int width); 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 // main functions
int view_tasks(sqlite3 *db); int view_tasks(sqlite3 *db);