-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddChildToParent.sql
72 lines (59 loc) · 2.61 KB
/
AddChildToParent.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
CREATE DEFINER=`root`@`%` PROCEDURE `AddChildToParent`(IN Child INT, IN Parent INT)
BEGIN
-- ----------------------------------------------------------------------------------------------------------------------------------------------
-- Author: Frans Dekkers (GoNomics)
-- Date: 31-01-2020
-- -----------------------------------
-- Prurpose of this Sproc:
-- Add a child to a parent
--
-- Parameters of this Sproc:
-- 'Parent'= The person to add the child to
-- 'Child'= The person to add as child
--
-- High level flow of this Sproc:
-- => Simply add a record to table relations which ties one person as a child to another person as a father
--
-- Note: None
--
-- TODO's:
-- => xx/xx/xxxx ->
-- ----------------------------------------------------------------------------------------------------------------------------------------------
DECLARE CompletedOk INT;
DECLARE NewTransNo INT;
DECLARE TransResult INT;
DECLARE GenderOfPerson INT;
DECLARE RelationType INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SET CompletedOk = 2;
INSERT INTO humans.testlog
SET TestLog = CONCAT("Transaction-", IFNULL(NewTransNo, "null"), ". ", "Error occured in SPROC: AddChildToParrent(). Rollback executed. Not completed OK (NOK) for parent= ", IFNULL(Parent, 'null'), " and child= ", IFNULL(Child, 'null')),
TestLogDateTime = NOW();
SELECT "NOK" as Result;
END;
SET CompletedOk = true;
SET TransResult = 0;
SET NewTransNo = GetTranNo("AddChildToParent");
SET GenderOfPerson = fGetGenderOfPerson(Parent);
IF GenderOfPerson = 1 THEN
SET RelationType = 1; -- Father
ELSEIF GenderOfPerson = 0 THEN
SET RelationType = 2; -- Mother
ELSE
SET RelationType = 99; -- Gender was null, -99 signifies unexisting (parent) type
END IF;
INSERT INTO humans.testlog
SET TestLog = CONCAT('TransAction-', IFNULL(NewTransNo, 'null'), '. TransResult= ', IFNULL(TransResult, ''),
'. Start SPROC: AddChildToParent(). Add a child to a parent. Child= ', IFNULL(Child, 'null'), '. Parent= ', IFNULL(Parent, 'null')),
TestLogDateTime = NOW();
INSERT INTO relations (RelationPerson, RelationName, RelationWithPerson)
VALUES (Child, RelationType, Parent);
INSERT INTO humans.testlog
SET TestLog = CONCAT('TransAction-', IFNULL(NewTransNo, 'null'),
'. TransResult= ', IFNULL(TransResult, ''),
'. End SPROC: AddChildToParent(). Added child: ', IFNULL(Child, 'null'), ' to parent: ', IFNULL(Parent, 'null')),
TestLogDateTime = NOW();
SELECT 'OK' as Result;
END