ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PgPostBankBackupLog.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PgPostBankBackupLog.cc
1 #include "PgPostBankBackupLog.h"
2 
3 #include <RDBC/TSQL.h>
4 #include <RDBC/TSQLConnection.h>
5 #include <RDBC/TSQLDriverManager.h>
6 #include <RDBC/TSQLPreparedStatement.h>
7 
8 #include <TString.h>
9 
10 #include <cassert>
11 #include <cstdlib>
12 #include <exception>
13 #include <fstream>
14 #include <iostream>
15 #include <sstream>
16 
17 using namespace std;
18 
20  static_cast<TSQLConnection*>(nullptr));
21 
22 PgPostBankBackupLog::PgPostBankBackupLog(const std::string& TableName,
23  const std::string& Tag)
24  : verbosity(1)
25  , tablename(TableName)
26  , tag(Tag)
27  , pstmt(nullptr)
28 {
29 }
30 
32 {
33 }
34 
36 {
37  try
38  {
39  if (!con)
40  {
41  const TString s_con = "dsn=calibrations_backup_log; uid=phnxrc; pwd= ";
42 
43  if (Verbosity() >= 1)
44  cout << "PgPostBankBackupLog::Init - connect to " << s_con << endl;
45 
46  con = gSQLDriverManager->GetConnection(s_con);
47  if (!con)
48  {
49  cout << "PgPostBankBackupLog::Init - Error - "
50  << "cannot init connection to " << s_con << endl;
51  exit(1);
52  }
53  }
54 
55  if (!pstmt)
56  {
57  assert(con);
58 
59  ostringstream sqlcmd;
60  sqlcmd << "insert into calib_log (tablename,rid,ops,tag) values ('"
61  << tablename << "',?,?,'" << tag << "');";
62 
63  if (Verbosity() >= 1)
64  cout << "PgPostBankBackupLog::Init - make TSQLPreparedStatement "
65  << sqlcmd.str() << endl;
66  pstmt = con->PrepareStatement(sqlcmd.str().c_str());
67 
68  if (!pstmt)
69  {
70  cout << "PgPostBankBackupLog::Init - Error - "
71  << "cannot prepare statement " << sqlcmd.str() << endl;
72  exit(1);
73  }
74  }
75  }
76  catch (std::exception& e)
77  {
78  cout << "PgPostBankBackupLog::Init - Error - "
79  << "Initialization error "
80  << e.what() << endl;
81  exit(1);
82  }
83 }
84 
85 void PgPostBankBackupLog::Log(const int rid, const enu_ops ops)
86 {
87  Init();
88 
89  if (Verbosity() >= 2)
90  {
91  cout << "PgPostBankBackupLog::Log - log rid = " << rid << ", ops = "
92  << ops << " for table " << tablename << " and tag " << tag << endl;
93  }
94 
95  assert(con);
96  assert(pstmt);
97 
98  pstmt->SetInt(1, rid);
99  pstmt->SetInt(2, (int) ops);
100  try
101  {
102  const int res = pstmt->ExecuteUpdate();
103 
104  if (res == 0)
105  {
106  cout << "PgPostBankBackupLog::Log - Error - "
107  << "DATABASE: commit failed. "
108  << "Make sure you commit to the writable database " << endl;
109  exit(1);
110  }
111 
112  con->Commit();
113  }
114  catch (TSQLException& e)
115  {
116  cout << "PgPostBankBackupLog::Log - Error - "
117  << " Exception caught during connection->commit()" << endl;
118  cout << e.GetMessage() << endl;
119  exit(1);
120  }
121 }