From df50e34958a7e6346c81516f17b417f5955f4e97 Mon Sep 17 00:00:00 2001 From: xavi Date: Tue, 17 Sep 2024 22:10:11 -0700 Subject: [PATCH] Modified the gen_sql functions to use task struct TODO: sqlite3_changes() to make sure rows were affected --- src/dodo.c | 45 +++++++++++++++++++++++++-------------------- src/dodo.h | 9 +++++---- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/dodo.c b/src/dodo.c index f26bd06..db4b5a2 100644 --- a/src/dodo.c +++ b/src/dodo.c @@ -28,16 +28,16 @@ int view_all(sqlite3 *db){ return rc; } -int gen_sql_update_stmt_v2(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* new_title, char* new_due_date, char* new_project_tag, char* title_or_active_id){ +int gen_sql_update_stmt_v2(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){ char sql_query[SQLQUERY_MAX]; int rc = 0; - if (new_title[0] != '\0' && new_due_date[0] != '\0'){ + if (task->new_title[0] != '\0' && task->due_date[0] != '\0'){ //snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s', due_date='%s', project_tag='%s' WHERE title='%s' OR active_id='%s'", table, status, title_or_active_id, title_or_active_id); - snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s', due_date='%s' WHERE title='%s' OR active_id='%s'", table, new_title, new_due_date, title_or_active_id, title_or_active_id); + snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s', due_date='%s' WHERE title='%s' OR active_id='%d'", task->table, task->new_title, task->due_date, task->title, task->active_id); } - else if (new_title[0] != '\0'){ - snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s' WHERE title='%s' OR active_id='%s'", table, new_title, title_or_active_id, title_or_active_id); + else if (task->new_title[0] != '\0'){ + snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s' WHERE title='%s' OR active_id='%d'", task->table, task->new_title, task->title, task->active_id); } else{ return -1; @@ -53,15 +53,15 @@ int gen_sql_update_stmt_v2(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, ch } // sql generators TODO: might be able to boil this down to 1 func -int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* status, char* title_or_active_id){ +int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){ char sql_query[SQLQUERY_MAX]; int rc = 0; - if (status != NULL){ - snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET status='%s' WHERE title='%s' OR active_id='%s'", table, status, title_or_active_id, title_or_active_id); + if (task->status[0] != '\0'){ + snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET status='%s' WHERE title='%s' OR active_id='%d'", task->table, task->status, task->title, task->active_id); } else{ - snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET active_id='NULL',status='complete' WHERE title='%s' OR active_id='%s'", table, title_or_active_id, title_or_active_id); + snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET active_id='NULL',status='complete' WHERE title='%s' OR active_id='%d'", task->table, task->title, task->active_id); } rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL); @@ -73,11 +73,11 @@ int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* } -int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* title_or_active_id){ +int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){ char sql_query[SQLQUERY_MAX]; int rc = 0; - snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM %s WHERE title='%s' OR active_id='%s'", table, title_or_active_id, title_or_active_id); + snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM %s WHERE title='%s' OR active_id='%d'", task->table, task->title, task->active_id); rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL); if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){ @@ -87,11 +87,11 @@ int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* return 0; } -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, filtered_tasks* task, char* values){ char sql_query[SQLQUERY_MAX]; int rc = 0; - snprintf(sql_query, SQLQUERY_MAX, "INSERT INTO %s %s VALUES %s", table, colnames, values); + snprintf(sql_query, SQLQUERY_MAX, "INSERT INTO %s %s VALUES %s", task->table, task->selected_columns, values); rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL); if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){ return -1; @@ -204,6 +204,7 @@ int parse_args(int argc, char** argv, filtered_tasks* task){ if ( argc > 2 ){ x_strcpy(task->title, argv[2], ARG_MAX); + task->active_id = atoi(argv[2]); } if ( argc > 3 ){ @@ -389,14 +390,14 @@ int add_new_task(sqlite3 *db, int argc, char** argv){ if ( task->title[0] != '\0' ){ if ( task->due_date[0] != '\0' ){ x_strcpy(task->selected_columns, "(title, due_date)", ARG_MAX); - snprintf(values, 100, "('%s', '%s')", task->title, task->due_date); + snprintf(values, ARG_MAX, "('%s', '%s')", task->title, task->due_date); }else{ x_strcpy(task->selected_columns, "(title)", ARG_MAX); - snprintf(values, 100, "('%s')", task->title); + snprintf(values, ARG_MAX, "('%s')", task->title); } } - if ( gen_sql_insert_stmt(db, &out_stmt, task->table, task->selected_columns, values) ){ + if ( gen_sql_insert_stmt(db, &out_stmt, task, values) ){ return -1; } if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){ @@ -413,12 +414,14 @@ int update_task_status(sqlite3 *db, int argc, char** argv){ sqlite3_stmt* out_stmt; filtered_tasks* task = malloc(sizeof(filtered_tasks)); + task->status[0] = '\0'; x_strcpy(task->table, "tasks", ARG_MAX); // TODO: this is not just title but also active ID so fix this parse_args(argc, argv, task); - if ( gen_sql_update_stmt(db, &out_stmt, task->table, argv[1], task->title) ){ + x_strcpy(task->status, argv[1], ARG_MAX); + if ( gen_sql_update_stmt(db, &out_stmt, task) ){ free(task); return -1; } @@ -441,10 +444,11 @@ int complete_task(sqlite3 *db, int argc, char** argv){ sqlite3_stmt* out_stmt; filtered_tasks* task = malloc(sizeof(filtered_tasks)); + task->status[0] = '\0'; x_strcpy(task->table, "tasks", ARG_MAX); parse_args(argc, argv, task); - if ( gen_sql_update_stmt(db, &out_stmt, task->table, NULL, task->title) ){ + if ( gen_sql_update_stmt(db, &out_stmt, task) ){ free(task); return -1; } @@ -472,7 +476,7 @@ int update_task(sqlite3 *db, int argc, char** argv){ x_strcpy(task->table, "tasks", ARG_MAX); parse_args(argc, argv, task); - if ( gen_sql_update_stmt_v2(db, &out_stmt, task->table, task->new_title, task->due_date, task->project_tag, task->title) ){ + if ( gen_sql_update_stmt_v2(db, &out_stmt, task) ){ free(task); return -1; } @@ -494,10 +498,11 @@ int del_task(sqlite3 *db, int argc, char** argv){ sqlite3_stmt* out_stmt; filtered_tasks* task = malloc(sizeof(filtered_tasks)); + task->active_id = 0; x_strcpy(task->table, "tasks", ARG_MAX); parse_args(argc, argv, task); - if ( gen_sql_delete_stmt(db, &out_stmt, task->table, task->title) ){ + if ( gen_sql_delete_stmt(db, &out_stmt, task) ){ free(task); return -1; } diff --git a/src/dodo.h b/src/dodo.h index bbbf5dc..b563e60 100644 --- a/src/dodo.h +++ b/src/dodo.h @@ -12,7 +12,7 @@ #define FIXED_MAX_WIDTH (FIXED_COLUMN_WIDTH) #define TODAY_COL_START -1 #define BACKLOG_COL_START (FIXED_COLUMN_WIDTH + 1) -#define BLOCKED_COL_START (2 * (FIXED_COLUMN_WIDTH + 1)) +#define BLOCKED_COL_START (FIXED_COLUMN_WIDTH + 1) #define DUEPARSE_SIZE 4 typedef struct{ @@ -40,9 +40,10 @@ int callback(void *NotUsed, int argc, char **argv, char **azColName); int view_all(sqlite3 *db); // sql generators TODO: might be able to boil this down to 1 func -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_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task); +int gen_sql_update_stmt_v2(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task); +int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task); +int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task, char* values); int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task); // helpers