From 922aee4f6d55c88c13858818d93f287f45d9f8f4 Mon Sep 17 00:00:00 2001 From: Steven Murray <steven.murray@cern.ch> Date: Fri, 20 May 2016 18:56:07 +0200 Subject: [PATCH] SqliteStmt now inherits from DbStmt --- catalogue/DbStmt.hpp | 7 +- catalogue/OcciRsetTest.cpp | 2 +- catalogue/OcciStmt.cpp | 9 +- catalogue/OcciStmt.hpp | 16 ++- catalogue/SqliteCatalogue.cpp | 254 +++++++++++++++++----------------- catalogue/SqliteStmt.cpp | 9 +- catalogue/SqliteStmt.hpp | 16 ++- catalogue/SqliteStmtTest.cpp | 26 ++-- 8 files changed, 178 insertions(+), 161 deletions(-) diff --git a/catalogue/DbStmt.hpp b/catalogue/DbStmt.hpp index 297ac6a711..1855828b5b 100644 --- a/catalogue/DbStmt.hpp +++ b/catalogue/DbStmt.hpp @@ -58,7 +58,7 @@ public: * @param paramName The name of the parameter. * @param paramValue The value to be bound. */ - virtual void bind(const char *const paramName, const uint64_t paramValue) = 0; + virtual void bindUint64(const char *const paramName, const uint64_t paramValue) = 0; /** * Binds an SQL parameter. @@ -76,6 +76,11 @@ public: */ virtual DbRset *executeQuery() = 0; + /** + * Executes the statement. + */ + virtual void executeNonQuery() = 0; + }; // class DbStmt } // namespace catalogue diff --git a/catalogue/OcciRsetTest.cpp b/catalogue/OcciRsetTest.cpp index bab853efb3..a773b12215 100644 --- a/catalogue/OcciRsetTest.cpp +++ b/catalogue/OcciRsetTest.cpp @@ -129,7 +129,7 @@ TEST_F(cta_catalogue_OcciRsetTest, bind_uint32_t) { dbLogin.database.c_str())); const char *const sql = "SELECT :N AS AN_UNSIGNED_INT FROM DUAL"; std::unique_ptr<DbStmt> stmt(conn->createStmt(sql)); - stmt->bind(":N", 1234); + stmt->bindUint64(":N", 1234); std::unique_ptr<DbRset> rset(stmt->executeQuery()); ASSERT_TRUE(rset->next()); const uint32_t n = rset->columnUint64("AN_UNSIGNED_INT"); diff --git a/catalogue/OcciStmt.cpp b/catalogue/OcciStmt.cpp index 6daa0d8f70..673981dd2e 100644 --- a/catalogue/OcciStmt.cpp +++ b/catalogue/OcciStmt.cpp @@ -168,7 +168,7 @@ const char *OcciStmt::getSql() const { //------------------------------------------------------------------------------ // bind //------------------------------------------------------------------------------ -void OcciStmt::bind(const char *paramName, const uint64_t paramValue) { +void OcciStmt::bindUint64(const char *const paramName, const uint64_t paramValue) { try { const unsigned paramIdx = m_paramNameToIdx->getIdx(paramName); m_stmt->setUInt(paramIdx, paramValue); @@ -202,6 +202,13 @@ DbRset *OcciStmt::executeQuery() { } } +//------------------------------------------------------------------------------ +// executeNonQuery +//------------------------------------------------------------------------------ +void OcciStmt::executeNonQuery() { + throw std::runtime_error(std::string(__FUNCTION__) + " not implemented"); +} + //------------------------------------------------------------------------------ // get //------------------------------------------------------------------------------ diff --git a/catalogue/OcciStmt.hpp b/catalogue/OcciStmt.hpp index 1aadf6061c..382e499de3 100644 --- a/catalogue/OcciStmt.hpp +++ b/catalogue/OcciStmt.hpp @@ -60,13 +60,12 @@ public: * @param conn The database connection. * @param stmt The prepared statement. */ - OcciStmt(const char *const sql, OcciConn &conn, - oracle::occi::Statement *const stmt); + OcciStmt(const char *const sql, OcciConn &conn, oracle::occi::Statement *const stmt); /** * Destructor. */ - ~OcciStmt() throw(); + virtual ~OcciStmt() throw(); /** * Prevent copying the object. @@ -91,7 +90,7 @@ public: * @param paramName The name of the parameter. * @param paramValue The value to be bound. */ - void bind(const char *const paramName, const uint64_t paramValue); + virtual void bindUint64(const char *const paramName, const uint64_t paramValue); /** * Binds an SQL parameter. @@ -99,7 +98,7 @@ public: * @param paramName The name of the parameter. * @param paramValue The value to be bound. */ - void bind(const char*paramName, const char *paramValue); + virtual void bind(const char*paramName, const char *paramValue); /** * Executes the statement and returns the result set. @@ -107,7 +106,12 @@ public: * @return The result set. Please note that it is the responsibility of the * caller to free the memory associated with the result set. */ - DbRset *executeQuery(); + virtual DbRset *executeQuery(); + + /** + * Executes the statement. + */ + virtual void executeNonQuery(); /** * Returns the underlying OCCI result set. diff --git a/catalogue/SqliteCatalogue.cpp b/catalogue/SqliteCatalogue.cpp index cbd73daf39..32c57317fe 100644 --- a/catalogue/SqliteCatalogue.cpp +++ b/catalogue/SqliteCatalogue.cpp @@ -107,14 +107,14 @@ void SqliteCatalogue::createAdminUser( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":ADMIN_USER_NAME", user.name); + stmt->bind(":ADMIN_USER_NAME", user.name.c_str()); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -153,7 +153,7 @@ std::list<common::dataStructures::AdminUser> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM ADMIN_USER;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::AdminUser admin; @@ -239,14 +239,14 @@ void SqliteCatalogue::createAdminHost( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":ADMIN_HOST_NAME", hostName); + stmt->bind(":ADMIN_HOST_NAME", hostName.c_str()); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -284,7 +284,7 @@ std::list<common::dataStructures::AdminHost> SqliteCatalogue::getAdminHosts() co "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM ADMIN_HOST;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::AdminHost host; @@ -372,15 +372,15 @@ void SqliteCatalogue::createStorageClass( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":STORAGE_CLASS_NAME", name); - stmt->bind(":NB_COPIES", nbCopies); + stmt->bind(":STORAGE_CLASS_NAME", name.c_str()); + stmt->bindUint64(":NB_COPIES", nbCopies); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -420,7 +420,7 @@ std::list<common::dataStructures::StorageClass> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM STORAGE_CLASS;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::StorageClass storageClass; @@ -519,16 +519,16 @@ void SqliteCatalogue::createTapePool( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":TAPE_POOL_NAME", name); - stmt->bind(":NB_PARTIAL_TAPES", nbPartialTapes); - stmt->bind(":IS_ENCRYPTED", encryptionValue); + stmt->bind(":TAPE_POOL_NAME", name.c_str()); + stmt->bindUint64(":NB_PARTIAL_TAPES", nbPartialTapes); + stmt->bindUint64(":IS_ENCRYPTED", encryptionValue); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -569,7 +569,7 @@ std::list<common::dataStructures::TapePool> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM TAPE_POOL;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::TapePool pool; @@ -677,16 +677,16 @@ void SqliteCatalogue::createArchiveRoute( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":STORAGE_CLASS_NAME", storageClassName); - stmt->bind(":COPY_NB", copyNb); - stmt->bind(":TAPE_POOL_NAME", tapePoolName); + stmt->bind(":STORAGE_CLASS_NAME", storageClassName.c_str()); + stmt->bindUint64(":COPY_NB", copyNb); + stmt->bind(":TAPE_POOL_NAME", tapePoolName.c_str()); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -727,7 +727,7 @@ std::list<common::dataStructures::ArchiveRoute> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM ARCHIVE_ROUTE;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::ArchiveRoute route; @@ -822,14 +822,14 @@ void SqliteCatalogue::createLogicalLibrary( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":LOGICAL_LIBRARY_NAME", name); + stmt->bind(":LOGICAL_LIBRARY_NAME", name.c_str()); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ex) { @@ -868,7 +868,7 @@ std::list<common::dataStructures::LogicalLibrary> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM LOGICAL_LIBRARY;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::LogicalLibrary lib; @@ -996,32 +996,32 @@ void SqliteCatalogue::createTape( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":VID", vid); - stmt->bind(":LOGICAL_LIBRARY_NAME", logicalLibraryName); - stmt->bind(":TAPE_POOL_NAME", tapePoolName); - stmt->bind(":ENCRYPTION_KEY", encryptionKey); - stmt->bind(":CAPACITY_IN_BYTES", capacityInBytes); - stmt->bind(":DATA_IN_BYTES", 0); - stmt->bind(":LAST_FSEQ", 0); - stmt->bind(":IS_DISABLED", disabledValue); - stmt->bind(":IS_FULL", fullValue); - stmt->bind(":LBP_IS_ON", 0); + stmt->bind(":VID", vid.c_str()); + stmt->bind(":LOGICAL_LIBRARY_NAME", logicalLibraryName.c_str()); + stmt->bind(":TAPE_POOL_NAME", tapePoolName.c_str()); + stmt->bind(":ENCRYPTION_KEY", encryptionKey.c_str()); + stmt->bindUint64(":CAPACITY_IN_BYTES", capacityInBytes); + stmt->bindUint64(":DATA_IN_BYTES", 0); + stmt->bindUint64(":LAST_FSEQ", 0); + stmt->bindUint64(":IS_DISABLED", disabledValue); + stmt->bindUint64(":IS_FULL", fullValue); + stmt->bindUint64(":LBP_IS_ON", 0); stmt->bind(":LABEL_DRIVE", ""); - stmt->bind(":LABEL_TIME", 0); + stmt->bindUint64(":LABEL_TIME", 0); stmt->bind(":LAST_READ_DRIVE", ""); - stmt->bind(":LAST_READ_TIME", 0); + stmt->bindUint64(":LAST_READ_TIME", 0); stmt->bind(":LAST_WRITE_DRIVE", ""); - stmt->bind(":LAST_WRITE_TIME", 0); + stmt->bindUint64(":LAST_WRITE_TIME", 0); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -1086,7 +1086,7 @@ std::list<common::dataStructures::Tape> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM TAPE;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::Tape tape; @@ -1278,15 +1278,15 @@ void SqliteCatalogue::createRequester( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":REQUESTER_NAME", user.name); - stmt->bind(":MOUNT_POLICY_NAME", mountPolicy); + stmt->bind(":REQUESTER_NAME", user.name.c_str()); + stmt->bind(":MOUNT_POLICY_NAME", mountPolicy.c_str()); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -1326,7 +1326,7 @@ std::list<common::dataStructures::Requester> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM REQUESTER;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while(rset->next()) { common::dataStructures::Requester user; @@ -1442,22 +1442,22 @@ void SqliteCatalogue::createMountPolicy( ":CREATION_LOG_TIME);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":MOUNT_POLICY_NAME", name); + stmt->bind(":MOUNT_POLICY_NAME", name.c_str()); - stmt->bind(":ARCHIVE_PRIORITY", archivePriority); - stmt->bind(":ARCHIVE_MIN_REQUEST_AGE", minArchiveRequestAge); + stmt->bindUint64(":ARCHIVE_PRIORITY", archivePriority); + stmt->bindUint64(":ARCHIVE_MIN_REQUEST_AGE", minArchiveRequestAge); - stmt->bind(":RETRIEVE_PRIORITY", retrievePriority); - stmt->bind(":RETRIEVE_MIN_REQUEST_AGE", minRetrieveRequestAge); + stmt->bindUint64(":RETRIEVE_PRIORITY", retrievePriority); + stmt->bindUint64(":RETRIEVE_MIN_REQUEST_AGE", minRetrieveRequestAge); - stmt->bind(":MAX_DRIVES_ALLOWED", maxDrivesAllowed); + stmt->bindUint64(":MAX_DRIVES_ALLOWED", maxDrivesAllowed); - stmt->bind(":USER_COMMENT", comment); + stmt->bind(":USER_COMMENT", comment.c_str()); - stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name); - stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group); - stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host); - stmt->bind(":CREATION_LOG_TIME", now); + stmt->bind(":CREATION_LOG_USER_NAME", cliIdentity.user.name.c_str()); + stmt->bind(":CREATION_LOG_GROUP_NAME", cliIdentity.user.group.c_str()); + stmt->bind(":CREATION_LOG_HOST_NAME", cliIdentity.host.c_str()); + stmt->bindUint64(":CREATION_LOG_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -1504,7 +1504,7 @@ std::list<common::dataStructures::MountPolicy> "LAST_UPDATE_TIME AS LAST_UPDATE_TIME " "FROM MOUNT_POLICY;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { common::dataStructures::MountPolicy policy; @@ -1726,19 +1726,19 @@ void SqliteCatalogue::insertArchiveFile(const ArchiveFileRow &row) { ":RECONCILIATION_TIME)"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":ARCHIVE_FILE_ID", row.archiveFileId); - stmt->bind(":DISK_INSTANCE", row.diskInstance); - stmt->bind(":DISK_FILE_ID", row.diskFileId); - stmt->bind(":DISK_FILE_PATH", row.diskFilePath); - stmt->bind(":DISK_FILE_USER", row.diskFileUser); - stmt->bind(":DISK_FILE_GROUP", row.diskFileGroup); - stmt->bind(":DISK_FILE_RECOVERY_BLOB", row.diskFileRecoveryBlob); - stmt->bind(":FILE_SIZE", row.size); + stmt->bindUint64(":ARCHIVE_FILE_ID", row.archiveFileId); + stmt->bind(":DISK_INSTANCE", row.diskInstance.c_str()); + stmt->bind(":DISK_FILE_ID", row.diskFileId.c_str()); + stmt->bind(":DISK_FILE_PATH", row.diskFilePath.c_str()); + stmt->bind(":DISK_FILE_USER", row.diskFileUser.c_str()); + stmt->bind(":DISK_FILE_GROUP", row.diskFileGroup.c_str()); + stmt->bind(":DISK_FILE_RECOVERY_BLOB", row.diskFileRecoveryBlob.c_str()); + stmt->bindUint64(":FILE_SIZE", row.size); stmt->bind(":CHECKSUM_TYPE", "HELP"); stmt->bind(":CHECKSUM_VALUE", "HELP"); - stmt->bind(":STORAGE_CLASS_NAME", row.storageClassName); - stmt->bind(":CREATION_TIME", now); - stmt->bind(":RECONCILIATION_TIME", now); + stmt->bind(":STORAGE_CLASS_NAME", row.storageClassName.c_str()); + stmt->bindUint64(":CREATION_TIME", now); + stmt->bindUint64(":RECONCILIATION_TIME", now); stmt->executeNonQuery(); } catch(std::exception &ne) { @@ -1758,9 +1758,9 @@ uint64_t SqliteCatalogue::getArchiveFileId(const std::string &diskInstance, cons "DISK_INSTANCE = :DISK_INSTANCE AND " "DISK_FILE_ID = :DISK_FILE_ID;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":DISK_INSTANCE", diskInstance); - stmt->bind(":DISK_FILE_ID", diskFileId); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":DISK_INSTANCE", diskInstance.c_str()); + stmt->bind(":DISK_FILE_ID", diskFileId.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); if (rset->next()) { return rset->columnUint64("ARCHIVE_FILE_ID"); @@ -1817,7 +1817,7 @@ std::list<common::dataStructures::ArchiveFile> SqliteCatalogue::getArchiveFiles( "TAPE_FILE.COPY_NB;" ; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); std::list<common::dataStructures::ArchiveFile> archiveFiles; while (rset->next()) { const uint64_t archiveFileId = rset->columnUint64("ARCHIVE_FILE_ID"); @@ -2044,8 +2044,8 @@ common::dataStructures::TapeCopyToPoolMap SqliteCatalogue:: "FROM ARCHIVE_ROUTE WHERE " "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":STORAGE_CLASS_NAME", storageClass); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":STORAGE_CLASS_NAME", storageClass.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { const uint64_t copyNb = rset->columnUint64("COPY_NB"); const std::string tapePoolName = rset->columnText("TAPE_POOL_NAME"); @@ -2070,8 +2070,8 @@ uint64_t SqliteCatalogue::getExpectedNbArchiveRoutes(const std::string &storageC "FROM ARCHIVE_ROUTE WHERE " "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":STORAGE_CLASS_NAME", storageClass); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":STORAGE_CLASS_NAME", storageClass.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { nbRoutes = rset->columnUint64("NB_ROUTES"); } @@ -2113,8 +2113,8 @@ common::dataStructures::MountPolicy SqliteCatalogue:: "WHERE " "REQUESTER.REQUESTER_NAME = :REQUESTER_NAME;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":REQUESTER_NAME", user.name); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":REQUESTER_NAME", user.name.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); if(rset->next()) { common::dataStructures::MountPolicy policy; @@ -2178,8 +2178,8 @@ bool SqliteCatalogue::userIsAdmin(const std::string &userName) const { "FROM ADMIN_USER WHERE " "ADMIN_USER_NAME = :ADMIN_USER_NAME;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":ADMIN_USER_NAME", userName); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":ADMIN_USER_NAME", userName.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); return rset->next(); } @@ -2193,8 +2193,8 @@ bool SqliteCatalogue::hostIsAdmin(const std::string &hostName) const { "FROM ADMIN_HOST WHERE " "ADMIN_HOST_NAME = :ADMIN_HOST_NAME;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":ADMIN_HOST_NAME", hostName); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":ADMIN_HOST_NAME", hostName.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); return rset->next(); } @@ -2216,8 +2216,8 @@ std::list<TapeForWriting> SqliteCatalogue::getTapesForWriting(const std::string "IS_FULL = 0 AND " "LOGICAL_LIBRARY_NAME = :LOGICAL_LIBRARY_NAME;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":LOGICAL_LIBRARY_NAME", logicalLibraryName); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":LOGICAL_LIBRARY_NAME", logicalLibraryName.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while (rset->next()) { TapeForWriting tape; @@ -2260,13 +2260,13 @@ void SqliteCatalogue::createTapeFile(const common::dataStructures::TapeFile &tap ":ARCHIVE_FILE_ID);"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":VID", tapeFile.vid); - stmt->bind(":FSEQ", tapeFile.fSeq); - stmt->bind(":BLOCK_ID", tapeFile.blockId); - stmt->bind(":COMPRESSED_SIZE", tapeFile.compressedSize); - stmt->bind(":COPY_NB", tapeFile.copyNb); - stmt->bind(":CREATION_TIME", now); - stmt->bind(":ARCHIVE_FILE_ID", archiveFileId); + stmt->bind(":VID", tapeFile.vid.c_str()); + stmt->bindUint64(":FSEQ", tapeFile.fSeq); + stmt->bindUint64(":BLOCK_ID", tapeFile.blockId); + stmt->bindUint64(":COMPRESSED_SIZE", tapeFile.compressedSize); + stmt->bindUint64(":COPY_NB", tapeFile.copyNb); + stmt->bindUint64(":CREATION_TIME", now); + stmt->bindUint64(":ARCHIVE_FILE_ID", archiveFileId); stmt->executeNonQuery(); } @@ -2286,7 +2286,7 @@ std::list<common::dataStructures::TapeFile> SqliteCatalogue::getTapeFiles() cons "CREATION_TIME AS CREATION_TIME " "FROM TAPE_FILE;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); while(rset->next()) { common::dataStructures::TapeFile file; @@ -2322,8 +2322,8 @@ void SqliteCatalogue::setTapeLastFSeq(const std::string &vid, const uint64_t las "WHERE " "VID=:VID;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":VID", vid); - stmt->bind(":LAST_FSEQ", lastFSeq); + stmt->bind(":VID", vid.c_str()); + stmt->bindUint64(":LAST_FSEQ", lastFSeq); stmt->executeNonQuery(); } @@ -2337,8 +2337,8 @@ uint64_t SqliteCatalogue::getTapeLastFSeq(const std::string &vid) const { "FROM TAPE WHERE " "VID = :VID;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":VID", vid); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bind(":VID", vid.c_str()); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); if(rset->next()) { return rset->columnUint64("LAST_FSEQ"); } else { @@ -2382,8 +2382,8 @@ std::unique_ptr<common::dataStructures::ArchiveFile> SqliteCatalogue::getArchive "WHERE " "ARCHIVE_FILE.ARCHIVE_FILE_ID = :ARCHIVE_FILE_ID;"; std::unique_ptr<SqliteStmt> stmt(m_conn.createStmt(sql)); - stmt->bind(":ARCHIVE_FILE_ID", archiveFileId); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + stmt->bindUint64(":ARCHIVE_FILE_ID", archiveFileId); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); std::unique_ptr<common::dataStructures::ArchiveFile> archiveFile; while (rset->next()) { if(NULL == archiveFile.get()) { diff --git a/catalogue/SqliteStmt.cpp b/catalogue/SqliteStmt.cpp index 94ee444558..a1ae809ac7 100644 --- a/catalogue/SqliteStmt.cpp +++ b/catalogue/SqliteStmt.cpp @@ -92,7 +92,7 @@ const char *SqliteStmt::getSql() const { //------------------------------------------------------------------------------ // bind //------------------------------------------------------------------------------ -void SqliteStmt::bind(const char *const paramName, const uint64_t paramValue) { +void SqliteStmt::bindUint64(const char *const paramName, const uint64_t paramValue) { const int paramIdx = getParamIndex(paramName); const int bindRc = sqlite3_bind_int64(m_stmt, paramIdx, (sqlite3_int64)paramValue); if(SQLITE_OK != bindRc) { @@ -103,10 +103,9 @@ void SqliteStmt::bind(const char *const paramName, const uint64_t paramValue) { //------------------------------------------------------------------------------ // bind //------------------------------------------------------------------------------ -void SqliteStmt::bind(const char *const paramName, const std::string ¶mValue) { +void SqliteStmt::bind(const char *const paramName, const char *const paramValue) { const int paramIdx = getParamIndex(paramName); - const int bindRc = sqlite3_bind_text(m_stmt, paramIdx, paramValue.c_str(), -1, - SQLITE_TRANSIENT); + const int bindRc = sqlite3_bind_text(m_stmt, paramIdx, paramValue, -1, SQLITE_TRANSIENT); if(SQLITE_OK != bindRc) { throw std::runtime_error(std::string(__FUNCTION__) + "failed for SQL statement " + getSql()); } @@ -115,7 +114,7 @@ void SqliteStmt::bind(const char *const paramName, const std::string ¶mValue //------------------------------------------------------------------------------ // executeQuery //------------------------------------------------------------------------------ -SqliteRset *SqliteStmt::executeQuery() { +DbRset *SqliteStmt::executeQuery() { return new SqliteRset(*this); } diff --git a/catalogue/SqliteStmt.hpp b/catalogue/SqliteStmt.hpp index d172938b0c..32fb9f5519 100644 --- a/catalogue/SqliteStmt.hpp +++ b/catalogue/SqliteStmt.hpp @@ -18,6 +18,8 @@ #pragma once +#include "catalogue/DbStmt.hpp" + #include <map> #include <memory> #include <mutex> @@ -33,7 +35,7 @@ class SqliteRset; /** * A convenience wrapper around an SQLite prepared statement. */ -class SqliteStmt { +class SqliteStmt: public DbStmt { public: /** @@ -47,12 +49,12 @@ public: /** * Destructor. */ - ~SqliteStmt() throw(); + virtual ~SqliteStmt() throw(); /** * Idempotent close() method. The destructor calls this method. */ - void close(); + virtual void close(); /** * Returns a pointer to the underlying prepared statement. @@ -69,7 +71,7 @@ public: * * @return The SQL statement. */ - const char *getSql() const; + virtual const char *getSql() const; /** * Binds an SQL parameter. @@ -77,7 +79,7 @@ public: * @param paramName The name of the parameter. * @param paramValue The value to be bound. */ - void bind(const char *const paramName, const uint64_t paramValue); + virtual void bindUint64(const char *const paramName, const uint64_t paramValue); /** * Binds an SQL parameter. @@ -85,7 +87,7 @@ public: * @param paramName The name of the parameter. * @param paramValue The value to be bound. */ - void bind(const char *const paramName, const std::string ¶mValue); + virtual void bind(const char *const paramName, const char *const paramValue); /** * Executes the statement and returns the result set. @@ -93,7 +95,7 @@ public: * @return The result set. Please note that it is the responsibility of the * caller to free the memory associated with the result set. */ - SqliteRset *executeQuery(); + virtual DbRset *executeQuery(); /** * Executes the statement. diff --git a/catalogue/SqliteStmtTest.cpp b/catalogue/SqliteStmtTest.cpp index aee22facad..3ad2722a2e 100644 --- a/catalogue/SqliteStmtTest.cpp +++ b/catalogue/SqliteStmtTest.cpp @@ -47,7 +47,7 @@ TEST_F(cta_catalogue_SqliteStmtTest, create_table) { "COL1 TEXT," "COL2 TEXT," "COL3 INTEGER);"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); stmt->executeNonQuery(); } } @@ -64,7 +64,7 @@ TEST_F(cta_catalogue_SqliteStmtTest, select_from_empty_table) { "COL1 TEXT," "COL2 TEXT," "COL3 INTEGER);"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); stmt->executeNonQuery(); } @@ -76,8 +76,8 @@ TEST_F(cta_catalogue_SqliteStmtTest, select_from_empty_table) { "COL2," "COL3 " "FROM TEST;"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); ASSERT_FALSE(rset->next()); } } @@ -94,7 +94,7 @@ TEST_F(cta_catalogue_SqliteStmtTest, insert_without_bind) { "COL1 TEXT," "COL2 TEXT," "COL3 INTEGER);"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); stmt->executeNonQuery(); } @@ -109,7 +109,7 @@ TEST_F(cta_catalogue_SqliteStmtTest, insert_without_bind) { "'one'," "'two'," "3);"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); stmt->executeNonQuery(); } @@ -121,8 +121,8 @@ TEST_F(cta_catalogue_SqliteStmtTest, insert_without_bind) { "COL2 AS COL2," "COL3 AS COL3 " "FROM TEST;"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); ASSERT_TRUE(rset->next()); std::string col1; @@ -153,7 +153,7 @@ TEST_F(cta_catalogue_SqliteStmtTest, insert_with_bind) { "COL1 TEXT," "COL2 TEXT," "COL3 INTEGER);"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); stmt->executeNonQuery(); } @@ -168,10 +168,10 @@ TEST_F(cta_catalogue_SqliteStmtTest, insert_with_bind) { ":COL1," ":COL2," ":COL3);"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); ASSERT_NO_THROW(stmt->bind(":COL1", "one")); ASSERT_NO_THROW(stmt->bind(":COL2", "two")); - ASSERT_NO_THROW(stmt->bind(":COL3", 3)); + ASSERT_NO_THROW(stmt->bindUint64(":COL3", 3)); stmt->executeNonQuery(); } @@ -183,8 +183,8 @@ TEST_F(cta_catalogue_SqliteStmtTest, insert_with_bind) { "COL2 AS COL2," "COL3 AS COL3 " "FROM TEST;"; - std::unique_ptr<SqliteStmt> stmt(conn.createStmt(sql)); - std::unique_ptr<SqliteRset> rset(stmt->executeQuery()); + std::unique_ptr<DbStmt> stmt(conn.createStmt(sql)); + std::unique_ptr<DbRset> rset(stmt->executeQuery()); ASSERT_TRUE(rset->next()); std::string col1; -- GitLab