From d8d472aa6e5ad8838b1cd42bd692edd99099e1f7 Mon Sep 17 00:00:00 2001
From: Michael Davis <michael.davis@cern.ch>
Date: Thu, 4 Jul 2019 16:36:29 +0200
Subject: [PATCH] [catalogue] Fixes DB tests

---
 catalogue/MysqlCatalogue.cpp    |  2 --
 catalogue/PostgresCatalogue.cpp | 11 +++++++++++
 catalogue/RdbmsCatalogue.cpp    | 11 ++++++-----
 rdbms/wrapper/MysqlStmt.cpp     |  4 ++--
 4 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/catalogue/MysqlCatalogue.cpp b/catalogue/MysqlCatalogue.cpp
index 0ad64512d7..8702631ec2 100644
--- a/catalogue/MysqlCatalogue.cpp
+++ b/catalogue/MysqlCatalogue.cpp
@@ -322,8 +322,6 @@ void MysqlCatalogue::fileWrittenToTape(rdbms::Conn &conn, const TapeFileWritten
       insertArchiveFile(conn, row);
     } catch(exception::DatabasePrimaryKeyError &) {
       // Ignore this error
-    } catch(...) {
-      throw;
     }
 
     const time_t now = time(nullptr);
diff --git a/catalogue/PostgresCatalogue.cpp b/catalogue/PostgresCatalogue.cpp
index 4d941d36f6..485fa07f78 100644
--- a/catalogue/PostgresCatalogue.cpp
+++ b/catalogue/PostgresCatalogue.cpp
@@ -495,6 +495,16 @@ void PostgresCatalogue::idempotentBatchInsertArchiveFiles(rdbms::Conn &conn,
       archiveFileBatch.diskFileGroup.setFieldValue(i, event.diskFileGid);
       archiveFileBatch.size.setFieldValue(i, event.size);
       archiveFileBatch.checksumBlob.setFieldByteA(conn, i, event.checksumBlob.serialize());
+      // Keep transition ADLER32 checksum up-to-date if it exists
+      std::string adler32str;
+      try {
+        std::string adler32hex = checksum::ChecksumBlob::ByteArrayToHex(event.checksumBlob.at(checksum::ADLER32));
+        uint32_t adler32 = strtoul(adler32hex.c_str(), 0, 16);
+        adler32str = std::to_string(adler32);
+      } catch(exception::ChecksumTypeMismatch &ex) {
+        adler32str = "0";
+      }
+      archiveFileBatch.checksumAdler32.setFieldValue(i, adler32str);
       archiveFileBatch.storageClassName.setFieldValue(i, event.storageClassName);
       archiveFileBatch.creationTime.setFieldValue(i, now);
       archiveFileBatch.reconciliationTime.setFieldValue(i, now);
@@ -540,6 +550,7 @@ void PostgresCatalogue::idempotentBatchInsertArchiveFiles(rdbms::Conn &conn,
     postgresStmt.setColumn(archiveFileBatch.diskFileGroup);
     postgresStmt.setColumn(archiveFileBatch.size);
     postgresStmt.setColumn(archiveFileBatch.checksumBlob);
+    postgresStmt.setColumn(archiveFileBatch.checksumAdler32);
     postgresStmt.setColumn(archiveFileBatch.storageClassName);
     postgresStmt.setColumn(archiveFileBatch.creationTime);
     postgresStmt.setColumn(archiveFileBatch.reconciliationTime);
diff --git a/catalogue/RdbmsCatalogue.cpp b/catalogue/RdbmsCatalogue.cpp
index 8334552f18..00faa087a6 100644
--- a/catalogue/RdbmsCatalogue.cpp
+++ b/catalogue/RdbmsCatalogue.cpp
@@ -4086,7 +4086,7 @@ void RdbmsCatalogue::createActivitiesFairShareWeight(const common::dataStructure
     
     const time_t now = time(nullptr);
     const char *const sql =
-      "INSERT INTO ACTIVITIES_WEIGHTS ("
+      "INSERT INTO ACTIVITIES_WEIGHTS("
         "DISK_INSTANCE_NAME,"
         "ACTIVITY,"
         "WEIGHT,"
@@ -4330,13 +4330,14 @@ void RdbmsCatalogue::insertArchiveFile(rdbms::Conn &conn, const ArchiveFileRow &
     stmt.bindUint64(":SIZE_IN_BYTES", row.size);
     stmt.bindBlob  (":CHECKSUM_BLOB", row.checksumBlob.serialize());
     // Keep transition ADLER32 checksum up-to-date if it exists
-    std::string adler32str;
+    uint32_t adler32;
     try {
-      adler32str = checksum::ChecksumBlob::ByteArrayToHex(row.checksumBlob.at(checksum::ADLER32));
+      std::string adler32hex = checksum::ChecksumBlob::ByteArrayToHex(row.checksumBlob.at(checksum::ADLER32));
+      adler32 = strtoul(adler32hex.c_str(), 0, 16);
     } catch(exception::ChecksumTypeMismatch &ex) {
-      adler32str = "0";
+      adler32 = 0;
     }
-    stmt.bindBlob  (":CHECKSUM_ADLER32", "0x" + adler32str);
+    stmt.bindUint64(":CHECKSUM_ADLER32", adler32);
     stmt.bindString(":STORAGE_CLASS_NAME", row.storageClassName);
     stmt.bindUint64(":CREATION_TIME", now);
     stmt.bindUint64(":RECONCILIATION_TIME", now);
diff --git a/rdbms/wrapper/MysqlStmt.cpp b/rdbms/wrapper/MysqlStmt.cpp
index d4d2246369..d007cf8e10 100644
--- a/rdbms/wrapper/MysqlStmt.cpp
+++ b/rdbms/wrapper/MysqlStmt.cpp
@@ -315,7 +315,8 @@ void MysqlStmt::bindOptionalString(const std::string &paramName, const optional<
 
       // reset memory
       holder->reset();
-      snprintf(holder->val, holder->get_buffer_length(), paramValue.value().c_str());
+      // need to use memcpy for VARBINARY strings, which are not null-terminated
+      memcpy(holder->val, paramValue.value().c_str(), holder->get_buffer_length());
     } else {
       holder->length = 0;
     }
@@ -325,7 +326,6 @@ void MysqlStmt::bindOptionalString(const std::string &paramName, const optional<
     // delete m_placeholder[idx]; // remove the previous placeholder
 
     m_placeholder[idx] = holder;
-
   } catch(exception::Exception &ex) {
     throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
       getSqlForException() + ": " + ex.getMessage().str()); 
-- 
GitLab