diff --git a/src/dodo.c b/src/dodo.c index f669a87..30e6dbf 100644 --- a/src/dodo.c +++ b/src/dodo.c @@ -45,7 +45,6 @@ int gen_sql_update_stmt_v2(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* int rc = 0; if (task->new_title != NULL && task->due_date != NULL){ - // TODO: we might want to have a function that builds the final sql_query snprintf(sql_query, SQLQUERY_MAX, "UPDATE tasks SET title='%s', due_date='%s' WHERE title='%s' OR active_id='%d'", task->new_title, task->due_date, task->title, task->active_id); } else if (task->new_title != NULL){ @@ -67,28 +66,72 @@ int gen_sql_update_stmt_v2(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* } -int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){ - char sql_query[SQLQUERY_MAX]; +char* concat_with_mem_cleanup(char* str1, char* str2){ + char *temp; + + if ( str1 == NULL || str2 == NULL ){ + return NULL; + } + + temp = str1; + str1 = x_strconcat(str1, str2); + free(temp); + + return str1; +} + + +int prepare_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){ + char *sql_query; int rc = 0; + int num_params = 0; + + sql_query = malloc(SQLQUERY_MAX * sizeof(char)); + x_strcpy(sql_query,"UPDATE tasks SET ", SQLQUERY_MAX); - if (task->new_title != NULL && task->due_date != NULL){ - // TODO: we might want to have a function that builds the final sql_query - snprintf(sql_query, SQLQUERY_MAX, "UPDATE tasks SET title='%s', due_date='%s' WHERE title='%s' OR active_id='%d'", task->new_title, task->due_date, task->title, task->active_id); - } - else if (task->status != NULL){ - snprintf(sql_query, SQLQUERY_MAX, "UPDATE tasks SET status='%s' WHERE title='%s' OR active_id='%d'", - task->status, task->title, task->active_id); - } - else{ - snprintf(sql_query, SQLQUERY_MAX, "UPDATE tasks SET active_id='NULL',status='complete' WHERE title='%s' OR active_id='%d'", task->title, task->active_id); + if ( task->new_title ){ + sql_query = concat_with_mem_cleanup(sql_query, "title=?"); + num_params++; } - rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL); - if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){ - return -1; + if ( task->due_date ){ + if ( num_params > 0 ){ + sql_query = concat_with_mem_cleanup(sql_query, ","); + } + sql_query = concat_with_mem_cleanup(sql_query, "due_date=?"); + num_params++; } + if ( task->status ){ + if ( num_params > 0 ){ + sql_query = concat_with_mem_cleanup(sql_query, ","); + } + sql_query = concat_with_mem_cleanup(sql_query, "status=?"); + num_params++; + + if ( !(x_strcmp(task->status, "complete")) ){ + sql_query = concat_with_mem_cleanup(sql_query, ","); + sql_query = concat_with_mem_cleanup(sql_query, "active_id='NULL'"); + num_params++; + } + } + + + + //if (task->status != NULL){ + // snprintf(sql_query, SQLQUERY_MAX, "UPDATE tasks SET status='%s' WHERE title='%s' OR active_id='%d'", + // task->status, task->title, task->active_id); + //} + //else{ + // snprintf(sql_query, SQLQUERY_MAX, "UPDATE tasks SET active_id='NULL',status='complete' WHERE title='%s' OR active_id='%d'", 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")){ + // return -1; + //} + return 0; } @@ -444,7 +487,7 @@ int update_task_status(sqlite3 *db, int argc, char** argv){ parse_args(argc, argv, task); task->status = argv[1]; - if ( gen_sql_update_stmt(db, &out_stmt, task) ){ + if ( prepare_sql_update_stmt(db, &out_stmt, task) ){ free(task); return -1; } @@ -470,7 +513,7 @@ int complete_task(sqlite3 *db, int argc, char** argv){ init_filtered_tasks(task); parse_args(argc, argv, task); - if ( gen_sql_update_stmt(db, &out_stmt, task) ){ + if ( prepare_sql_update_stmt(db, &out_stmt, task) ){ free(task); return -1; } @@ -495,10 +538,14 @@ int update_task(sqlite3 *db, int argc, char** argv){ init_filtered_tasks(task); - parse_args(argc, argv, task); + //parse_args(argc, argv, task); // TODO this can definitely be made into one func with all the other updates // no v2s allowed - if ( gen_sql_update_stmt_v2(db, &out_stmt, task) ){ + + task->new_title = "cool_beans"; + task->due_date = "0303-03-03"; + task->status = "complete"; + if ( prepare_sql_update_stmt(db, &out_stmt, task) ){ free(task); return -1; } diff --git a/tests/tests.c b/tests/tests.c index f826225..ce009b7 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -15,7 +15,7 @@ // SET THE NUMBER OF TESTS -#define NUM_TESTS 6 +#define NUM_TESTS 7 // DEFINE REUSABLE TEST SETUPS int set_up_db(sqlite3 **db){ @@ -133,6 +133,16 @@ int test_update_task(){ printf("%s... ", __func__); int rc = 0; sqlite3 *db; + char *argv_1[3] = {"./dodo", "new", "jello"}; + + if ( (rc = set_up_db(&db)) ){ + return rc; + } + + update_task(db, 3, argv_1); + + return 1; + } @@ -168,11 +178,12 @@ int main(){ // PASS TESTS INTO ARRAY tests[0] = fail; - tests[1] = test_add_new_task_no_date; - tests[2] = test_del_task; - tests[3] = test_add_new_task_with_date_and_delete; - tests[4] = test_add_then_update_task_status_then_delete; - tests[5] = test_display_task; + tests[1] = test_update_task; + tests[2] = test_add_new_task_no_date; + tests[3] = test_del_task; + tests[4] = test_add_new_task_with_date_and_delete; + tests[5] = test_add_then_update_task_status_then_delete; + tests[6] = test_display_task; // END OF PASSING TESTS