202 lines
6.7 KiB
Dart
202 lines
6.7 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 camera trap locations (places)
|
|
// * Provides CRUD operations for both main entries and templates
|
|
// * Uses SQLite database with two tables:
|
|
// * - place: Stores finalized camera trap locations
|
|
// * - placeTemplates: Stores template entries for quick creation
|
|
|
|
/// Database helper class implementing IDb interface
|
|
/// Handles all database operations for camera trap locations
|
|
class PlaceDBHelper implements IDb {
|
|
// Database instance
|
|
static Database? _dB;
|
|
|
|
/// Get database instance, creating it if necessary
|
|
/// Returns existing database or initializes a new one
|
|
@override
|
|
Future<Database> get dB async {
|
|
if (_dB != null) {
|
|
return _dB!;
|
|
}
|
|
_dB = await initDatabases();
|
|
return _dB!;
|
|
}
|
|
|
|
/// 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, 'placeDB.db');
|
|
var placeDB =
|
|
await openDatabase(path, version: 1, onCreate: onCreateDatabases);
|
|
return placeDB;
|
|
}
|
|
|
|
/// Create database tables
|
|
/// Sets up schema for both main entries and templates
|
|
@override
|
|
onCreateDatabases(Database placeDB, int version) async {
|
|
// Create main places table
|
|
await placeDB.execute(
|
|
'CREATE TABLE place (ID INTEGER PRIMARY KEY AUTOINCREMENT, CID TEXT, Standort TEXT, Rudel TEXT, Datum TEXT, 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 TEXT)');
|
|
|
|
// Create templates table (similar structure but without Sent flag)
|
|
await placeDB.execute(
|
|
'CREATE TABLE placeTemplates (ID INTEGER PRIMARY KEY AUTOINCREMENT, CID TEXT, Standort TEXT, Rudel TEXT, Datum TEXT, 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))');
|
|
}
|
|
|
|
/// Add a new main entry to the database
|
|
/// @param place Map containing the place data
|
|
/// @return ID of the newly inserted entry
|
|
@override
|
|
Future<int> addMainEntry(Map<String, String> place) async {
|
|
var placeDBClient = await dB;
|
|
// Commented out code for handling existing entries
|
|
// final existingID = await placeDBClient.query(
|
|
// 'place',
|
|
// where: 'ID = ?',
|
|
// whereArgs: [place['ID']],
|
|
// );
|
|
//
|
|
// if (existingID.isNotEmpty) {
|
|
// updateMainEntry(place);
|
|
// return existingID.first['ID'] as int;
|
|
// }
|
|
|
|
int id = await placeDBClient.insert(
|
|
'place',
|
|
place,
|
|
//conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
|
|
return id;
|
|
}
|
|
|
|
/// Update an existing main entry
|
|
/// @param place Map containing the updated place data
|
|
/// @return Number of rows affected
|
|
@override
|
|
Future<int> updateMainEntry(Map<String, String> place) async {
|
|
var placeDBClient = await dB;
|
|
return await placeDBClient
|
|
.update('place', place, where: "ID = ?", whereArgs: [place['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 placeDBClient = await dB;
|
|
await placeDBClient.update('place', {'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 placeDBClient = await dB;
|
|
// Commented out code for handling existing templates
|
|
// final existingCID = await placeDBClient.query(
|
|
// 'placeTemplates',
|
|
// where: 'ID = ?',
|
|
// whereArgs: [templates['ID']],
|
|
// );
|
|
// if (existingCID.isNotEmpty) {
|
|
// return;
|
|
// }
|
|
|
|
int id = await placeDBClient.insert(
|
|
'placeTemplates',
|
|
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 placeDBClient = await dB;
|
|
await placeDBClient.update(
|
|
'placeTemplates',
|
|
template,
|
|
where: "ID = ?",
|
|
whereArgs: [template['ID']],
|
|
);
|
|
}
|
|
|
|
/// Retrieve all main entries from the database
|
|
/// @return List of all places or empty list if none exist
|
|
@override
|
|
Future<List<Map<String, dynamic>>> getAllMainEntries() async {
|
|
var placeDBClient = await dB;
|
|
var erg = await placeDBClient.query('place');
|
|
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 placeDBClient = await dB;
|
|
var erg = await placeDBClient.query('placeTemplates');
|
|
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 placeDBClient = await dB;
|
|
await placeDBClient.delete('place');
|
|
}
|
|
|
|
/// Delete all templates from the local database
|
|
/// Note: This only affects the local database, not the server
|
|
@override
|
|
Future<void> deleteAllTemplates() async {
|
|
var placeDBClient = await dB;
|
|
await placeDBClient.delete('placeTemplates');
|
|
}
|
|
|
|
/// Delete a specific template by ID
|
|
/// @param id ID of the template to delete
|
|
@override
|
|
Future<void> deleteTemplateById(String id) async {
|
|
var placeDBClient = await dB;
|
|
await placeDBClient.delete(
|
|
'placeTemplates',
|
|
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 placeDBClient = await dB;
|
|
await placeDBClient.delete(
|
|
'place',
|
|
where: 'ID = ?',
|
|
whereArgs: [id],
|
|
);
|
|
}
|
|
}
|