1 #include <det/VSQLManager_MySQL.h>
2 #include <utl/Reader.h>
3 #include <utl/AugerUnits.h>
4 #include <fwk/CentralConfig.h>
5 #include <fwk/CommandLineOptions.h>
13 int VSQLManager::fgGlobalLogQueries = 0;
19 if (fIsInitialized && fMySQL) {
20 const double time = fTime.GetTime();
23 info <<
"Total time spent for database '" << fDatabaseName
24 <<
"' queries is " << time/
second <<
" sec.";
31 if (fLogQueryStream) {
32 fLogQueryStream->close();
33 delete fLogQueryStream;
34 fLogQueryStream =
nullptr;
36 fIsInitialized =
false;
45 fBranch.GetChild(
"databaseName").GetData(fDatabaseName);
46 fBranch.GetChild(
"softwareVersion").GetData(fDatabaseSoftwareVersion);
47 if (fgGlobalLogQueries) {
48 fLogQueries = fgGlobalLogQueries;
52 logQueriesB.
GetData(fLogQueries);
57 fname <<
"mysql-" << fDatabaseName <<
'-' << getpid() <<
".query";
58 fLogQueryStream =
new ofstream(fname.str().c_str());
60 INFO(
string(
"Database queries will be logged into file '") +
67 VSQLManager::Connect()
70 if (!fIsInitialized) {
71 const string err =
"MySQL Manager not initialized.";
77 WARNING(
"MySQL Manager already connected!");
84 const string err =
"VSQLManager cannot access databaseServers configuration file!";
92 const vector<string> hostName = serverB.GetChild(
"hostName").Get<vector<string>>();
94 fDatabaseUserName = serverB.GetChild(
"userName").Get<
string>();
95 fDatabasePassword = serverB.GetChild(
"password").Get<
string>();
102 for (
const auto& hn : hostName) {
104 fDatabaseServer = hn;
106 fMySQL = mysql_init(
nullptr);
108 if (mysql_real_connect(fMySQL, fDatabaseServer.c_str(),
109 fDatabaseUserName.c_str(),
110 fDatabasePassword.c_str(),
111 fDatabaseName.c_str(),
112 fPort,
nullptr, 0)) {
114 info <<
"Connected to database '" << fDatabaseName <<
"' "
115 "on host " << mysql_get_host_info(fMySQL);
118 *fLogQueryStream <<
"* * " << info.str() << endl;
119 fIsInitialized =
true;
124 info <<
"Connection to the database '" << fDatabaseName <<
"' "
125 "on " << fDatabaseUserName
126 <<
'@' << fDatabaseServer <<
':' << fPort <<
" failed. "
127 "MySQL reports the following error: \"" << mysql_error(fMySQL) <<
"\". "
128 "Trying an alternative database configuration, if one has been specified...";
130 if (fLogQueryStream) {
131 *fLogQueryStream <<
"* * " << info.str() << endl;
132 fLogQueryStream->close();
133 delete fLogQueryStream;
134 fLogQueryStream =
nullptr;
139 fIsInitialized =
false;
148 if (!fIsInitialized) {
150 err <<
"Could not connect to the requested database: '" << fDatabaseName
151 <<
"' on any of the specified hosts. Giving up.";
159 VSQLManager::Query(
const string& query,
const string& what)
166 const int ret = mysql_query(fMySQL, query.c_str());
167 const double elapsedTime = fTime.Stop();
170 *fLogQueryStream << IsReportingErrors() <<
' '
171 << elapsedTime/
second <<
'\t' << query.c_str()
172 << (ret ?
" -> FAILED\n" :
" -> OK") << flush;
175 if (IsReportingErrors()) {
177 err <<
"Query '" << query <<
"' ";
179 err <<
"for " << what <<
' ';
181 "Reason: \"" << mysql_error(fMySQL) <<
'"';
189 fMySQLResult = mysql_store_result(fMySQL);
193 *fLogQueryStream <<
": NO_RESULT\n";
194 if (!what.empty() && IsReportingErrors()) {
196 warn <<
"Query for " << what <<
" "
197 "returned no results.";
204 if (fLogQueryStream && fLogQueries > 1) {
205 *fLogQueryStream <<
": (";
206 const int nCols = NumFields();
209 while ((row = FetchRow())) {
210 *fLogQueryStream <<
" ( ";
212 *fLogQueryStream <<
'\'' << row[0] <<
'\'';
214 *fLogQueryStream <<
"NULL*";
215 for (
int i = 1; i < nCols; ++i)
217 *fLogQueryStream <<
", '" << row[i] <<
'\'';
219 *fLogQueryStream <<
", NULL*";
220 *fLogQueryStream <<
" )";
224 mysql_query(fMySQL, query.c_str());
225 fMySQLResult = mysql_store_result(fMySQL);
227 *fLogQueryStream <<
" )" << endl;
235 VSQLManager::MergeIndexMap(
const IndexMap& componentIndex)
237 VManager::IndexMap::const_iterator it = componentIndex.begin();
238 if (it == componentIndex.end())
241 res << it->first <<
" = " << it->second;
242 for (++it; it != componentIndex.end(); ++it)
243 res <<
" AND " << it->first <<
" = " << it->second;
#define INFO(message)
Macro for logging informational messages.
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Base class to report exceptions in IO.
Branch GetNextSibling() const
Get next sibling of this branch.
Class representing a document branch.
#define WARNING(message)
Macro for logging warning messages.
void GetData(bool &b) const
Overloads of the GetData member template function.
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
std::map< std::string, std::string > IndexMap
Main configuration utility.
Branch GetFirstChild() const
Get first child of this Branch.
#define ERROR(message)
Macro for logging error messages.
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)
Status
Specifies success or (eventually) various possible failure modes.