From 772363cf154682562290495d1b1fd7abc945c2bb Mon Sep 17 00:00:00 2001 From: fbachus Date: Mon, 6 Jul 2026 18:01:41 +0200 Subject: [PATCH] feat(database): add queries for all tables --- lib/transport_helper.dart | 222 +++++++++++++++++++++++++++++++++++--- 1 file changed, 209 insertions(+), 13 deletions(-) diff --git a/lib/transport_helper.dart b/lib/transport_helper.dart index 55d2f5b..f3bc074 100644 --- a/lib/transport_helper.dart +++ b/lib/transport_helper.dart @@ -76,6 +76,17 @@ class Route { return tmp; } + factory Route.fromMap( + Map routeMaps, + List segmentMaps, + ) { + return Route( + id: routeMaps["id"] as int, + segments: segmentMaps, + duration: Duration(minutes: 0), + ); + } + // from trip API request // TODO: how to connect this best with the database for read/write? factory Route.fromJson(Map json) { @@ -111,6 +122,32 @@ class Route { """); } + static Future dbGet(int id) async { + Database database = DbHelper.db; + Future> routeSegments = dbGetRouteSegments(id); + var result = await database.query( + "route", + where: "id = ?", + whereArgs: [id], + ); + assert(result.length < 2, "found more than 1 route for id $id"); + Route route = Route.fromMap(result.first, await routeSegments); + return route; + } + + static Future> dbGetRouteSegments( + int routeId, + ) async { + Database database = DbHelper.db; + var tmp = await database.rawQuery(""" + SELECT * FROM segments + JOIN routesegments ON routesegments.segmentid = segments.id + WHERE routesegments.route = $routeId + ORDER BY routesegments.segmentorder ASC; + """); + return tmp.map((x) => Segment.fromMap(x)).toList(); + } + void dbInsert() async { Database database = DbHelper.db; id = await database.insert( @@ -193,6 +230,17 @@ class Segment { }; } + factory Segment.fromMap(Map map) { + return Segment( + id: map["id"] as int, + startPoint: map["startPoint"] as Station, + endPoint: map["endPoint"] as Station, + startTime: map["startTime"] as DateTime, + endTime: map["endTime"] as DateTime, + vehicle: map["vehicle"] as Vehicle, + ); + } + factory Segment.fromJson(Map json) { Database database = DbHelper.db; Segment tmp = Segment( @@ -224,6 +272,21 @@ class Segment { """); } + static Future dbGet(int id) async { + Database database = DbHelper.db; + var tmp = await database.query(tableName, where: "id = ?", whereArgs: [id]); + var seg = tmp.map((x) => Segment.fromMap(x)); + //since we are querying for the primary key, this list should never be > 1 + assert(seg.length < 2, "found more than one segment for id $id"); + return seg.first; + } + + static Future> dbGetAll() async { + Database database = DbHelper.db; + var seglist = await database.query(tableName); + return seglist.map((seg) => Segment.fromMap(seg)).toList(); + } + void dbInsert() async { Database database = DbHelper.db; id = await database.insert(tableName, toMap()); @@ -244,7 +307,7 @@ class Station { String id; String name; - List transportLines; // could be normalized away + List transportLines = List.empty(); // could be normalized away Station({ required this.id, @@ -264,6 +327,17 @@ class Station { .toList(); } + factory Station.fromMap( + Map map, { + List? stationLines, + }) { + return Station( + id: map["id"] as String, + name: map["name"] as String, + transportLines: stationLines ?? List.empty(), + ); + } + factory Station.fromJson(Map json) => Station( id: json["id"] as String? ?? json["extId"] as String, name: json["name"], @@ -295,6 +369,30 @@ class Station { """); } + static Future dbGet(String stationId) async { + Database database = DbHelper.db; + var stationLines = dbGetStationLines(stationId); + var stationMap = await database.query( + tableName, + where: "stationid = ?", + whereArgs: [stationId], + ); + var station = Station.fromMap(stationMap.first); + station.transportLines = await stationLines; + return station; + } + + static Future> dbGetStationLines(String stationId) async { + Database database = DbHelper.db; + var lines = await database.rawQuery(""" + SELECT * FROM lines + JOIN stationlines sl ON sl.lineid = line.id + WHERE stationlines.stationid = $stationId + ORDER BY line.id ASC; + """); + return lines.map((x) => Line.fromMap(x)).toList(); + } + void dbInsert() async { Database database = DbHelper.db; database.insert(tableName, toMap()); @@ -324,25 +422,34 @@ class Vehicle { static const String tableName = "vehicle"; String id; - Line line; + String vehicleLineId; Vehicle({ required this.id, - required this.line, + required this.vehicleLineId, }); Map toMap() { return { "id": id, - "line": line.lineId, + "line": vehicleLineId, }; } + factory Vehicle.fromMap( + Map map, + ) { + return Vehicle( + id: map["id"] as String, + vehicleLineId: map["line"] as String, + ); + } + // we can use the "product" class from within any JSON response // (within hafas API, on the appropriate level) factory Vehicle.fromJson(Map json) => Vehicle( id: json["matchId"], - line: Line.fromJson(json), + vehicleLineId: Line.fromJson(json).lineId, ); static void initVehicleTable() async { @@ -356,6 +463,16 @@ class Vehicle { """); } + static Future dbGet(String vehicleId) async { + Database database = DbHelper.db; + var vehicleMap = await database.query( + tableName, + where: "id = ?", + whereArgs: [vehicleId], + ); + return Vehicle.fromMap(vehicleMap.first); + } + void dbInsert() async { Database database = DbHelper.db; database.insert(tableName, toMap()); @@ -371,8 +488,8 @@ class Line { static const String tableName = "line"; String lineId; - Station? destination; - Station? direction; + String? destination; + String? direction; VehicleType mode; String name; Color? fgColor; @@ -380,6 +497,8 @@ class Line { Line({ required this.lineId, + this.destination, + this.direction, required this.name, required this.mode, this.fgColor, @@ -398,6 +517,18 @@ class Line { }; } + factory Line.fromMap(Map map) { + return Line( + lineId: map["lineId"] as String, + destination: map["destination"] as String, + direction: map["direction"] as String, + mode: map["mode"] as VehicleType, + name: map["name"] as String, + fgColor: map["fgcolor"] as Color, + bgColor: map["bgcolor"] as Color, + ); + } + // TODO: EXPLAIN!!!!!!!!!!!! static const _vehicleTypeAsCatCode = [ VehicleType.metro, @@ -431,6 +562,16 @@ class Line { """); } + static Future dbGet(String lineId) async { + Database database = DbHelper.db; + var tmp = await database.query( + tableName, + where: "lineid = ?", + whereArgs: [lineId], + ); + return Line.fromMap(tmp.first); + } + void dbInsert() async { Database database = DbHelper.db; database.insert(tableName, toMap()); @@ -447,26 +588,34 @@ class Departure { static const String tableName = "departure"; int? id; - Vehicle vehicle; - Station station; + String vehicleId; + String stationId; DateTime departureTime; Departure({ this.id, - required this.vehicle, - required this.station, + required this.vehicleId, + required this.stationId, required this.departureTime, }); Map toMap() { return { "id": id, - "vehicle": vehicle, - "station": station, + "vehicle": vehicleId, + "station": stationId, "departuretime": departureTime, }; } + factory Departure.fromMap(Map map) { + return Departure( + id: map["id"] as int, + vehicleId: map["vehicle"] as String, + stationId: map["station"] as String, + departureTime: map["departuretime"] as DateTime, + ); + } static void initDepartureTable() async { Database database = DbHelper.db; String vehicle = Vehicle.tableName; @@ -482,6 +631,53 @@ class Departure { """); } + static Future> dbGetByVehicle( + String vehicleId, { + DateTime? intervalStart, + }) async { + Database database = DbHelper.db; + intervalStart ??= DateTime.now(); // assign if null + int limit = 50; + List results = await database.query( + tableName, + where: "vehicle= ? AND departuretime > ?", + whereArgs: [ + vehicleId, + intervalStart, + ], + orderBy: "departuretime ASC", + limit: limit, + ); + return results.map((dep) => Departure.fromMap(dep)).toList(); + } + + static Future> dbGetByStation( + String stationId, { + DateTime? intervalStart, + }) async { + Database database = DbHelper.db; + intervalStart ??= DateTime.now(); // assign if null + int limit = 50; + List results = await database.query( + tableName, + where: "station = ? AND departuretime > ?", + whereArgs: [stationId, intervalStart], + orderBy: "departuretime", + limit: limit, + ); + return results.map((dep) => Departure.fromMap(dep)).toList(); + } + + static Future dbGet(int id) async { + Database database = DbHelper.db; + var depMap = await database.query( + tableName, + where: "id = ?", + whereArgs: [id], + ); + return Departure.fromMap(depMap.first); + } + void dbInsert() async { Database database = DbHelper.db; id = await database.insert(tableName, toMap());