Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OnlMonStatusDB.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file OnlMonStatusDB.cc
1 #include "OnlMonStatusDB.h"
2 
3 #include <odbc++/connection.h>
4 #include <odbc++/drivermanager.h>
5 #include <odbc++/resultset.h>
6 #include <odbc++/statement.h> // for Statement
7 #include <odbc++/types.h> // for SQLException
8 
9 #include <iostream>
10 #include <sstream>
11 
12 static odbc::Connection* con = nullptr;
13 
15  : table(tablename)
16 {
17 }
18 
20 {
21  delete con;
22  con = nullptr;
23 }
24 
26 {
27  if (GetConnection())
28  {
29  return -1;
30  }
31 
32  //Postgres version
33  //std::cout << con->getMetaData()-> getDatabaseProductVersion() << std::endl;
34  odbc::Statement* stmt = con->createStatement();
35  std::ostringstream cmd;
36  // cmd << "SELECT COUNT(*) FROM " << name << " WHERE 1 = 2" ;
37  cmd << "SELECT * FROM pg_tables where tablename = '" << table << "'";
38 #ifdef VERBOSE
39 
40  std::cout << cmd.str() << std::endl;
41 #endif
42 
43  odbc::ResultSet* rs = nullptr;
44  try
45  {
46  rs = stmt->executeQuery(cmd.str());
47  }
48  catch (odbc::SQLException& e)
49  {
50  const std::string& message = e.getMessage();
51  if (message.find("does not exist") == std::string::npos)
52  {
53  std::cout << "Exception caught" << std::endl;
54  std::cout << "Message: " << e.getMessage() << std::endl;
55  }
56  }
57  cmd.str("");
58  if (!rs->next())
59  {
60  delete rs;
61  cmd << "CREATE TABLE " << table << "(runnumber int, primary key(runnumber))";
62  try
63  {
64  stmt->executeUpdate(cmd.str());
65  }
66  catch (odbc::SQLException& e)
67  {
68  std::cout << "caught exception Message: " << e.getMessage() << std::endl;
69  }
70  }
71  return 0;
72 }
73 
75 {
76  std::ostringstream cmd;
77  if (CheckAndCreateTable())
78  {
79  std::cout << "Problem creating " << table << std::endl;
80  return -1;
81  }
82  cmd << "SELECT * FROM " << table << " LIMIT 1";
83  odbc::ResultSet* rs = nullptr;
84  odbc::Statement* stmt = con->createStatement();
85  try
86  {
87  rs = stmt->executeQuery(cmd.str());
88  }
89  catch (odbc::SQLException& e)
90  {
91  std::cout << __PRETTY_FUNCTION__ << "Exception caught" << std::endl;
92  std::cout << "Message: " << e.getMessage() << std::endl;
93  return -1;
94  }
95  try
96  {
97  rs->findColumn(name);
98  }
99  catch (odbc::SQLException& e)
100  {
101  const std::string& exceptionmessage = e.getMessage();
102  if (exceptionmessage.find("not found in result set") != std::string::npos)
103  {
104  cmd.str("");
105  cmd << "ALTER TABLE "
106  << table
107  << " ADD "
108  << name
109  << " int default 0";
110  try
111  {
112  odbc::Statement* stmtup = con->createStatement();
113  stmtup->executeUpdate(cmd.str());
114  }
115  catch (odbc::SQLException& e2)
116  {
117  std::cout << "Exception caught: " << e2.getMessage() << std::endl;
118  return -1;
119  }
120  }
121  }
122  return 0;
123 }
124 
126 {
127  if (GetConnection() != 0)
128  {
129  std::cout << "problem" << std::endl;
130  return -1;
131  }
132  if (findRunNumInDB(runnumber) == 0)
133  {
134  return 0;
135  }
136  odbc::Statement* statement = con->createStatement();
137  std::ostringstream cmd;
138  cmd << "INSERT INTO "
139  << table
140  << " (runnumber) VALUES ("
141  << runnumber << ")";
142  try
143  {
144  statement->executeUpdate(cmd.str());
145  }
146  catch (odbc::SQLException& e)
147  {
148  std::cout << e.getMessage() << std::endl;
149  return -1;
150  }
151  return 0;
152 }
153 
155 {
156  odbc::Statement* statement = nullptr;
157  odbc::ResultSet* rs = nullptr;
158  std::ostringstream cmd;
159  cmd << "SELECT runnumber FROM "
160  << table
161  << " WHERE runnumber = "
162  << runnumber;
163 
164  statement = con->createStatement();
165 
166  try
167  {
168  rs = statement->executeQuery(cmd.str());
169  }
170  catch (odbc::SQLException& e)
171  {
172  std::cout << "exception caught: " << e.getMessage() << std::endl;
173  return -1;
174  }
175 
176  if (rs->next())
177  {
178  try
179  {
180  rs->getInt("runnumber");
181  }
182  catch (odbc::SQLException& e)
183  {
184  std::cout << "exception caught: " << e.getMessage() << std::endl;
185  return -1;
186  }
187  }
188  else
189  {
190  return -1;
191  }
192  return 0;
193 }
194 
196 {
197  if (CheckAndCreateMonitor(name))
198  {
199  std::cout << __PRETTY_FUNCTION__ << "Problem encountered, cannot do update" << std::endl;
200  return -1;
201  }
202  if (FindAndInsertRunNum(runnumber) != 0)
203  {
204  std::cout << __PRETTY_FUNCTION__ << "Problem updating runnumber encountered, cannot do update" << std::endl;
205  return -1;
206  }
207 
208  std::ostringstream cmd;
209  cmd << "Update "
210  << table
211  << " set " << name
212  << " = " << status
213  << " where runnumber = "
214  << runnumber;
215  odbc::Statement* stmtupd = nullptr;
216  try
217  {
218  stmtupd = con->createStatement();
219  }
220  catch (odbc::SQLException& e)
221  {
222  std::cout << "Cannot create statement" << std::endl;
223  std::cout << e.getMessage() << std::endl;
224  return -1;
225  }
226 
227  try
228  {
229  stmtupd->executeUpdate(cmd.str());
230  }
231  catch (odbc::SQLException& e)
232  {
233  std::cout << __PRETTY_FUNCTION__ << "Exception caught" << std::endl;
234  std::cout << "Message: " << e.getMessage() << std::endl;
235  return -1;
236  }
237  return 0;
238 }
239 
241 {
242  if (con)
243  {
244  return 0;
245  }
246  try
247  {
248  con = odbc::DriverManager::getConnection(dbname.c_str(), dbowner.c_str(), dbpasswd.c_str());
249  }
250  catch (odbc::SQLException& e)
251  {
252  std::cout << __PRETTY_FUNCTION__
253  << " Exception caught during DriverManager::getConnection" << std::endl;
254  std::cout << "Message: " << e.getMessage() << std::endl;
255  if (con)
256  {
257  delete con;
258  con = nullptr;
259  }
260  return -1;
261  }
262  return 0;
263 }