Compare commits

...

3 Commits

3 changed files with 100 additions and 41 deletions

View File

@ -30,7 +30,6 @@ int view_all(sqlite3 *db){
}
void init_filtered_tasks(filtered_tasks* task){
task->table = NULL;
task->selected_columns = NULL;
task->title = NULL;
task->new_title = NULL;
@ -46,12 +45,11 @@ 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 %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);
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){
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);
snprintf(sql_query, SQLQUERY_MAX, "UPDATE tasks SET title='%s' WHERE title='%s' OR active_id='%d'",
task->new_title, task->title, task->active_id);
}
else{
return -1;
@ -68,23 +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;
if (task->status != NULL){
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='%d'", task->table, task->title, task->active_id);
sql_query = malloc(SQLQUERY_MAX * sizeof(char));
x_strcpy(sql_query,"UPDATE tasks SET ", SQLQUERY_MAX);
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;
}
@ -92,7 +139,7 @@ int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* ta
char sql_query[SQLQUERY_MAX];
int rc = 0;
snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM %s WHERE (title='%s' OR active_id='%d') AND active_id IS NOT NULL", task->table, task->title, task->active_id);
snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM tasks WHERE (title='%s' OR active_id='%d') AND active_id IS NOT NULL", 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")){
@ -106,8 +153,8 @@ int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* ta
char sql_query[SQLQUERY_MAX];
int rc = 0;
snprintf(sql_query, SQLQUERY_MAX, "INSERT INTO %s %s VALUES %s",
task->table, task->selected_columns, values);
snprintf(sql_query, SQLQUERY_MAX, "INSERT INTO tasks %s VALUES %s",
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;
@ -121,7 +168,7 @@ int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* ta
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", task->selected_columns, task->table, task->status);
snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM tasks WHERE status='%s' AND active_id!='NULL' ORDER BY due_date NULLS LAST", task->selected_columns, 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;
@ -250,7 +297,6 @@ int parse_args(int argc, char** argv, filtered_tasks* task){
// 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));
task->table = table;
task->status = status;
task->selected_columns = "COUNT(*)";
sqlite3_stmt *out_stmt;
@ -290,10 +336,11 @@ int display_task_list(int start_col, sqlite3 *db, filtered_tasks* task){
const unsigned char* col_val;
const unsigned char* col_name;
sqlite3_stmt* out_stmt;
char* table = "tasks";
// Get the number of rows in current task list
num_rows = get_num_rows(db, task->table, task->status);
num_rows = get_num_rows(db, table, task->status);
// Then keep track of the furthest down we go
if (num_rows > max_rows){
max_rows = num_rows;
@ -326,12 +373,16 @@ int display_task_list(int start_col, sqlite3 *db, filtered_tasks* task){
}
print_fixed_width(col_val, fixed_width);
}
// end
// move down one and over to the start of the current task column
printf("\n");
// end
X_goright(start_col);
}
// reset to the beginning of the line
printf("\r");
// if while loop broke and rc returned an error
if ( rc == SQLITE_ERROR ){
X_godown(max_rows);
@ -362,7 +413,6 @@ int view_tasks(sqlite3 *db){
// TODO check that malloc is ok
init_filtered_tasks(tasks);
tasks->table = "tasks";
tasks->selected_columns = "active_id, title, due_date";
// Print Heading
@ -402,7 +452,6 @@ int add_new_task(sqlite3 *db, int argc, char** argv){
filtered_tasks* task = malloc(sizeof(filtered_tasks));
init_filtered_tasks(task);
task->table = "tasks";
parse_args(argc, argv, task);
@ -434,12 +483,11 @@ int update_task_status(sqlite3 *db, int argc, char** argv){
filtered_tasks* task = malloc(sizeof(filtered_tasks));
init_filtered_tasks(task);
task->table = "tasks";
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;
}
@ -463,10 +511,9 @@ int complete_task(sqlite3 *db, int argc, char** argv){
filtered_tasks* task = malloc(sizeof(filtered_tasks));
init_filtered_tasks(task);
task->table = "tasks";
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;
}
@ -490,12 +537,15 @@ int update_task(sqlite3 *db, int argc, char** argv){
init_filtered_tasks(task);
task->table = "tasks";
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;
}
@ -519,7 +569,6 @@ int del_task(sqlite3 *db, int argc, char** argv){
init_filtered_tasks(task);
task->table = "tasks";
parse_args(argc, argv, task);
if ( gen_sql_delete_stmt(db, &out_stmt, task) ){

View File

@ -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 ( 2 * (FIXED_COLUMN_WIDTH + 1) )
// TODO maybe I should make another module that is strictly
@ -24,7 +24,6 @@ typedef struct{
// Filters for sql selection
typedef struct {
char *table;
char *selected_columns;
char *title;
char *new_title;

View File

@ -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