 END $$
 DELIMITER ;
 
+/* This function check if a task have a parent task */
+USE easygoing;
+
+DELIMITER $$
+DROP FUNCTION IF EXISTS taskHasParent $$
+
+CREATE FUNCTION taskHasParent
+(
+	task INT
+)
+RETURNS BOOLEAN
+BEGIN
+	RETURN EXISTS(
+		SELECT parentTask 
+		FROM tasks 
+		WHERE id = task AND parentTask <> NULL
+	);
+END $$
+DELIMITER ;
+END $$
+DELIMITER ;
 
 /* TRIGGERS */
 USE easygoing;

END $$
DELIMITER;

USE easygoing;

DELIMITER $$


DELIMITER ;

USE easygoing;
DROP TRIGGER IF EXISTS tasksBeforeInsert

DELIMITER $$
USE easygoing $$

CREATE TRIGGER tasksBeforeInsert
BEFORE INSERT ON tasks
FOR EACH ROW
BEGIN

	IF taskHasParent(NEW.parent_task) THEN
		SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "The parent_task has a parent.";
	END IF;
	
END $$

DELIMITER ;
USE easygoing;
DROP TRIGGER IF EXISTS tasksBeforeUpdate

DELIMITER $$
USE easygoing $$

CREATE TRIGGER tasksBeforeUpdate
BEFORE UPDATE ON tasks
FOR EACH ROW
BEGIN

	IF taskHasParent(NEW.parent_task) THEN
		SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "The parent_task has a parent.";
	END IF;
	
END $$

DELIMITER ;





/* Insert some data */
