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'; // * Gives the complete functionality for the databases // ! functions may not be named complete correctly class PlaceDBHelper implements IDb{ static Database? _dB; // checks if the databses are existing and creates them with the initPlaceDatabase function if not @override Future get dB async { if (_dB != null) { return _dB!; } _dB = await initDatabases(); return _dB!; } // Creates the databases with help from the _onCreatePlace function @override initDatabases() async { io.Directory documentsDirectory = await getApplicationCacheDirectory(); String path = join(documentsDirectory.path, 'placeDB.db'); var placeDB = await openDatabase(path, version: 1, onCreate: onCreateDatabases); return placeDB; } // The function that helps @override onCreateDatabases(Database placeDB, int version) async { await placeDB.execute( 'CREATE TABLE place (ID INTEGER PRIMARY KEY AUTOINCREMENT, CID TEXT, Standort TEXT, Rudel TEXT, Datum DATE, Adresse1 TEXT, Adresse2 TEXT, Adresse3 TEXT, BLand TEXT, Lkr TEXT, BeiOrt TEXT, OrtInfo TEXT, Status TEXT, FFTyp TEXT, FotoFilm TEXT, MEZ TEXT, Platzung TEXT, KSchloNr TEXT, KontDat DATE, Betreuung TEXT, AbbauDat DATE, Auftrag TEXT, KontAbsp TEXT, SonstBem TEXT, FKontakt1 TEXT, FKontakt2 TEXT, FKontakt3 TEXT, KTage1 INTEGER, KTage2 INTEGER, ProtoAm DATE, IntKomm TEXT, DECLNG DECIMALS(4,8), DECLAT DECIMALS(4,8), Sent INTEGER DEFAULT 0)'); await placeDB.execute( 'CREATE TABLE placeTemplates (ID INTEGER PRIMARY KEY AUTOINCREMENT, CID TEXT, Standort TEXT, Rudel TEXT, Datum DATE, Adresse1 TEXT, Adresse2 TEXT, Adresse3 TEXT, BLand TEXT, Lkr TEXT, BeiOrt TEXT, OrtInfo TEXT, Status TEXT, FFTyp TEXT, FotoFilm TEXT, MEZ TEXT, Platzung TEXT, KSchloNr TEXT, KontDat DATE, Betreuung TEXT, AbbauDat DATE, Auftrag TEXT, KontAbsp TEXT, SonstBem TEXT, FKontakt1 TEXT, FKontakt2 TEXT, FKontakt3 TEXT, KTage1 INTEGER, KTage2 INTEGER, ProtoAm DATE, IntKomm TEXT, DECLNG DECIMALS(4,8), DECLAT DECIMALS(4,8))'); } // Function to add a finished entry and return its ID @override Future addMainEntry(Map place) async { var placeDBClient = await dB; // final existingID = await placeDBClient.query( // 'place', // where: 'ID = ?', // whereArgs: [place['ID']], // ); // // if (existingID.isNotEmpty) { // updateMainEntry(place); // return existingID.first['ID'] as int; // Return existing ID // } int id = await placeDBClient.insert( 'place', place, //conflictAlgorithm: ConflictAlgorithm.replace, ); return id; // Return the ID of the newly inserted entry } @override Future updateMainEntry(Map place) async { var placeDBClient = await dB; return await placeDBClient .update('place', place, where: "ID = ?", whereArgs: [place['ID']]); } // function to update the sent value @override Future updateSent(int id) async { var placeDBClient = await dB; await placeDBClient.update('place', {'Sent': 1}, where: 'ID = ?', whereArgs: [id]); } // same thing as before but with templatews @override Future addTemplate(Map templates) async { var placeDBClient = await dB; // final existingCID = await placeDBClient.query( // 'placeTemplates', // where: 'ID = ?', // whereArgs: [templates['ID']], // ); // if (existingCID.isNotEmpty) { // return; // } await placeDBClient.insert( 'placeTemplates', templates, // conflictAlgorithm: ConflictAlgorithm.replace, ); } // Updates a existing template @override Future updateTemplate(Map template) async { var placeDBClient = await dB; await placeDBClient.update( 'placeTemplates', template, where: "ID = ?", whereArgs: [template['ID']], ); } // get the finished entries from db @override Future>> getAllMainEntries() async { var placeDBClient = await dB; var erg = await placeDBClient.query('place'); if (erg.isEmpty) { return []; } else { return erg; } } // get the finished templates from db @override Future>> getAllTemplates() async { var placeDBClient = await dB; var erg = await placeDBClient.query('placeTemplates'); if (erg.isEmpty) { return []; } else { return erg; } } // deletes all finished entries from the db LOCALLY @override Future deleteAllMainEntries() async { var placeDBClient = await dB; await placeDBClient.delete('place'); } // deletes all templates from the db LOCALLY @override Future deleteAllTemplates() async { var placeDBClient = await dB; await placeDBClient.delete('placeTemplates'); } // delete specific template @override Future deleteTemplateById(String id) async { var placeDBClient = await dB; await placeDBClient.delete( 'placeTemplates', where: 'ID = ?', whereArgs: [id], ); } // delete specific place @override Future deleteMainEntryById(String id) async { var placeDBClient = await dB; await placeDBClient.delete( 'place', where: 'ID = ?', whereArgs: [id], ); } }