Compare commits
3 Commits
7bc91f34e6
...
d12c968b4a
Author | SHA1 | Date | |
---|---|---|---|
d12c968b4a | |||
de35e45daf | |||
f3be9dc13c |
89
src/dodo.c
89
src/dodo.c
@ -28,20 +28,21 @@ int view_all(sqlite3 *db){
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int gen_sql_update_stmt_v2(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* new_title, char* new_due_date, char* new_project_tag, char* title_or_active_id){
|
||||||
// sql generators TODO: might be able to boil this down to 1 func
|
|
||||||
int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* status, char* title){
|
|
||||||
char sql_query[SQLQUERY_MAX];
|
char sql_query[SQLQUERY_MAX];
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
if (status != NULL){
|
if (new_title != NULL && new_due_date != NULL){
|
||||||
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET status='%s' WHERE title='%s'", table, status, title);
|
//snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s', due_date='%s', project_tag='%s' WHERE title='%s' OR active_id='%s'", table, status, title_or_active_id, title_or_active_id);
|
||||||
|
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s', due_date='%s' WHERE title='%s' OR active_id='%s'", table, new_title, new_due_date, title_or_active_id, title_or_active_id);
|
||||||
|
}
|
||||||
|
else if (new_title != NULL){
|
||||||
|
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET title='%s' WHERE title='%s' OR active_id='%s'", table, new_title, title_or_active_id, title_or_active_id);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET active_id='NULL',status='complete' WHERE title='%s'", table, title);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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_insert_stmt")){
|
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
|
||||||
return -1;
|
return -1;
|
||||||
@ -51,11 +52,32 @@ int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char*
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int gen_sql_delete_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* title){
|
// sql generators TODO: might be able to boil this down to 1 func
|
||||||
|
int gen_sql_update_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* table, char* status, char* title_or_active_id){
|
||||||
char sql_query[SQLQUERY_MAX];
|
char sql_query[SQLQUERY_MAX];
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM %s WHERE title='%s'", table, title);
|
if (status != NULL){
|
||||||
|
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET status='%s' WHERE title='%s' OR active_id='%s'", table, status, title_or_active_id, title_or_active_id);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
snprintf(sql_query, SQLQUERY_MAX, "UPDATE %s SET active_id='NULL',status='complete' WHERE title='%s' OR active_id='%s'", table, title_or_active_id, title_or_active_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
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_or_active_id){
|
||||||
|
char sql_query[SQLQUERY_MAX];
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
|
snprintf(sql_query, SQLQUERY_MAX, "DELETE FROM %s WHERE title='%s' OR active_id='%s'", table, title_or_active_id, title_or_active_id);
|
||||||
|
|
||||||
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_insert_stmt")){
|
if (checksqlerr(rc, "prepare broken in gen_sql_insert_stmt")){
|
||||||
@ -83,7 +105,7 @@ int gen_sql_select_stmt(sqlite3 *db, sqlite3_stmt** out_stmt, char* colnames, ch
|
|||||||
char sql_query[SQLQUERY_MAX];
|
char sql_query[SQLQUERY_MAX];
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s' AND active_id!='NULL'", colnames, table, status);
|
snprintf(sql_query, SQLQUERY_MAX, "SELECT %s FROM %s WHERE status='%s' AND active_id!='NULL' ORDER BY due_date NULLS LAST", 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_select_stmt")){
|
if (checksqlerr(rc, "prepare broken in gen_sql_select_stmt")){
|
||||||
return -1;
|
return -1;
|
||||||
@ -190,6 +212,21 @@ int parse_args(int argc, char** argv, char** title, char** due_date){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int parse_args_v2(int argc, char** argv, char** title, char** new_title, char** new_due_date, char** new_project_tag){
|
||||||
|
if ( argc > 2 ){
|
||||||
|
*title = argv[2];
|
||||||
|
}
|
||||||
|
if ( argc > 3 ){
|
||||||
|
*new_title = argv[3];
|
||||||
|
}
|
||||||
|
if ( argc > 4 ){
|
||||||
|
*new_due_date = argv[4];
|
||||||
|
}
|
||||||
|
if ( argc > 5 ){
|
||||||
|
*new_project_tag = argv[5];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get number of tasks from tasks table give status
|
// Get number of tasks from tasks table give status
|
||||||
int get_num_rows(sqlite3 *db, char* table, char* status){
|
int get_num_rows(sqlite3 *db, char* table, char* status){
|
||||||
sqlite3_stmt *out_stmt;
|
sqlite3_stmt *out_stmt;
|
||||||
@ -210,7 +247,7 @@ int get_num_rows(sqlite3 *db, char* table, char* status){
|
|||||||
// Print with FIXED_WIDTH
|
// Print with FIXED_WIDTH
|
||||||
int print_fixed_width(const unsigned char* str, int width){
|
int print_fixed_width(const unsigned char* str, int width){
|
||||||
if (str){
|
if (str){
|
||||||
printf("%-*.*s", width, width, str);
|
printf("%-*.*s", width, width - FIXED_WHITESPACE, str);
|
||||||
}else{
|
}else{
|
||||||
printf("%-*.*s", width, width, "");
|
printf("%-*.*s", width, width, "");
|
||||||
}
|
}
|
||||||
@ -355,11 +392,11 @@ int add_new_task(sqlite3 *db, int argc, char** argv){
|
|||||||
int update_task_status(sqlite3 *db, int argc, char** argv){
|
int update_task_status(sqlite3 *db, int argc, char** argv){
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
char* table = "tasks";
|
char* table = "tasks";
|
||||||
char* title;
|
char* title_or_active_id;
|
||||||
sqlite3_stmt* out_stmt;
|
sqlite3_stmt* out_stmt;
|
||||||
parse_args(argc, argv, &title, NULL);
|
parse_args(argc, argv, &title_or_active_id, NULL);
|
||||||
|
|
||||||
if ( gen_sql_update_stmt(db, &out_stmt, table, argv[1], title) ){
|
if ( gen_sql_update_stmt(db, &out_stmt, table, argv[1], title_or_active_id) ){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,6 +429,30 @@ int complete_task(sqlite3 *db, int argc, char** argv){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO this can definatly be made into one func with all the other updates
|
||||||
|
int update_task(sqlite3 *db, int argc, char** argv){
|
||||||
|
int rc = 0;
|
||||||
|
char* table = "tasks";
|
||||||
|
char* title;
|
||||||
|
char* new_title;
|
||||||
|
char* new_due_date;
|
||||||
|
char* new_project_tag;
|
||||||
|
sqlite3_stmt* out_stmt;
|
||||||
|
|
||||||
|
parse_args_v2(argc, argv, &title, &new_title, &new_due_date, &new_project_tag);
|
||||||
|
if ( gen_sql_update_stmt_v2(db, &out_stmt, table, new_title, new_due_date, new_project_tag, title) ){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ( rc = sqlite3_step(out_stmt) ) == SQLITE_DONE){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
checksqlerr(rc, "broken in complete_task");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int del_task(sqlite3 *db, int argc, char** argv){
|
int del_task(sqlite3 *db, int argc, char** argv){
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
char* table = "tasks";
|
char* table = "tasks";
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
#ifndef DODO_H
|
#ifndef DODO_H
|
||||||
#define DODO_H
|
#define DODO_H
|
||||||
|
|
||||||
#define SQLQUERY_MAX 100
|
#define SQLQUERY_MAX 400
|
||||||
#define ARG_MAX 100
|
#define ARG_MAX 100
|
||||||
#define NUM_TASK_LISTS 3
|
#define NUM_TASK_LISTS 3
|
||||||
#define FIXED_TITLE_WIDTH 19
|
#define FIXED_TITLE_WIDTH 21
|
||||||
#define FIXED_DATE_WIDTH 14
|
#define FIXED_DATE_WIDTH 12
|
||||||
#define FIXED_ID_WIDTH 4
|
#define FIXED_ID_WIDTH 4
|
||||||
#define FIXED_COLUMN_WIDTH (FIXED_ID_WIDTH + FIXED_TITLE_WIDTH +FIXED_DATE_WIDTH)
|
#define FIXED_COLUMN_WIDTH (FIXED_ID_WIDTH + FIXED_TITLE_WIDTH +FIXED_DATE_WIDTH)
|
||||||
|
#define FIXED_WHITESPACE 2
|
||||||
#define FIXED_MAX_WIDTH (FIXED_COLUMN_WIDTH)
|
#define FIXED_MAX_WIDTH (FIXED_COLUMN_WIDTH)
|
||||||
#define TODAY_COL_START -1
|
#define TODAY_COL_START -1
|
||||||
#define BACKLOG_COL_START (FIXED_COLUMN_WIDTH + 1)
|
#define BACKLOG_COL_START (FIXED_COLUMN_WIDTH + 1)
|
||||||
@ -52,7 +53,7 @@ int view_tasks(sqlite3 *db);
|
|||||||
int add_new_task(sqlite3 *db, int argc, char** argv);
|
int add_new_task(sqlite3 *db, int argc, char** argv);
|
||||||
int update_task_status(sqlite3 *db, int argc, char** argv);
|
int update_task_status(sqlite3 *db, int argc, char** argv);
|
||||||
int complete_task(sqlite3 *db, int argc, char** argv);
|
int complete_task(sqlite3 *db, int argc, char** argv);
|
||||||
int modify_task(sqlite3 *db, int argc, char** argv);
|
int update_task(sqlite3 *db, int argc, char** argv);
|
||||||
|
|
||||||
|
|
||||||
int del_task(sqlite3 *db, int argc, char** argv);
|
int del_task(sqlite3 *db, int argc, char** argv);
|
||||||
|
@ -37,6 +37,9 @@ int main( int argc, char **argv ){
|
|||||||
else if (x_strcmp(argv[1], "view_all") == 0){
|
else if (x_strcmp(argv[1], "view_all") == 0){
|
||||||
rc = view_all(db);
|
rc = view_all(db);
|
||||||
}
|
}
|
||||||
|
else if (x_strcmp(argv[1], "update") == 0){
|
||||||
|
rc = update_task(db,argc,argv);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
rc = view_tasks(db);
|
rc = view_tasks(db);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
// printf Formating
|
// printf Formating
|
||||||
#define X_RED "\x1B[31m"
|
#define X_RED "\x1B[31m"
|
||||||
@ -14,7 +15,7 @@
|
|||||||
|
|
||||||
|
|
||||||
// SET THE NUMBER OF TESTS
|
// SET THE NUMBER OF TESTS
|
||||||
#define NUM_TESTS 5
|
#define NUM_TESTS 6
|
||||||
|
|
||||||
// DEFINE REUSABLE TEST SETUPS
|
// DEFINE REUSABLE TEST SETUPS
|
||||||
int set_up_db(sqlite3 **db){
|
int set_up_db(sqlite3 **db){
|
||||||
@ -99,11 +100,40 @@ int test_add_then_update_task_status_then_delete(){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int test_display_task(){
|
||||||
|
printf("%s... ", __func__);
|
||||||
|
FILE *fd1;
|
||||||
|
FILE *stdout_org;
|
||||||
|
sqlite3 *db;
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
|
if ( (rc = set_up_db(&db)) ){
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout_org = stdout;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
fd1 = fopen("/dev/null", "w");
|
||||||
|
if(fd1 == NULL){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout = fd1;
|
||||||
|
if ( (rc = view_tasks(db)) ){
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout = stdout_org;
|
||||||
|
fclose(fd1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int test_update_task(){
|
int test_update_task(){
|
||||||
printf("%s... ", __func__);
|
printf("%s... ", __func__);
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
sqlite3 *db;
|
sqlite3 *db;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DUMMY TEST
|
// DUMMY TEST
|
||||||
@ -142,6 +172,7 @@ int main(){
|
|||||||
tests[2] = test_del_task;
|
tests[2] = test_del_task;
|
||||||
tests[3] = test_add_new_task_with_date_and_delete;
|
tests[3] = test_add_new_task_with_date_and_delete;
|
||||||
tests[4] = test_add_then_update_task_status_then_delete;
|
tests[4] = test_add_then_update_task_status_then_delete;
|
||||||
|
tests[5] = test_display_task;
|
||||||
|
|
||||||
|
|
||||||
// END OF PASSING TESTS
|
// END OF PASSING TESTS
|
||||||
|
Loading…
Reference in New Issue
Block a user