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