Skip to content
Snippets Groups Projects
Commit 3a1d3401 authored by Steven Murray's avatar Steven Murray
Browse files

Improved the code readability of the catalogue directory

parent 901afa43
No related branches found
No related tags found
No related merge requests found
......@@ -32,64 +32,69 @@
#include <string.h>
namespace {
using namespace cta;
/**
* Reads the entire contents of the specified files and returns a list of the
* non-empty lines.
*
* A line is considered not empty if it contains characters that are not white
* space and are not part of a comment.
*
* @return A list of the non-empty lines.
*/
std::list<std::string> readNonEmptyLines(
const std::string &filename) {
std::ifstream file(filename);
if(!file) {
exception::Exception ex;
ex.getMessage() << "Failed to open " << filename;
throw ex;
}
std::list<std::string> lines;
std::string line;
using namespace cta;
while(std::getline(file, line)) {
// Remove the newline character if there is one
{
const std::string::size_type newlinePos = line.find("\n");
if(newlinePos != std::string::npos) {
line = line.substr(0, newlinePos);
}
}
/**
* Reads the entire contents of the specified files and returns a list of the
* non-empty lines.
*
* A line is considered not empty if it contains characters that are not white
* space and are not part of a comment.
*
* @return A list of the non-empty lines.
*/
std::list<std::string> readNonEmptyLines(
const std::string &filename) {
// If there is a comment, then remove it from the line
{
const std::string::size_type startOfComment = line.find("#");
if(startOfComment != std::string::npos) {
line = line.substr(0, startOfComment);
}
}
std::ifstream file(filename);
if(!file) {
exception::Exception ex;
ex.getMessage() << "Failed to open " << filename;
throw ex;
}
std::list<std::string> lines;
std::string line;
// Left and right trim the line of whitespace
line = utils::trimString(std::string(line));
while(std::getline(file, line)) {
// Remove the newline character if there is one
{
const std::string::size_type newlinePos = line.find("\n");
if(newlinePos != std::string::npos) {
line = line.substr(0, newlinePos);
}
}
// If the line is not empty
if(!line.empty()) {
lines.push_back(line);
// If there is a comment, then remove it from the line
{
const std::string::size_type startOfComment = line.find("#");
if(startOfComment != std::string::npos) {
line = line.substr(0, startOfComment);
}
}
return lines;
// Left and right trim the line of whitespace
line = utils::trimString(std::string(line));
// If the line is not empty
if(!line.empty()) {
lines.push_back(line);
}
}
return lines;
}
} // anonymous namespace
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::catalogue::DbLogin::DbLogin(
DbLogin::DbLogin(
const std::string &username,
const std::string &password,
const std::string &database):
......@@ -101,8 +106,7 @@ cta::catalogue::DbLogin::DbLogin(
//------------------------------------------------------------------------------
// readFromFile
//------------------------------------------------------------------------------
cta::catalogue::DbLogin cta::catalogue::DbLogin::readFromFile(
const std::string &filename) {
DbLogin DbLogin::readFromFile(const std::string &filename) {
const std::string fileFormat = "username/password@database";
const std::list<std::string> lines = readNonEmptyLines(filename);
......@@ -130,3 +134,6 @@ cta::catalogue::DbLogin cta::catalogue::DbLogin::readFromFile(
return DbLogin(userAndPass[0], userAndPass[1], userPassAndDb[1]);
}
} // namesapce catalogue
} // namespace cta
......@@ -26,11 +26,13 @@
#include <stdexcept>
#include <string>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::catalogue::OcciConn::OcciConn(OcciEnv &env,
oracle::occi::Connection *const conn):
OcciConn::OcciConn(OcciEnv &env, oracle::occi::Connection *const conn):
m_env(env),
m_conn(conn) {
if(NULL == conn) {
......@@ -42,7 +44,7 @@ cta::catalogue::OcciConn::OcciConn(OcciEnv &env,
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::catalogue::OcciConn::~OcciConn() throw() {
OcciConn::~OcciConn() throw() {
try {
close(); // Idempotent close() mthod
} catch(...) {
......@@ -53,7 +55,7 @@ cta::catalogue::OcciConn::~OcciConn() throw() {
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void cta::catalogue::OcciConn::close() {
void OcciConn::close() {
std::lock_guard<std::mutex> lock(m_mutex);
if(m_conn != NULL) {
......@@ -65,21 +67,21 @@ void cta::catalogue::OcciConn::close() {
//------------------------------------------------------------------------------
// get
//------------------------------------------------------------------------------
oracle::occi::Connection *cta::catalogue::OcciConn::get() const {
oracle::occi::Connection *OcciConn::get() const {
return m_conn;
}
//------------------------------------------------------------------------------
// operator->()
//------------------------------------------------------------------------------
oracle::occi::Connection *cta::catalogue::OcciConn::operator->() const {
oracle::occi::Connection *OcciConn::operator->() const {
return get();
}
//------------------------------------------------------------------------------
// createStmt
//------------------------------------------------------------------------------
cta::catalogue::OcciStmt *cta::catalogue::OcciConn::createStmt(const char *sql) {
OcciStmt *OcciConn::createStmt(const char *const sql) {
try {
if(NULL == sql) throw std::runtime_error("sql is NULL");
......@@ -94,3 +96,6 @@ cta::catalogue::OcciStmt *cta::catalogue::OcciConn::createStmt(const char *sql)
throw std::runtime_error(std::string(__FUNCTION__) + "failed: " + ne.what());
}
}
} // namespace catalogue
} // namespace cta
......@@ -25,10 +25,13 @@
#include <stdexcept>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::catalogue::OcciEnv::OcciEnv() {
OcciEnv::OcciEnv() {
using namespace oracle::occi;
m_env = Environment::createEnvironment(Environment::THREADED_MUTEXED);
if(NULL == m_env) {
......@@ -40,7 +43,7 @@ cta::catalogue::OcciEnv::OcciEnv() {
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::catalogue::OcciEnv::~OcciEnv() throw() {
OcciEnv::~OcciEnv() throw() {
using namespace oracle::occi;
Environment::terminateEnvironment(m_env);
......@@ -49,21 +52,21 @@ cta::catalogue::OcciEnv::~OcciEnv() throw() {
//------------------------------------------------------------------------------
// get
//------------------------------------------------------------------------------
oracle::occi::Environment *cta::catalogue::OcciEnv::get() const {
oracle::occi::Environment *OcciEnv::get() const {
return m_env;
}
//------------------------------------------------------------------------------
// operator->
//------------------------------------------------------------------------------
oracle::occi::Environment *cta::catalogue::OcciEnv::operator->() const {
oracle::occi::Environment *OcciEnv::operator->() const {
return get();
}
//------------------------------------------------------------------------------
// creatConn
// createConn
//------------------------------------------------------------------------------
cta::catalogue::OcciConn *cta::catalogue::OcciEnv::createConn(
cta::catalogue::OcciConn *OcciEnv::createConn(
const char *const username,
const char *const password,
const char *const database) {
......@@ -82,3 +85,6 @@ cta::catalogue::OcciConn *cta::catalogue::OcciEnv::createConn(
throw std::runtime_error(std::string(__FUNCTION__) + " failed:" + ne.what());
}
}
} // namespace catalogue
} // namespace cta
......@@ -24,22 +24,22 @@
#include <stdexcept>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::catalogue::OcciRset::OcciRset(OcciStmt &stmt,
oracle::occi::ResultSet *const rset):
OcciRset::OcciRset(OcciStmt &stmt, oracle::occi::ResultSet *const rset):
m_stmt(stmt),
m_rset(rset) {
if(NULL == rset) {
throw std::runtime_error(std::string(__FUNCTION__) + " failed: rset is NULL");
}
if(NULL == rset) throw std::runtime_error(std::string(__FUNCTION__) + " failed: rset is NULL");
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::catalogue::OcciRset::~OcciRset() throw() {
OcciRset::~OcciRset() throw() {
try {
close(); // Idempotent close()
} catch(...) {
......@@ -50,7 +50,7 @@ cta::catalogue::OcciRset::~OcciRset() throw() {
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void cta::catalogue::OcciRset::close() {
void OcciRset::close() {
std::lock_guard<std::mutex> lock(m_mutex);
if(NULL != m_rset) {
......@@ -62,13 +62,16 @@ void cta::catalogue::OcciRset::close() {
//------------------------------------------------------------------------------
// get
//------------------------------------------------------------------------------
oracle::occi::ResultSet *cta::catalogue::OcciRset::get() const {
oracle::occi::ResultSet *OcciRset::get() const {
return m_rset;
}
//------------------------------------------------------------------------------
// operator->
//------------------------------------------------------------------------------
oracle::occi::ResultSet *cta::catalogue::OcciRset::operator->() const {
oracle::occi::ResultSet *OcciRset::operator->() const {
return get();
}
} // namespace catalogue
} // namespace cta
......@@ -25,18 +25,16 @@
#include <cstring>
#include <stdexcept>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::catalogue::OcciStmt::OcciStmt(const char *const sql, OcciConn &conn,
oracle::occi::Statement *const stmt):
OcciStmt::OcciStmt(const char *const sql, OcciConn &conn, oracle::occi::Statement *const stmt) :
m_conn(conn),
m_stmt(stmt) {
if(NULL == stmt) {
std::runtime_error ex(std::string(__FUNCTION__) + " failed"
": OCCI statment is a NULL pointer");
throw ex;
}
if(NULL == stmt) throw std::runtime_error(std::string(__FUNCTION__) + " failed: stmt is NULL");
// Work with C strings because they haven't changed with respect to _GLIBCXX_USE_CXX11_ABI
const std::size_t sqlLen = std::strlen(sql);
......@@ -48,12 +46,12 @@ cta::catalogue::OcciStmt::OcciStmt(const char *const sql, OcciConn &conn,
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::catalogue::OcciStmt::~OcciStmt() throw() {
OcciStmt::~OcciStmt() throw() {
delete m_sql;
try {
close(); // Idempotent close() method
} catch(...) {
} catch (...) {
// Destructor does not throw
}
}
......@@ -61,10 +59,10 @@ cta::catalogue::OcciStmt::~OcciStmt() throw() {
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void cta::catalogue::OcciStmt::close() {
void OcciStmt::close() {
std::lock_guard<std::mutex> lock(m_mutex);
if(NULL != m_stmt) {
if (NULL != m_stmt) {
m_conn->terminateStatement(m_stmt);
m_stmt = NULL;
}
......@@ -73,28 +71,28 @@ void cta::catalogue::OcciStmt::close() {
//------------------------------------------------------------------------------
// getSql
//------------------------------------------------------------------------------
const char *cta::catalogue::OcciStmt::getSql() const {
const char *OcciStmt::getSql() const {
return m_sql;
}
//------------------------------------------------------------------------------
// get
//------------------------------------------------------------------------------
oracle::occi::Statement *cta::catalogue::OcciStmt::get() const {
oracle::occi::Statement *OcciStmt::get() const {
return m_stmt;
}
//------------------------------------------------------------------------------
// operator->
//------------------------------------------------------------------------------
oracle::occi::Statement *cta::catalogue::OcciStmt::operator->() const {
oracle::occi::Statement *OcciStmt::operator->() const {
return get();
}
//------------------------------------------------------------------------------
// bind
//------------------------------------------------------------------------------
void cta::catalogue::OcciStmt::bind(const char *paramName, const uint64_t paramValue) {
void OcciStmt::bind(const char *paramName, const uint64_t paramValue) {
std::runtime_error ex(std::string(__FUNCTION__) + " is not implemented");
throw ex;
}
......@@ -102,7 +100,7 @@ void cta::catalogue::OcciStmt::bind(const char *paramName, const uint64_t paramV
//------------------------------------------------------------------------------
// bind
//------------------------------------------------------------------------------
void cta::catalogue::OcciStmt::bind(const char *paramName, const char *paramValue) {
void OcciStmt::bind(const char *paramName, const char *paramValue) {
std::runtime_error ex(std::string(__FUNCTION__) + " is not implemented");
throw ex;
}
......@@ -110,6 +108,9 @@ void cta::catalogue::OcciStmt::bind(const char *paramName, const char *paramValu
//------------------------------------------------------------------------------
// execute
//------------------------------------------------------------------------------
cta::catalogue::OcciRset *cta::catalogue::OcciStmt::execute() {
OcciRset *OcciStmt::execute() {
return NULL;
}
} // namespace catalogue
} // namespace cta
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment