VSQLManager_SQLite.cc
Go to the documentation of this file.
1 #include <det/VSQLManager_SQLite.h>
2 #include <utl/Reader.h>
3 #include <utl/AugerUnits.h>
4 #include <fwk/CentralConfig.h>
5 #include <fwk/CommandLineOptions.h>
6 #include <boost/algorithm/string.hpp>
7 
8 using namespace det;
9 using namespace std;
10 using namespace utl;
11 
12 
13 void
15 {
16  if (!fIsInitialized)
17  return;
18 
19  const double time = fTime.GetTime();
20  if (time) {
21  ostringstream info;
22  info << "Total time spent for SQLite database '" << fDatabaseName
23  << "' queries is " << time/second << " sec.";
24  // reinstate this INFO(info);
25  }
26 
27  FreeResult(); // free resources from previous query if not yet freed
28 
29  sqlite3_exec(fDb, "END TRANSACTION", 0, 0, 0);
30  sqlite3_close(fDb);
31 
32  fDb = nullptr;
33  fIsInitialized = false;
34 }
35 
36 
37 int
39  const
40 {
41  const int rc = sqlite3_finalize(fResult);
42  fResult = nullptr;
43  return rc;
44 }
45 
46 
47 int
49  const
50 {
51  return sqlite3_step(fResult);
52 }
53 
54 
55 void
56 VSQLManager::Init(const std::string& configLink)
57 {
58  VManager::Init(configLink);
59  fIsInitialized = false;
60 
61  fBranch.GetChild("databaseName").GetData(fDatabaseName);
62  fBranch.GetChild("softwareVersion").GetData(fDatabaseSoftwareVersion);
63 
64  if (sqlite3_open_v2(fDatabaseName.c_str(), &fDb, SQLITE_OPEN_READONLY, 0)) {
65  ostringstream err;
66  err << VSQLManager::GetName() << " could not open the requested SQLite database file " << fDatabaseName << endl;
67  ERROR(err);
68  throw IOFailureException(err.str());
69  }
70  ostringstream info;
71  info << VSQLManager::GetName() << " connects to the database '" << fDatabaseName << '\'';
72  INFO(info);
73  fIsInitialized = true;
74 
75  sqlite3_exec(fDb, "PRAGMA cache_size=500000", 0, 0, 0);
76  sqlite3_exec(fDb, "PRAGMA temp_store=MEMORY", 0, 0, 0);
77  sqlite3_exec(fDb, "BEGIN TRANSACTION", 0, 0, 0);
78 }
79 
80 
82 VSQLManager::Query(const std::string& query, const std::string& /*what*/ /*for error report*/)
83  const
84 {
85  if (!fIsInitialized) {
86  const string err = "SQLite database is not opened.";
87  ERROR(err);
88  throw IOFailureException(err);
89  }
90 
91  // before preparing the next query, make sure the memory used for preparing
92  // the last one is freed (if it is already freed, no harm is done)
93  FreeResult();
94 
95  Status stat = eNotFound;
96  const char* s = nullptr;
97  fTime.Start();
98  const string queryMod = boost::replace_all_copy(query, "UNIX_TIMESTAMP(", "strftime('%s',");
99  const int rc = sqlite3_prepare_v2(fDb, queryMod.c_str(), queryMod.size(), &fResult, &s);
100  fTime.Stop();
101  if (rc == SQLITE_OK)
102  stat = eFound;
103  else {
104  FreeResult();
105  cerr << "SQLite error " << rc
106  << ": could not process the query \"" << query << "\"" << endl;
107  // put proper error handling here (eg, check 'what' and IsReportingErrors).
108  }
109  return stat;
110 }
111 
112 
113 string
114 VSQLManager::MergeIndexMap(const IndexMap& componentIndex)
115 {
116  ostringstream res;
117  VManager::IndexMap::const_iterator it = componentIndex.begin();
118  res << it->first << " = " << it->second;
119  for (++it; it != componentIndex.end(); ++it)
120  res << " AND " << it->first << " = " << it->second;
121  return res.str();
122 }
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
virtual void Init(const std::string &configLink)
Manager Initialization. configLink is the CentralConfig hook for the configuration file...
Definition: VManager.cc:16
Base class to report exceptions in IO.
virtual void Init(const std::string &configLink)
Manager Initialization. configLink is the CentralConfig hook for the configuration file...
Status
Return code for seek operation.
Definition: IoCodes.h:24
constexpr double s
Definition: AugerUnits.h:163
const double second
Definition: GalacticUnits.h:32
const std::string & GetName() const
Definition: VManager.h:330
MYSQL_ROW FetchRow() const
Status Query(const std::ostringstream &os, const std::string &what="") const
query MySQL
static std::string MergeIndexMap(const IndexMap &componentIndex)
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127

, generated on Tue Sep 26 2023.