bool connect(const char* connect_string = NULL, int num_retries = 0, int retry_delay_in_seconds = 0) { assert(m_handle == NULL); m_handle= ndb_mgm_create_handle(); if (!m_handle){ error("connect: ndb_mgm_create_handle failed"); return false; } if (ndb_mgm_set_connectstring(m_handle, connect_string ? connect_string : getConnectString()) != 0){ error("connect: ndb_mgm_set_connectstring failed"); return false; } if (m_timeout > 0 && ndb_mgm_set_timeout(m_handle, m_timeout) != 0){ error("connect: ndb_mgm_set_timeout failed"); return false; } if (ndb_mgm_connect(m_handle,num_retries,retry_delay_in_seconds,0) != 0){ error("connect: ndb_mgm_connect failed"); return false; } // Handshake with the server to make sure it's really there int major, minor, build; char buf[16]; if (ndb_mgm_get_version(m_handle, &major, &minor, &build, sizeof(buf), buf) != 1) { error("connect: ndb_get_version failed"); return false; } //printf("connected to ndb_mgmd version %d.%d.%d\n", // major, minor, build); if ((m_nodeid = ndb_mgm_get_mgmd_nodeid(m_handle)) == 0){ error("connect: could not get nodeid of connected mgmd"); return false; } return true; }
bool SociContainer::connect(StdString const &oConnectString) { StdString con = getConnectString(); if(oConnectString.length() > 0) { if(con != oConnectString) { disconnect(); con = oConnectString; } } if(mSession != NULL) // Same connect string and session already exists. return true; if(con.length() == 0) return false; setConnectString(con); bool rc = true; Progress prg("Connecting to database"); prg->setLabelText("Connecting to database ..."); prg->setMaximum(prg->maximum()+1); QApplication::processEvents(); StdString connectStr = sociConnectString(con); try { mSession = new soci::session(sociFactory(), connectStr); mSociConnectString = connectStr; refreshTables(); } catch(std::runtime_error const &e) { mSociConnectString = ""; ErrorMessage(spt::logging::LoggingItem::LOG_ERROR, MODULENAME, e.what()); rc = false; } prg->setValue(prg->value()+1); return rc; }
bool CSVContainer::connect(StdString const &oFilename) { StdString fn = oFilename; if(fn.length() == 0) fn = getConnectString(); if(fn.length() == 0) return false; bool rc = super::connect(fn); if(rc == false) return false; Progress prg("Open CSV", "Opening File"); setRownum(0); CSV &csv = getCSV(); if(fn.length() > 0) { CSV::ErrorCode rc; disconnect(); csv.setFilename(fn); CSV::Openmode md = getOpenmode(); if((rc = csv.open(md)) != CSV::ErrorCode::CSV_OK) { ErrorMessage(spt::logging::LoggingItem::LOG_ERROR, "CSV", "Unable to open "+fn); csv.setFilename(""); return false; } } else return false; csv.rewind(); return true; }
std::vector<std::vector<StdString>> SociContainer::fetchRows(std::vector<DatabaseColumn *> &oColumns, StdString const &oQuery, bool &bException, int nCount) { StdString t; StdString sql; std::vector<std::vector<StdString>> rows; bException = false; if(oQuery.length() == 0) return rows; Progress prg("Fetching rows ..."); int n = 0; if(mSession == NULL) { connect(); if(mSession == NULL) { ErrorMessage(spt::logging::LoggingItem::LOG_ERROR, MODULENAME, "Unable to connect to "+getConnectString()); return rows; } } prg->setLabelText("Fetching rows ..."); if(nCount > 0) { prg->setMaximum(prg->maximum()+nCount+1); sql = limitQuery(oQuery, nCount); } else { prg->setMaximum(prg->maximum()+1); sql = oQuery; } try { soci::rowset<soci::row> rs = (mSession->prepare << sql); for(soci::row const &row : rs) { QApplication::processEvents(); if(prg->wasCanceled()) break; n++; std::stringstream ss; ss << "Row "<< n << " ..."; prg->setLabelText(spt::string::toQt(ss.str())); if(nCount) prg->setValue(prg->value()+1); rows.push_back(fromRow(oColumns, row)); if(n >= nCount) break; } } catch(std::exception &e) { bException = true; ErrorMessage(spt::logging::LoggingItem::LOG_ERROR, MODULENAME, sql); ErrorMessage(spt::logging::LoggingItem::LOG_ERROR, MODULENAME, e.what()); } n -= nCount; prg->setValue(prg->value()+1+n); return rows; }
std::vector<std::string> const& oracleCredentials() { std::string word; std::string username = ""; std::string password = ""; std::string connectstring = ""; bool userOk = false, passOk = false, connectOk = false; std::string filename; try { filename = getGliteLocationFile(); if (filename.length() == 0) return recoveryVector; std::ifstream in(filename.c_str()); if(!in){ logger::writeLog("Database credentials file cannot be read, check location and permissions", true); return recoveryVector; } std::vector<std::string>::iterator it; while (in >> word){ fileDB.push_back(word); } for (it = fileDB.begin(); it < fileDB.end(); it++) { if (!userOk) { username = getUserName(*it, it); if (username.length() > 0) { userOk = true; continue; } } if (!passOk) { password = getPassword(*it, it); if (password.length() > 0) { passOk = true; continue; } } if (!connectOk) { connectstring = getConnectString(*it, it); if (connectstring.length() > 0) { connectOk = true; continue; } } } in.close(); fileDB.clear(); fileDB.push_back(username); fileDB.push_back(password); fileDB.push_back(connectstring); return fileDB; } catch (...) { logger::writeLog("Database credentials file cannot be found", true); return recoveryVector; } }
StdString FileContainerBase::getFilename(void) { return getConnectString(); }