Since every command now has its own help, I decided to point the user to
[ctodo.git] / del.c
blob7df632d189060eeb0a17a88fc44774017f5dc096
1 #include <del.h>
2 #include <stdio.h>
3 #include <readline/readline.h>
4 #include <stdlib.h>
5 #include <string.h>
7 void del_help() {
8 printf(" Usage: del TODO#\n");
11 int del(sqlite3 *db, MArray tokens) {
12 char *query;
13 char *confirmation;
14 char *errmsg;
15 int todo;
16 int retVal;
17 int hasChildren = 0;
18 sqlite3_stmt *statement;
20 if (tokens->len == 2 && !strcmp(myarray_get(tokens, char *, 1), "help")) {
21 del_help();
22 return 0;
23 } else if (tokens->len != 2) {
24 printf("Invalid amount of arguments.\n");
25 del_help();
27 return 0;
30 if (sscanf(myarray_get(tokens, char *, 1), "%d", &todo) != 1) {
31 printf("TODO# must be a number\n");
32 return 0;
35 query = sqlite3_mprintf("SELECT COUNT(key) FROM todo WHERE parent = %d;", todo);
37 retVal = sqlite3_prepare_v2(db, query, -1, &statement, NULL);
38 sqlite3_free(query);
39 if (retVal != SQLITE_OK) {
40 printf("An error occurred while trying to get the data from the database.\n");
41 sqlite3_finalize(statement);
42 return 1;
45 while ((retVal = sqlite3_step(statement)) == SQLITE_ROW) {
46 hasChildren = sqlite3_column_int(statement, 0);
48 sqlite3_finalize(statement);
50 if (hasChildren) {
51 printf("This todo has children. Do you want to delete them too?\n");
52 printf("Saying no will make these children parentless (toplevel todo's).\n");
53 confirmation = readline("yes/no (default is no)? ");
55 if (confirmation != NULL && !strncmp(confirmation, "y", 1)) {
56 query = sqlite3_mprintf("DELETE FROM todo WHERE parent = %d;", todo);
57 sqlite3_exec(db, query, NULL, NULL, &errmsg);
58 sqlite3_free(errmsg);
59 sqlite3_free(query);
60 } else {
61 query = sqlite3_mprintf("UPDATE todo SET parent = NULL WHERE parent = %d;\n", todo);
62 sqlite3_exec(db, query, NULL, NULL, &errmsg);
63 sqlite3_free(errmsg);
64 sqlite3_free(query);
67 if (confirmation != NULL) free(confirmation);
70 query = sqlite3_mprintf("DELETE FROM todo WHERE key = %d;", todo);
71 sqlite3_exec(db, query, NULL, NULL, &errmsg);
72 sqlite3_free(errmsg);
73 sqlite3_free(query);
75 return 0;