2012-02-05 18:37:25 +01:00
|
|
|
-- Create a new task
|
2012-02-09 18:50:54 +01:00
|
|
|
DROP FUNCTION IF EXISTS add_task( INT , TEXT , TEXT , INT , INT ) CASCADE;
|
|
|
|
CREATE FUNCTION add_task( t_item INT , t_title TEXT , t_description TEXT , t_priority INT , t_user INT )
|
2012-02-05 18:37:25 +01:00
|
|
|
RETURNS INT
|
|
|
|
STRICT VOLATILE
|
|
|
|
SECURITY INVOKER
|
|
|
|
AS $add_task$
|
2012-02-09 18:50:54 +01:00
|
|
|
DECLARE
|
|
|
|
_logical_container INT;
|
2012-02-05 18:37:25 +01:00
|
|
|
BEGIN
|
2012-02-09 18:50:54 +01:00
|
|
|
SELECT INTO _logical_container ltc_id
|
|
|
|
FROM logical_task_containers
|
|
|
|
WHERE task_id IS NULL;
|
|
|
|
|
2012-02-15 10:04:11 +01:00
|
|
|
INSERT INTO tasks ( item_id , ltc_id , task_title , task_description , task_priority , user_id )
|
|
|
|
VALUES ( t_item , _logical_container , t_title , t_description , t_priority , t_user );
|
2012-02-05 18:37:25 +01:00
|
|
|
RETURN 0;
|
|
|
|
EXCEPTION
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RETURN 1;
|
2012-02-15 10:04:11 +01:00
|
|
|
WHEN foreign_key_violation THEN
|
|
|
|
RETURN 2;
|
2012-02-05 18:37:25 +01:00
|
|
|
END;
|
|
|
|
$add_task$ LANGUAGE plpgsql;
|
|
|
|
|
2012-02-09 18:50:54 +01:00
|
|
|
REVOKE EXECUTE ON FUNCTION add_task( INT , TEXT , TEXT , INT , INT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION add_task( INT , TEXT , TEXT , INT , INT ) TO :webapp_user;
|
|
|
|
|
|
|
|
-- Create a new nested task
|
|
|
|
DROP FUNCTION IF EXISTS tasks_add_nested( INT , TEXT , TEXT , INT , INT ) CASCADE;
|
|
|
|
CREATE FUNCTION tasks_add_nested( t_parent INT , t_title TEXT , t_description TEXT , t_priority INT , t_user INT )
|
|
|
|
RETURNS INT
|
|
|
|
STRICT VOLATILE
|
|
|
|
SECURITY INVOKER
|
|
|
|
AS $tasks_add_nested$
|
|
|
|
DECLARE
|
2012-02-15 10:04:11 +01:00
|
|
|
_item INT;
|
2012-02-09 18:50:54 +01:00
|
|
|
_logical_container INT;
|
|
|
|
BEGIN
|
2012-02-15 10:04:11 +01:00
|
|
|
SELECT INTO _item item_id
|
|
|
|
FROM tasks t
|
2012-02-09 18:50:54 +01:00
|
|
|
LEFT OUTER JOIN completed_tasks ct USING ( task_id )
|
2012-02-15 10:04:11 +01:00
|
|
|
WHERE t.task_id = t_parent AND ct.task_id IS NULL
|
|
|
|
FOR UPDATE OF t;
|
2012-02-09 18:50:54 +01:00
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN 2;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT INTO _logical_container ltc_id
|
|
|
|
FROM logical_task_containers
|
|
|
|
WHERE task_id = t_parent;
|
|
|
|
|
2012-02-15 10:04:11 +01:00
|
|
|
INSERT INTO tasks ( task_id_parent , item_id , ltc_id , task_title , task_description , task_priority , user_id )
|
|
|
|
VALUES ( t_parent , _item , _logical_container , t_title , t_description , t_priority , t_user );
|
2012-02-09 18:50:54 +01:00
|
|
|
RETURN 0;
|
|
|
|
EXCEPTION
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RETURN 1;
|
|
|
|
END;
|
|
|
|
$tasks_add_nested$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
REVOKE EXECUTE ON FUNCTION tasks_add_nested( INT , TEXT , TEXT , INT , INT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION tasks_add_nested( INT , TEXT , TEXT , INT , INT ) TO :webapp_user;
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
|
|
|
|
-- Mark a task as finished
|
2012-02-09 18:50:54 +01:00
|
|
|
DROP FUNCTION IF EXISTS finish_task( INT , INT , TEXT );
|
|
|
|
CREATE FUNCTION finish_task( t_id INT , u_id INT , n_text TEXT )
|
2012-02-05 18:37:25 +01:00
|
|
|
RETURNS INT
|
|
|
|
STRICT VOLATILE
|
|
|
|
SECURITY INVOKER
|
|
|
|
AS $finish_task$
|
|
|
|
BEGIN
|
2012-02-15 10:04:11 +01:00
|
|
|
PERFORM 1 FROM tasks_single_view t
|
2012-02-15 10:58:55 +01:00
|
|
|
WHERE id = t_id AND badness = 0;
|
2012-02-09 18:50:54 +01:00
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN 2;
|
|
|
|
END IF;
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
BEGIN
|
|
|
|
INSERT INTO completed_tasks ( task_id , user_id )
|
|
|
|
VALUES ( t_id , u_id );
|
|
|
|
EXCEPTION
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RETURN 1;
|
|
|
|
END;
|
|
|
|
|
2012-02-06 16:38:11 +01:00
|
|
|
UPDATE tasks SET user_id_assigned = NULL WHERE task_id = t_id;
|
2012-02-05 18:37:25 +01:00
|
|
|
INSERT INTO notes ( task_id , user_id , note_text )
|
|
|
|
VALUES ( t_id , u_id , n_text );
|
|
|
|
RETURN 0;
|
|
|
|
END;
|
|
|
|
$finish_task$ LANGUAGE plpgsql;
|
|
|
|
|
2012-02-06 16:38:11 +01:00
|
|
|
REVOKE EXECUTE ON FUNCTION finish_task( INT , INT , TEXT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION finish_task( INT , INT , TEXT ) TO :webapp_user;
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
|
|
|
|
-- Restart a task
|
2012-02-09 18:50:54 +01:00
|
|
|
DROP FUNCTION IF EXISTS restart_task( INT , INT , TEXT );
|
|
|
|
CREATE FUNCTION restart_task( t_id INT , u_id INT , n_text TEXT )
|
2012-02-05 18:37:25 +01:00
|
|
|
RETURNS INT
|
|
|
|
STRICT VOLATILE
|
|
|
|
SECURITY INVOKER
|
|
|
|
AS $restart_task$
|
|
|
|
BEGIN
|
2012-02-09 18:50:54 +01:00
|
|
|
PERFORM 1
|
|
|
|
FROM tasks t
|
|
|
|
INNER JOIN logical_task_containers ltc
|
|
|
|
USING ( ltc_id )
|
|
|
|
INNER JOIN completed_tasks ct
|
|
|
|
ON ct.task_id = ltc.task_id
|
|
|
|
WHERE t.task_id = t_id;
|
|
|
|
IF FOUND THEN
|
|
|
|
RETURN 2;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
PERFORM 1
|
|
|
|
FROM task_dependencies td
|
|
|
|
INNER JOIN completed_tasks ct
|
|
|
|
USING ( task_id )
|
|
|
|
WHERE td.task_id_depends = t_id;
|
|
|
|
IF FOUND THEN
|
|
|
|
RETURN 2;
|
|
|
|
END IF;
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
DELETE FROM completed_tasks WHERE task_id = t_id;
|
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN 1;
|
|
|
|
END IF;
|
2012-02-06 16:38:11 +01:00
|
|
|
UPDATE tasks SET user_id_assigned = u_id
|
|
|
|
WHERE task_id = t_id;
|
2012-02-05 18:37:25 +01:00
|
|
|
INSERT INTO notes ( task_id , user_id , note_text )
|
|
|
|
VALUES ( t_id , u_id , n_text );
|
|
|
|
RETURN 0;
|
|
|
|
END;
|
|
|
|
$restart_task$ LANGUAGE plpgsql;
|
|
|
|
|
2012-02-06 16:38:11 +01:00
|
|
|
REVOKE EXECUTE ON FUNCTION restart_task( INT , INT , TEXT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION restart_task( INT , INT , TEXT ) TO :webapp_user;
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
|
|
|
|
-- Update a task
|
2012-02-09 18:50:54 +01:00
|
|
|
DROP FUNCTION IF EXISTS update_task( INT , INT , TEXT , TEXT , INT , INT );
|
|
|
|
CREATE FUNCTION update_task( t_id INT , p_id INT , t_title TEXT , t_description TEXT , t_priority INT , t_assignee INT )
|
2012-02-05 18:37:25 +01:00
|
|
|
RETURNS INT
|
|
|
|
STRICT VOLATILE
|
|
|
|
SECURITY INVOKER
|
|
|
|
AS $update_task$
|
2012-02-09 18:50:54 +01:00
|
|
|
|
|
|
|
DECLARE
|
|
|
|
tc INT;
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
BEGIN
|
2012-02-06 16:38:11 +01:00
|
|
|
PERFORM 1
|
|
|
|
FROM tasks
|
2012-02-09 18:50:54 +01:00
|
|
|
INNER JOIN logical_task_containers
|
|
|
|
USING ( ltc_id )
|
2012-02-06 16:38:11 +01:00
|
|
|
LEFT OUTER JOIN completed_tasks
|
2012-02-09 18:50:54 +01:00
|
|
|
ON tasks.task_id = completed_tasks.task_id
|
|
|
|
WHERE tasks.task_id = t_id
|
|
|
|
AND logical_task_containers.task_id IS NULL
|
|
|
|
AND completed_task_time IS NULL
|
2012-02-06 16:38:11 +01:00
|
|
|
FOR UPDATE OF tasks;
|
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN 4;
|
|
|
|
END IF;
|
|
|
|
|
2012-02-15 10:04:11 +01:00
|
|
|
PERFORM 1 FROM items
|
|
|
|
WHERE item_id = p_id
|
|
|
|
FOR UPDATE;
|
2012-02-09 18:50:54 +01:00
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN 2;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF t_assignee = 0 THEN
|
|
|
|
t_assignee := NULL;
|
|
|
|
END IF;
|
2012-02-15 10:04:11 +01:00
|
|
|
UPDATE tasks SET item_id = p_id , task_title = t_title ,
|
2012-02-09 18:50:54 +01:00
|
|
|
task_description = t_description ,
|
|
|
|
task_priority = t_priority ,
|
|
|
|
user_id_assigned = t_assignee
|
|
|
|
WHERE task_id = t_id;
|
2012-02-06 16:38:11 +01:00
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
RETURN 0;
|
2012-02-09 18:50:54 +01:00
|
|
|
EXCEPTION
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RETURN 1;
|
|
|
|
WHEN foreign_key_violation THEN
|
|
|
|
RETURN 3;
|
2012-02-05 18:37:25 +01:00
|
|
|
END;
|
|
|
|
$update_task$ LANGUAGE plpgsql;
|
2012-02-06 08:55:33 +01:00
|
|
|
|
2012-02-06 16:38:11 +01:00
|
|
|
REVOKE EXECUTE ON FUNCTION update_task( INT , INT , TEXT , TEXT , INT , INT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION update_task( INT , INT , TEXT , TEXT , INT , INT ) TO :webapp_user;
|
2012-02-09 18:50:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
-- Update a nested task
|
|
|
|
DROP FUNCTION IF EXISTS update_task( INT , TEXT , TEXT , INT , INT );
|
|
|
|
CREATE FUNCTION update_task( t_id INT , t_title TEXT , t_description TEXT , t_priority INT , t_assignee INT )
|
|
|
|
RETURNS INT
|
|
|
|
STRICT VOLATILE
|
|
|
|
SECURITY INVOKER
|
|
|
|
AS $update_task$
|
|
|
|
BEGIN
|
|
|
|
PERFORM 1
|
|
|
|
FROM tasks
|
|
|
|
INNER JOIN logical_task_containers
|
|
|
|
USING ( ltc_id )
|
|
|
|
LEFT OUTER JOIN completed_tasks
|
|
|
|
ON tasks.task_id = completed_tasks.task_id
|
|
|
|
WHERE tasks.task_id = t_id
|
|
|
|
AND logical_task_containers.task_id IS NOT NULL
|
|
|
|
AND completed_task_time IS NULL
|
|
|
|
FOR UPDATE OF tasks;
|
|
|
|
IF NOT FOUND THEN
|
|
|
|
RETURN 4;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF t_assignee = 0 THEN
|
|
|
|
t_assignee := NULL;
|
|
|
|
END IF;
|
|
|
|
UPDATE tasks
|
|
|
|
SET task_title = t_title ,
|
|
|
|
task_description = t_description ,
|
|
|
|
task_priority = t_priority ,
|
|
|
|
user_id_assigned = t_assignee
|
|
|
|
WHERE task_id = t_id;
|
|
|
|
RETURN 0;
|
|
|
|
EXCEPTION
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RETURN 1;
|
|
|
|
WHEN foreign_key_violation THEN
|
|
|
|
RETURN 2;
|
|
|
|
END;
|
|
|
|
$update_task$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
REVOKE EXECUTE ON FUNCTION update_task( INT , TEXT , TEXT , INT , INT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION update_task( INT , TEXT , TEXT , INT , INT ) TO :webapp_user;
|