ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PgPostApplication.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PgPostApplication.cc
1 #include "PgPostApplication.h"
2 #include "PgPostBankWrapper.h"
4 
5 #include <pdbcalbase/PdbApplicationFactory.h>
6 #include <pdbcalbase/PHGenericFactoryT.h>
7 #include <pdbcalbase/Pdb.h>
8 #include <pdbcalbase/PdbApplication.h>
9 #include <pdbcalbase/PdbCalBank.h>
10 
11 #include <phool/phool.h>
12 
13 #include <RDBC/TSQLDriverManager.h>
14 #include <RDBC/TSQLConnection.h>
15 
16 #include <iostream>
17 #include <memory>
18 #include <sstream>
19 
20 using namespace std;
21 
23 TSQLConnection *PgPostApplication::con = nullptr;
24 
25 namespace
26 {
27 PdbApplication *singletonCreator()
28 {
29  // rememeber that this will not neccessarily return a
30  // pointer to the singleton PgPostApplication. If
31  // an Objy application is instantiated, it will return 0.
32 
33  cout << "*** deprecated interface -- use PdbApplication::instance()" << endl;
35 }
36 
37 const std::string name = "Pg";
38 const bool registered =
39  PdbApplicationFactory::instance().registerCreator(name, singletonCreator, "PdbApplication");
40 } // namespace
41 
43 {
44  return mySpecificCopy;
45 }
46 
47 int PgPostApplication::Register(const string &dbname)
48 {
49  if (__instance.get()) return -1;
50  mySpecificCopy = new PgPostApplication(dbname);
51  __instance = std::unique_ptr<PdbApplication>(mySpecificCopy);
52  return 0;
53 }
54 
56 {
57  if (!__instance.get()) return -1;
58  if (con)
59  {
60  con->Close();
61  con = nullptr;
62  }
63  return 0;
64 }
65 
67  : readOnly(1)
68  , dsn(dbname)
69 {
70 }
71 
73 {
74  mySpecificCopy = 0;
75  if (con)
76  {
77  con->Close();
78  con = 0;
79  }
80 }
81 
83 {
84  if (!__instance.get())
85  {
87  }
88  if (!con)
89  {
90  ostringstream constr;
91  constr << "dsn=" << dsn << "; uid=phnxrc; pwd= ";
92  con = gSQLDriverManager->GetConnection(constr.str().c_str());
93  }
94  return con;
95 }
96 
97 int PgPostApplication::setDBName(const string &name)
98 {
99  if (dsn != name)
100  {
101  dsn = name;
102  if (con)
103  {
104  con->Close();
105  }
106  ostringstream constr;
107  constr << "dsn=" << dsn << "; uid=phnxrc; pwd= ";
108  con = gSQLDriverManager->GetConnection(constr.str().c_str());
109  if (!con)
110  {
111  cout << PHWHERE << " Could not open data base " << dsn << endl;
112  }
113  }
114  return 0;
115 }
116 
117 PdbStatus
119 {
120  readOnly = 0;
121  return 1;
122 }
123 
124 PdbStatus
126 {
127  readOnly = 1;
128  return 1;
129 }
130 
131 PdbStatus
133 {
135  // will roll back a Xact in progres
136  if (con) con->Rollback();
137  return 1;
138 }
139 
140 PdbStatus
142 {
143  if (readOnly)
144  {
145  // Forget about the registered wrappers.
147  return 1;
148  }
149  else
150  {
151  // Commit the registered wrappers (this will also forget them).
153  if (con) con->Commit();
154  return (ok == true);
155  }
156 }
157 
158 PdbStatus
160 {
161  PgPostBankWrapper *tb = dynamic_cast<PgPostBankWrapper *>(b);
162  if (tb)
163  {
164  return tb->commit();
165  }
166  else
167  {
168  cerr << PHWHERE << "Can only commit PgPostBankWrapper2" << endl;
169  return 0;
170  }
171 }
172 
173 PdbStatus
174 PgPostApplication::commit(PdbCalBank *b, int /*rid*/, long /*it*/, long /*st*/, long /*et*/)
175 {
176  PgPostBankWrapper *tb = dynamic_cast<PgPostBankWrapper *>(b);
177  if (tb)
178  {
179  return tb->commit();
180  }
181  else
182  {
183  cerr << PHWHERE << "Can only commit PgPostBankWrapper2" << endl;
184  return 0;
185  }
186 }
187 
189 {
190  if (con)
191  {
192  con->Close();
193  }
194  con = nullptr;
195  return 0;
196 }