diff --git a/SQL/PLSQL.sql b/SQL/PLSQL.sql index eb8b8e1..145650e 100644 --- a/SQL/PLSQL.sql +++ b/SQL/PLSQL.sql @@ -7,21 +7,25 @@ IS finalclerkid NUMBER; maxanzahlcleanings NUMBER; BEGIN - startMonat := ADD_MONTHS(SYSDATE,-months); + IF months > 0 THEN + startMonat := ADD_MONTHS(SYSDATE,-months); - SELECT CLERKID, Anzahl INTO finalclerkid, maxanzahlcleanings FROM - ( - SELECT DISTINCT CLERKID, COUNT(CLERKID) AS Anzahl - From + SELECT CLERKID, Anzahl INTO finalclerkid, maxanzahlcleanings FROM ( - SELECT clcl.CLERKID - FROM CLEANINGCLERK clcl - INNER JOIN CLEANINGPLAN clpl ON clcl.CLEANINGPLANID = clpl.CLEANINGPLANID - WHERE clpl."date" >= startMonat - ) - GROUP BY CLERKID ORDER BY Anzahl desc - ) - WHERE ROWNUM <= 1; + SELECT DISTINCT CLERKID, COUNT(CLERKID) AS Anzahl + From + ( + SELECT clcl.CLERKID + FROM CLEANINGCLERK clcl + INNER JOIN CLEANINGPLAN clpl ON clcl.CLEANINGPLANID = clpl.CLEANINGPLANID + WHERE clpl."date" >= startMonat + ) + GROUP BY CLERKID ORDER BY Anzahl desc + ) + WHERE ROWNUM <= 1; + ELSE + RAISE_APPLICATION_ERROR(-20111, 'Nur Positive Werte (>0) sind als Eingabe erlaubt'); + END IF; return finalclerkid; END; @@ -56,16 +60,24 @@ BEGIN END rooms; CREATE OR REPLACE TRIGGER booking_created -BEFORE INSERT OR UPDATE +BEFORE INSERT OR UPDATE OR DELETE ON BOOKINGROOM FOR EACH ROW DECLARE cleaningDate DATE; BEGIN - SELECT arrivalDate INTO cleaningDate FROM BOOKING - WHERE BOOKING.BOOKINGID = :NEW.bookingId; - cleaningDate := cleaningDate - 1; - INSERT INTO CLEANINGPLAN VALUES (cleaningplan_seq.nextval, cleaningDate, 90, :NEW.roomNumber); + IF INSERTING OR UPDATING THEN + SELECT arrivalDate INTO cleaningDate FROM BOOKING + WHERE BOOKING.BOOKINGID = :NEW.bookingId; + cleaningDate := cleaningDate - 1; + INSERT INTO CLEANINGPLAN VALUES (cleaningplan_seq.nextval, cleaningDate, 90, :NEW.roomNumber); + ELSIF DELETING THEN + SELECT arrivalDate INTO cleaningDate FROM BOOKING + WHERE BOOKING.BOOKINGID = :OLD.bookingId; + cleaningDate := cleaningDate - 1; + DELETE FROM CLEANINGPLAN + WHERE "date" = cleaningDate AND :OLD.roomNumber = CLEANINGPLAN.ROOMNUMBER; + END IF; END; CREATE OR REPLACE TRIGGER cleaning_while_booked diff --git a/SQL/TestTrigger.sql b/SQL/TestTrigger.sql index a7806b7..d63ed43 100644 --- a/SQL/TestTrigger.sql +++ b/SQL/TestTrigger.sql @@ -1,5 +1,5 @@ --- Automatisch Cleaningplan generieren +-- Automatisch Cleaningplan erstellen INSERT INTO BOOKING VALUES (444, TO_DATE('2023-09-07', 'YYYY-MM-DD'), TO_DATE('2023-10-01', 'YYYY-MM-DD'), 65, 'No Pension', 0, 15); @@ -8,6 +8,19 @@ INSERT INTO BOOKINGROOM VALUES (444, 8); SELECT * FROM CLEANINGPLAN WHERE "date" = TO_DATE('2023-09-06', 'YYYY-MM-DD'); ROLLBACK; +-- Automatisch Cleaningplan erstellen und löschen +INSERT INTO BOOKING VALUES (444, TO_DATE('2023-09-07', 'YYYY-MM-DD'), TO_DATE('2023-10-01', 'YYYY-MM-DD'), 65, 'No Pension', 0, 15); + + +INSERT INTO BOOKINGROOM VALUES (444, 8); + +SELECT * FROM CLEANINGPLAN WHERE "date" = TO_DATE('2023-09-06', 'YYYY-MM-DD'); + +DELETE FROM BOOKINGROOM WHERE bookingID = 444; + +SELECT * FROM CLEANINGPLAN WHERE "date" = TO_DATE('2023-09-06', 'YYYY-MM-DD'); +ROLLBACK; + -- Cleaningplan kann nicht erstellt werden INSERT INTO BOOKING VALUES (555, TO_DATE('2023-09-03', 'YYYY-MM-DD'), TO_DATE('2023-09-05', 'YYYY-MM-DD'), 65, 'No Pension', 0, 15); INSERT INTO BOOKING VALUES (556, TO_DATE('2023-09-06', 'YYYY-MM-DD'), TO_DATE('2023-10-01', 'YYYY-MM-DD'), 65, 'No Pension', 0, 15);