Public Access
feat(database): add queries for all tables
This commit is contained in:
+209
-13
@@ -76,6 +76,17 @@ class Route {
|
||||
return tmp;
|
||||
}
|
||||
|
||||
factory Route.fromMap(
|
||||
Map<String, Object?> routeMaps,
|
||||
List<Segment> 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<String, dynamic> json) {
|
||||
@@ -111,6 +122,32 @@ class Route {
|
||||
""");
|
||||
}
|
||||
|
||||
static Future<Route> dbGet(int id) async {
|
||||
Database database = DbHelper.db;
|
||||
Future<List<Segment>> 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<List<Segment>> 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<String, Object?> 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<String, dynamic> json) {
|
||||
Database database = DbHelper.db;
|
||||
Segment tmp = Segment(
|
||||
@@ -224,6 +272,21 @@ class Segment {
|
||||
""");
|
||||
}
|
||||
|
||||
static Future<Segment> 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<List<Segment>> 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<Line> transportLines; // could be normalized away
|
||||
List<Line> transportLines = List.empty(); // could be normalized away
|
||||
|
||||
Station({
|
||||
required this.id,
|
||||
@@ -264,6 +327,17 @@ class Station {
|
||||
.toList();
|
||||
}
|
||||
|
||||
factory Station.fromMap(
|
||||
Map<String, Object?> map, {
|
||||
List<Line>? stationLines,
|
||||
}) {
|
||||
return Station(
|
||||
id: map["id"] as String,
|
||||
name: map["name"] as String,
|
||||
transportLines: stationLines ?? List.empty(),
|
||||
);
|
||||
}
|
||||
|
||||
factory Station.fromJson(Map<String, dynamic> json) => Station(
|
||||
id: json["id"] as String? ?? json["extId"] as String,
|
||||
name: json["name"],
|
||||
@@ -295,6 +369,30 @@ class Station {
|
||||
""");
|
||||
}
|
||||
|
||||
static Future<Station> 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<List<Line>> 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<String, Object?> toMap() {
|
||||
return {
|
||||
"id": id,
|
||||
"line": line.lineId,
|
||||
"line": vehicleLineId,
|
||||
};
|
||||
}
|
||||
|
||||
factory Vehicle.fromMap(
|
||||
Map<String, Object?> 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<String, dynamic> 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<Vehicle> 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<String, Object?> 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<Line> 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<String, Object?> toMap() {
|
||||
return {
|
||||
"id": id,
|
||||
"vehicle": vehicle,
|
||||
"station": station,
|
||||
"vehicle": vehicleId,
|
||||
"station": stationId,
|
||||
"departuretime": departureTime,
|
||||
};
|
||||
}
|
||||
|
||||
factory Departure.fromMap(Map<String, Object?> 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<List<Departure>> 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<List<Departure>> 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<Departure> 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());
|
||||
|
||||
Reference in New Issue
Block a user