Skip to content

Commit

Permalink
Merge pull request #156 from HSLdevcom/AB#32085_departures_function
Browse files Browse the repository at this point in the history
AB#32085: Line Timetable functions
  • Loading branch information
e-halinen authored May 28, 2024
2 parents 0bbf87f + d182015 commit b240805
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ Data importer for [jore-graphql](https://github.com/HSLdevcom/jore-graphql)

Read more about the [JORE import process](https://github.com/HSLdevcom/hsl-map-documentation/blob/master/Process%20schema/README.md#jore-import-process).

## Development

### Creating custom functions with PostGraphile
When creating a custom function (called "computed column") the naming scheme must adhere to following rules:
[PostGraphile documentation](https://www.graphile.org/postgraphile/computed-columns/)

In short:
- Must adhere to common PostGraphile function restrictions
- Name must begin with the name of the table it applies to, followed by an underscore (_)
- First parameter must be the table type
- Must NOT return VOID
- Must be marked as STABLE (or IMMUTABLE, though that tends to be less common)
- Must be defined in the same PostgreSQL schema as the table

## Prerequisites

Expand Down
32 changes: 25 additions & 7 deletions src/setup/createFunctions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ $$ language sql stable;
create or replace function jore.line_routes_for_date_range(line jore.line, route_date_begin date, route_date_end date) returns setof jore.route as
$$
select * from jore.route route
where route.line_id = line.line_id
WHERE route.line_id = line.line_id
AND NOT (route.date_begin < route_date_begin AND route.date_end < route_date_begin)
AND NOT (route.date_begin > route_date_end AND route.date_end > route_date_end);
$$ language sql stable;
Expand All @@ -1110,14 +1110,16 @@ $$
hours integer,
minutes integer,
is_next_day boolean,
timing_stop_type integer
timing_stop_type integer,
date_begin date,
date_end date
);
EXCEPTION
WHEN duplicate_object THEN null;
END
$$;

create or replace function jore.get_route_departures_for_timed_stops(route_identifier text, date date) returns setof jore.route_timed_stop_departure as
create or replace function jore.route_timed_stops_departures(route jore.route, user_date_begin date, user_date_end date) returns setof jore.route_timed_stop_departure as
$$
SELECT departure.stop_id,
departure.route_id,
Expand All @@ -1130,15 +1132,31 @@ SELECT departure.stop_id,
segment.timing_stop_type
FROM jore.departure departure
JOIN jore.route_segment segment
ON segment.stop_id = departure.stop_id AND segment.route_id = departure.route_id AND segment.direction = departure.direction AND date between segment.date_begin and segment.date_end
WHERE departure.route_id = route_identifier
AND date between departure.date_begin and departure.date_end
AND ((segment.timing_stop_type = 1) OR (segment.timing_stop_type = 2) OR (segment.stop_index = 1))
ON segment.stop_id = departure.stop_id AND segment.route_id = departure.route_id AND segment.direction = departure.direction
AND NOT (segment.date_begin < user_date_begin AND segment.date_end < user_date_begin)
AND NOT (segment.date_begin > user_date_end AND segment.date_end > user_date_end)
AND NOT (segment.date_begin < route.date_begin AND segment.date_end < route.date_begin)
AND NOT (segment.date_begin > route.date_end AND segment.date_end > route.date_end)
AND ((segment.timing_stop_type = 1) OR (segment.timing_stop_type = 2) OR (segment.stop_index = 1))
WHERE departure.route_id = route.route_id
AND NOT (departure.date_begin < route.date_begin AND departure.date_end < route.date_begin)
AND NOT (departure.date_begin > route.date_end AND departure.date_end > route.date_end)
AND NOT (segment.date_begin < user_date_begin AND segment.date_end < user_date_begin)
AND NOT (segment.date_begin > user_date_end AND segment.date_end > user_date_end)
GROUP BY departure.stop_id, departure.departure_id, departure.day_type,
departure.route_id, departure.direction, departure.hours, departure.minutes, departure.is_next_day,
segment.timing_stop_type;
$$ language sql stable;

create or replace function jore.route_all_timed_stops(route jore.route) returns setof jore.route_segment as
$$
SELECT * FROM jore.route_segment segment WHERE segment.route_id = route.route_id
AND segment.direction = route.direction
AND NOT (segment.date_begin < route.date_begin AND segment.date_end < route.date_begin)
AND NOT (segment.date_begin > route.date_end AND segment.date_end > route.date_end)
AND ((segment.timing_stop_type = 1) OR (segment.timing_stop_type = 2) OR (segment.stop_index = 1));
$$ language sql stable;

-- jorestatic functions

-- New procedu
Expand Down

0 comments on commit b240805

Please sign in to comment.