diff --git a/queries/db.go b/queries/db.go new file mode 100644 index 0000000..30effd6 --- /dev/null +++ b/queries/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.28.0 + +package bahndb_rest + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/queries/models.go b/queries/models.go new file mode 100644 index 0000000..d129a08 --- /dev/null +++ b/queries/models.go @@ -0,0 +1,173 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.28.0 + +package bahndb_rest + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type HashHafasTid struct { + Thash pgtype.Text `json:"thash"` + Tripid pgtype.Text `json:"tripid"` +} + +type Location struct { + Tripid pgtype.Text `json:"tripid"` + Latitude pgtype.Float8 `json:"latitude"` + Longitude pgtype.Float8 `json:"longitude"` + Realtimedataupdatedat pgtype.Timestamptz `json:"realtimedataupdatedat"` +} + +type Operator struct { + ID string `json:"id"` + Name pgtype.Text `json:"name"` +} + +type Polyline struct { + Polyline []byte `json:"polyline"` + Linehash string `json:"linehash"` +} + +type PolylineConnection struct { + Origin pgtype.Text `json:"origin"` + Destination pgtype.Text `json:"destination"` + Path []byte `json:"path"` + Hash pgtype.Text `json:"hash"` +} + +type PolylineLinehashConnection struct { + Hash string `json:"hash"` + Connections []string `json:"connections"` +} + +type PolylinesTrip struct { + Trip pgtype.Text `json:"trip"` + Polyline pgtype.Text `json:"polyline"` +} + +type Remark struct { + Remhash string `json:"remhash"` + Content pgtype.Text `json:"content"` + Type pgtype.Text `json:"type"` + Code pgtype.Text `json:"code"` + Summary pgtype.Text `json:"summary"` +} + +type RemarksTrip struct { + Tripid pgtype.Text `json:"tripid"` + Remhash pgtype.Text `json:"remhash"` +} + +type SpatialRefSy struct { + Srid int32 `json:"srid"` + AuthName pgtype.Text `json:"auth_name"` + AuthSrid pgtype.Int4 `json:"auth_srid"` + Srtext pgtype.Text `json:"srtext"` + Proj4text pgtype.Text `json:"proj4text"` +} + +type StatLocation struct { + ID string `json:"id"` + Latitude pgtype.Float8 `json:"latitude"` + Longitude pgtype.Float8 `json:"longitude"` +} + +type StatService struct { + ID string `json:"id"` + Bus pgtype.Bool `json:"bus"` + Taxi pgtype.Bool `json:"taxi"` + Tram pgtype.Bool `json:"tram"` + Ferry pgtype.Bool `json:"ferry"` + Subway pgtype.Bool `json:"subway"` + National pgtype.Bool `json:"national"` + Regional pgtype.Bool `json:"regional"` + Suburban pgtype.Bool `json:"suburban"` + Regionalexp pgtype.Bool `json:"regionalexp"` + Nationalexp pgtype.Bool `json:"nationalexp"` +} + +type StationRank struct { + Stop string `json:"stop"` + Count pgtype.Int8 `json:"count"` +} + +type Stop struct { + ID string `json:"id"` + Name pgtype.Text `json:"name"` + LocationID pgtype.Text `json:"location_id"` + ServiceID pgtype.Text `json:"service_id"` +} + +type StopRemark struct { + Remhash string `json:"remhash"` + Content pgtype.Text `json:"content"` + Type pgtype.Text `json:"type"` + Code pgtype.Text `json:"code"` + Summary pgtype.Text `json:"summary"` +} + +type StopRemarksTrip struct { + Tripid pgtype.Text `json:"tripid"` + Stopid pgtype.Text `json:"stopid"` + Remhash pgtype.Text `json:"remhash"` +} + +type Stopover struct { + ID pgtype.Text `json:"id"` + Stop pgtype.Text `json:"stop"` + Departure pgtype.Timestamptz `json:"departure"` + Planneddeparture pgtype.Timestamptz `json:"planneddeparture"` + Departuredelay pgtype.Int4 `json:"departuredelay"` + Arrival pgtype.Timestamptz `json:"arrival"` + Plannedarrival pgtype.Timestamptz `json:"plannedarrival"` + Arrivaldelay pgtype.Int4 `json:"arrivaldelay"` + Reachable pgtype.Bool `json:"reachable"` + Direction pgtype.Text `json:"direction"` + Arrivalplatform pgtype.Text `json:"arrivalplatform"` + Plannedarrivalplatform pgtype.Text `json:"plannedarrivalplatform"` + Departureplatform pgtype.Text `json:"departureplatform"` + Planneddepartureplatform pgtype.Text `json:"planneddepartureplatform"` + Arrivalprognosistype pgtype.Text `json:"arrivalprognosistype"` + Departureprognosistype pgtype.Text `json:"departureprognosistype"` + Cancelled pgtype.Text `json:"cancelled"` + Realtimedataupdatedat pgtype.Timestamptz `json:"realtimedataupdatedat"` + Nextstop pgtype.Bool `json:"nextstop"` +} + +type Trip struct { + ID string `json:"id"` + LineID pgtype.Text `json:"line_id"` + LineFahrtnr pgtype.Text `json:"line_fahrtnr"` + LineName pgtype.Text `json:"line_name"` + LineAdmincode pgtype.Text `json:"line_admincode"` + LineProductname pgtype.Text `json:"line_productname"` + LineMode pgtype.Text `json:"line_mode"` + LineProduct pgtype.Text `json:"line_product"` + Operator pgtype.Text `json:"operator"` + Origin pgtype.Text `json:"origin"` + Destination pgtype.Text `json:"destination"` + Loadfactor pgtype.Text `json:"loadfactor"` + Realtimedataupdatedat pgtype.Timestamptz `json:"realtimedataupdatedat"` +} + +type TripMetum struct { + ID pgtype.Text `json:"id"` + Departure pgtype.Timestamptz `json:"departure"` + Planneddeparture pgtype.Timestamptz `json:"planneddeparture"` + Departuredelay pgtype.Int4 `json:"departuredelay"` + Arrival pgtype.Timestamptz `json:"arrival"` + Plannedarrival pgtype.Timestamptz `json:"plannedarrival"` + Arrivaldelay pgtype.Int4 `json:"arrivaldelay"` + Reachable pgtype.Bool `json:"reachable"` + Direction pgtype.Text `json:"direction"` + Arrivalplatform pgtype.Text `json:"arrivalplatform"` + Plannedarrivalplatform pgtype.Text `json:"plannedarrivalplatform"` + Departureplatform pgtype.Text `json:"departureplatform"` + Planneddepartureplatform pgtype.Text `json:"planneddepartureplatform"` + Arrivalprognosistype pgtype.Text `json:"arrivalprognosistype"` + Departureprognosistype pgtype.Text `json:"departureprognosistype"` + Cancelled pgtype.Text `json:"cancelled"` + Realtimedataupdatedat pgtype.Timestamptz `json:"realtimedataupdatedat"` +} diff --git a/queries/query.sql.go b/queries/query.sql.go new file mode 100644 index 0000000..07517ed --- /dev/null +++ b/queries/query.sql.go @@ -0,0 +1,267 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.28.0 +// source: query.sql + +package bahndb_rest + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +const getDeparturesById = `-- name: GetDeparturesById :many +SELECT DISTINCT (t.id) as "id", + t.line_name as "lineName", + t.line_productname as "lineProductName", + so.departuredelay as "departureDelay", + 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 <= $1) + GROUP BY so3.stop))) + AS "stopovers", + so.departure as "departure", + so.planneddeparture as "plannedDeparture", + 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 = $2 + AND (so.id, so.realtimedataupdatedat) IN (SELECT DISTINCT (id), max(realtimedataupdatedat) + FROM stopovers so3 + WHERE so3.stop = $2 + AND (so3.departure > $3) + AND (so3.departure < $4) + AND (so3.realtimedataupdatedat <= $1) + GROUP BY so3.id) +ORDER BY so.departure ASC +` + +type GetDeparturesByIdParams struct { + RtUpdatedAt pgtype.Timestamptz `json:"rt_updated_at"` + StopID pgtype.Text `json:"stop_id"` + DepartureFrom pgtype.Timestamptz `json:"departure_from"` + DepartureTo pgtype.Timestamptz `json:"departure_to"` +} + +type GetDeparturesByIdRow struct { + ID string `json:"id"` + LineName pgtype.Text `json:"lineName"` + LineProductName pgtype.Text `json:"lineProductName"` + DepartureDelay pgtype.Int4 `json:"departureDelay"` + Direction []byte `json:"direction"` + Stopovers interface{} `json:"stopovers"` + Departure pgtype.Timestamptz `json:"departure"` + PlannedDeparture pgtype.Timestamptz `json:"plannedDeparture"` + DeparturePlatform pgtype.Text `json:"departurePlatform"` + PlannedDeparturePlatform pgtype.Text `json:"plannedDeparturePlatform"` + Cancelled pgtype.Text `json:"cancelled"` + RealtimeDataUpdatedAt pgtype.Timestamptz `json:"realtimeDataUpdatedAt"` +} + +func (q *Queries) GetDeparturesById(ctx context.Context, arg GetDeparturesByIdParams) ([]GetDeparturesByIdRow, error) { + rows, err := q.db.Query(ctx, getDeparturesById, + arg.RtUpdatedAt, + arg.StopID, + arg.DepartureFrom, + arg.DepartureTo, + ) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetDeparturesByIdRow + for rows.Next() { + var i GetDeparturesByIdRow + if err := rows.Scan( + &i.ID, + &i.LineName, + &i.LineProductName, + &i.DepartureDelay, + &i.Direction, + &i.Stopovers, + &i.Departure, + &i.PlannedDeparture, + &i.DeparturePlatform, + &i.PlannedDeparturePlatform, + &i.Cancelled, + &i.RealtimeDataUpdatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getStationById = `-- name: GetStationById :one +SELECT s.id as "id", + s.name as "name", + sl.latitude as "latitude", + sl.longitude as "longitude", + bus, + taxi, + tram, + ferry, + subway, + national, + regional, + suburban, + regionalexp, + 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 +WHERE s.id = $1 +` + +type GetStationByIdRow struct { + ID string `json:"id"` + Name pgtype.Text `json:"name"` + Latitude pgtype.Float8 `json:"latitude"` + Longitude pgtype.Float8 `json:"longitude"` + Bus pgtype.Bool `json:"bus"` + Taxi pgtype.Bool `json:"taxi"` + Tram pgtype.Bool `json:"tram"` + Ferry pgtype.Bool `json:"ferry"` + Subway pgtype.Bool `json:"subway"` + National pgtype.Bool `json:"national"` + Regional pgtype.Bool `json:"regional"` + Suburban pgtype.Bool `json:"suburban"` + Regionalexp pgtype.Bool `json:"regionalexp"` + Nationalexp pgtype.Bool `json:"nationalexp"` +} + +func (q *Queries) GetStationById(ctx context.Context, stopID string) (GetStationByIdRow, error) { + row := q.db.QueryRow(ctx, getStationById, stopID) + var i GetStationByIdRow + err := row.Scan( + &i.ID, + &i.Name, + &i.Latitude, + &i.Longitude, + &i.Bus, + &i.Taxi, + &i.Tram, + &i.Ferry, + &i.Subway, + &i.National, + &i.Regional, + &i.Suburban, + &i.Regionalexp, + &i.Nationalexp, + ) + return i, err +} + +const getStationsByName = `-- name: GetStationsByName :many +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 ILIKE ALL ($1::text[]) + ) + AND CASE WHEN $2::bool THEN (ss.national = $2::bool OR ss.nationalexp = $2::bool) ELSE TRUE END +ORDER BY sr.count DESC +` + +type GetStationsByNameParams struct { + SearchParams []string `json:"search_params"` + LongDistance pgtype.Bool `json:"long_distance"` +} + +type GetStationsByNameRow struct { + ID string `json:"id"` + Name pgtype.Text `json:"name"` + Latitude pgtype.Float8 `json:"latitude"` + Longitude pgtype.Float8 `json:"longitude"` + Bus pgtype.Bool `json:"bus"` + Taxi pgtype.Bool `json:"taxi"` + Tram pgtype.Bool `json:"tram"` + Ferry pgtype.Bool `json:"ferry"` + Subway pgtype.Bool `json:"subway"` + National pgtype.Bool `json:"national"` + Regional pgtype.Bool `json:"regional"` + Suburban pgtype.Bool `json:"suburban"` + Regionalexp pgtype.Bool `json:"regionalexp"` + Nationalexp pgtype.Bool `json:"nationalexp"` +} + +func (q *Queries) GetStationsByName(ctx context.Context, arg GetStationsByNameParams) ([]GetStationsByNameRow, error) { + rows, err := q.db.Query(ctx, getStationsByName, arg.SearchParams, arg.LongDistance) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetStationsByNameRow + for rows.Next() { + var i GetStationsByNameRow + if err := rows.Scan( + &i.ID, + &i.Name, + &i.Latitude, + &i.Longitude, + &i.Bus, + &i.Taxi, + &i.Tram, + &i.Ferry, + &i.Subway, + &i.National, + &i.Regional, + &i.Suburban, + &i.Regionalexp, + &i.Nationalexp, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +}