diff --git a/lib/transport_helper.dart b/lib/transport_helper.dart index 364eecf..e1cc12d 100644 --- a/lib/transport_helper.dart +++ b/lib/transport_helper.dart @@ -41,11 +41,11 @@ class DbHelper { } } -/// this class covers two database tables: "route" and "routesegments" -/// while route will for now only hold an id, routesegments will store the -/// m-n/ 1-n relations between routes and segments - the segments contain the most -/// relevant info, while routesegments stores which route the segments belong to -/// and in which order they make up a route +/// this class covers two database tables: "journey" and "routesegments" +/// while journey will for now only hold an id, routesegments will store the +/// m-n/ 1-n relations between journeys and segments - the segments contain the most +/// relevant info, while routesegments stores which journey the segments belong to +/// and in which order they make up a journey class Journey { static const String tableName = "journey"; // TODO: add last_viewed, last_modified and such @@ -63,7 +63,7 @@ class Journey { required this.duration, }); - /// toMap for Journey but it's just route ids for now + /// toMap for Journey but it's just journey ids for now Map _toMapJourney() { return {"id": id, "startstation": startStation, "endstation": endStation}; } @@ -74,7 +74,7 @@ class Journey { List> tmp = []; for (final (segmentIdx, segment) in segments.indexed) { tmp.add({ - "route": id, + "journey": id, "segment": segment.id, "segmentorder": segmentIdx, }); @@ -91,13 +91,13 @@ class Journey { } factory Journey.fromMap( - Map routeMaps, + Map journeyMaps, List segmentMaps, ) { return Journey( - id: routeMaps["id"] as int, - startStation: routeMaps["startstation"] as String?, - endStation: routeMaps["endstation"] as String?, + id: journeyMaps["id"] as int, + startStation: journeyMaps["startstation"] as String?, + endStation: journeyMaps["endstation"] as String?, segments: segmentMaps, duration: Duration(minutes: 0), ); @@ -138,10 +138,10 @@ class Journey { segments.last.end.arrivalTime, ); } else { - // if the route has no segments yet, at least the startStation should remain, + // if the journey has no segments yet, at least the startStation should remain, // otherwise delete the thing // having a startStation makes the first step faster at least - // no need to put a mostly empty route into the db though + // no need to put a mostly empty journey into the db though endStation = null; duration = Duration(minutes: 0); } @@ -199,7 +199,7 @@ class Journey { String segment = Segment.tableName; database.execute("""CREATE TABLE IF NOT EXISTS routesegments( id INTEGER PRIMARY KEY AUTOINCREMENT, - route INTEGER, + journey INTEGER, segment INTEGER, segmentorder INTEGER, -- TODO: implement sorting FOREIGN KEY('$tableName') REFERENCES '$tableName'(id), @@ -216,9 +216,9 @@ class Journey { where: "id = ?", whereArgs: [id], ); - assert(result.length < 2, "found more than 1 route for id $id"); - Journey route = Journey.fromMap(result.first, await routeSegments); - return route; + assert(result.length < 2, "found more than 1 journey for id $id"); + Journey journey = Journey.fromMap(result.first, await routeSegments); + return journey; } /// gets routesegments and hands them to the Journey.fromMap factory, works async @@ -244,14 +244,14 @@ class Journey { } static Future> _dbGetJourneySegments( - int routeId, + int journeyId, ) async { Database database = DbHelper.db; String segment = Segment.tableName; var tmp = await database.rawQuery(""" SELECT * FROM '$segment' JOIN routesegments ON routesegments.segment = '$segment'.id - WHERE routesegments.route = '$routeId' + WHERE routesegments.journey = '$journeyId' ORDER BY routesegments.segmentorder ASC; """); @@ -273,15 +273,15 @@ class Journey { return id_tmp; } - static void dbAppendJourneySegment(int routeId, int segmentid) { + static void dbAppendJourneySegment(int journeyId, int segmentid) { Database database = DbHelper.db; - // generate segmentorder to be 0 if no segment exists for route, otherwise - // take highest segmentorder+1 for given route + // generate segmentorder to be 0 if no segment exists for journey, otherwise + // take highest segmentorder+1 for given journey database.rawInsert( - """INSERT INTO routesegments(route, segment, segmentorder) VALUES - ($routeId, $segmentid, CASE WHEN EXISTS ( - SELECT segmentorder FROM routesegments r WHERE r.route=$routeId) THEN ( - SELECT segmentorder AS so FROM routesegments r WHERE r.route=$routeId + """INSERT INTO routesegments(journey, segment, segmentorder) VALUES + ($journeyId, $segmentid, CASE WHEN EXISTS ( + SELECT segmentorder FROM routesegments r WHERE r.journey=$journeyId) THEN ( + SELECT segmentorder AS so FROM routesegments r WHERE r.journey=$journeyId ORDER BY so DESC LIMIT 1) +1 ELSE 0 END); """, ); @@ -322,7 +322,7 @@ class Journey { database.update( "routesegments", routeSegments[i], - where: "route = ? AND segmentoder = ?", + where: "journey = ? AND segmentoder = ?", whereArgs: [id, i], ); } @@ -342,7 +342,7 @@ class Journey { Database database = DbHelper.db; database.delete( "routesegments", - where: "route = ? AND segmentorder = ?", + where: "journey = ? AND segmentorder = ?", whereArgs: [id, idx], // TODO: test the index ); } @@ -351,7 +351,7 @@ class Journey { Database database = DbHelper.db; database.delete( "routesegments", - where: "route = ? AND segmentorder = ?", + where: "journey = ? AND segmentorder = ?", whereArgs: [id, segments.length - 1], // TODO: test the index ); } @@ -361,7 +361,7 @@ class Journey { if (position <= segments.length) { database.delete( "routesegments", - where: "route = ? AND segmentorder >= ?", + where: "journey = ? AND segmentorder >= ?", whereArgs: [id, position], ); } @@ -1173,4 +1173,4 @@ enum VehicleType { subway, regionalTrain, PLACEHOLDER, -} \ No newline at end of file +}