BmnRoot
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
14
15// ----- Constructor with connection ----------------------
16UniConnection::UniConnection(TSQLServer* pSQLServer)
17{
18 db_server = pSQLServer;
19}
20
21UniConnection* 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)
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// -------------------------------------------------------------------
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// -------------------------------------------------------------------
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// -------------------------------------------------------------------
99{
100 // if (db_server)
101 // delete db_server;
102}
int i
Definition P4_F32vec4.h:22
static UniConnection * Open()
virtual ~UniConnection()
static mapSQLServer * mapConnection
map< string, TSQLServer * > mapSQLServer
map< string, TSQLServer * >::iterator itSQLServer
const char *const UNI_DB_HOST
const char *const UNI_DB_NAME
const char *const UNI_DB_PASSWORD
const char *const UNI_DB_USERNAME
map< string, TSQLServer * > mapSQLServer
pair< string, TSQLServer * > pairSQLServer