BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
ElogConnection.cxx
Go to the documentation of this file.
1
// -------------------------------------------------------------------------
2
// ----- ElogConnection source file -----
3
// -------------------------------------------------------------------------
4
#include "
ElogConnection.h
"
5
6
#include "
TRandom.h
"
7
#include "
TSystem.h
"
8
9
#include <chrono>
10
#include <thread>
11
12
mapSQLServer
*
ElogConnection::mapConnection
=
nullptr
;
13
14
// ----- Constructor with connection ----------------------
15
ElogConnection::ElogConnection
(
TSQLServer
* pSQLServer)
16
{
17
elog_db_server
= pSQLServer;
18
}
19
20
ElogConnection
*
ElogConnection::OpenConnection
(
const
TString
& connect_string,
21
const
char
* db_host,
22
const
char
* db_user,
23
const
char
* db_password)
24
{
25
if
(
ElogConnection::mapConnection
==
nullptr
)
26
ElogConnection::mapConnection
=
new
mapSQLServer
();
27
28
TSQLServer
* pSQLServer =
nullptr
;
29
itSQLServer
it =
ElogConnection::mapConnection
->find(connect_string.
Data
());
30
if
(it !=
ElogConnection::mapConnection
->end())
31
pSQLServer = it->second;
32
else
{
33
// try to connect 3 times
34
for
(
int
i
= 0;
i
< 4;
i
++) {
35
pSQLServer =
TSQLServer::Connect
(connect_string, db_user, db_password);
36
if
(pSQLServer)
37
break
;
38
if
(
i
== 3) {
39
cout <<
"ERROR: database connection was not established ("
<< db_host <<
") for '"
<< db_user
40
<<
"' user"
<< endl;
41
return
nullptr
;
42
}
43
unsigned
int
random_integer =
pow
(10,
i
) +
gRandom
->Integer(
pow
(10,
i
) * 2 + 1);
44
this_thread::sleep_for(chrono::seconds(random_integer));
45
}
46
// cout<<"Server info: "<<pSQLServer->ServerInfo()<<endl;
47
48
ElogConnection::mapConnection
->insert(
pairSQLServer
(connect_string.
Data
(), pSQLServer));
49
}
50
51
return
new
ElogConnection
(pSQLServer);
52
}
53
54
// -------------------------------------------------------------------
55
ElogConnection
*
ElogConnection::Open
()
56
{
57
// get database host and name from global variables in C++ header (elog_db_settings.h)
58
TString
conString =
TString::Format
(
"pgsql://%s/%s"
,
ELOG_DB_HOST
,
ELOG_DB_NAME
);
59
60
// try to get dynamic database username and password from bash environment
61
const
char
*db_username =
gSystem
->Getenv(
"ELOG_USERNAME"
), *db_password =
gSystem
->Getenv(
"ELOG_PASSWORD"
);
62
// if no bash variables then get from global variables in C++ header
63
if
((db_username ==
nullptr
) || (db_password ==
nullptr
)) {
64
db_username =
ELOG_DB_USERNAME
;
65
db_password =
ELOG_DB_PASSWORD
;
66
}
67
68
return
OpenConnection
(conString,
ELOG_DB_HOST
, db_username, db_password);
69
}
70
71
// -------------------------------------------------------------------
72
ElogConnection
*
ElogConnection::Open
(
enumDBMS
database_type,
73
const
char
* strDBHost,
74
const
char
* strDBName,
75
const
char
* strUID,
76
const
char
* strPassword)
77
{
78
char
* db_type;
79
switch
(database_type) {
80
case
MySQL
:
81
db_type = (
char
*)
"mysql"
;
82
break
;
83
case
PgSQL
:
84
db_type = (
char
*)
"pgsql"
;
85
break
;
86
default
: {
87
cout <<
"ERROR: incorrect database type!"
<< endl;
88
return
nullptr
;
89
}
90
}
91
TString
conString =
TString::Format
(
"%s://%s/%s"
, db_type, strDBHost, strDBName);
92
return
OpenConnection
(conString, strDBHost, strUID, strPassword);
93
}
94
95
// -------------------------------------------------------------------
96
ElogConnection::~ElogConnection
()
97
{
98
// if (elog_db_server)
99
// delete elog_db_server;
100
}
i
int i
Definition
P4_F32vec4.h:22
TRandom.h
gRandom
R__EXTERN TRandom * gRandom
TSystem.h
gSystem
R__EXTERN TSystem * gSystem
ElogConnection::OpenConnection
static ElogConnection * OpenConnection(const TString &connect_string, const char *strDBHost, const char *strUID, const char *strPassword)
Definition
ElogConnection.cxx:20
ElogConnection::Open
static ElogConnection * Open()
Definition
ElogConnection.cxx:55
ElogConnection::~ElogConnection
virtual ~ElogConnection()
Definition
ElogConnection.cxx:96
ElogConnection::ElogConnection
ElogConnection(TSQLServer *pSQLServer)
Definition
ElogConnection.cxx:15
ElogConnection::enumDBMS
enumDBMS
Definition
ElogConnection.h:30
ElogConnection::PgSQL
@ PgSQL
Definition
ElogConnection.h:32
ElogConnection::MySQL
@ MySQL
Definition
ElogConnection.h:31
ElogConnection::mapConnection
static mapSQLServer * mapConnection
Definition
ElogConnection.h:48
ElogConnection::elog_db_server
TSQLServer * elog_db_server
Definition
ElogConnection.h:51
TSQLServer
TSQLServer::Connect
static TSQLServer * Connect(const char *db, const char *uid, const char *pw)
TString
TString::Data
const char * Data() const
TString::Format
static TString Format(const char *fmt,...)
ElogConnection.h
mapSQLServer
map< string, TSQLServer * > mapSQLServer
Definition
ElogConnection.h:22
ELOG_DB_PASSWORD
const char *const ELOG_DB_PASSWORD
Definition
elog_db_settings.h:8
ELOG_DB_USERNAME
const char *const ELOG_DB_USERNAME
Definition
elog_db_settings.h:7
ELOG_DB_HOST
const char *const ELOG_DB_HOST
Definition
elog_db_settings.h:9
ELOG_DB_NAME
const char *const ELOG_DB_NAME
Definition
elog_db_settings.h:10
pow
RVec< PromoteTypes< T0, T1 > > pow(const RVec< T0 > &v, const T1 &y)
mapSQLServer
map< string, TSQLServer * > mapSQLServer
Definition
ElogConnection.h:22
itSQLServer
map< string, TSQLServer * >::iterator itSQLServer
Definition
ElogConnection.h:24
pairSQLServer
pair< string, TSQLServer * > pairSQLServer
Definition
ElogConnection.h:23
database
elog_db
ElogConnection.cxx
Generated on
for BmnRoot by
1.17.0