Compare commits

...

3 Commits

View File

@ -14,7 +14,7 @@ int checksqlerr(int rc, char *errmsg){
if( rc!=SQLITE_OK ){ if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", errmsg); fprintf(stderr, "SQL error: %s\n", errmsg);
sqlite3_free(errmsg); sqlite3_free(errmsg);
return 1; return -1;
} }
return 0; return 0;
} }
@ -56,20 +56,14 @@ int view_all(sqlite3 *db){
return rc; return rc;
} }
int print_select(void *passed_col, int argc, char **argv, char **azColName){ // Print Heading TODO: obvi make this better
int *currcol = (int *)passed_col; void display_heading(){
int i = 0; printf("\n");
X_goright(*currcol); printf("%s Today %s", X_BOLD, X_RST);
for(i=0; i<argc; i++){ printf(" %s Backlog %s", X_BOLD, X_RST);
printf("%s ", argv[i] ? argv[i] : "NULL"); printf(" %s Blocked %s", X_BOLD, X_RST);
}
printf("\n"); printf("\n");
return 0;
}
// Print Heading TODO: obvi make this better
void view_heading(){
printf("%s%stitle due date %s", X_BOLD, X_UNDL, X_RST); printf("%s%stitle due date %s", X_BOLD, X_UNDL, X_RST);
printf(" %s%stitle due date %s", X_BOLD, X_UNDL, X_RST); printf(" %s%stitle due date %s", X_BOLD, X_UNDL, X_RST);
printf(" %s%stitle due date %s", X_BOLD, X_UNDL, X_RST); printf(" %s%stitle due date %s", X_BOLD, X_UNDL, X_RST);
@ -79,43 +73,36 @@ void view_heading(){
// Generate the sql statement by giving the columns, table, and current task list wanted // 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 make the stmt an arg pass and return an int
// TODO ME THINKS THIS IS THE NEXT THING TO DO BECAUSE BREAKING EVERYTHING // TODO ME THINKS THIS IS THE NEXT THING TO DO BECAUSE BREAKING EVERYTHING
sqlite3_stmt *gen_sql_stmt(sqlite3 *db, char* colnames, char* table, char* status){ int gen_sql_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* colnames, char* table, char* status){
char sql_query[SQLQUERY_MAX]; char sql_query[SQLQUERY_MAX];
int rc = 0; int rc = 0;
sqlite3_stmt *out_stmt; //sqlite3_stmt *out_stmt;
snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s'", colnames, table, status); 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); 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_stmt")){
return NULL; return -1;
} }
return out_stmt; return 0;
} }
// Get number of tasks from tasks table give status // Get number of tasks from tasks table give status
// TODO: Gotta make the error checking not broken // TODO: Gotta make the error checking not broken
int get_num_rows(sqlite3 *db, char* table, char* status){ int get_num_rows(sqlite3 *db, char* table, char* status){
char *sql_query = "SELECT COUNT(*) FROM tasks WHERE status = ?";
sqlite3_stmt *out_stmt; sqlite3_stmt *out_stmt;
int rc = 0; int rc = 0;
char *col_name = "COUNT(*)"; char *col_name = "COUNT(*)";
if ( ( out_stmt = gen_sql_stmt(db, col_name, table, status) ) == NULL ){ if ( gen_sql_stmt(db, &out_stmt, col_name, table, status) == 1 ){
return 1; return -1;
} }
//rc = sqlite3_prepare_v2(db, sql_query, -1, &out_stmt, NULL);
//checksqlerr(rc, "prepare broken in get_num_rows");
rc = sqlite3_bind_text(out_stmt, 1, status, -1, SQLITE_STATIC);
checksqlerr(rc, "bind broken in get_num_rows");
while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){ while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){
return sqlite3_column_int(out_stmt, 0); return sqlite3_column_int(out_stmt, 0);
} }
return 1; return -1;
} }
// Print columns // Print columns
@ -148,8 +135,8 @@ 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 // Generate the sql statement by giving the columns, table, and current task list wanted
if ( ( out_stmt = gen_sql_stmt(db, colnames, table, status) ) == NULL ){ if ( gen_sql_stmt(db, &out_stmt, colnames, table, status) ){
return 1; return -1;
} }
// while there is still rows available // while there is still rows available
while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){ while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){
@ -168,7 +155,7 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c
if ( rc == SQLITE_ERROR ){ if ( rc == SQLITE_ERROR ){
X_godown(max_rows); X_godown(max_rows);
checksqlerr(rc, "step broken in display_task_list"); checksqlerr(rc, "step broken in display_task_list");
return 1; return -1;
} }
// Once the task list is completely printed // Once the task list is completely printed
@ -192,23 +179,23 @@ int view(sqlite3 *db){
char* colnames = "title,creation_date"; char* colnames = "title,creation_date";
// Print Heading // Print Heading
view_heading(); display_heading();
// Print "today" tasks // Print "today" tasks
if ( display_task_list(TODAY_COL_START, db, colnames, table, status) ){ if ( display_task_list(TODAY_COL_START, db, colnames, table, status) ){
return 1; return -1;
} }
// Print "backlog" tasks // Print "backlog" tasks
status = "backlog"; status = "backlog";
if ( display_task_list(BACKLOG_COL_START, db, colnames, table, status) ){ if ( display_task_list(BACKLOG_COL_START, db, colnames, table, status) ){
return 1; return -1;
} }
// Print "blocked" tasks // Print "blocked" tasks
status = "blocked"; status = "blocked";
if ( display_task_list(BLOCKED_COL_START, db, colnames, table, status) ){ if ( display_task_list(BLOCKED_COL_START, db, colnames, table, status) ){
return 1; return -1;
} }
return 0; return 0;