BmnRoot
Loading...
Searching...
No Matches
TangoConnection.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- TangoConnection source file -----
3// -------------------------------------------------------------------------
4#include "TangoConnection.h"
5
6#include "TRandom.h"
7
8#include <chrono>
9#include <thread>
10
12
13// ----- Constructor with connection ----------------------
14TangoConnection::TangoConnection(TSQLServer* pSQLServer)
15{
16 tango_db_server = pSQLServer;
17}
18
19TangoConnection* TangoConnection::OpenConnection(const TString& connect_string,
20 const char* db_host,
21 const char* db_user,
22 const char* db_password)
23{
24 if (TangoConnection::mapConnection == nullptr)
26
27 TSQLServer* pSQLServer = nullptr;
28 itSQLServer it = TangoConnection::mapConnection->find(connect_string.Data());
29 if (it != TangoConnection::mapConnection->end())
30 pSQLServer = it->second;
31 else {
32 // try to connect 3 times
33 for (int i = 0; i < 4; i++) {
34 pSQLServer = TSQLServer::Connect(connect_string, db_user, db_password);
35 if (pSQLServer)
36 break;
37 if (i == 3) {
38 cout << "ERROR: database connection was not established (" << db_host << ") for '" << db_user
39 << "' user" << endl;
40 return nullptr;
41 }
42 unsigned int random_integer = pow(10, i) + gRandom->Integer(pow(10, i) * 2 + 1);
43 this_thread::sleep_for(chrono::seconds(random_integer));
44 }
45 // cout<<"Server info: "<<pSQLServer->ServerInfo()<<endl;
46
47 TangoConnection::mapConnection->insert(pairSQLServer(connect_string.Data(), pSQLServer));
48 }
49
50 return new TangoConnection(pSQLServer);
51}
52
53// -------------------------------------------------------------------
55{
56 TString conString = TString::Format("pgsql://%s/%s", TANGO_DB_HOST, TANGO_DB_NAME);
57 return OpenConnection(conString, TANGO_DB_HOST, TANGO_DB_USERNAME, TANGO_DB_PASSWORD);
58}
59
60// -------------------------------------------------------------------
62 const char* strDBHost,
63 const char* strDBName,
64 const char* strUID,
65 const char* strPassword)
66{
67 char* db_type;
68 switch (database_type) {
69 case MySQL:
70 db_type = (char*)"mysql";
71 break;
72 case PgSQL:
73 db_type = (char*)"pgsql";
74 break;
75 default: {
76 cout << "ERROR: incorrect database type!" << endl;
77 return nullptr;
78 }
79 }
80 TString conString = TString::Format("%s://%s/%s", db_type, strDBHost, strDBName);
81 return OpenConnection(conString, strDBHost, strUID, strPassword);
82}
83
84// -------------------------------------------------------------------
86{
87 // if (tango_db_server)
88 // delete tango_db_server;
89}
int i
Definition P4_F32vec4.h:22
virtual ~TangoConnection()
static TangoConnection * Open()
static mapSQLServer * mapConnection
map< string, TSQLServer * > mapSQLServer
map< string, TSQLServer * >::iterator itSQLServer
const char *const TANGO_DB_HOST
const char *const TANGO_DB_USERNAME
const char *const TANGO_DB_PASSWORD
const char *const TANGO_DB_NAME
map< string, TSQLServer * > mapSQLServer
pair< string, TSQLServer * > pairSQLServer