File indexing completed on 2024-04-06 12:00:07
0001
0002 #include <cstring>
0003 #include <cstdio>
0004
0005 #include "OnlineDB/Oracle/interface/Oracle.h"
0006 #include "CaloOnlineTools/HcalOnlineDb/interface/ConnectionManager.h"
0007 #include <cctype>
0008
0009 ConnectionManager::ConnectionManager() : env(nullptr), conn(nullptr) {}
0010
0011 static const std::string keyFile("/nfshome0/hcalsw/.ReadOMDSKey");
0012
0013 static void clean(char* s) {
0014 for (int x = strlen(s) - 1; x >= 0; x--) {
0015 if (isspace(s[x]))
0016 s[x] = 0;
0017 }
0018 }
0019
0020 bool ConnectionManager::connect() {
0021 if (env != nullptr)
0022 return true;
0023 std::string username, password, database;
0024
0025 char s[100];
0026 FILE* f = fopen(keyFile.c_str(), "r");
0027 s[0] = 0;
0028 fgets(s, 100, f);
0029 clean(s);
0030 username = s;
0031 s[0] = 0;
0032 fgets(s, 100, f);
0033 clean(s);
0034 password = s;
0035 s[0] = 0;
0036 fgets(s, 100, f);
0037 clean(s);
0038 database = s;
0039 fclose(f);
0040
0041
0042 try {
0043 env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);
0044 conn = env->createConnection(username, password, database);
0045 } catch (...) {
0046 return false;
0047 }
0048 return true;
0049 }
0050 oracle::occi::Statement* ConnectionManager::getStatement(const std::string& query) {
0051 if (env == nullptr)
0052 return nullptr;
0053 return conn->createStatement(query);
0054 }
0055 void ConnectionManager::disconnect() {
0056 if (env == nullptr)
0057 return;
0058 env->terminateConnection(conn);
0059 oracle::occi::Environment::terminateEnvironment(env);
0060 env = nullptr;
0061 conn = nullptr;
0062 }