1 (* Implement subcommands for listing and adding tags. *)
11 let cmd_tag_list dbh anon_params =
12 if anon_params <> [] then
13 error "extra parameters to 'tag-list' subcommand";
16 let rows = PGSQL(dbh) "
17 select tags.name, tags.colour, tasks.id
18 from tags_tasks, tags, tasks
19 where tags_tasks.tagid = tags.id
20 and tags_tasks.taskid = tasks.id
21 order by tags.name, tasks.id" in
22 if rows <> [] then printf "Tags used:\n";
23 let prev = ref None in
25 fun (name, colour, taskid) ->
26 let cur = name, colour in
28 | None -> printf " %s used by" (string_of_tag name colour)
29 | Some prev when prev <> cur ->
30 printf "\n %s used by" (string_of_tag name colour)
36 if !prev <> None then printf "\n";
39 let rows = PGSQL(dbh) "
40 select tags.name, tags.colour
42 where not exists (select 1 from tags_tasks where tagid = tags.id)
43 order by tags.name" in
44 if rows <> [] then printf "Tags not used (delete with 'todo tag-del'):\n";
47 printf " %s\n" (string_of_tag name colour)
50 let cmd_tag dbh anon_params tag_del =
52 match anon_params with
53 | id :: rest -> Int32.of_string id, rest
54 | _ -> error "incorrect parameters to 'tag' subcommand" in
55 if anon_params = [] && tag_del = [] then
56 error "no tags to add or delete from this task";
58 (* Map the tag_add and tag_del names to tag IDs. *)
61 PGSQL(dbh) "select id from tags where name in $@tag_add"
65 PGSQL(dbh) "select id from tags where name in $@tag_del"
68 PGOCaml.begin_work dbh;
71 PGSQL(dbh) "delete from tags_tasks
72 where taskid = $id and tagid in $@tag_del";
76 PGSQL(dbh) "insert into tags_tasks (taskid, tagid)
82 let cmd_tag_add dbh anon_params =
84 match anon_params with
85 | [name; colour] -> name, colour
86 | _ -> error "incorrect parameters to 'tag-add' subcommand" in
88 PGOCaml.begin_work dbh;
90 PGSQL(dbh) "insert into tags (name, colour) values ($name, $colour)";
94 let cmd_tag_del dbh anon_params =
96 match anon_params with
98 | _ -> error "incorrect parameters to 'tag-del' subcommand" in
100 PGOCaml.begin_work dbh;
102 PGSQL(dbh) "delete from tags where name = $name";
106 let cmd_tag_colour dbh anon_params =
108 match anon_params with
109 | [name; colour] -> name, colour
110 | _ -> error "incorrect parameters to 'tag-colour' subcommand" in
112 PGOCaml.begin_work dbh;
114 PGSQL(dbh) "update tags set colour = $colour where name = $name";