Added update_task func TODO:refacter to use structs
This commit is contained in:
parent
de35e45daf
commit
d12c968b4a
87
src/dodo.c
87
src/dodo.c
@ -28,20 +28,21 @@ int view_all(sqlite3 *db){
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
// 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_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){
|
||||
char sql_query[SQLQUERY_MAX];
|
||||
int rc = 0;
|
||||
|
||||
if (status != NULL){
|
||||
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET status='%s' WHERE title='%s'", table, status, title);
|
||||
if (new_title != NULL && new_due_date != NULL){
|
||||
//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);
|
||||
}
|
||||
else if (new_title != NULL){
|
||||
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{
|
||||
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET active_id='NULL',status='complete' WHERE title='%s'", table, title);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
|
||||
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
|
||||
return -1;
|
||||
@ -51,11 +52,32 @@ 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){
|
||||
// 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){
|
||||
char sql_query[SQLQUERY_MAX];
|
||||
int rc = 0;
|
||||
|
||||
snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM %s WHERE title='%s'", table, title);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* title_or_active_id){
|
||||
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);
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
|
||||
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
|
||||
@ -83,7 +105,7 @@ int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* colnames, ch
|
||||
char sql_query[SQLQUERY_MAX];
|
||||
int rc = 0;
|
||||
|
||||
snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s' AND active_id!='NULL'", 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", colnames, table, status);
|
||||
rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
|
||||
if (checksqlerr(rc, "prepare broken in gen_sql_select_stmt")){
|
||||
return -1;
|
||||
@ -190,6 +212,21 @@ int parse_args(int argc, char** argv, char** title, char** due_date){
|
||||
|
||||
}
|
||||
|
||||
int parse_args_v2(int argc, char** argv, char** title, char** new_title, char** new_due_date, char** new_project_tag){
|
||||
if ( argc > 2 ){
|
||||
*title = argv[2];
|
||||
}
|
||||
if ( argc > 3 ){
|
||||
*new_title = argv[3];
|
||||
}
|
||||
if ( argc > 4 ){
|
||||
*new_due_date = argv[4];
|
||||
}
|
||||
if ( argc > 5 ){
|
||||
*new_project_tag = argv[5];
|
||||
}
|
||||
}
|
||||
|
||||
// Get number of tasks from tasks table give status
|
||||
int get_num_rows(sqlite3 *db, char* table, char* status){
|
||||
sqlite3_stmt *out_stmt;
|
||||
@ -355,11 +392,11 @@ int add_new_task(sqlite3 *db, int argc, char** argv){
|
||||
int update_task_status(sqlite3 *db, int argc, char** argv){
|
||||
int rc = 0;
|
||||
char* table = "tasks";
|
||||
char* title;
|
||||
char* title_or_active_id;
|
||||
sqlite3_stmt* out_stmt;
|
||||
parse_args(argc, argv, &title, NULL);
|
||||
parse_args(argc, argv, &title_or_active_id, NULL);
|
||||
|
||||
if ( gen_sql_update_stmt(db, &out_stmt, table, argv[1], title) ){
|
||||
if ( gen_sql_update_stmt(db, &out_stmt, table, argv[1], title_or_active_id) ){
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -392,6 +429,30 @@ int complete_task(sqlite3 *db, int argc, char** argv){
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO this can definatly be made into one func with all the other updates
|
||||
int update_task(sqlite3 *db, int argc, char** argv){
|
||||
int rc = 0;
|
||||
char* table = "tasks";
|
||||
char* title;
|
||||
char* new_title;
|
||||
char* new_due_date;
|
||||
char* new_project_tag;
|
||||
sqlite3_stmt* out_stmt;
|
||||
|
||||
parse_args_v2(argc, argv, &title, &new_title, &new_due_date, &new_project_tag);
|
||||
if ( gen_sql_update_stmt_v2(db, &out_stmt, table, new_title, new_due_date, new_project_tag, title) ){
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
|
||||
return 0;
|
||||
}
|
||||
|
||||
checksqlerr(rc, "broken in complete_task");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int del_task(sqlite3 *db, int argc, char** argv){
|
||||
int rc = 0;
|
||||
char* table = "tasks";
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef DODO_H
|
||||
#define DODO_H
|
||||
|
||||
#define SQLQUERY_MAX 100
|
||||
#define SQLQUERY_MAX 400
|
||||
#define ARG_MAX 100
|
||||
#define NUM_TASK_LISTS 3
|
||||
#define FIXED_TITLE_WIDTH 21
|
||||
@ -53,7 +53,7 @@ int view_tasks(sqlite3 *db);
|
||||
int add_new_task(sqlite3 *db, int argc, char** argv);
|
||||
int update_task_status(sqlite3 *db, int argc, char** argv);
|
||||
int complete_task(sqlite3 *db, int argc, char** argv);
|
||||
int modify_task(sqlite3 *db, int argc, char** argv);
|
||||
int update_task(sqlite3 *db, int argc, char** argv);
|
||||
|
||||
|
||||
int del_task(sqlite3 *db, int argc, char** argv);
|
||||
|
@ -37,6 +37,9 @@ int main( int argc, char **argv ){
|
||||
else if (x_strcmp(argv[1], "view_all") == 0){
|
||||
rc = view_all(db);
|
||||
}
|
||||
else if (x_strcmp(argv[1], "update") == 0){
|
||||
rc = update_task(db,argc,argv);
|
||||
}
|
||||
}
|
||||
else{
|
||||
rc = view_tasks(db);
|
||||
|
Loading…
Reference in New Issue
Block a user