Changes between Version 31 and Version 32 of DatabaseProgramming


Ignore:
Timestamp:
06/16/26 02:22:05 (26 hours ago)
Author:
231119
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DatabaseProgramming

    v31 v32  
    439439$$
    440440declare
    441     v_driver_user_id  int4;
    442     v_start_latitude  double precision;
    443     v_start_longitude double precision;
     441    v_customer_user_id      int4;
     442    v_dispatcher_company_id int4;
     443    v_driver_user_id        int4;
     444    v_start_latitude        double precision;
     445    v_start_longitude       double precision;
    444446begin
    445447    if not exists(select * from request where request.id = request_id and request.status = 'pending') then
     
    458460    where id = request_id;
    459461
    460     select d.user_id, dc.vin_vehicle
    461     into v_driver_user_id
    462     from driver d
    463              join driver_vehicle dc on d.user_id = dc.id_driver
    464     where dc.time_to is null
    465       and d.latitude is not null
    466       and d.longitude is not null
    467     order by power(v_start_latitude - d.latitude, 2) + power(v_start_longitude - d.longitude, 2)
    468     limit 1;
     462    v_customer_user_id := (select request.customer_user_id from request where request.id = request_id);
     463
     464    v_dispatcher_company_id := (select eh.company_id
     465                                from dispatcher d
     466                                         join employmenthistory eh on eh.employee_user_id = d.user_id and
     467                                                                      (eh.end_date is null or eh.end_date > now())
     468                                where d.user_id = dispatcher_user_id
     469                                limit 1);
     470
     471    if v_dispatcher_company_id is null then
     472        raise exception 'Dispatcher % is not assigned to a company',
     473            dispatcher_user_id;
     474    end if;
     475
     476    v_driver_user_id := (select d.user_id
     477                         from driver d
     478                                  join driver_vehicle dc on d.user_id = dc.id_driver
     479                                  join employmenthistory eh
     480                                       on eh.employee_user_id = d.user_id and (eh.end_date is null or
     481                                                                               eh.end_date > now())
     482                         where dc.time_to is null
     483                           and eh.company_id = v_dispatcher_company_id
     484                         order by power(v_start_latitude - d.latitude, 2) + power(v_start_longitude - d.longitude, 2)
     485                         limit 1);
     486
     487    if not exists(select * from driver where user_id = v_driver_user_id) then
     488        raise exception 'No available drivers!';
     489    end if;
    469490
    470491    if (select company_id from employmenthistory where employee_user_id = dispatcher_user_id and end_date is null) !=
     
    663684{{{
    664685create or replace function check_available_seats()
    665 returns trigger
    666 language plpgsql
    667 as $$
    668     begin
    669         if (select v.passenger_capacity
    670             from vehicle v
    671             inner join driver_vehicle dv on v.vin=dv.vin_vehicle
    672             where dv.id_driver=new.driver_user_id)<(select r.number_of_children+r.number_of_adult_passengers+r.baby_seat_count
    673                                                     from request r
    674                                                     inner join offer o on r.id=o.request_id
    675                                                     where o.id=new.id)
    676             then
    677             raise exception 'Not enough seats in the vehicle';
    678         end if;
    679         return new;
    680     end;
     686    returns trigger
     687    language plpgsql
     688as
     689$$
     690begin
     691    if (select v.passenger_capacity
     692        from vehicle v
     693                 inner join driver_vehicle dv on v.vin = dv.vin_vehicle
     694        where dv.id_driver = new.driver_user_id and (dv.time_to is null or dv.time_to > now())) <
     695       (select r.number_of_children + r.number_of_adult_passengers + r.baby_seat_count
     696        from request r
     697                 inner join offer o on r.id = o.request_id
     698        where o.id = new.id)
     699    then
     700        raise exception 'Not enough seats in the vehicle';
     701    end if;
     702    return new;
     703end;
    681704$$;
    682705create or replace trigger check_valid_seats