Compare commits
4 Commits
e8e73da981
...
09492950b8
Author | SHA1 | Date | |
---|---|---|---|
09492950b8 | |||
586786ff6c | |||
22eadc8ad7 | |||
7d326f6057 |
112
src/dodo.c
112
src/dodo.c
@ -72,6 +72,36 @@ void display_heading(){
|
|||||||
printf("\n");
|
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){
|
int gen_sql_insert_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* colnames, char* values){
|
||||||
char sql_query[SQLQUERY_MAX];
|
char sql_query[SQLQUERY_MAX];
|
||||||
int rc = 0;
|
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")){
|
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@ -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
|
// due date passed as NULL if for delete
|
||||||
// TODO input validation for strings implement in strings!
|
// TODO input validation for strings implement in strings!
|
||||||
int parse_args(int argc, char** argv, char** title, char** due_date){
|
int parse_args(int argc, char** argv, char** title, char** due_date){
|
||||||
if ( argc > 1 ){
|
if ( argc > 2 ){
|
||||||
*title = argv[2];
|
*title = argv[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( argc > 2 ){
|
if ( argc > 3 ){
|
||||||
*due_date = argv[3];
|
*due_date = argv[3];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: the way this ensures that we are only passing in
|
int update_task_status(sqlite3 *db, int argc, char** argv){
|
||||||
// valid inputs is stupid and ugly FIX
|
|
||||||
int add_task(sqlite3 *db, int argc, char** argv){
|
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
char* table = "tasks";
|
char* table = "tasks";
|
||||||
char* title;
|
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* colnames = "(title, due_date)";
|
||||||
char values[100];
|
char values[100];
|
||||||
sqlite3_stmt* out_stmt;
|
sqlite3_stmt* out_stmt;
|
||||||
@ -225,13 +297,13 @@ int add_task(sqlite3 *db, int argc, char** argv){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
checksqlerr(rc, "broken in add_task");
|
checksqlerr(rc, "broken in add_new_task");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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){
|
int view_tasks(sqlite3 *db){
|
||||||
char* table = "tasks";
|
char* table = "tasks";
|
||||||
char* status = "today";
|
char* status = "today";
|
||||||
char* colnames = "title,due_date";
|
char* colnames = "title,due_date";
|
||||||
@ -267,18 +339,30 @@ int main( int argc, char **argv ){
|
|||||||
|
|
||||||
rc = opendb(&db, filename);
|
rc = opendb(&db, filename);
|
||||||
if ( argv[1] ){
|
if ( argv[1] ){
|
||||||
if (argv[1][0] == 'b'){
|
if (x_strcmp(argv[1], "view") == 0){
|
||||||
rc = view(db);
|
rc = view_tasks(db);
|
||||||
}
|
}
|
||||||
else if ( argv[1][0] == 'a'){
|
else if (x_strcmp(argv[1], "new") == 0){
|
||||||
rc = add_task(db,argc,argv);
|
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);
|
rc = view_all(db);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
rc = view(db);
|
rc = view_tasks(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
CREATE TABLE tasks(
|
CREATE TABLE tasks(
|
||||||
task_id INTEGER PRIMARY KEY,
|
task_id INTEGER PRIMARY KEY,
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL UNIQUE,
|
||||||
active_id INTEGER UNIQUE,
|
active_id INTEGER UNIQUE,
|
||||||
status TEXT NOT NULL DEFAULT 'backlog',
|
status TEXT NOT NULL DEFAULT 'backlog',
|
||||||
creation_date DATE DEFAULT (date('now')),
|
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(
|
CREATE TABLE entries(
|
||||||
entry_id INTEGER PRIMARY KEY,
|
entry_id INTEGER PRIMARY KEY,
|
||||||
task_id INTEGER NOT NULL REFERENCES tasks(task_id),
|
task_id INTEGER NOT NULL REFERENCES tasks(task_id),
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// return the length of string to the null terminator
|
||||||
int x_strlen(char* str){
|
int x_strlen(char* str){
|
||||||
char* ptr = str;
|
char* ptr = str;
|
||||||
while(*ptr != '\0'){
|
while(*ptr != '\0'){
|
||||||
@ -9,6 +10,8 @@ int x_strlen(char* str){
|
|||||||
return ptr - 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){
|
void x_strcpy(char* dest, char* src, const int size_dest){
|
||||||
char* dest_ptr = dest;
|
char* dest_ptr = dest;
|
||||||
char* src_ptr = src;
|
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* x_strconcat(char* str1, char* str2){
|
||||||
|
|
||||||
char* new_string;
|
char* new_string;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
int x_strlen(char*);
|
int x_strlen(char*);
|
||||||
void x_strcpy(char*, char*, const int);
|
void x_strcpy(char*, char*, const int);
|
||||||
|
int x_strcmp(char*, char*);
|
||||||
char* x_strconcat(char*, char*);
|
char* x_strconcat(char*, char*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user