source: database/Advanced Database Developement/Triggers/Validation of employee authorization before making a product change.txt@ 6c7cfa6

main
Last change on this file since 6c7cfa6 was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 834 bytes
Line 
1CREATE OR REPLACE FUNCTION validate_employee_authorization()
2RETURNS TRIGGER
3LANGUAGE plpgsql
4AS $$
5DECLARE v_authorisation TEXT;
6BEGIN
7 -- Get the employee's authorization
8 SELECT authorisation
9 INTO v_authorisation
10 FROM personal
11 WHERE id = NEW.id;
12 -- Check whether the employee exists
13 IF v_authorisation IS NULL THEN
14 RAISE EXCEPTION 'Employee % does not have valid authorization.', NEW.id;
15 END IF;
16 -- Check whether the authorization matches
17 IF NEW.authorisation IS DISTINCT FROM v_authorisation THEN
18 RAISE EXCEPTION 'Employee % is not authorized to make this product change.', NEW.id;
19 END IF;
20 RETURN NEW;
21END;
22$$;
23
24
25CREATE TRIGGER trg_validate_employee_authorization
26BEFORE INSERT OR UPDATE
27ON makes_change
28FOR EACH ROW
29EXECUTE FUNCTION validate_employee_authorization();
Note: See TracBrowser for help on using the repository browser.