feat(database): add functions for DB and DB-table creation

This commit is contained in:
fbachus
2026-06-06 01:22:36 +02:00
parent 5fd3e8faad
commit 7cfc415f9f
4 changed files with 279 additions and 24 deletions
+3
View File
@@ -1,9 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'api_handler.dart';
import 'transport_helper.dart';
void main() async {
await dotenv.load();
const String dbName = "oeffimasterTransportDb";
initDB(dbName);
runApp(const MyApp());
}
+99 -17
View File
@@ -1,3 +1,24 @@
import 'package:sqflite/sqflite.dart';
void initDB(dbName) async {
var databasesPath = await getDatabasesPath();
var path = databasesPath + dbName;
var db = await openDatabase(path);
// var db = await openDatabase('/var/db/oeffimaster/transport_db.db');
// TODO: Make sure the directory exists
// TODO: future optimisation: batch transactions
// TODO: create tables for all classes
// - [] route
// - [] segment
// - [] station
// - [] vehicle
// - [] line
// - [] departure
}
class Route {
List<Segment> segments = List.empty();
Duration duration = Duration(minutes: 0);
@@ -15,6 +36,10 @@ class Route {
json["LegList"]["Leg"][0]["Origin"]["time"],
);
// TODO: do we need route in the database???? // what beyond and ID is needed?
void initRouteTable(Database database) async {
database.execute("CREATE TABLE IF NOT EXISTS route();");
}
}
class Segment {
@@ -39,22 +64,36 @@ class Segment {
endTime: json["Destination"]["rtTime"],
vehicle: Vehicle.fromJson(json["Product"]),
);
void initSegmentTable(Database database) async {
database.execute("""
CREATE TABLE IF NOT EXISTS segment(
startpoint TEXT,
endpoint TEXT,
starttime TEXT,
endtime TEXT,
vehicle INTEGER,
FOREIGN KEY(startpoint) REFERENCES station(stationid),
FOREIGN KEY(endpoint) REFERENCES station(stationid)
FOREIGN KEY(vehicle) REFERENCES vehicle(vehicleid)
);
""");
}
}
class Station {
final String id;
final String stationId;
final String name;
List<Line>? transportLines;
List<Departure>? departures;
List<Line>? transportLines; // could be normalized away
Station({
required this.id,
required this.stationId,
required this.name,
required this.transportLines
required this.transportLines, //TODO: make extra db table
});
factory Station.fromJson(Map<String, dynamic> json) => Station(
id: json["id"] as String? ?? json["extId"] as String,
stationId: json["id"] as String? ?? json["extId"] as String,
name: json["name"],
transportLines: json.containsKey("productAtStop")
? json["productAtStop"].map(
@@ -62,27 +101,44 @@ class Station {
)
: const [],
);
void initStationTable(Database database) async {
database.execute("""CREATE TABLE IF NOT EXISTS station(
stationid TEXT PRIMARY KEY,
name TEXT
);
""");
}
}
class Vehicle {
final String id;
final String vehicleId;
final Line line;
Vehicle({
required this.id,
required this.vehicleId,
required this.line,
});
// 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"],
vehicleId: json["matchId"],
line: Line.fromJson(json),
);
void initVehicleTable(Database database) async {
database.execute("""CREATE TABLE IF NOT EXISTS vehicle(
vehicleid TEXT PRIMARY KEY,
line TEXT,
FOREIGN KEY(line) REFERENCES line(lineid)
);
""");
}
}
class Line {
final String id;
final String lineId;
Station? destination;
Station? direction;
final VehicleType mode;
@@ -91,7 +147,7 @@ class Line {
Color? bgColor;
Line({
required this.id,
required this.lineId,
required this.name,
required this.mode,
this.fgColor,
@@ -103,18 +159,32 @@ class Line {
VehicleType.metro,
VehicleType.subway,
VehicleType.PLACEHOLDER,
VehicleType.bus
VehicleType.bus,
];
factory Line.fromJson(Map<String, dynamic> json) => Line(
id: json["lineId"],
lineId: json["lineId"],
name: json["name"],
mode: _vehicleTypeAsCatCode[int.parse(json["catCode"])],
fgColor: Color.fromJson(json["foregroundColor"]) as Color?
?? Color(r: 255, g: 255, b: 255), // fallback
bgColor: Color.fromJson(json["backgroundColor"]) as Color?
?? Color(r: 50, g: 50, b: 50), // fallback
fgColor:
Color.fromJson(json["foregroundColor"]) as Color? ??
Color(r: 255, g: 255, b: 255), // fallback
bgColor:
Color.fromJson(json["backgroundColor"]) as Color? ??
Color(r: 50, g: 50, b: 50), // fallback
);
void initLineTable(Database database) async {
database.execute("""CREATE TABLE IF NOT EXISTS line(
lineid TEXT PRIMARY KEY,
destination TEXT,
direction TEXT,
mode TEXT,
name TEXT,
fgcolor TEXT,
bgcolor TEXT
);
""");
}
}
class Departure {
@@ -128,6 +198,18 @@ class Departure {
required this.station,
required this.departureTime,
});
void initDepartureTable(Database database) async {
database.execute("""CREATE TABLE IF NOT EXISTS departure(
departureid INTEGER,
vehicle TEXT,
station TEXT,
departuretime TEXT
FOREIGN KEY(vehicle) REFERENCES vehicle(vehicleid),
FOREIGN KEY(station) REFERENCES station(stationid)
);
""");
}
}
class Color {