Public Access
Compare commits
2
Commits
v0.0.1
...
0372185e96
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0372185e96 | ||
|
|
168ff7b9a4 |
+41
-36
@@ -41,11 +41,11 @@ class DbHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// this class covers two database tables: "route" and "routesegments"
|
/// this class covers two database tables: "journey" and "routesegments"
|
||||||
/// while route will for now only hold an id, routesegments will store the
|
/// while journey 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
|
/// m-n/ 1-n relations between journeys and segments - the segments contain the most
|
||||||
/// relevant info, while routesegments stores which route the segments belong to
|
/// relevant info, while routesegments stores which journey the segments belong to
|
||||||
/// and in which order they make up a route
|
/// and in which order they make up a journey
|
||||||
class Journey {
|
class Journey {
|
||||||
static const String tableName = "journey";
|
static const String tableName = "journey";
|
||||||
// TODO: add last_viewed, last_modified and such
|
// TODO: add last_viewed, last_modified and such
|
||||||
@@ -63,7 +63,7 @@ class Journey {
|
|||||||
required this.duration,
|
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<String, Object?> _toMapJourney() {
|
Map<String, Object?> _toMapJourney() {
|
||||||
return {"id": id, "startstation": startStation, "endstation": endStation};
|
return {"id": id, "startstation": startStation, "endstation": endStation};
|
||||||
}
|
}
|
||||||
@@ -74,7 +74,7 @@ class Journey {
|
|||||||
List<Map<String, Object?>> tmp = [];
|
List<Map<String, Object?>> tmp = [];
|
||||||
for (final (segmentIdx, segment) in segments.indexed) {
|
for (final (segmentIdx, segment) in segments.indexed) {
|
||||||
tmp.add({
|
tmp.add({
|
||||||
"route": id,
|
"journey": id,
|
||||||
"segment": segment.id,
|
"segment": segment.id,
|
||||||
"segmentorder": segmentIdx,
|
"segmentorder": segmentIdx,
|
||||||
});
|
});
|
||||||
@@ -91,13 +91,13 @@ class Journey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
factory Journey.fromMap(
|
factory Journey.fromMap(
|
||||||
Map<String, Object?> routeMaps,
|
Map<String, Object?> journeyMaps,
|
||||||
List<Segment> segmentMaps,
|
List<Segment> segmentMaps,
|
||||||
) {
|
) {
|
||||||
return Journey(
|
return Journey(
|
||||||
id: routeMaps["id"] as int,
|
id: journeyMaps["id"] as int,
|
||||||
startStation: routeMaps["startstation"] as String?,
|
startStation: journeyMaps["startstation"] as String?,
|
||||||
endStation: routeMaps["endstation"] as String?,
|
endStation: journeyMaps["endstation"] as String?,
|
||||||
segments: segmentMaps,
|
segments: segmentMaps,
|
||||||
duration: Duration(minutes: 0),
|
duration: Duration(minutes: 0),
|
||||||
);
|
);
|
||||||
@@ -138,10 +138,10 @@ class Journey {
|
|||||||
segments.last.end.arrivalTime,
|
segments.last.end.arrivalTime,
|
||||||
);
|
);
|
||||||
} else {
|
} 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
|
// otherwise delete the thing
|
||||||
// having a startStation makes the first step faster at least
|
// 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;
|
endStation = null;
|
||||||
duration = Duration(minutes: 0);
|
duration = Duration(minutes: 0);
|
||||||
}
|
}
|
||||||
@@ -199,7 +199,7 @@ class Journey {
|
|||||||
String segment = Segment.tableName;
|
String segment = Segment.tableName;
|
||||||
database.execute("""CREATE TABLE IF NOT EXISTS routesegments(
|
database.execute("""CREATE TABLE IF NOT EXISTS routesegments(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
route INTEGER,
|
journey INTEGER,
|
||||||
segment INTEGER,
|
segment INTEGER,
|
||||||
segmentorder INTEGER, -- TODO: implement sorting
|
segmentorder INTEGER, -- TODO: implement sorting
|
||||||
FOREIGN KEY('$tableName') REFERENCES '$tableName'(id),
|
FOREIGN KEY('$tableName') REFERENCES '$tableName'(id),
|
||||||
@@ -216,9 +216,9 @@ class Journey {
|
|||||||
where: "id = ?",
|
where: "id = ?",
|
||||||
whereArgs: [id],
|
whereArgs: [id],
|
||||||
);
|
);
|
||||||
assert(result.length < 2, "found more than 1 route for id $id");
|
assert(result.length < 2, "found more than 1 journey for id $id");
|
||||||
Journey route = Journey.fromMap(result.first, await routeSegments);
|
Journey journey = Journey.fromMap(result.first, await routeSegments);
|
||||||
return route;
|
return journey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// gets routesegments and hands them to the Journey.fromMap factory, works async
|
/// gets routesegments and hands them to the Journey.fromMap factory, works async
|
||||||
@@ -244,14 +244,14 @@ class Journey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Future<List<Segment>> _dbGetJourneySegments(
|
static Future<List<Segment>> _dbGetJourneySegments(
|
||||||
int routeId,
|
int journeyId,
|
||||||
) async {
|
) async {
|
||||||
Database database = DbHelper.db;
|
Database database = DbHelper.db;
|
||||||
String segment = Segment.tableName;
|
String segment = Segment.tableName;
|
||||||
var tmp = await database.rawQuery("""
|
var tmp = await database.rawQuery("""
|
||||||
SELECT * FROM '$segment'
|
SELECT * FROM '$segment'
|
||||||
JOIN routesegments ON routesegments.segment = '$segment'.id
|
JOIN routesegments ON routesegments.segment = '$segment'.id
|
||||||
WHERE routesegments.route = '$routeId'
|
WHERE routesegments.journey = '$journeyId'
|
||||||
ORDER BY routesegments.segmentorder ASC;
|
ORDER BY routesegments.segmentorder ASC;
|
||||||
""");
|
""");
|
||||||
|
|
||||||
@@ -273,15 +273,15 @@ class Journey {
|
|||||||
return id_tmp;
|
return id_tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dbAppendJourneySegment(int routeId, int segmentid) {
|
static void dbAppendJourneySegment(int journeyId, int segmentid) {
|
||||||
Database database = DbHelper.db;
|
Database database = DbHelper.db;
|
||||||
// generate segmentorder to be 0 if no segment exists for route, otherwise
|
// generate segmentorder to be 0 if no segment exists for journey, otherwise
|
||||||
// take highest segmentorder+1 for given route
|
// take highest segmentorder+1 for given journey
|
||||||
database.rawInsert(
|
database.rawInsert(
|
||||||
"""INSERT INTO routesegments(route, segment, segmentorder) VALUES
|
"""INSERT INTO routesegments(journey, segment, segmentorder) VALUES
|
||||||
($routeId, $segmentid, CASE WHEN EXISTS (
|
($journeyId, $segmentid, CASE WHEN EXISTS (
|
||||||
SELECT segmentorder FROM routesegments r WHERE r.route=$routeId) THEN (
|
SELECT segmentorder FROM routesegments r WHERE r.journey=$journeyId) THEN (
|
||||||
SELECT segmentorder AS so FROM routesegments r WHERE r.route=$routeId
|
SELECT segmentorder AS so FROM routesegments r WHERE r.journey=$journeyId
|
||||||
ORDER BY so DESC LIMIT 1) +1 ELSE 0 END);
|
ORDER BY so DESC LIMIT 1) +1 ELSE 0 END);
|
||||||
""",
|
""",
|
||||||
);
|
);
|
||||||
@@ -322,7 +322,7 @@ class Journey {
|
|||||||
database.update(
|
database.update(
|
||||||
"routesegments",
|
"routesegments",
|
||||||
routeSegments[i],
|
routeSegments[i],
|
||||||
where: "route = ? AND segmentoder = ?",
|
where: "journey = ? AND segmentoder = ?",
|
||||||
whereArgs: [id, i],
|
whereArgs: [id, i],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -342,7 +342,7 @@ class Journey {
|
|||||||
Database database = DbHelper.db;
|
Database database = DbHelper.db;
|
||||||
database.delete(
|
database.delete(
|
||||||
"routesegments",
|
"routesegments",
|
||||||
where: "route = ? AND segmentorder = ?",
|
where: "journey = ? AND segmentorder = ?",
|
||||||
whereArgs: [id, idx], // TODO: test the index
|
whereArgs: [id, idx], // TODO: test the index
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -351,7 +351,7 @@ class Journey {
|
|||||||
Database database = DbHelper.db;
|
Database database = DbHelper.db;
|
||||||
database.delete(
|
database.delete(
|
||||||
"routesegments",
|
"routesegments",
|
||||||
where: "route = ? AND segmentorder = ?",
|
where: "journey = ? AND segmentorder = ?",
|
||||||
whereArgs: [id, segments.length - 1], // TODO: test the index
|
whereArgs: [id, segments.length - 1], // TODO: test the index
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -361,7 +361,7 @@ class Journey {
|
|||||||
if (position <= segments.length) {
|
if (position <= segments.length) {
|
||||||
database.delete(
|
database.delete(
|
||||||
"routesegments",
|
"routesegments",
|
||||||
where: "route = ? AND segmentorder >= ?",
|
where: "journey = ? AND segmentorder >= ?",
|
||||||
whereArgs: [id, position],
|
whereArgs: [id, position],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -493,7 +493,7 @@ class Segment {
|
|||||||
FROM $tableName s
|
FROM $tableName s
|
||||||
JOIN $departure start ON s.startid = start.id
|
JOIN $departure start ON s.startid = start.id
|
||||||
JOIN $departure end ON s.endid = end.id
|
JOIN $departure end ON s.endid = end.id
|
||||||
JOIN $vehicle v ON s.vehicleid = v.id
|
JOIN $vehicle v ON s.vehicleid = v.vehicleid
|
||||||
where s.id = $id;
|
where s.id = $id;
|
||||||
""",
|
""",
|
||||||
);
|
);
|
||||||
@@ -526,7 +526,7 @@ class Segment {
|
|||||||
"departuretime": seg["end_departuretime"] as String,
|
"departuretime": seg["end_departuretime"] as String,
|
||||||
};
|
};
|
||||||
Map<String, String> vehicleMap = {
|
Map<String, String> vehicleMap = {
|
||||||
"id": seg["vehicleid"] as String,
|
"vehicleid": seg["vehicleid"] as String,
|
||||||
"name": seg["vehiclename"] as String,
|
"name": seg["vehiclename"] as String,
|
||||||
"line": seg["vehicleline"] as String,
|
"line": seg["vehicleline"] as String,
|
||||||
};
|
};
|
||||||
@@ -735,7 +735,7 @@ class Vehicle {
|
|||||||
|
|
||||||
Map<String, Object?> _toMap() {
|
Map<String, Object?> _toMap() {
|
||||||
return {
|
return {
|
||||||
"id": vehicleid,
|
"vehicleid": vehicleid,
|
||||||
"name": name,
|
"name": name,
|
||||||
"line": vehicleLineId,
|
"line": vehicleLineId,
|
||||||
};
|
};
|
||||||
@@ -745,7 +745,7 @@ class Vehicle {
|
|||||||
Map<String, Object?> map,
|
Map<String, Object?> map,
|
||||||
) {
|
) {
|
||||||
return Vehicle(
|
return Vehicle(
|
||||||
vehicleid: map["id"] as String,
|
vehicleid: map["vehicleid"] as String,
|
||||||
name: map["name"] as String,
|
name: map["name"] as String,
|
||||||
vehicleLineId: map["line"] as String,
|
vehicleLineId: map["line"] as String,
|
||||||
);
|
);
|
||||||
@@ -775,7 +775,7 @@ class Vehicle {
|
|||||||
Database database = DbHelper.db;
|
Database database = DbHelper.db;
|
||||||
var vehicleMap = await database.query(
|
var vehicleMap = await database.query(
|
||||||
tableName,
|
tableName,
|
||||||
where: "id = ?",
|
where: "vehicleid = ?",
|
||||||
whereArgs: [vehicleId],
|
whereArgs: [vehicleId],
|
||||||
);
|
);
|
||||||
return Vehicle.fromMap(vehicleMap.first);
|
return Vehicle.fromMap(vehicleMap.first);
|
||||||
@@ -792,7 +792,12 @@ class Vehicle {
|
|||||||
|
|
||||||
void dbUpdate() async {
|
void dbUpdate() async {
|
||||||
Database database = DbHelper.db;
|
Database database = DbHelper.db;
|
||||||
database.update(tableName, _toMap(), where: "id=?", whereArgs: [vehicleid]);
|
database.update(
|
||||||
|
tableName,
|
||||||
|
_toMap(),
|
||||||
|
where: "vehicleid=?",
|
||||||
|
whereArgs: [vehicleid],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dbDelete() async {
|
void dbDelete() async {
|
||||||
|
|||||||
Reference in New Issue
Block a user