-
Steven Murray authoredSteven Murray authored
Stmt.cpp 12.45 KiB
/*
* 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 "common/exception/Exception.hpp"
#include "rdbms/Stmt.hpp"
#include "rdbms/StmtPool.hpp"
#include "rdbms/wrapper/StmtWrapper.hpp"
namespace cta {
namespace rdbms {
//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
Stmt::Stmt():
m_stmtPool(nullptr) {
}
//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
Stmt::Stmt(std::unique_ptr<wrapper::StmtWrapper> stmt, StmtPool &stmtPool):
m_stmt(std::move(stmt)),
m_stmtPool(&stmtPool) {
}
//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
Stmt::Stmt(Stmt &&other):
m_stmt(std::move(other.m_stmt)),
m_stmtPool(other.m_stmtPool){
}
//-----------------------------------------------------------------------------
// destructor
//-----------------------------------------------------------------------------
Stmt::~Stmt() noexcept {
try {
// If this smart prepared statement currently points to a prepared
// statement then return it back to its pool
if(nullptr != m_stmtPool && nullptr != m_stmt) {
m_stmtPool->returnStmt(std::move(m_stmt));
}
} catch(...) {
// Ignore any exceptions
}
}
//-----------------------------------------------------------------------------
// operator=
//-----------------------------------------------------------------------------
Stmt &Stmt::operator=(Stmt &&rhs) {
// If the cached statement is not already owned
if(rhs.m_stmt != m_stmt) {
// If this smart cached statement already points to cached statement, then
// return it back to its pool
if(nullptr != m_stmt && nullptr != m_stmtPool) {
m_stmtPool->returnStmt(std::move(m_stmt));
}
// Take ownership of the new cached statement
m_stmt = std::move(rhs.m_stmt);
m_stmtPool = rhs.m_stmtPool;
rhs.m_stmtPool = nullptr;
}
return *this;
}
//-----------------------------------------------------------------------------
// getSql
//-----------------------------------------------------------------------------
const std::string &Stmt::getSql() const {
try {
if(nullptr != m_stmt) {
return m_stmt->getSql();
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// getParamIdx
//-----------------------------------------------------------------------------
uint32_t Stmt::getParamIdx(const std::string ¶mName) const {
try {
if(nullptr != m_stmt) {
return m_stmt->getParamIdx(paramName);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindUint16
//-----------------------------------------------------------------------------
void Stmt::bindUint16(const std::string ¶mName, const uint16_t paramValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindUint16(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindOptionalUint16
//-----------------------------------------------------------------------------
void Stmt::bindOptionalUint16(const std::string ¶mName, const optional<uint16_t> ¶mValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindOptionalUint16(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindUint64
//-----------------------------------------------------------------------------
void Stmt::bindUint64(const std::string ¶mName, const uint64_t paramValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindUint64(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindOptionalUint64
//-----------------------------------------------------------------------------
void Stmt::bindOptionalUint64(const std::string ¶mName, const optional<uint64_t> ¶mValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindOptionalUint64(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindDouble
//-----------------------------------------------------------------------------
void Stmt::bindDouble(const std::string ¶mName, const double paramValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindDouble(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindOptionalDouble
//-----------------------------------------------------------------------------
void Stmt::bindOptionalDouble(const std::string ¶mName, const optional<double> ¶mValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindOptionalDouble(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindBool
//-----------------------------------------------------------------------------
void Stmt::bindBool(const std::string ¶mName, const bool paramValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindBool(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindOptionalBool
//-----------------------------------------------------------------------------
void Stmt::bindOptionalBool(const std::string ¶mName, const optional<bool> ¶mValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindOptionalBool(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindString
//-----------------------------------------------------------------------------
void Stmt::bindBlob(const std::string ¶mName, const std::string ¶mValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindBlob(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindString
//-----------------------------------------------------------------------------
void Stmt::bindString(const std::string ¶mName, const std::string ¶mValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindString(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// bindOptionalString
//-----------------------------------------------------------------------------
void Stmt::bindOptionalString(const std::string ¶mName, const optional<std::string> ¶mValue) {
try {
if(nullptr != m_stmt) {
return m_stmt->bindOptionalString(paramName, paramValue);
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// executeQuery
//-----------------------------------------------------------------------------
Rset Stmt::executeQuery() {
try {
if(nullptr != m_stmt) {
return Rset(m_stmt->executeQuery());
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// executeNonQuery
//-----------------------------------------------------------------------------
void Stmt::executeNonQuery() {
try {
if(nullptr != m_stmt) {
return m_stmt->executeNonQuery();
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// getNbAffectedRows
//-----------------------------------------------------------------------------
uint64_t Stmt::getNbAffectedRows() const {
try {
if(nullptr != m_stmt) {
return m_stmt->getNbAffectedRows();
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
//-----------------------------------------------------------------------------
// getStmt
//-----------------------------------------------------------------------------
wrapper::StmtWrapper &Stmt::getStmt() {
try {
if(nullptr != m_stmt) {
return *m_stmt;
} else {
throw exception::Exception("Stmt does not contain a cached statement");
}
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
throw;
}
}
} // namespace rdbms
} // namespace cta