cleanup and consolidate db code

This commit is contained in:
frederik
2025-09-13 16:11:04 +02:00
parent 59e378791c
commit 8daf913ecf
+16 -17
View File
@@ -140,20 +140,25 @@ function get_label($record, $sort_by, $date=null) {
return $ret;
}
function fetch_species_array($sort_by, $date=null) {
function get_db() {
if (!isset($_db)) {
$_db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
$_db->busyTimeout(1000);
}
return $_db;
}
function fetch_species_array($sort_by, $date=null) {
$db = get_db();
$where = (isset($date)) ? "WHERE Date == \"$date\"" : "";
if ($sort_by === "occurrences") {
$statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY COUNT(*) DESC");
$statement = $db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY COUNT(*) DESC");
} elseif ($sort_by === "confidence") {
$statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY MAX(Confidence) DESC");
$statement = $db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY MAX(Confidence) DESC");
} elseif ($sort_by === "date") {
$statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY MIN(Date) DESC, Time DESC");
$statement = $db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY MIN(Date) DESC, Time DESC");
} else {
$statement = $_db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY Com_Name ASC");
$statement = $db->prepare("SELECT Date, Time, File_Name, Com_Name, Sci_Name, COUNT(*) as Count, MAX(Confidence) as MaxConfidence FROM detections $where GROUP BY Sci_Name ORDER BY Com_Name ASC");
}
ensure_db_ok($statement);
$result = $statement->execute();
@@ -161,29 +166,23 @@ function fetch_species_array($sort_by, $date=null) {
}
function fetch_best_detection($com_name) {
if (!isset($_db)) {
$_db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
$_db->busyTimeout(1000);
}
$statement = $_db->prepare("SELECT Com_Name, Sci_Name, COUNT(*), MAX(Confidence), File_Name, Date, Time from detections WHERE Com_Name = \"$com_name\"");
$db = get_db();
$statement = $db->prepare("SELECT Com_Name, Sci_Name, COUNT(*), MAX(Confidence), File_Name, Date, Time from detections WHERE Com_Name = \"$com_name\"");
ensure_db_ok($statement);
$result = $statement->execute();
return $result;
}
function fetch_all_detections($sci_name, $sort_by, $date=null) {
if (!isset($_db)) {
$_db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
$_db->busyTimeout(1000);
}
$db = get_db();
$filter = (isset($date)) ? "AND Date == \"$date\"" : "";
if ($sort_by === "occurrences") {
$statement = $_db->prepare("SELECT * FROM detections WHERE Sci_Name == \"$sci_name\" $filter ORDER BY COUNT(*) DESC");
$statement = $db->prepare("SELECT * FROM detections WHERE Sci_Name == \"$sci_name\" $filter ORDER BY COUNT(*) DESC");
} elseif ($sort_by === "confidence") {
$statement = $_db->prepare("SELECT * FROM detections WHERE Sci_Name == \"$sci_name\" $filter ORDER BY Confidence DESC");
$statement = $db->prepare("SELECT * FROM detections WHERE Sci_Name == \"$sci_name\" $filter ORDER BY Confidence DESC");
} else {
$order = (isset($date)) ? "Time DESC" : "Date DESC, Time DESC";
$statement = $_db->prepare("SELECT * FROM detections where Sci_Name == \"$sci_name\" $filter ORDER BY $order");
$statement = $db->prepare("SELECT * FROM detections where Sci_Name == \"$sci_name\" $filter ORDER BY $order");
}
ensure_db_ok($statement);
$result = $statement->execute();