Initial commit.
[todo.git] / todo_retire.ml
1 (* Implement 'retire' subcommand. *)
2
3 open CalendarLib
4
5 open Todo_types
6 open Todo_utils
7
8 open Printf
9
10 let cmd_retire dbh anon_params =
11   let ids =
12     match anon_params with
13     | [] -> error "give a list of IDs (#ID) to retire"
14     | ids -> List.map Int32.of_string ids in
15
16   PGOCaml.begin_work dbh;
17
18   (* It's an error to retire a task which is already retired. *)
19   List.iter (
20     fun taskid ->
21       let rows = PGSQL(dbh) "select 1 from retired where taskid = $taskid" in
22       if rows = [Some 1_l] then
23         error "task %ld is already retired" taskid;
24   ) ids;
25
26   (* Drop the tasks from every other table. *)
27   PGSQL(dbh) "delete from imminent where taskid in $@ids";
28   PGSQL(dbh) "delete from todo where taskid in $@ids";
29   PGSQL(dbh) "delete from ideas where taskid in $@ids";
30
31   (* Add them to the retired table. *)
32   List.iter (
33     fun taskid ->
34       PGSQL(dbh) "insert into retired (taskid) values ($taskid)"
35   ) ids;
36
37   PGOCaml.commit dbh