Added initial imp of add_task

This commit is contained in:
xavi 2024-09-04 14:18:05 -07:00
parent 58893119c7
commit 9c46269868

View File

@ -12,6 +12,7 @@
int checksqlerr(int rc, char *errmsg){
if( rc!=SQLITE_OK ){
fprintf(stderr, "rc = %d\n", rc);
fprintf(stderr, "SQL error: %s\n", errmsg);
sqlite3_free(errmsg);
return -1;
@ -70,17 +71,25 @@ void display_heading(){
printf("\n");
}
// Generate the sql statement by giving the columns, table, and current task list wanted
// TODO make the stmt an arg pass and return an int
// TODO ME THINKS THIS IS THE NEXT THING TO DO BECAUSE BREAKING EVERYTHING
int gen_sql_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* colnames, char* table, char* status){
int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* colnames, char* values){
char sql_query[SQLQUERY_MAX];
int rc = 0;
snprintf(sql_query, SQLQUERY_MAX, "INSERT INTO %s %s VALUES %s", table, colnames, values);
rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
return -1;
}
}
// 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, char* colnames, char* table, char* status){
char sql_query[SQLQUERY_MAX];
int rc = 0;
//sqlite3_stmt *out_stmt;
snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s'", colnames, table, status);
rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
if (checksqlerr(rc, "prepare broken in gen_sql_stmt")){
if (checksqlerr(rc, "prepare broken in gen_sql_select_stmt")){
return -1;
}
@ -88,13 +97,12 @@ int gen_sql_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* colnames, char* tab
}
// Get number of tasks from tasks table give status
// TODO: Gotta make the error checking not broken
int get_num_rows(sqlite3 *db, char* table, char* status){
sqlite3_stmt *out_stmt;
int rc = 0;
char *col_name = "COUNT(*)";
if ( gen_sql_stmt(db, &out_stmt, col_name, table, status) == 1 ){
if ( gen_sql_select_stmt(db, &out_stmt, col_name, table, status) == 1 ){
return -1;
}
@ -105,7 +113,7 @@ int get_num_rows(sqlite3 *db, char* table, char* status){
return -1;
}
// Print columns
// Print with FIXED_WIDTH
int print_fixed_width(const unsigned char* str){
if (str){
printf("%-*.*s", FIXED_WIDTH, FIXED_WIDTH, str);
@ -114,6 +122,7 @@ int print_fixed_width(const unsigned char* str){
}
}
int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, char* status){
static int max_rows = 0;
int rc = 0;
@ -135,7 +144,7 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c
}
// Generate the sql statement by giving the columns, table, and current task list wanted
if ( gen_sql_stmt(db, &out_stmt, colnames, table, status) ){
if ( gen_sql_select_stmt(db, &out_stmt, colnames, table, status) ){
return -1;
}
// while there is still rows available
@ -169,6 +178,32 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c
return 0;
}
int add_task(sqlite3 *db){
int rc = 0;
char* table = "tasks";
char* status = "today";
// Format Col names like this (col1, col2, col3)
char* colnames = "(title, due_date)";
// Format values like this
// For first row For second row For third row
// (col1val, col2val, col3val), (col1val, col2val, col3val), (col1val, col2val, col3val)
char* values = "('weeeeeeep',2024-09-05)";
sqlite3_stmt* out_stmt;
if ( gen_sql_insert_stmt(db, &out_stmt, table, colnames, values) ){
return -1;
}
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
return 0;
}
checksqlerr(rc, "broken in add_task");
return 1;
}
// Print kanban table
// All lists with task name and due date
@ -208,7 +243,14 @@ int main( int argc, char **argv ){
char* filename = x_strconcat(home_dir, DB_PATH);
rc = opendb(&db, filename);
if ( argv[1] ){
if (argv[1][0] == 'a'){
rc = view(db);
}
else if ( argv[1][0] == 'c'){
rc = add_task(db);
}
}
puts("");
puts("");