Changes between Version 2 and Version 3 of DatabaseProgramming


Ignore:
Timestamp:
05/23/26 20:06:51 (2 days ago)
Author:
231028
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DatabaseProgramming

    v2 v3  
    33Во оваа фаза е имплементирана апликациската логика со функции, процедури и тригери. Секоја од нив е документирана во продолжение.
    44
    5 **Процедури**
     5== Процедури
    66
    7 **Функции и тригери**
     7**Процедура за вработување на возач во компанија**
    88
     9Процедурата проверува дали id-то на возачот е валидно, доколку не постои фрла соодветен исклучок.
     10
     11create or replace procedure employ_driver(id_employee int, id_company int)
     12language plpgsql
     13as $$
     14    begin
     15        if not exists (
     16                   select 1
     17                   from driver
     18                   where user_id=id_employee)
     19               then
     20                   raise exception 'Driver does not exist';
     21        else
     22               insert into
     23                   employmenthistory (employee_user_id, start_date, end_date, company_id)
     24                   values (id_employee,current_date, null,id_company);
     25        end if;
     26    end;
     27$$;
     28
     29**Процедура за вработување на диспечер во компанија**
     30
     31Процедурата проверува дали id-то на диспечерот е валидно, доколку не постои фрла соодветен исклучок.
     32
     33create or replace procedure employ_dispatcher(id_employee int, id_company int)
     34language plpgsql
     35as $$
     36    begin
     37        if not exists (
     38                   select 1
     39                   from dispatcher
     40                   where user_id=id_employee)
     41               then
     42                   raise notice 'Dispatcher does not exist';
     43        else
     44               insert into
     45                   employmenthistory (employee_user_id, start_date, end_date, company_id)
     46                   values (id_employee,current_date, null,id_company);
     47        end if;
     48    end;
     49$$;
     50
     51create or replace procedure fire_driver(id_employee int, id_company int)
     52language plpgsql
     53as $$
     54    begin
     55        if not exists (
     56               select 1
     57               from driver
     58               where user_id=id_employee)
     59           then
     60               raise exception 'Driver does not exist';
     61       else
     62           if not exists (
     63               select 1
     64               from employmenthistory
     65               where employee_user_id=id_employee and end_date==null and company_id=id_company)
     66           then
     67               raise exception 'Driver does not have an employment';
     68           else
     69               update employmenthistory
     70               set end_date=current_date
     71               where employee_user_id==id_employee and end_date==null and company_id=id_company;
     72               raise notice 'Driver is fired';
     73               commit;
     74           end if;
     75       end if;
     76    end;
     77$$;
     78
     79create or replace procedure fire_dispatcher(id_employee int, id_company int)
     80language plpgsql
     81as $$
     82    begin
     83        if not exists (
     84               select 1
     85               from dispatcher
     86               where user_id=id_employee)
     87           then
     88               raise notice 'Dispatcher does not exist';
     89       else
     90           if not exists(
     91               select 1
     92               from employmenthistory
     93               where employee_user_id=id_employee and end_date==null and company_id=id_company)
     94           then
     95               raise exception 'Dispatcher does not have an employment';
     96           else
     97               update employmenthistory
     98               set end_date=current_date
     99               where employee_user_id=id_employee and end_date==null and company_id=id_company;
     100               raise notice 'Dispatcher is fired';
     101               commit;
     102           end if;
     103       end if;
     104    end;
     105$$;
     106
     107
     108create or replace procedure write_rating(rating numeric, comment text, id_ride int, id_customer int)
     109language plpgsql
     110as $$
     111    begin
     112        insert into review (rating, comment, ride_id, customer_user_id)
     113        values (write_rating.rating,write_rating.comment,id_ride,id_customer);
     114    end;
     115$$;
     116
     117create or replace procedure write_chat(new_message text, id_ride int, id_user int)
     118language plpgsql
     119as $$
     120    begin
     121        insert into chatmessage(message, user_id_from, ride_id)
     122        values (new_message,id_user,id_ride);
     123    end;
     124$$;
     125
     126create or replace procedure write_report(id_ride int, user_id int, new_message text, new_title text,temp_latitude float, temp_long float, new_reason text)
     127language plpgsql
     128as $$
     129    begin
     130        insert into report(ride_id, customer_user_id, message, title, latitude, longitude, reason)
     131        values (id_ride,user_id,new_message,new_title,temp_latitude,temp_long,new_reason);
     132    end;
     133$$;
     134
     135== Функции и тригери
     136