Compare commits

...

2 Commits

View File

@ -59,7 +59,6 @@ int prepare_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks
char *sql_query;
int rc = 0;
int num_params = 0;
char test[SQLQUERY_MAX];
sql_query = malloc(SQLQUERY_MAX * sizeof(char));
@ -171,13 +170,28 @@ int bind_sql_update_stmt(filtered_tasks* task, int num_params, sqlite3_stmt* out
return 0;
}
int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){
char sql_query[SQLQUERY_MAX];
int prepare_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){
char *sql_query;
int rc = 0;
sql_query = malloc(SQLQUERY_MAX * sizeof(char));
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);
x_strcpy(sql_query,"DELETE FROM tasks WHERE ", SQLQUERY_MAX);
if ( task->title ){
sql_query = concat_with_mem_cleanup(sql_query, "title=? AND active_id IS NOT NULL");
}
else if ( task->active_id != -1 ){
sql_query = concat_with_mem_cleanup(sql_query, "active_id=?");
}
else{
free(sql_query);
return -1;
}
rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
free(sql_query);
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
return -1;
}
@ -185,13 +199,20 @@ int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* ta
return 0;
}
int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task, char* values){
char sql_query[SQLQUERY_MAX];
int bind_sql_delete_stmt(filtered_tasks* task, sqlite3_stmt* out_stmt){
int rc = 0;
const int param_pos = 1;
if ( task->title ){
rc = sqlite3_bind_text(out_stmt, param_pos, task->title, -1, SQLITE_STATIC);
}
else if ( task->active_id != -1 ){
rc = sqlite3_bind_int(out_stmt, param_pos, task->active_id);
}
else{
return -1;
}
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;
}
@ -199,20 +220,77 @@ int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* ta
return 0;
}
int prepare_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 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;
}
return 0;
}
int bind_sql_insert_stmt(filtered_tasks* task, sqlite3_stmt* out_stmt){
int rc = 0;
int param_pos = 1;
if ( task->title ){
rc = sqlite3_bind_text(out_stmt, param_pos, task->title, -1, SQLITE_STATIC);
}
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
return -1;
}
if ( task->due_date ){
param_pos = 2;
rc = sqlite3_bind_text(out_stmt, param_pos, task->due_date, -1, SQLITE_STATIC);
}
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
return -1;
}
return 0;
}
// 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, filtered_tasks* task){
int prepare_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task){
char sql_query[SQLQUERY_MAX];
int rc = 0;
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);
snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM tasks WHERE status=? AND active_id!='NULL' ORDER BY due_date NULLS LAST", task->selected_columns);
rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
if (checksqlerr(rc, "prepare broken in gen_sql_select_stmt")){
return -1;
}
return 0;
}
int bind_sql_select_stmt(filtered_tasks* task, sqlite3_stmt* out_stmt){
int rc = 0;
const int param_pos = 1;
if ( task->status ){
rc = sqlite3_bind_text(out_stmt, param_pos, task->status, -1, SQLITE_STATIC);
}
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
return -1;
}
return 0;
}
// TODO: I think this needs a bit of a refactor not sure how
int checksqlerr(int rc, char *errmsg){
if( rc!=SQLITE_OK ){
@ -342,7 +420,11 @@ int get_num_rows(sqlite3 *db, char* table, char* status){
sqlite3_stmt *out_stmt;
int rc = 0;
if ( gen_sql_select_stmt(db, &out_stmt, task) == 1 ){
if ( prepare_sql_select_stmt(db, &out_stmt, task) == 1 ){
return -1;
}
if ( ( rc = bind_sql_select_stmt(task, out_stmt) ) ){
return -1;
}
@ -378,7 +460,6 @@ int display_task_list(int start_col, sqlite3 *db, filtered_tasks* task){
sqlite3_stmt* out_stmt;
char* table = "tasks";
// Get the number of rows in current task list
num_rows = get_num_rows(db, table, task->status);
// Then keep track of the furthest down we go
@ -387,7 +468,11 @@ int display_task_list(int start_col, sqlite3 *db, filtered_tasks* task){
}
// Generate the sql statement by giving the columns, table, and current task list wanted
if ( gen_sql_select_stmt(db, &out_stmt, task) ){
if ( prepare_sql_select_stmt(db, &out_stmt, task) ){
return -1;
}
if ( bind_sql_select_stmt(task, out_stmt) ){
return -1;
}
@ -484,7 +569,6 @@ int view_tasks(sqlite3 *db){
}
int add_new_task(sqlite3 *db, int argc, char** argv){
int rc = 0;
char values[ARG_MAX];
sqlite3_stmt* out_stmt;
@ -498,16 +582,25 @@ int add_new_task(sqlite3 *db, int argc, char** argv){
if ( task->title != NULL ){
if ( task->due_date != NULL ){
task->selected_columns = "(title, due_date)";
snprintf(values, ARG_MAX, "('%s', '%s')", task->title, task->due_date);
}else{
snprintf(values, ARG_MAX, "(?, ?)");
}
else{
task->selected_columns = "(title)";
snprintf(values, ARG_MAX, "('%s')", task->title);
snprintf(values, ARG_MAX, "(?)");
}
}
if ( gen_sql_insert_stmt(db, &out_stmt, task, values) ){
else{
return -1;
}
if ( ( rc = prepare_sql_insert_stmt(db, &out_stmt, task, values) ) ){
return -1;
}
if ( ( rc = bind_sql_insert_stmt(task, out_stmt) ) ){
return -1;
}
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
return 0;
}
@ -519,6 +612,7 @@ int add_new_task(sqlite3 *db, int argc, char** argv){
int update_task_status(sqlite3 *db, int argc, char** argv){
int rc = 0;
int num_params = 0;
sqlite3_stmt* out_stmt;
filtered_tasks* task = malloc(sizeof(filtered_tasks));
@ -527,11 +621,16 @@ int update_task_status(sqlite3 *db, int argc, char** argv){
parse_args(argc, argv, task);
task->status = argv[1];
if ( ( rc = prepare_sql_update_stmt(db, &out_stmt, task) ) ){
if ( ( num_params = prepare_sql_update_stmt(db, &out_stmt, task) ) == -1 ){
free(task);
return -1;
}
if ( ( rc = bind_sql_update_stmt(task, num_params, out_stmt) ) ){
free(task);
return -1;
}
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
free(task);
@ -548,17 +647,24 @@ int update_task_status(sqlite3 *db, int argc, char** argv){
int complete_task(sqlite3 *db, int argc, char** argv){
int rc = 0;
int num_params = 0;
sqlite3_stmt* out_stmt;
filtered_tasks* task = malloc(sizeof(filtered_tasks));
init_filtered_tasks(task);
task->status = "complete";
parse_args(argc, argv, task);
if ( prepare_sql_update_stmt(db, &out_stmt, task) ){
if ( ( num_params = prepare_sql_update_stmt(db, &out_stmt, task) ) == -1 ){
free(task);
return -1;
}
if ( ( rc = bind_sql_update_stmt(task, num_params, out_stmt) ) ){
free(task);
return -1;
}
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
free(task);
@ -581,8 +687,6 @@ int update_task(sqlite3 *db, int argc, char** argv){
parse_args(argc, argv, task);
// TODO this can definitely be made into one func with all the other updates
// no v2s allowed
if ( ( num_params = prepare_sql_update_stmt(db, &out_stmt, task) ) == -1 ){
free(task);
@ -599,7 +703,7 @@ int update_task(sqlite3 *db, int argc, char** argv){
return 0;
}
checksqlerr(rc, "broken in complete_task");
checksqlerr(rc, "broken in update_task");
free(task);
return -1;
@ -615,7 +719,12 @@ int del_task(sqlite3 *db, int argc, char** argv){
parse_args(argc, argv, task);
if ( gen_sql_delete_stmt(db, &out_stmt, task) ){
if ( prepare_sql_delete_stmt(db, &out_stmt, task) ){
free(task);
return -1;
}
if ( ( rc = bind_sql_delete_stmt(task, out_stmt) ) ){
free(task);
return -1;
}