diff --git a/query.sql b/query.sql index dfc966a..8fb0724 100644 --- a/query.sql +++ b/query.sql @@ -1,34 +1,38 @@ -- name: GetStationsByName :many -SELECT s.id, - s.name, - sl.latitude, - sl.longitude, - bus, - taxi, - tram, - ferry, - subway, - national, - regional, - suburban, - regionalexp, - nationalexp +SELECT s.id as "id", + s.name as "name", + sl.latitude as "latitude", + sl.longitude as "longitude", + bus as "bus", + taxi as "taxi", + tram as "tram", + ferry as "ferry", + subway as "subway", + national as "national", + regional as "regional", + suburban as "suburban", + regionalexp as "regionalexp", + nationalexp as "nationalexp" FROM stops s JOIN public.stat_locations sl on sl.id = s.location_id JOIN public.stat_services ss on ss.id = s.service_id + JOIN public.station_rank sr + on sr.stop = s.id WHERE s.id in (SELECT id FROM stops s2 - WHERE s2.name LIKE $1 - ); + WHERE s2.name ILIKE ALL (@search_params::text[]) + ) + AND CASE WHEN sqlc.narg(long_distance)::bool THEN (ss.national = sqlc.narg(long_distance)::bool OR ss.nationalexp = sqlc.narg(long_distance)::bool) ELSE TRUE END +ORDER BY sr.count DESC; -- name: GetStationById :one -SELECT s.id, - s.name, - sl.latitude, - sl.longitude, +SELECT s.id as "id", + s.name as "name", + sl.latitude as "latitude", + sl.longitude as "longitude", bus, taxi, tram, @@ -44,31 +48,48 @@ FROM stops s on sl.id = s.location_id JOIN public.stat_services ss on ss.id = s.service_id -WHERE s.id = $1; +WHERE s.id = @stop_id; -- name: GetDeparturesById :many SELECT DISTINCT (t.id) as "id", t.line_name as "lineName", t.line_productname as "lineProductName", so.departuredelay as "departureDelay", - sd.id as "directionId", - sd.name as "directionName", + jsonb_build_object('id', sd.id, 'name', sd.name) as "direction", + ARRAY((SELECT jsonb_build_object('stop', row('id', sd.id, 'name', sd.name), + 'arrival', so2.arrival, + 'plannedArrival', so2.plannedarrival, + 'arrivalDelay', so2.arrivaldelay, + 'arrivalPlatform', so2.arrivalplatform, + 'plannedArrivalPlatform', so2.plannedarrivalplatform, + 'cancelled', so2.cancelled) + FROM stopovers so2 + JOIN public.stops s2 + ON so2.stop = s2.id + WHERE so2.id = t.id + AND so2.plannedarrival > so.planneddeparture + AND (so2.stop, so2.realtimedataupdatedat) IN (SELECT DISTINCT (stop), max(realtimedataupdatedat) + FROM stopovers so3 + WHERE (so3.id = t.id + AND so3.realtimedataupdatedat <= @rt_updated_at) + GROUP BY so3.stop))) + AS "stopovers", so.departure as "departure", so.planneddeparture as "plannedDeparture", - so.departureplatform as "departurePlattform", - so.planneddepartureplatform as "plannedDeparturePlattform", + so.departureplatform as "departurePlatform", + so.planneddepartureplatform as "plannedDeparturePlatform", + so.cancelled as "cancelled", so.realtimedataupdatedat as "realtimeDataUpdatedAt" FROM stopovers so JOIN trips t ON so.id = t.id JOIN public.trip_meta tm on t.id = tm.id JOIN public.stops sd on t.destination = sd.id -WHERE so.stop = $1 +WHERE so.stop = @stop_id AND (so.id, so.realtimedataupdatedat) IN (SELECT DISTINCT (id), max(realtimedataupdatedat) - FROM stopovers so2 - WHERE so2.stop = $1 - AND (so2.departure > $2) - AND (so2.departure < $3) - AND (so2.realtimedataupdatedat <= $4) - GROUP BY so2.id - LIMIT 50) + FROM stopovers so3 + WHERE so3.stop = @stop_id + AND (so3.departure > @departure_from) + AND (so3.departure < @departure_to) + AND (so3.realtimedataupdatedat <= @rt_updated_at) + GROUP BY so3.id) ORDER BY so.departure ASC; diff --git a/schema.sql b/schema.sql index e4b8ef1..f819aab 100644 --- a/schema.sql +++ b/schema.sql @@ -372,4 +372,12 @@ alter table hash_hafas_tid grant select on hash_hafas_tid to grafana; -SELECT * FROM trips WHERE line_name LIKE 'RE%' ORDER BY realtimedataupdatedat ASC LIMIT 100; \ No newline at end of file +create table public.station_rank +( + stop text not null + constraint station_rank_pk + primary key + constraint station_rank_stops_id_fk + references public.stops, + count bigint +); diff --git a/sqlc.yml b/sqlc.yml index d0e78d7..f9d5cd8 100644 --- a/sqlc.yml +++ b/sqlc.yml @@ -7,4 +7,5 @@ sql: go: package: "bahndb_rest" out: "queries" - sql_package: "pgx/v5" \ No newline at end of file + sql_package: "pgx/v5" + emit_json_tags: true \ No newline at end of file