-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetPartner.sql
141 lines (65 loc) · 2.97 KB
/
GetPartner.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `GetPartner`(IN `PersonIdIn` INT)
SQL SECURITY INVOKER
COMMENT 'To get the partner of a person based on the persons ID'
BEGIN
-- CompletedOk defines the result of a database transaction, like this:
-- 0 = Transaction finished without problems.
-- 1 =
-- 2 = Transaction aborted due to problems during update and rollback performed
-- ...
DECLARE CompletedOk int;
-- NewTransNo is autonumber counter fetched from a seperate table and used for logging in a seperate log table
DECLARE NewTransNo int;
-- TransResult is used to count the number of seperate database operations and rissen with each step
DECLARE TransResult int;
-- RecCount is used to count the number of related records in depended tables.
DECLARE RecCount 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: GetPartner(). Rollback executed. CompletedOk= ", CompletedOk),
TestLogDateTime = NOW();
SELECT CompletedOk;
END;
main_proc:
BEGIN
SET CompletedOk = 0;
SET TransResult = 0;
SET NewTransNo = GetTranNo("GetPartner");
-- Schrijf start van deze SQL transactie naar log
INSERT INTO humans.testlog
SET TestLog = CONCAT('TransAction-', IFNULL(NewTransNo, 'null'), '. TransResult= ', TransResult, '. Start SPROC: GetPartner() voor persoon met ID= ', IFNULL(PersonIdIn, 'null')),
TestLogDateTime = NOW();
IF PersonIdIn IS null THEN
INSERT INTO humans.testlog
SET TestLog = CONCAT('TransAction-', IFNULL(NewTransNo, 'null'), '. TransResult= ', TransResult, '. Error end SPROC GetPartner(), PersonIdIn should NOT be null but IS null.'),
TestLogDateTime = NOW();
ELSE
SELECT DISTINCT
P.PersonID as PartnerId,
concat(P.PersonGivvenName, ' ', P.PersonFamilyName) as Partner
FROM persons P
WHERE P.PersonID =
(SELECT DISTINCT
RelationWithPerson
FROM relations R
JOIN (relationnames RN, persons P)
ON (R.RelationName = RN.RelationnameID AND
P.PersonID = R.RelationPerson AND
RN.RelationnameName = "Partner")
WHERE P.PersonID = PersonIdIn);
SET TransResult = TransResult + 1 ;
SET RecCount = FOUND_ROWS();
# Commented out on 24-11-2019 in order to only return result set (empty or not) and no accompanying seperate metadata anymore
# This in order to simplify handlig of the result in API middlware and/or end user apps (as far as the later consumes the result directly
# SELECT CompletedOk, RecCount AS VaderGevonden;
INSERT INTO humans.testlog
SET TestLog = CONCAT('TransAction-', IFNULL(NewTransNo, 'null'), '. TransResult= ', TransResult, '. Einde SPROC GetPartner() voor persoon met ID= ', PersonIdIn, '. CompletedOk= ', CompletedOk, '. Partner gevonden=', RecCount),
TestLogDateTime = NOW();
END IF;
END;
END$$
DELIMITER ;