Commit f8765319 authored by Steven Murray's avatar Steven Murray
Browse files

Added skeleton versions of OcciConn and OcciStmt

parent 1660da57
......@@ -15,13 +15,18 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
cmake_minimum_required (VERSION 2.6)
find_package (oracle-instantclient REQUIRED)
find_package (sqlite REQUIRED)
include_directories (${ORACLE-INSTANTCLIENT_INCLUDE_DIRS})
set (CATALOGUE_LIB_SRC_FILES
Catalogue.cpp
ColumnNameToIdx.cpp
DbLogin.cpp
DummyCatalogue.cpp
OcciConn.cpp
OcciStmt.cpp
Sqlite.cpp
SqliteCatalogue.cpp
SqliteConn.cpp
......
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/OcciConn.hpp"
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::catalogue::OcciConn::OcciConn(oracle::occi::Environment *const env,
const DbLogin &dbLogin):
m_env(env),
m_conn(m_env->createConnection(
dbLogin.username,
dbLogin.password,
dbLogin.database)) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::catalogue::OcciConn::~OcciConn() throw() {
try {
close(); // Idempotent close() mthod
} catch(...) {
// Destructor should not throw any exceptions
}
}
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void cta::catalogue::OcciConn::close() {
std::lock_guard<std::mutex> lock(m_mutex);
if(m_conn != NULL) {
m_env->terminateConnection(m_conn);
m_conn = NULL;
}
}
//------------------------------------------------------------------------------
// get
//------------------------------------------------------------------------------
oracle::occi::Connection *cta::catalogue::OcciConn::get() const {
return m_conn;
}
//------------------------------------------------------------------------------
// operator->()
//------------------------------------------------------------------------------
oracle::occi::Connection *cta::catalogue::OcciConn::operator->() const {
return get();
}
//------------------------------------------------------------------------------
// createStmt
//------------------------------------------------------------------------------
cta::catalogue::OcciStmt *cta::catalogue::OcciConn::createStmt(
const std::string &sql) {
return NULL;
}
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "catalogue/DbLogin.hpp"
#include <occi.h>
#include <mutex>
#include <string>
namespace cta {
namespace catalogue {
/**
* Forward declaraion to avoid a circular dependency beween OcciConn and
* OcciStmt.
*/
class OcciStmt;
/**
* A C++ wrapper around a connection to an OCCI database.
*/
class OcciConn {
public:
/**
* Constructor.
*
* @param env The OCCI environment.
* @param dbLogin The database connetion details.
*/
OcciConn(oracle::occi::Environment *const env,
const DbLogin &dbLogin);
/**
* Destructor.
*/
~OcciConn() throw();
/**
* Idempotent close() method. The destructor calls this method.
*/
void close();
/**
* Returns the underlying OCCI connection.
*
* This method will always return a valid pointer.
*
* @return The underlying OCCI connection.
*/
oracle::occi::Connection *get() const;
/**
* An alias for the get() method.
*
* @return The underlying OCCI connection.
*/
oracle::occi::Connection *operator->() const;
/**
* Creates a prepared statement.
*
* @sql The SQL statement.
* @return The prepared statement.
*/
OcciStmt *createStmt(const std::string &sql);
private:
/**
* Mutex used to serialize access to the database connection.
*/
std::mutex m_mutex;
/**
* The OCCI environment.
*/
oracle::occi::Environment *m_env;
/**
* The OCCI connection.
*/
oracle::occi::Connection *m_conn;
}; // class OcciConn
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/OcciConn.hpp"
#include "catalogue/OcciStmt.hpp"
#include "common/exception/Exception.hpp"
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::catalogue::OcciStmt::OcciStmt(const std::string &sql, OcciConn &conn,
oracle::occi::Statement *const stmt):
m_sql(sql),
m_conn(conn),
m_stmt(stmt) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::catalogue::OcciStmt::~OcciStmt() throw() {
try {
close(); // Idempotent close() method
} catch(...) {
// Destructor does not throw
}
}
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void cta::catalogue::OcciStmt::close() {
std::lock_guard<std::mutex> lock(m_mutex);
if(NULL != m_stmt) {
m_conn->terminateStatement(m_stmt);
m_stmt = NULL;
}
}
//------------------------------------------------------------------------------
// getSql
//------------------------------------------------------------------------------
const std::string &cta::catalogue::OcciStmt::getSql() const {
return m_sql;
}
//------------------------------------------------------------------------------
// bind
//------------------------------------------------------------------------------
void cta::catalogue::OcciStmt::bind(const std::string &paramName,
const uint64_t paramValue) {
exception::Exception ex;
ex.getMessage() << __FUNCTION__ << " is not implemented";
throw ex;
}
//------------------------------------------------------------------------------
// bind
//------------------------------------------------------------------------------
void cta::catalogue::OcciStmt::bind(const std::string &paramName,
const std::string &paramValue) {
exception::Exception ex;
ex.getMessage() << __FUNCTION__ << " is not implemented";
throw ex;
}
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "catalogue/ColumnNameToIdx.hpp"
#include <mutex>
#include <occi.h>
#include <stdint.h>
#include <string>
namespace cta {
namespace catalogue {
/**
* Forward decalaration to avoid a circular depedency between OcciConn and
* OcciStmt.
*/
class OcciConn;
/**
* A C++ wrapper around an OCCI prepared statement.
*/
class OcciStmt {
public:
/**
* Constructor.
*
* @param sql The SQL statement.
* @param conn The database connection.
* @param stmt The prepared statement.
*/
OcciStmt(const std::string &sql, OcciConn &conn,
oracle::occi::Statement *const stmt);
/**
* Destructor.
*/
~OcciStmt() throw();
/**
* Idempotent close() method. The destructor calls this method.
*/
void close();
/**
* Returns the SQL statement.
*
* @return The SQL statement.
*/
const std::string &getSql() const;
/**
* Binds an SQL parameter.
*
* @param paramName The name of the parameter.
* @param paramValue The value to be bound.
*/
void bind(const std::string &paramName, const uint64_t paramValue);
/**
* Binds an SQL parameter.
*
* @param paramName The name of the parameter.
* @param paramValue The value to be bound.
*/
void bind(const std::string &paramName, const std::string &paramValue);
private:
/**
* Mutex used to serialize access to the prepared statement.
*/
std::mutex m_mutex;
/**
* The SQL statement.
*/
const std::string m_sql;
/**
* The database connection.
*/
OcciConn &m_conn;
/**
* The prepared statement.
*/
oracle::occi::Statement *m_stmt;
}; // class SqlLiteStmt
} // namespace catalogue
} // namespace cta
......@@ -39,7 +39,19 @@ cta::catalogue::SqliteConn::SqliteConn(const std::string &filename) {
// destructor
//------------------------------------------------------------------------------
cta::catalogue::SqliteConn::~SqliteConn() throw() {
sqlite3_close(m_conn);
close();
}
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void cta::catalogue::SqliteConn::close() {
std::lock_guard<std::mutex> lock(m_mutex);
if(m_conn != NULL) {
sqlite3_close(m_conn);
m_conn = NULL;
}
}
//------------------------------------------------------------------------------
......
......@@ -32,7 +32,7 @@ namespace catalogue {
class SqliteStmt;
/**
* A C++ wrapper around a connectioin an SQLite database.
* A C++ wrapper around a connection to an SQLite database.
*/
class SqliteConn {
public:
......@@ -40,7 +40,7 @@ public:
/**
* Constructor.
*
* @param filename The filename to be passed to the sqlit3_open() function.
* @param filename The filename to be passed to the sqlite3_open() function.
*/
SqliteConn(const std::string &filename);
......@@ -49,6 +49,11 @@ public:
*/
~SqliteConn() throw();
/**
* Idempotent close() method. The destructor calls this method.
*/
void close();
/**
* Returns the underlying database connection.
*
......@@ -79,8 +84,7 @@ public:
private:
/**
* Mutex to be used to create a critical section around the database
* connection.
* Mutex used to serialize access to the database connection.
*/
std::mutex m_mutex;
......@@ -89,7 +93,7 @@ private:
*/
sqlite3 *m_conn;
}; // class SqlLiteConn
}; // class SqliteConn
} // namespace catalogue
} // namespace cta
......@@ -34,7 +34,23 @@ cta::catalogue::SqliteStmt::SqliteStmt(const std::string &sql,
// destructor
//------------------------------------------------------------------------------
cta::catalogue::SqliteStmt::~SqliteStmt() throw() {
sqlite3_finalize(m_stmt);
try {
close(); // Idempotent close() method
} catch(...) {
// Destructor does not throw
}
}
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void cta::catalogue::SqliteStmt::close() {
std::lock_guard<std::mutex> lock(m_mutex);
if(NULL != m_stmt) {
sqlite3_finalize(m_stmt);
m_stmt = NULL;
}
}
//------------------------------------------------------------------------------
......
......@@ -44,7 +44,7 @@ public:
* Constructor.
*
* @param sql The SQL statement.
* @param stmt The SQLite prepared statement.
* @param stmt The prepared statement.
*/
SqliteStmt(const std::string &sql, sqlite3_stmt *const stmt);
......@@ -53,6 +53,11 @@ public:
*/
~SqliteStmt() throw();
/**
* Idempotent close() method. The destructor calls this method.
*/
void close();
/**
* Returns the SQL statement.
*
......@@ -120,6 +125,11 @@ public:
private:
/**
* Mutex used to serialize access to the prepared statement.
*/
std::mutex m_mutex;
/**
* The SQL statement.
*/
......
# The CERN Tape Archive(CTA) project
# Copyright(C) 2015 CERN
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This module will set the following variables:
# ORACLE-INSTANTCLIENT_FOUND
# ORACLE-INSTANTCLIENT_INCLUDE_DIRS
# ORACLE-INSTANTCLIENT_LIBRARIES
find_path(ORACLE-INSTANTCLIENT_INCLUDE_DIRS
occi.h
PATHS /usr/include/oracle/12.1/client64
NO_DEFAULT_PATH)
find_library(ORACLE-INSTANTCLIENT_LIBRARIES
NAME occi
PATHS /usr/lib/oracle/12.1/client64/lib
NO_DEFAULT_PATH)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(oracle-instantclient DEFAULT_MSG
ORACLE-INSTANTCLIENT_INCLUDE_DIRS ORACLE-INSTANTCLIENT_LIBRARIES)
......@@ -41,6 +41,7 @@ BuildRequires: cryptopp-devel >= 5.6.2
BuildRequires: libuuid-devel >= 2.17
BuildRequires: json-c-devel >= 0.11
BuildRequires: libattr-devel >= 2.4.44
BuildRequires: oracle-instantclient12.1-devel
# only build debug info if you're building the whole code
%description
......@@ -165,4 +166,4 @@ Unit tests and system tests with virtual tape drives
%attr(0755,root,root) %{_libdir}/libctatapeserverutilsunittests.so
%attr(0755,root,root) %{_libdir}/libctautilsunittests.so
%attr(0755,root,root) %{_libdir}/libctadaemonunittests.so
%attr(0644,root,root) %{_datadir}/%{name}-%{ctaVersion}/unittest/*.suppr
\ No newline at end of file
%attr(0644,root,root) %{_datadir}/%{name}-%{ctaVersion}/unittest/*.suppr
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment