Files
fforte/lib/methods/excursion_db_helper.dart
2025-05-15 22:11:31 +02:00

191 lines
6.2 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 ExcursionDBHelper implements IDb {
static Database? _excursionDB;
// checks if the databses are existing and creates them with the initPlaceDatabase function if not
@override
Future<Database> get dB async {
if (_excursionDB != null) {
return _excursionDB!;
}
_excursionDB = await initDatabases();
return _excursionDB!;
}
// Creates the databases with help from the _onCreateExcursion function
@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;
}
// The function that helps
@override
onCreateDatabases(Database excursionDB, int version) async {
await excursionDB.execute(
'CREATE TABLE excursion (ID INTEGER PRIMARY KEY AUTOINCREMENT, LogDat TEXT, Rudel TEXT, Teilnehmer TEXT, Jahr 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)',
);
await excursionDB.execute(
'CREATE TABLE excursionTemplates (ID INTEGER PRIMARY KEY AUTOINCREMENT, LogDat TEXT, Rudel TEXT, Teilnehmer TEXT, Jahr 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)',
);
}
// Function to add a finished entry and return its ID
@override
Future<int> addMainEntry(Map<String, String> excursion) async {
var excursionDBClient = await dB;
final existingID = await excursionDBClient.query(
'excursion',
where: 'ID = ?',
whereArgs: [excursion['ID']],
);
if (existingID.isNotEmpty) {
updateMainEntry(excursion);
return existingID.first['ID'] as int; // Return existing ID
}
int id = await excursionDBClient.insert(
'excursion',
excursion,
//conflictAlgorithm: ConflictAlgorithm.replace,
);
return id; // Return the ID of the newly inserted entry
}
@override
Future<int> updateMainEntry(Map<String, String> excursion) async {
var excursionDBClient = await dB;
return await excursionDBClient.update(
'excursion',
excursion,
where: "ID = ?",
whereArgs: [excursion['ID']],
);
}
// function to update the sent value
@override
Future<void> updateSent(int id) async {
var excursionDBClient = await dB;
await excursionDBClient.update(
'excursion',
{'Sent': 1},
where: 'ID = ?',
whereArgs: [id],
);
}
// same thing as before but with templatews
@override
Future<int> addTemplate(Map<String, String> templates) async {
var excursionDBClient = await dB;
// final existingCID = await excursionDBClient.query(
// 'excursionTemplates',
// where: 'ID = ?',
// whereArgs: [templates['ID']],
// );
// if (existingCID.isNotEmpty) {
// return;
// }
int id = await excursionDBClient.insert(
'excursionTemplates',
templates,
// conflictAlgorithm: ConflictAlgorithm.replace,
);
return id;
}
// Updates a existing template
@override
Future<void> updateTemplate(Map<String, String> template) async {
var excursionDBClient = await dB;
await excursionDBClient.update(
'excursionTemplates',
template,
where: "ID = ?",
whereArgs: [template['ID']],
);
}
// get the finished entries from db
@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;
}
}
// get the finished templates from db
@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;
}
}
// deletes all finished entries from the db LOCALLY
@override
Future<void> deleteAllMainEntries() async {
var excursionDBClient = await dB;
await excursionDBClient.delete('excursion');
}
// deletes all templates from the db LOCALLY
@override
Future<void> deleteAllTemplates() async {
var excursionDBClient = await dB;
await excursionDBClient.delete('excursionTemplates');
}
// delete specific template
@override
Future<void> deleteTemplateById(String id) async {
var excursionDBClient = await dB;
await excursionDBClient.delete(
'excursionTemplates',
where: 'ID = ?',
whereArgs: [id],
);
}
// delete specific excursion
@override
Future<void> deleteMainEntryById(String id) async {
var excursionDBClient = await dB;
await excursionDBClient.delete(
'excursion',
where: 'ID = ?',
whereArgs: [id],
);
}
}