Compare commits

..

4 Commits

Author SHA1 Message Date
09492950b8 Added active_id increment in schema 2024-09-08 16:33:54 -07:00
586786ff6c Added update_task_status func 2024-09-08 15:01:31 -07:00
22eadc8ad7 Added updated x_string 2024-09-08 15:01:18 -07:00
7d326f6057 Added del_task function 2024-09-08 13:56:40 -07:00
4 changed files with 139 additions and 15 deletions

View File

@ -72,6 +72,36 @@ void display_heading(){
printf("\n");
}
int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* status, char* title){
char sql_query[SQLQUERY_MAX];
int rc = 0;
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET status='%s' WHERE title='%s'", table, status, title);
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 gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* title){
char sql_query[SQLQUERY_MAX];
int rc = 0;
snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM %s WHERE title='%s'", table, title);
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 gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* colnames, char* values){
char sql_query[SQLQUERY_MAX];
int rc = 0;
@ -81,6 +111,8 @@ int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char*
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
@ -188,23 +220,63 @@ int display_task_list(int start_col, sqlite3 *db, char* colnames, char* table, c
// due date passed as NULL if for delete
// TODO input validation for strings implement in strings!
int parse_args(int argc, char** argv, char** title, char** due_date){
if ( argc > 1 ){
if ( argc > 2 ){
*title = argv[2];
}
if ( argc > 2 ){
if ( argc > 3 ){
*due_date = argv[3];
}
}
// TODO: the way this ensures that we are only passing in
// valid inputs is stupid and ugly FIX
int add_task(sqlite3 *db, int argc, char** argv){
int update_task_status(sqlite3 *db, int argc, char** argv){
int rc = 0;
char* table = "tasks";
char* title;
char* due_date;
sqlite3_stmt* out_stmt;
parse_args(argc, argv, &title, NULL);
if ( gen_sql_update_stmt(db, &out_stmt, table, argv[1], title) ){
return -1;
}
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
return 0;
}
checksqlerr(rc, "broken in update_task_status");
return 1;
}
int del_task(sqlite3 *db, int argc, char** argv){
int rc = 0;
char* table = "tasks";
char* title;
sqlite3_stmt* out_stmt;
parse_args(argc, argv, &title, NULL);
if ( gen_sql_delete_stmt(db, &out_stmt, table, title) ){
return -1;
}
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
return 0;
}
checksqlerr(rc, "broken in del_task");
return 1;
}
// TODO: the way this ensures that we are only passing in
// valid inputs is stupid and ugly FIX
int add_new_task(sqlite3 *db, int argc, char** argv){
int rc = 0;
char* table = "tasks";
char* title = NULL;
char* due_date = NULL;
char* colnames = "(title, due_date)";
char values[100];
sqlite3_stmt* out_stmt;
@ -225,13 +297,13 @@ int add_task(sqlite3 *db, int argc, char** argv){
return 0;
}
checksqlerr(rc, "broken in add_task");
checksqlerr(rc, "broken in add_new_task");
return 1;
}
// Print kanban table
// All lists with task name and due date
int view(sqlite3 *db){
int view_tasks(sqlite3 *db){
char* table = "tasks";
char* status = "today";
char* colnames = "title,due_date";
@ -267,18 +339,30 @@ int main( int argc, char **argv ){
rc = opendb(&db, filename);
if ( argv[1] ){
if (argv[1][0] == 'b'){
rc = view(db);
if (x_strcmp(argv[1], "view") == 0){
rc = view_tasks(db);
}
else if ( argv[1][0] == 'a'){
rc = add_task(db,argc,argv);
else if (x_strcmp(argv[1], "new") == 0){
rc = add_new_task(db,argc,argv);
}
else if ( argv[1][0] == 'v'){
else if (x_strcmp(argv[1], "del") == 0){
rc = del_task(db,argc,argv);
}
else if (x_strcmp(argv[1], "today") == 0){
rc = update_task_status(db,argc,argv);
}
else if (x_strcmp(argv[1], "blocked") == 0){
rc = update_task_status(db,argc,argv);
}
else if (x_strcmp(argv[1], "backlog") == 0){
rc = update_task_status(db,argc,argv);
}
else if (x_strcmp(argv[1], "view_all") == 0){
rc = view_all(db);
}
}
else{
rc = view(db);
rc = view_tasks(db);
}
sqlite3_close(db);

View File

@ -1,6 +1,6 @@
CREATE TABLE tasks(
task_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
title TEXT NOT NULL UNIQUE,
active_id INTEGER UNIQUE,
status TEXT NOT NULL DEFAULT 'backlog',
creation_date DATE DEFAULT (date('now')),
@ -11,6 +11,17 @@ CREATE TABLE tasks(
)
);
-- Basically when we add a new task the active id is set
-- but when a task is done we want it to be able to be null so we need to increment
-- COALESCE is a cool function that returns the first non-null argument which takes care of empty
-- active task list
CREATE TRIGGER autoinc_active_id
AFTER INSERT ON tasks
BEGIN
UPDATE tasks SET active_id = (SELECT COALESCE((SELECT MAX(active_id) FROM tasks) + 1, 1)) WHERE task_id=NEW.task_id;
END;
CREATE TABLE entries(
entry_id INTEGER PRIMARY KEY,
task_id INTEGER NOT NULL REFERENCES tasks(task_id),

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
// return the length of string to the null terminator
int x_strlen(char* str){
char* ptr = str;
while(*ptr != '\0'){
@ -9,6 +10,8 @@ int x_strlen(char* str){
return ptr - str;
}
// copy one string to another destination
// must include the size of the destination
void x_strcpy(char* dest, char* src, const int size_dest){
char* dest_ptr = dest;
char* src_ptr = src;
@ -26,7 +29,32 @@ void x_strcpy(char* dest, char* src, const int size_dest){
}
}
// compare two strings
// return 0 if equal to eachother
// return -1 if not
int x_strcmp (char* str1, char* str2){
char* ptr1 = str1;
char* ptr2 = str2;
while(*ptr1 != '\0' && *ptr2 != '\0'){
if(*ptr1 != *ptr2){
return -1;
}
ptr1++;
ptr2++;
}
// if havent reached the end of one of the strings
if (*ptr1 != '\0' || *ptr2 != '\0'){
return -1;
}
return 0;
}
// concatinate two strings
char* x_strconcat(char* str1, char* str2){
char* new_string;

View File

@ -3,6 +3,7 @@
int x_strlen(char*);
void x_strcpy(char*, char*, const int);
int x_strcmp(char*, char*);
char* x_strconcat(char*, char*);
#endif