Files
fforte/lib/methods/place_db_helper.dart
Nico ecafe2df02 added sent and id column again for add_cam_main
id checks if entry already exists. sent says if the entry is already
sent to the db
2025-05-12 18:18:36 +02:00

167 lines
5.3 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';
// * 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<Database> 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<int> addMainEntry(Map<String, String> 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<int> updateMainEntry(Map<String, String> 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<void> 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<void> addTemplate(Map<String, String> 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<void> updateTemplate(Map<String, String> template) async {
var placeDBClient = await dB;
await placeDBClient.update(
'placeTemplates',
template,
where: "ID = ?",
whereArgs: [template['ID']],
);
}
// get the finished entries from db
@override
Future<List<Map<String, dynamic>>> getAllMainEntries() async {
var placeDBClient = await dB;
return await placeDBClient.query('place');
}
// get the finished templates from db
@override
Future<List<Map<String, dynamic>>> getAllTemplates() async {
var placeDBClient = await dB;
return await placeDBClient.query('placeTemplates');
}
// deletes all finished entries from the db LOCALLY
@override
Future<void> deleteAllMainEntries() async {
var placeDBClient = await dB;
await placeDBClient.delete('place');
}
// deletes all templates from the db LOCALLY
@override
Future<void> deleteAllTemplates() async {
var placeDBClient = await dB;
await placeDBClient.delete('placeTemplates');
}
// delete specific template
@override
Future<void> deleteTemplateById(String id) async {
var placeDBClient = await dB;
await placeDBClient.delete(
'placeTemplates',
where: 'ID = ?',
whereArgs: [id],
);
}
// delete specific place
@override
Future<void> deleteMainEntryById(String id) async {
var placeDBClient = await dB;
await placeDBClient.delete(
'place',
where: 'ID = ?',
whereArgs: [id],
);
}
}