Initial commit.
[todo.git] / todo_add.ml
1 (* Implement subcommands for adding new tasks. *)
2
3 open CalendarLib
4
5 open Todo_types
6 open Todo_utils
7
8 open Printf
9
10 let parse_tags tags = nsplit "," tags
11
12 let insert_tags dbh taskid tags =
13   List.iter (
14     fun tag ->
15       let rows = PGSQL(dbh) "select id from tags where name = $tag" in
16       let id =
17         match rows with
18         | [] -> error "unknown tag: %s" tag
19         | [id] -> id
20         | _ -> assert false in
21       PGSQL(dbh) "insert into tags_tasks (tagid, taskid)
22                                   values ($id, $taskid)"
23   ) tags
24
25 let cmd_todo dbh anon_params estimate =
26   let deadline, description, tags =
27     match anon_params with
28     | [deadline; description] ->
29        Printer.Date.from_string deadline, description, []
30     | [deadline; description; tags] ->
31        Printer.Date.from_string deadline, description, parse_tags tags
32     | _ -> error "incorrect number of parameters to 'todo' subcommand" in
33
34   PGOCaml.begin_work dbh;
35
36   PGSQL(dbh) "insert into tasks (description) values ($description)";
37   let taskid = PGOCaml.serial4 dbh "tasks_id_seq" in
38   PGSQL(dbh) "insert into todo (taskid, deadline, estimate)
39                         values ($taskid, $deadline :: date, $?estimate)";
40   insert_tags dbh taskid tags;
41
42   PGOCaml.commit dbh
43
44 let cmd_today dbh anon_params =
45   let description, tags =
46     match anon_params with
47     | [description] -> description, []
48     | [description; tags] ->
49        description, parse_tags tags
50     | _ -> error "incorrect number of parameters to 'today' subcommand" in
51
52   PGOCaml.begin_work dbh;
53
54   PGSQL(dbh) "insert into tasks (description) values ($description)";
55   let taskid = PGOCaml.serial4 dbh "tasks_id_seq" in
56   PGSQL(dbh) "insert into imminent (taskid) values ($taskid)";
57   insert_tags dbh taskid tags;
58
59   PGOCaml.commit dbh
60
61 let cmd_idea dbh anon_params =
62   let description, tags =
63     match anon_params with
64     | [description] -> description, []
65     | [description; tags] ->
66        description, parse_tags tags
67     | _ -> error "incorrect number of parameters to 'idea' subcommand" in
68
69   PGOCaml.begin_work dbh;
70
71   PGSQL(dbh) "insert into tasks (description) values ($description)";
72   let taskid = PGOCaml.serial4 dbh "tasks_id_seq" in
73   PGSQL(dbh) "insert into ideas (taskid) values ($taskid)";
74   insert_tags dbh taskid tags;
75
76   PGOCaml.commit dbh