Changes between Version 1 and Version 2 of Triggers


Ignore:
Timestamp:
08/24/25 17:20:01 (9 days ago)
Author:
221007
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Triggers

    v1 v2  
    9595}}}
    9696
    97 === 4.
     97=== 4. Валидирање на годината на создавање на објект од типот Institution
     98
     99{{{
     100CREATE OR REPLACE FUNCTION validate_institution_year()
     101RETURNS TRIGGER AS $$
     102BEGIN
     103    IF NEW.year_established > EXTRACT(YEAR FROM CURRENT_DATE) THEN
     104        RAISE EXCEPTION 'Institution cannot be established in the future.';
     105    END IF;
     106    RETURN NEW;
     107END;
     108$$ LANGUAGE plpgsql;
     109
     110CREATE TRIGGER trg_validate_year_established
     111BEFORE INSERT OR UPDATE ON Institution
     112FOR EACH ROW
     113EXECUTE FUNCTION validate_institution_year();
     114}}}
     115
     116=== 5. Форматирчки тригер кој се однесува на објектите од типот Institution
     117
     118{{{
     119CREATE OR REPLACE FUNCTION format_institution_fields()
     120RETURNS TRIGGER AS $$
     121BEGIN
     122    NEW.name := INITCAP(NEW.name);
     123    NEW.city := INITCAP(NEW.city);
     124    RETURN NEW;
     125END;
     126$$ LANGUAGE plpgsql;
     127
     128CREATE TRIGGER trg_format_institution_fields
     129BEFORE INSERT OR UPDATE ON Institution
     130FOR EACH ROW
     131EXECUTE FUNCTION format_institution_fields();
     132}}}
     133
     134=== 6. Тригер кој поставува custom description на !CriminalReport
     135
     136{{{
     137CREATE OR REPLACE FUNCTION update_criminal_report_description()
     138RETURNS TRIGGER AS $$
     139DECLARE
     140    deadline_date DATE;
     141    report_date DATE;
     142    formatted_fine TEXT;
     143BEGIN
     144    SELECT created_at INTO report_date
     145    FROM report
     146    WHERE report_id = NEW.report_id;
     147
     148    IF NEW.punishment_type = 'fine' THEN
     149        deadline_date := report_date + INTERVAL '1 month';
     150        formatted_fine := TO_CHAR(NEW.fine_to_pay, 'FM999999990.00');
     151
     152        UPDATE CriminalReport
     153        SET descriptive_punishment = FORMAT(
     154            'The fine to be paid is %s euros, and shall be paid within one month after the report is made (until %s).',
     155            formatted_fine,
     156            TO_CHAR(deadline_date, 'YYYY-MM-DD')
     157        )
     158        WHERE report_id = NEW.report_id;
     159
     160    ELSIF NEW.punishment_type = 'prison' THEN
     161        UPDATE CriminalReport
     162        SET descriptive_punishment = FORMAT(
     163            'The accused shall be in prison until %s.',
     164            TO_CHAR(NEW.release_date, 'YYYY-MM-DD')
     165        )
     166        WHERE report_id = NEW.report_id;
     167    END IF;
     168
     169    RETURN NULL;
     170END;
     171$$ LANGUAGE plpgsql;
     172
     173CREATE TRIGGER trg_update_description_on_punishment_insert
     174AFTER INSERT OR UPDATE ON Punishment
     175FOR EACH ROW
     176EXECUTE FUNCTION update_criminal_report_description();
     177}}}
     178
     179=== 7. Тригер кој поставува вредносна валута за казната на објектот Punishment
     180
     181{{{
     182CREATE OR REPLACE FUNCTION setting_punishment_depending_on_type()
     183RETURNS TRIGGER AS $$
     184DECLARE
     185BEGIN
     186    IF NEW.punishment_type = 'fine' THEN
     187        NEW.value_unit := 'euros';
     188        IF NEW.fine_to_pay IS NULL THEN
     189            RAISE EXCEPTION 'Fine punishment must include fine_to_pay amount.';
     190        END IF;
     191        IF NEW.release_date IS NOT NULL THEN
     192            RAISE EXCEPTION 'Fine punishment must not have a release_date.';
     193        END IF;
     194    ELSIF NEW.punishment_type = 'prison' THEN
     195        NEW.value_unit := 'years';
     196        IF NEW.release_date IS NULL THEN
     197            RAISE EXCEPTION 'Prison punishment must include release_date.';
     198        END IF;
     199        IF NEW.fine_to_pay IS NOT NULL THEN
     200            RAISE EXCEPTION 'Prison punishment must not include fine_to_pay.';
     201        END IF;
     202    ELSE
     203        RAISE EXCEPTION 'Invalid punishment_type: %', NEW.punishment_type;
     204    END IF;
     205    RETURN NEW;
     206END;
     207$$ LANGUAGE plpgsql;
     208
     209CREATE TRIGGER trg_set_punishment_unit
     210BEFORE INSERT OR UPDATE ON Punishment
     211FOR EACH ROW
     212EXECUTE FUNCTION setting_punishment_depending_on_type();
     213}}}