Modified gen_sql_stmts functions to split to prepare and bind

This commit is contained in:
xavi 2024-09-22 21:16:31 -07:00
parent c9fcef001b
commit b24aa19bac

View File

@ -201,7 +201,7 @@ int prepare_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks
int bind_sql_delete_stmt(filtered_tasks* task, sqlite3_stmt* out_stmt){
int rc = 0;
int param_pos = 1;
const int param_pos = 1;
if ( task->title ){
rc = sqlite3_bind_text(out_stmt, param_pos, task->title, -1, SQLITE_STATIC);
@ -221,13 +221,13 @@ int bind_sql_delete_stmt(filtered_tasks* task, sqlite3_stmt* out_stmt){
}
int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* task, char* values){
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);
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;
}
@ -235,20 +235,62 @@ int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, filtered_tasks* ta
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 ){
@ -378,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;
}
@ -414,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
@ -423,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;
}
@ -520,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;
@ -534,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;
}
@ -590,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);
@ -623,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);