Files
mysql-db-vorlesung/07-package-reservierung.sql
Sebastian Seedorf ff6962de52 initial commit
2020-12-06 12:40:52 +01:00

86 lines
2.5 KiB
SQL
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
DELIMITER $$
-- Reservierung-Package
DROP FUNCTION IF EXISTS res_priv_search_vorstellung$$
CREATE FUNCTION res_priv_search_vorstellung (p_saalname VARCHAR(50), p_beginn DATETIME)
RETURNS INTEGER UNSIGNED
BEGIN
DECLARE res INTEGER UNSIGNED;
DECLARE EXIT HANDLER FOR 1172 BEGIN
SIGNAL SQLSTATE '50200' SET MESSAGE_TEXT = 'Mehr als eine Vorstellung gefunden!';
END;
SELECT Vorstellung.ID INTO res FROM Vorstellung WHERE Vorstellung.fk_Saal_ID=saal_pub_search_saal(p_saalname) AND Vorstellung.Beginn=p_beginn;
IF res IS NULL THEN
SIGNAL SQLSTATE '50201' SET MESSAGE_TEXT = 'Keine Vorstellung gefunden!';
END IF;
RETURN res;
END $$
DROP FUNCTION IF EXISTS res_pub_chk_mail$$
CREATE FUNCTION res_pub_chk_mail(p_mail VARCHAR(255))
RETURNS BIT
BEGIN
if EXISTS(select 1 from (SELECT p_mail Mail) t where t.Mail REGEXP '^[a-zA-Z0-9][a-zA-Z0-9._-]*@[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,4}$')
then
return 1;
else
return 0;
end if;
END $$
DROP FUNCTION IF EXISTS `res_pub_reservieren`$$
CREATE FUNCTION `res_pub_reservieren` (
p_vorstellung_saalname VARCHAR(50),
p_vorstellung_beginn DATETIME,
p_vorname VARCHAR(255),
p_nachname VARCHAR(255),
p_mail VARCHAR(255),
p_reihe INTEGER UNSIGNED,
p_platz INTEGER UNSIGNED
)
RETURNS INTEGER UNSIGNED
BEGIN
DECLARE v_reservierungID INTEGER UNSIGNED DEFAULT NULL;
INSERT INTO Reservierung (Vorname, Nachname, Mail, fk_Vorstellung_ID) VALUES
(p_vorname, p_nachname, p_mail, res_priv_search_vorstellung(p_vorstellung_saalname, p_vorstellung_beginn))
;
SET v_reservierungID = LAST_INSERT_ID();
INSERT INTO nm_Reservierung_Sitzplatz (fk_Reservierung_ID, fk_Sitzplatz_ID) VALUES
(v_reservierungID, saal_pub_search_sitzplatz(p_vorstellung_saalname, p_reihe, p_platz))
;
RETURN v_reservierungID;
END $$
DROP PROCEDURE IF EXISTS `res_pub_add_reservieren_sitz`$$
CREATE PROCEDURE `res_pub_add_reservieren_sitz` (
p_reservierungID INTEGER UNSIGNED,
p_reihe INTEGER UNSIGNED,
p_platz INTEGER UNSIGNED
)
BEGIN
DECLARE v_saalID INTEGER UNSIGNED DEFAULT 0;
IF (p_reservierungID IS NULL) THEN
SIGNAL SQLSTATE '50210' SET MESSAGE_TEXT = 'Reservierungs-ID darf nicht NULL sein!';
END IF;
SELECT Vorstellung.fk_Saal_ID INTO v_saalID FROM Reservierung
JOIN Vorstellung ON Reservierung.fk_Vorstellung_ID = Vorstellung.ID
WHERE Reservierung.ID=p_reservierungID
;
INSERT INTO nm_Reservierung_Sitzplatz (fk_Reservierung_ID, fk_Sitzplatz_ID) VALUES
(p_reservierungID, saal_pub_search_sitzplatz_saalID(v_saalID, p_reihe, p_platz))
;
END $$
DELIMITER ;