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 DBHelper { static Database? _placeDB; // checks if the databses are existing and creates them with the initPlaceDatabase function if not Future get placeDB async { if (_placeDB != null) { return _placeDB!; } _placeDB = await initPlaceDatabase(); return _placeDB!; } // Creates the databases with help from the _onCreatePlace function initPlaceDatabase() async { io.Directory documentsDirectory = await getApplicationCacheDirectory(); String path = join(documentsDirectory.path, 'placeDB.db'); var placeDB = await openDatabase(path, version: 1, onCreate: _onCreatePlace); return placeDB; } // The function that helps _onCreatePlace(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, STTyp TEXT, FFTyp TEXT, FotoFilm TEXT, MEZ TEXT, Platzung TEXT, KSchloNr TEXT, Bearsafe 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 templates (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, STTyp TEXT, FFTyp TEXT, FotoFilm TEXT, MEZ TEXT, Platzung TEXT, KSchloNr TEXT, Bearsafe 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 entrie Future addPlace(Map place) async { var placeDBClient = await placeDB; // gets an camid if it already exists final existingID = await placeDBClient.query( 'place', where: 'ID = ?', whereArgs: [place['ID']], ); // checks if the camid var from before is empty to avoid double entries if (existingID.isNotEmpty) { updatePlace(place); return; } // inserts the entrie in the database await placeDBClient.insert( 'place', place, // replaces the entrie with the new onw if a unique value exists and conflicts // conflictAlgorithm: ConflictAlgorithm.replace, ); } Future updatePlace(Map place) async { var placeDBClient = await placeDB; await placeDBClient.update( 'place', place, where: "ID = ?", whereArgs: [place['ID']] ); } // function to update the sent value Future updateSent() async { var placeDBClient = await placeDB; placeDBClient.update('place', true as Map, where: 'ID = ?', whereArgs: ['ID']); } // same thing as before but with templatews Future addTemplate(Map templates) async { var placeDBClient = await placeDB; final existingCID = await placeDBClient.query( 'templates', where: 'ID = ?', whereArgs: [templates['ID']], ); if (existingCID.isNotEmpty) { return; } await placeDBClient.insert( 'templates', templates, // conflictAlgorithm: ConflictAlgorithm.replace, ); } // Updates a existing template Future updateTemplate(Map template) async { var placeDBClient = await placeDB; await placeDBClient.update( 'templates', template, where: "ID = ?", whereArgs: [template['ID']], ); } // get the finished entries from db Future>> getPlace() async { var placeDBClient = await placeDB; return await placeDBClient.query('place'); } // get the finished templates from db Future>> getTemplates() async { var placeDBClient = await placeDB; return await placeDBClient.query('templates'); } // deletes all finished entries from the db LOCALLY Future deleteAllPlaces() async { var placeDBClient = await placeDB; await placeDBClient.delete('place'); } // deletes all templates from the db LOCALLY Future deleteAllTemplates() async { var placeDBClient = await placeDB; await placeDBClient.delete('templates'); } // delete specific template Future deleteTemplate(String id) async { var placeDBClient = await placeDB; await placeDBClient.delete( 'templates', where: 'ID = ?', whereArgs: [id], ); } // delete specific place Future deletePlace(String id) async { var placeDBClient = await placeDB; await placeDBClient.delete( 'place', where: 'ID = ?', whereArgs: [id], ); } }