diff --git a/README.md b/README.md index 20ca555..72890fd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/setup/createFunctions.sql b/src/setup/createFunctions.sql index 4228641..eea491f 100644 --- a/src/setup/createFunctions.sql +++ b/src/setup/createFunctions.sql @@ -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; @@ -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, @@ -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