Initial commit.
[todo.git] / todo_move.ml
1 (* Implement 'move' subcommand. *)
2
3 open CalendarLib
4
5 open Todo_types
6 open Todo_utils
7
8 open Printf
9
10 (* The form of the command is one of:
11  *   move ID today
12  *   move ID [todo|task] deadline
13  *   move ID ideas
14  *   move ID retired
15  *   move ID unsorted
16  *)
17 let cmd_move dbh anon_params =
18   let id, anon_params =
19     match anon_params with
20     | id :: rest -> Int32.of_string id, rest
21     | _ -> error "incorrect parameters to 'move' subcommand" in
22   let dest =
23     match anon_params with
24     | ["today"|"imminent"] -> `Today
25     | ["todo"|"task"; deadline] ->
26        `Todo (Printer.Calendar.from_string deadline)
27     | ["idea"|"ideas"] -> `Ideas
28     | ["retire"|"retired"] -> `Retired
29     | ["none"|"unsorted"] -> `Unsorted
30     | _ -> error "incorrect destination for 'move' subcommand" in
31
32   PGOCaml.begin_work dbh;
33
34   (* Remove it from any other table. *)
35   PGSQL(dbh) "delete from imminent where taskid = $id";
36   PGSQL(dbh) "delete from todo where taskid = $id";
37   PGSQL(dbh) "delete from ideas where taskid = $id";
38   PGSQL(dbh) "delete from retired where taskid = $id";
39
40   (* Add it to the destination table. *)
41   (match dest with
42    | `Today ->
43       PGSQL(dbh) "insert into imminent (taskid) values ($id)"
44    | `Todo deadline ->
45       PGSQL(dbh) "insert into todo (taskid, deadline) values ($id, $deadline)"
46    | `Ideas ->
47       PGSQL(dbh) "insert into ideas (taskid) values ($id)"
48    | `Retired ->
49       PGSQL(dbh) "insert into retired (taskid) values ($id)"
50    | `Unsorted ->
51       () (* Nothing - unsorted items are not present in any table. *)
52   );
53
54   PGOCaml.commit dbh