-- Copyright © 2024, UChicago Argonne, LLC
-- BSD OPEN SOURCE LICENSE. Full license can be found in LICENSE.md

--##
create trigger if not exists location_parking_creates_distance_field_on_insert after insert on Location_Parking
begin
    update Location_Parking
    set "distance" = round(st_distance((select geo from Location where location = new.location),
                                 (select geo from Parking where parking = new.parking)), 8),
    "id" = new.rowid
    where Location_Parking.rowid = new.rowid;
end;

--##
create trigger if not exists location_parking_updates_distance_field_on_change after update on Location_Parking
begin
    update Location_Parking
    set "distance" = round(st_distance((select geo from Location where location = new.location),
                                       (select geo from Parking where parking = new.parking)), 8),
    "id" = new.rowid
    where Location_Parking.rowid = new.rowid;
end;

