Files
fforte/lib/methods/excursion_db_helper.dart

215 lines
7.6 KiB
Dart

import 'package:fforte/interfaces/i_db.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:io' as io;
import 'package:path/path.dart';
// * Database helper for managing excursions
// * Provides CRUD operations for both main entries and templates
// * Uses SQLite database with two tables:
// * - excursion: Stores finalized excursion records
// * - excursionTemplates: Stores template entries for quick creation
/// Database helper class implementing IDb interface
/// Handles all database operations for excursions and tracking data
class ExcursionDBHelper implements IDb {
// Database instance
static Database? _excursionDB;
/// Get database instance, creating it if necessary
/// Returns existing database or initializes a new one
@override
Future<Database> get dB async {
if (_excursionDB != null) {
return _excursionDB!;
}
_excursionDB = await initDatabases();
return _excursionDB!;
}
/// Initialize the database
/// Creates database file and tables if they don't exist
@override
initDatabases() async {
io.Directory documentsDirectory = await getApplicationCacheDirectory();
String path = join(documentsDirectory.path, 'excursionDB.db');
var excursionDB = await openDatabase(
path,
version: 1,
onCreate: onCreateDatabases,
);
return excursionDB;
}
/// Create database tables
/// Sets up schema for both main entries and templates
@override
onCreateDatabases(Database excursionDB, int version) async {
// Create main excursions table with tracking data fields
await excursionDB.execute(
'CREATE TABLE excursion (ID INTEGER PRIMARY KEY AUTOINCREMENT, Rudel TEXT, Teilnehmer TEXT, Datum TEXT, Dauer TEXT, MHund INTEGER, MLeine TEXT, BLand TEXT, Lkr TEXT, BeiOrt TEXT, BimaName TEXT, Wetter TEXT, Temperat TEXT, RegenVor TEXT, KmRad TEXT, KmAuto TEXT, KmFuss TEXT, KmTotal TEXT, KmAuProz TEXT, KmFuProz TEXT, KmRaProz TEXT, SpGut TEXT, SpMittel, SpSchlecht TEXT, SpurFund TEXT, SpurLang TEXT, SpurTiere Text, SpSicher TEXT, WelpenSp TEXT, WelpenAnz TEXT, WpSicher TEXT, LosungGes TEXT, LosungAnz TEXT, LosungGen TEXT, UrinAnz TEXT, UrinGen TEXT, OestrAnz TEXT, OestrGen TEXT, HaarAnz TEXT, HaarGen TEXT, LosungKm TEXT, GenetiKm TEXT, Hinweise TEXT, Bemerk TEXT, IntKomm TEXT, BimaNr TEXT, BimaNutzer TEXT, BimaAGV TEXT, FallNum INTEGER, Weg TEXT, Sent INTEGER DEFAULT 0)',
);
// Create templates table (similar structure but without Sent flag)
await excursionDB.execute(
'CREATE TABLE excursionTemplates (ID INTEGER PRIMARY KEY AUTOINCREMENT, Rudel TEXT, Teilnehmer TEXT, Datum TEXT, Dauer TEXT, MHund INTEGER, MLeine TEXT, BLand TEXT, Lkr TEXT, BeiOrt TEXT, BimaName TEXT, Wetter TEXT, Temperat TEXT, RegenVor TEXT, KmRad TEXT, KmAuto TEXT, KmFuss TEXT, KmTotal TEXT, KmAuProz TEXT, KmFuProz TEXT, KmRaProz TEXT, SpGut TEXT, SpMittel, SpSchlecht TEXT, SpurFund TEXT, SpurLang TEXT, SpurTiere Text, SpSicher TEXT, WelpenSp TEXT, WelpenAnz TEXT, WpSicher TEXT, LosungGes TEXT, LosungAnz TEXT, LosungGen TEXT, UrinAnz TEXT, UrinGen TEXT, OestrAnz TEXT, OestrGen TEXT, HaarAnz TEXT, HaarGen TEXT, LosungKm TEXT, GenetiKm TEXT, Hinweise TEXT, Bemerk TEXT, IntKomm TEXT, BimaNr TEXT, BimaNutzer TEXT, BimaAGV TEXT, FallNum INTEGER, Weg TEXT)',
);
}
/// Add a new main entry to the database
/// @param excursion Map containing the excursion data
/// @return ID of the newly inserted entry
@override
Future<int> addMainEntry(Map<String, String> excursion) async {
var excursionDBClient = await dB;
// Commented out code for handling existing entries
// final existingID = await excursionDBClient.query(
// 'excursion',
// where: 'ID = ?',
// whereArgs: [excursion['ID']],
// );
// if (existingID.isNotEmpty) {
// updateMainEntry(excursion);
// return existingID.first['ID'] as int;
// }
int id = await excursionDBClient.insert(
'excursion',
excursion,
conflictAlgorithm: ConflictAlgorithm.replace,
);
return id;
}
/// Update an existing main entry
/// @param excursion Map containing the updated excursion data
/// @return Number of rows affected
@override
Future<int> updateMainEntry(Map<String, String> excursion) async {
var excursionDBClient = await dB;
return await excursionDBClient.update(
'excursion',
excursion,
where: "ID = ?",
whereArgs: [excursion['ID']],
);
}
/// Mark an entry as sent to the server
/// @param id ID of the entry to update
@override
Future<void> updateSent(int id) async {
var excursionDBClient = await dB;
await excursionDBClient.update(
'excursion',
{'Sent': 1},
where: 'ID = ?',
whereArgs: [id],
);
}
/// Add a new template entry
/// @param templates Map containing the template data
/// @return ID of the newly inserted template
@override
Future<int> addTemplate(Map<String, String> templates) async {
var excursionDBClient = await dB;
// Commented out code for handling existing templates
// final existingCID = await excursionDBClient.query(
// 'excursionTemplates',
// where: 'ID = ?',
// whereArgs: [templates['ID']],
// );
// if (existingCID.isNotEmpty) {
// return;
// }
int id = await excursionDBClient.insert(
'excursionTemplates',
templates,
);
return id;
}
/// Update an existing template
/// @param template Map containing the updated template data
@override
Future<void> updateTemplate(Map<String, String> template) async {
var excursionDBClient = await dB;
await excursionDBClient.update(
'excursionTemplates',
template,
where: "ID = ?",
whereArgs: [template['ID']],
);
}
/// Retrieve all main entries from the database
/// @return List of all excursions or empty list if none exist
@override
Future<List<Map<String, dynamic>>> getAllMainEntries() async {
var excursionDBClient = await dB;
var erg = await excursionDBClient.query('excursion');
if (erg.isEmpty) {
return [];
} else {
return erg;
}
}
/// Retrieve all templates from the database
/// @return List of all templates or empty list if none exist
@override
Future<List<Map<String, dynamic>>> getAllTemplates() async {
var excursionDBClient = await dB;
var erg = await excursionDBClient.query('excursionTemplates');
if (erg.isEmpty) {
return [];
} else {
return erg;
}
}
/// Delete all main entries from the local database
/// Note: This only affects the local database, not the server
@override
Future<void> deleteAllMainEntries() async {
var excursionDBClient = await dB;
await excursionDBClient.delete('excursion');
}
/// Delete all templates from the local database
/// Note: This only affects the local database, not the server
@override
Future<void> deleteAllTemplates() async {
var excursionDBClient = await dB;
await excursionDBClient.delete('excursionTemplates');
}
/// Delete a specific template by ID
/// @param id ID of the template to delete
@override
Future<void> deleteTemplateById(String id) async {
var excursionDBClient = await dB;
await excursionDBClient.delete(
'excursionTemplates',
where: 'ID = ?',
whereArgs: [id],
);
}
/// Delete a specific main entry by ID
/// @param id ID of the entry to delete
@override
Future<void> deleteMainEntryById(String id) async {
var excursionDBClient = await dB;
await excursionDBClient.delete(
'excursion',
where: 'ID = ?',
whereArgs: [id],
);
}
}