Changes between Version 29 and Version 30 of DatabaseProgramming
- Timestamp:
- 06/16/26 01:57:29 (26 hours ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DatabaseProgramming
v29 v30 11 11 {{{ 12 12 create or replace procedure start_shift( 13 in driver_id int4,14 in v in_vehicle varchar(17)13 in v_driver_id int4, 14 in v_vin_vehicle varchar(17) 15 15 ) 16 16 language plpgsql as 17 17 $$ 18 18 begin 19 if not exists(select user_id from driver where user_id = driver_id) then20 raise exception 'Driver with id % does not exist', driver_id;21 end if; 22 23 if not exists(select vin from vehicle where vin = v in_vehicle) then24 raise exception 'Vehicle with VIN % does not exist', v in_vehicle;19 if not exists(select user_id from driver where user_id = v_driver_id) then 20 raise exception 'Driver with id % does not exist', v_driver_id; 21 end if; 22 23 if not exists(select vin from vehicle where vin = v_vin_vehicle) then 24 raise exception 'Vehicle with VIN % does not exist', v_vin_vehicle; 25 25 end if; 26 26 27 27 insert into driver_vehicle(vin_vehicle, id_driver, time_from, time_to) 28 values (v in_vehicle,driver_id, now(), null);28 values (v_vin_vehicle, v_driver_id, now(), null); 29 29 commit; 30 30 end; … … 38 38 {{{ 39 39 create or replace procedure make_request( 40 out request_id int4, 41 in customer_id int4, 42 in start_latitude double precision, 43 in start_longitude double precision, 44 in end_latitude double precision, 45 in end_longitude double precision, 46 in number_of_adult_passengers int4 default 1, 47 in number_of_children int4 default 0, 48 in female_driver boolean default false, 49 in luggage boolean default false, 50 in luggage_count int4 default 0, 51 in baby_seat_count int4 default 0 40 in v_customer_id int4, 41 in v_start_latitude double precision, 42 in v_start_longitude double precision, 43 in v_end_latitude double precision, 44 in v_end_longitude double precision, 45 in v_number_of_adult_passengers int4 default 1, 46 in v_number_of_children int4 default 0, 47 in v_female_driver boolean default false, 48 in v_luggage boolean default false, 49 in v_luggage_count int4 default 0, 50 in v_baby_seat_count int4 default 0 52 51 ) 53 52 language plpgsql as … … 56 55 if not exists(select user_id 57 56 from customer c 58 where c.user_id = customer_id)57 where c.user_id = v_customer_id) 59 58 then 60 raise exception 'Customer with id % does not exist', customer_id;61 end if; 62 63 if start_latitude < -90.0 orstart_latitude > 90.064 or start_longitude < -180.0 orstart_longitude > 180.065 or end_latitude < -90.0 orend_latitude > 90.066 or end_longitude < -180.0 orend_longitude > 180.059 raise exception 'Customer with id % does not exist', v_customer_id; 60 end if; 61 62 if v_start_latitude < -90.0 or v_start_latitude > 90.0 63 or v_start_longitude < -180.0 or v_start_longitude > 180.0 64 or v_end_latitude < -90.0 or v_end_latitude > 90.0 65 or v_end_longitude < -180.0 or v_end_longitude > 180.0 67 66 then 68 67 raise exception 'Invalid location values'; 69 68 end if; 70 69 71 if number_of_adult_passengers < 0 then70 if v_number_of_adult_passengers < 0 then 72 71 raise exception 'Number of adult passengers cannot be negative'; 73 72 end if; 74 73 75 if number_of_children < 0 then74 if v_number_of_children < 0 then 76 75 raise exception 'Number of children cannot be negative'; 77 76 end if; 78 77 79 if number_of_adult_passengers +number_of_children < 0 then78 if v_number_of_adult_passengers + v_number_of_children < 0 then 80 79 raise exception 'There must be at least 1 passenger'; 81 80 end if; 82 81 83 if baby_seat_count < 0 then82 if v_baby_seat_count < 0 then 84 83 raise exception 'Baby seat count cannot be negative'; 85 84 end if; 86 85 87 if luggage = false andluggage_count > 0 then86 if v_luggage = false and v_luggage_count > 0 then 88 87 raise exception 'Luggage count cannot be greater than zero if the luggage flag is set to false.'; 89 88 end if; 90 89 91 if luggage = true andluggage_count <= 0 then90 if v_luggage = true and v_luggage_count <= 0 then 92 91 raise exception 'Invalid luggage count parameters'; 93 92 end if; 94 93 95 if start_latitude = end_latitude and start_longitude =end_longitude then94 if v_start_latitude = v_end_latitude and v_start_longitude = v_end_longitude then 96 95 raise exception 'Start and destination location cannot be the same'; 97 96 end if; 98 97 99 98 insert into request(customer_user_id, 100 start_latitude,101 start_longitude,102 end_latitude,103 end_longitude,104 timestamp,105 number_of_adult_passengers,106 number_of_children,107 status,108 female_driver,109 luggage,110 luggage_count,111 baby_seat_count)112 values ( customer_id,113 make_request.start_latitude,114 make_request.start_longitude,115 make_request.end_latitude,116 make_request.end_longitude,99 start_latitude, 100 start_longitude, 101 end_latitude, 102 end_longitude, 103 timestamp, 104 number_of_adult_passengers, 105 number_of_children, 106 status, 107 female_driver, 108 luggage, 109 luggage_count, 110 baby_seat_count) 111 values (v_customer_id, 112 v_start_latitude, 113 v_start_longitude, 114 v_end_latitude, 115 v_end_longitude, 117 116 now(), 118 make_request.number_of_adult_passengers,119 make_request.number_of_children,117 v_number_of_adult_passengers, 118 v_number_of_children, 120 119 'pending', 121 make_request.female_driver, 122 make_request.luggage, 123 make_request.luggage_count, 124 make_request.baby_seat_count) 125 returning id into request_id; 120 v_female_driver, 121 v_luggage, 122 v_luggage_count, 123 v_baby_seat_count); 126 124 commit; 127 125 end; … … 135 133 {{{ 136 134 create or replace procedure add_waypoint( 137 in request_id int4,138 in latitude int4,139 in longitude int4135 in v_request_id int4, 136 in v_latitude int4, 137 in v_longitude int4 140 138 ) 141 139 language plpgsql as 142 140 $$ 143 141 declare 144 prev_seqno int4;145 begin 146 if not exists(select id from request where id = request_id) then147 raise exception 'Request with id % does not exist', request_id;148 end if; 149 150 if latitude < -90.0 orlatitude > 90.0151 or longitude < -180.0 orlongitude > 180.0 then142 v_prev_seqno int4; 143 begin 144 if not exists(select id from request where id = v_request_id) then 145 raise exception 'Request with id % does not exist', v_request_id; 146 end if; 147 148 if v_latitude < -90.0 or v_latitude > 90.0 149 or v_longitude < -180.0 or v_longitude > 180.0 then 152 150 raise exception 'Invalid waypoint coordinates'; 153 151 end if; … … 155 153 if exists(select id 156 154 from waypoints w 157 where w.latitude = add_waypoint.latitude158 and w.longitude = add_waypoint.longitude159 and w.request_id = add_waypoint.request_id) then155 where w.latitude = v_latitude 156 and w.longitude = v_longitude 157 and w.request_id = v_request_id) then 160 158 raise exception 'Waypoint already added'; 161 159 end if; 162 160 163 161 select count(*) 164 into prev_seqno162 into v_prev_seqno 165 163 from waypoints w 166 where w.request_id = add_waypoint.request_id;164 where w.request_id = v_request_id; 167 165 168 166 insert into waypoints(latitude, longitude, seqno, request_id) 169 values ( add_waypoint.latitude, add_waypoint.longitude, prev_seqno + 1, add_waypoint.request_id);167 values (v_latitude, v_longitude, v_prev_seqno + 1, v_request_id); 170 168 commit; 171 169 end; … … 179 177 {{{ 180 178 create or replace procedure start_ride( 181 in offer_id int4,182 out ride_id int4179 in v_offer_id int4, 180 out v_ride_id int4 183 181 ) 184 182 language plpgsql as 185 183 $$ 186 184 declare 187 v_request_id int4; 188 start_latitude double precision; 189 start_longitude double precision; 190 driver_id int4; 191 v_vehicle_vin varchar(17); 192 -- v_status ride_status; 193 begin 194 if not exists(select id from offer where id = offer_id) then 195 raise exception 'Offer with id % does not exist', offer_id; 185 v_request_id int4; 186 v_vehicle_vin varchar(17); 187 v_driver_id int4; 188 begin 189 if not exists(select id from offer where id = v_offer_id) then 190 raise exception 'Offer with id % does not exist', v_offer_id; 196 191 end if; 197 192 198 193 select o.request_id, 199 r.start_latitude, 200 r.start_longitude 194 o.driver_user_id 201 195 into 202 request_id, 203 start_latitude, 204 start_longitude 196 v_request_id, 197 v_driver_id 205 198 from offer o 206 199 join request r on o.request_id = r.id 207 where o.id = offer_id; 208 209 select d.user_id, dc.vin_vehicle 210 into driver_id, 211 v_vehicle_vin 212 from driver d 213 join driver_vehicle dc on d.user_id = dc.id_driver 214 where dc.time_to is null 215 and d.latitude is not null 216 and d.longitude is not null 217 order by power(start_latitude - d.latitude, 2) + power(start_longitude - d.longitude, 2) 218 limit 1; 219 220 if driver_id is null then 221 raise exception 'No active drivers available'; 222 end if; 200 where o.id = v_offer_id; 201 202 v_vehicle_vin := (select vin_vehicle from driver_vehicle where id_driver = v_driver_id); 223 203 224 204 insert into ride(start_time, … … 234 214 0, 235 215 v_vehicle_vin, 236 driver_id,216 v_driver_id, 237 217 v_request_id, 238 218 'in_progress', 239 start_ride.offer_id);219 v_offer_id); 240 220 241 221 update offer 242 222 set status='accepted' 243 where id = offer_id;223 where id = v_offer_id; 244 224 245 225 update request … … 450 430 request_id int4, 451 431 dispatcher_user_id int4, 452 driver_user_id int4,453 432 price numeric(19, 2), 454 433 currency_catalog_id int4, … … 459 438 AS 460 439 $$ 440 declare 441 v_driver_user_id int4; 442 v_start_latitude double precision; 443 v_start_longitude double precision; 461 444 begin 462 445 if not exists(select * from request where request.id = request_id and request.status = 'pending') then … … 470 453 end if; 471 454 455 select start_latitude, start_longitude 456 into v_start_latitude, v_start_longitude 457 from request 458 where id = request_id; 459 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; 469 470 if (select company_id from employmenthistory where employee_user_id = dispatcher_user_id and end_date is null) != 471 (select company_id from employmenthistory where employee_user_id = v_driver_user_id and end_date is null) then 472 raise exception 'Dispatcher and driver company mismatch'; 473 end if; 474 472 475 insert into offer(status, created_at, request_id, dispatcher_user_id, driver_user_id, price, currency_catalog_id, 473 476 eta, customer_user_id) 474 values ('pending', now(), create_offer.request_id, create_offer.dispatcher_user_id, create_offer.driver_user_id,477 values ('pending', now(), create_offer.request_id, create_offer.dispatcher_user_id, v_driver_user_id, 475 478 create_offer.price, create_offer.currency_catalog_id, create_offer.eta, create_offer.customer_user_id); 476 479 commit;
