Compare commits

..

No commits in common. "22e71dd40a788e4dc1de3cfa20d752f49d1eb1a5" and "61e9ee70476836110eb5a18be0f08dae4f14d761" have entirely different histories.

2 changed files with 72 additions and 116 deletions

View File

@ -4,12 +4,6 @@
#include "x_string.h" #include "x_string.h"
#include "x_curses.h" #include "x_curses.h"
#define SQLQUERY_MAX 100
#define TODAY_COL_START -1
#define BACKLOG_COL_START 33
#define BLOCKED_COL_START 66
#define FIXED_WIDTH 19
int checksqlerr(int rc, char *errmsg){ 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);
@ -45,9 +39,8 @@ int callback(void *NotUsed, int argc, char **argv, char **azColName){
} }
// FOR DEBUG // FOR DEBUG
int view_all(sqlite3 *db){ int view_all(sqlite3 *db, char *errmsg){
int rc = 0; int rc = 0;
char* errmsg;
//rc = sqlite3_exec(db, "SELECT * FROM all_info;", print_select, 0, &errmsg); //rc = sqlite3_exec(db, "SELECT * FROM all_info;", print_select, 0, &errmsg);
rc = sqlite3_exec(db, "SELECT * FROM tasks;", callback, 0, &errmsg); rc = sqlite3_exec(db, "SELECT * FROM tasks;", callback, 0, &errmsg);
@ -76,40 +69,30 @@ void view_heading(){
printf("\n"); printf("\n");
} }
// Generate the sql statement by giving the columns, table, and current task list wanted int print_select_test(sqlite3 *db, sqlite3_stmt* out_stmt, char* colnames, char* table, char* status){
// TODO make the stmt an arg pass and return an int char *sql_query = "SELECT ? FROM ? WHERE status = ?";
// 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){
char sql_query[SQLQUERY_MAX];
int rc = 0; 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); rc = sqlite3_prepare_v2(db, sql_query, -1, &out_stmt, NULL);
if (checksqlerr(rc, "prepare broken in gen_sql_stmt")){ rc = sqlite3_bind_text(out_stmt, 1, colnames, -1, SQLITE_STATIC);
return NULL; rc = sqlite3_bind_text(out_stmt, 2, table, -1, SQLITE_STATIC);
} rc = sqlite3_bind_text(out_stmt, 3, status, -1, SQLITE_STATIC);
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 *errmsg){
char *sql_query = "SELECT COUNT(*) FROM tasks WHERE 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(*)";
if ( ( out_stmt = gen_sql_stmt(db, col_name, table, status) ) == NULL ){ rc = sqlite3_prepare_v2(db, sql_query, -1, &out_stmt, NULL);
return 1; checksqlerr(rc, errmsg);
}
//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); rc = sqlite3_bind_text(out_stmt, 1, status, -1, SQLITE_STATIC);
checksqlerr(rc, "bind broken in get_num_rows"); checksqlerr(rc, errmsg);
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);
@ -118,116 +101,90 @@ int get_num_rows(sqlite3 *db, char* table, char* status){
return 1; return 1;
} }
// Print columns // TODO error checking needs done
int print_fixed_width(const unsigned char* str){ //int print_status_column(sqlite3 *db, char* table, char* status){
if (str){ // int rc = 0;
printf("%-*.*s", FIXED_WIDTH, FIXED_WIDTH, str); // sqlite3_stmt *out_stmt;
}else{ // char *sql_query = "SELECT title,due_date ? tasks WHERE status='?'";
printf("%-*.*s", FIXED_WIDTH, FIXED_WIDTH, ""); //
} // rc = sqlite3_prepare_v2(db, sql_query, -1, out_stmt, NULL);
} //
// rc = sqlite3_bind_text(out_stmt, 1, table, -1, SQLITE_STATIC);
int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, char* status){ // rc = sqlite3_bind_text(out_stmt, 2, status, -1, SQLITE_STATIC);
static int max_rows = 0; //
int rc = 0; //
int i = 0; //}
int num_rows = 0;
int num_cols = 0;
char* errmsg;
const unsigned char* col_val;
sqlite3_stmt* out_stmt;
// Start col for the current task list
X_goright(start_col);
// Get the number of rows in current task list
num_rows = get_num_rows(db, table, status);
// Then keep track of the furthest down we go
if (num_rows > max_rows){
max_rows = num_rows;
}
// 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 ){
return 1;
}
// while there is still rows available
while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){
// for each column print the column
num_cols = sqlite3_column_count(out_stmt);
for (i = 0; i < num_cols; i++){
col_val = sqlite3_column_text(out_stmt, i);
print_fixed_width(col_val);
}
// move down one and over to the start of the current task column
printf("\n");
X_goright(start_col);
}
// if while loop broke and rc returned an error
if ( rc == SQLITE_ERROR ){
X_godown(max_rows);
checksqlerr(rc, "step broken in display_task_list");
return 1;
}
// Once the task list is completely printed
// reset to the top of the task lists
X_goup(num_rows);
// if it is the last task list move down one past the longest list
if ( status == "blocked"){
X_godown(max_rows);
}
return 0;
}
// Print kanban table // Print kanban table
// All lists with task name and due date // All lists with task name and due date
int view(sqlite3 *db, char *errmsg){
// TODO just gotta use snprintf because can't bind column or table names int rc = 0;
int view(sqlite3 *db){ sqlite3_stmt* out_stmt;
int numrows = 0;
int* currcol = malloc(sizeof(int));
char* table = "tasks"; char* table = "tasks";
char* status = "today"; char* status = "today";
char* colnames = "title,creation_date"; char* colnames = "*";
char *sql_query = "SELECT title,due_date ? tasks WHERE status='?'";
// Print Heading // Print Heading
view_heading(); view_heading();
// Print "today" tasks
if ( display_task_list(TODAY_COL_START, db, colnames, table, status) ){
return 1;
}
// Print "backlog" tasks // Print "today" tasks
status = "backlog"; print_select_test(db, out_stmt, colnames, table, status);
if ( display_task_list(BACKLOG_COL_START, db, colnames, table, status) ){ while ( rc = sqlite3_step(out_stmt) == SQLITE_ROW ){
return 1; return sqlite3_column_int(out_stmt, 0);
} }
//*currcol = -1;
//numrows = get_num_rows(db, table, status, errmsg);
//rc = sqlite3_exec(db, sql_query, print_select, (void *)currcol, &errmsg);
//checksqlerr(rc, errmsg);
//X_goup(numrows);
//// Print "backlog" tasks
//*currcol = 25;
//status = "backlog";
//sql_query = "SELECT title,due_date FROM tasks WHERE status='backlog'";
//numrows = get_num_rows(db, table, status, errmsg);
//rc = sqlite3_exec(db, sql_query, print_select, (void *)currcol, &errmsg);
//checksqlerr(rc, errmsg);
//X_goup(numrows);
//X_goright(50);
// Print "blocked" tasks // Print "blocked" tasks
status = "blocked"; //status = "blocked";
if ( display_task_list(BLOCKED_COL_START, db, colnames, table, status) ){ //sql_query = "SELECT title,due_date FROM tasks WHERE status='blocked'";
return 1; //numrows = get_num_rows(db, table, status, errmsg);
} //rc = sqlite3_exec(db, sql_query, print_select, 0, &errmsg);
//checksqlerr(rc, errmsg);
//X_godown(2);
//printf("%d\n", numrows);
//get_num_rows(db, errmsg);
return 0;
} }
int main( int argc, char **argv ){ int main( int argc, char **argv ){
sqlite3 *db; sqlite3 *db;
int rc = 0; int rc = 0;
char *errmsg;
char* home_dir = getenv("HOME"); char* home_dir = getenv("HOME");
char* filename = x_strconcat(home_dir, DB_PATH); char* filename = x_strconcat(home_dir, DB_PATH);
rc = opendb(&db, filename); puts(filename);
rc = view(db);
puts(filename);
rc = opendb(&db, filename);
rc = view(db, errmsg);
puts(""); puts("");
puts(""); puts("");
puts(""); puts("");
puts(""); puts("");
rc = view_all(db); rc = view_all(db, errmsg);
sqlite3_close(db); sqlite3_close(db);

View File

@ -7,7 +7,6 @@
#define X_CYN "\x1B[36m" #define X_CYN "\x1B[36m"
#define X_goup(y) printf("\x1B[%dF", y) #define X_goup(y) printf("\x1B[%dF", y)
#define X_godown(y) printf("\x1B[%dE", y) #define X_godown(y) printf("\x1B[%dE", y)
#define X_goupnocr(y) printf("\x1B[%dA", y)
#define X_godownnocr(y) printf("\x1B[%dB", y) #define X_godownnocr(y) printf("\x1B[%dB", y)
#define X_goright(x) printf("\x1B[%dC", x) #define X_goright(x) printf("\x1B[%dC", x)