gsystem
Loading...
Searching...
No Matches
systemSqliteFactory.cc
Go to the documentation of this file.
1
8// string for gexit
9#include "gutilities.h"
10
11// gsystem
12#include "systemSqliteFactory.h"
13#include "gsystemConventions.h"
14
15bool GSystemSQLiteFactory::table_exists(sqlite3* db, const char* name) {
16 sqlite3_stmt* st = nullptr;
17 const char* sql = "SELECT 1 FROM sqlite_master WHERE type='table' AND name=? LIMIT 1";
18 if (sqlite3_prepare_v2(db, sql, -1, &st, nullptr) != SQLITE_OK) return false;
19 sqlite3_bind_text(st, 1, name, -1, SQLITE_TRANSIENT);
20 bool exists = (sqlite3_step(st) == SQLITE_ROW);
21 sqlite3_finalize(st);
22 return exists;
23}
24
25
26void GSystemSQLiteFactory::initialize_sqlite_db(GSystem* system) {
27 // skip ROOT system
28 if (system->getName() == ROOTWORLDGVOLUMENAME) { return; }
29
30 // Save parameters from the system (used to bind queries and for logs).
31 system_name = system->getName();
32 variation = system->getVariation();
33 runno = system->getRunno();
34
35 // Use system dbhost if not already set.
36 if (dbhost == "na") { dbhost = system->get_dbhost(); }
37 log->info(1, "GSystemSQLiteFactory: dbhost set to <", dbhost, ">");
38
39 // Try local dir, GEMC installation, then examples dir.
40 std::vector<std::string> dirs = {
41 ".",
42 gutilities::gemc_root().string(),
43 (gutilities::gemc_root() / "examples").string()
44 };
45
46 auto dbPath = gutilities::searchForFileInLocations(dirs, dbhost);
47 if (!dbPath) { log->error(ERR_GSQLITEERROR, "Failed to find database file. Exiting."); }
48
49 if (sqlite3_open_v2(dbPath.value().c_str(), &db, SQLITE_OPEN_READONLY, nullptr) != SQLITE_OK) {
50 sqlite3_close(db);
51 db = nullptr;
52 log->error(ERR_GSQLITEERROR, " Failed to open or validate database", dbhost);
53 }
54 log->info(1, "Opened database: " + dbhost, " found at ", dbPath.value());
55 return;
56
57 // If we are here, no valid database file was found.
58 log->error(ERR_GSETUPFILENOTOFOUND, "Sqlite database >" + dbhost + "< not found");
59}
60
61
62void GSystemSQLiteFactory::closeSystem() {
63 if (db) {
64 sqlite3_close(db);
65 db = nullptr; // Reset the pointer after closing
66 log->info(1, "Closing sqlite database >", dbhost, "<");
67 }
69}
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
std::vector< std::string > possibleLocationOfFiles
List of candidate directories used by file-based factories.
Represents a single detector system (e.g., calorimeter, tracker).
Definition gsystem.h:32
int getRunno() const
Definition gsystem.h:113
std::string getVariation() const
Definition gsystem.h:110
std::string getName() const
Definition gsystem.h:108
std::string get_dbhost() const
Definition gsystem.h:114
Conventions and shared constants for the detector-system module.
#define ROOTWORLDGVOLUMENAME
Canonical name for the ROOT/world gvolume entry.
#define ERR_GSETUPFILENOTOFOUND
#define ERR_GSQLITEERROR
std::filesystem::path gemc_root()
std::optional< std::string > searchForFileInLocations(const std::vector< std::string > &locations, std::string_view filename)