3 #include <readline/readline.h>
8 printf(" Usage: del TODO#\n");
11 int del(sqlite3
*db
, MArray tokens
) {
18 sqlite3_stmt
*statement
;
20 if (tokens
->len
== 2 && !strcmp(myarray_get(tokens
, char *, 1), "help")) {
23 } else if (tokens
->len
!= 2) {
24 printf("Invalid amount of arguments.\n");
30 if (sscanf(myarray_get(tokens
, char *, 1), "%d", &todo
) != 1) {
31 printf("TODO# must be a number\n");
35 query
= sqlite3_mprintf("SELECT COUNT(key) FROM todo WHERE parent = %d;", todo
);
37 retVal
= sqlite3_prepare_v2(db
, query
, -1, &statement
, NULL
);
39 if (retVal
!= SQLITE_OK
) {
40 printf("An error occurred while trying to get the data from the database.\n");
41 sqlite3_finalize(statement
);
45 while ((retVal
= sqlite3_step(statement
)) == SQLITE_ROW
) {
46 hasChildren
= sqlite3_column_int(statement
, 0);
48 sqlite3_finalize(statement
);
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
);
61 query
= sqlite3_mprintf("UPDATE todo SET parent = NULL WHERE parent = %d;\n", todo
);
62 sqlite3_exec(db
, query
, NULL
, NULL
, &errmsg
);
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
);