| 1 | create function UpdateAirplaneFlightHours()
|
|---|
| 2 | returns trigger as $$
|
|---|
| 3 | declare
|
|---|
| 4 | StatusID int;
|
|---|
| 5 | begin
|
|---|
| 6 | select id
|
|---|
| 7 | into StatusID
|
|---|
| 8 | from flightstatus
|
|---|
| 9 | where name like '%Arrived%';
|
|---|
| 10 |
|
|---|
| 11 | if new.statusid = StatusID
|
|---|
| 12 | and (old.statusid is distinct from StatusID) then
|
|---|
| 13 | update airplane
|
|---|
| 14 | set totalflighthours = totalflighthours + extract(epoch from (new.arrival - new.departure)) / 3600::int
|
|---|
| 15 | where id = new.airplaneid;
|
|---|
| 16 | end if;
|
|---|
| 17 |
|
|---|
| 18 | return new;
|
|---|
| 19 | end;
|
|---|
| 20 | $$ language plpgsql;
|
|---|
| 21 |
|
|---|
| 22 | create trigger UpdateAirplaneFlightHours
|
|---|
| 23 | after update of statusid on flight
|
|---|
| 24 | for each row execute function UpdateAirplaneFlightHours(); |
|---|