From 376b80cfd009b128a1ab704e7d5c932e642b1edb Mon Sep 17 00:00:00 2001
From: Steven Murray <steven.murray@cern.ch>
Date: Mon, 9 Jan 2017 17:19:17 +0100
Subject: [PATCH] SQL statements in exceptions clipped to 80 chars

---
 rdbms/OcciStmt.cpp   | 34 ++++++++++++++++++++--------------
 rdbms/SqliteStmt.cpp | 27 ++++++++++++++-------------
 rdbms/Stmt.hpp       |  5 +++++
 3 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/rdbms/OcciStmt.cpp b/rdbms/OcciStmt.cpp
index 46e502d00d..caab6da43d 100644
--- a/rdbms/OcciStmt.cpp
+++ b/rdbms/OcciStmt.cpp
@@ -59,11 +59,12 @@ OcciStmt::OcciStmt(
     }
   } catch(exception::Exception &ex) {
     close();
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + sql + ": " +
-      ex.getMessage().str());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      sql.substr(0, c_maxSqlLenInExceptions) + ": " + ex.getMessage().str());
   } catch(std::exception &se) {
     close();
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + sql + ": " + se.what());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      sql.substr(0, c_maxSqlLenInExceptions) + ": " + se.what());
   }
 }
 
@@ -90,10 +91,11 @@ void OcciStmt::close() {
       m_stmt = nullptr;
     }
   } catch(exception::Exception &ex) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " +
-                               ex.getMessage().str());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + ex.getMessage().str());
   } catch(std::exception &se) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " + se.what());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + se.what());
   }
 }
 
@@ -128,10 +130,11 @@ void OcciStmt::bindOptionalUint64(const std::string &paramName, const optional<u
       m_stmt->setNull(paramIdx, oracle::occi::OCCINUMBER);
     }
   } catch(exception::Exception &ex) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " +
-      ex.getMessage().str());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + ex.getMessage().str());
   } catch(std::exception &se) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " + se.what());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + se.what());
   }
 }
 
@@ -163,10 +166,11 @@ void OcciStmt::bindOptionalString(const std::string &paramName, const optional<s
       m_stmt->setNull(paramIdx, oracle::occi::OCCISTRING);
     }
   } catch(exception::Exception &ex) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " +
-      ex.getMessage().str());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + ex.getMessage().str());
   } catch(std::exception &se) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " + se.what());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + se.what());
   }
 }
 
@@ -191,7 +195,8 @@ std::unique_ptr<Rset> OcciStmt::executeQuery() {
       } catch(...) {
       }
     }
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " + ex.what());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + ex.what());
   }
 }
 
@@ -216,7 +221,8 @@ void OcciStmt::executeNonQuery() {
       } catch(...) {
       }
     }
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " + ex.what());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + ex.what());
   }
 }
 
diff --git a/rdbms/SqliteStmt.cpp b/rdbms/SqliteStmt.cpp
index 44b81a74be..063643b10f 100644
--- a/rdbms/SqliteStmt.cpp
+++ b/rdbms/SqliteStmt.cpp
@@ -94,11 +94,12 @@ SqliteStmt::SqliteStmt(
     }
   } catch(exception::Exception &ex) {
     close();
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + sql + ": " +
-      ex.getMessage().str());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      sql.substr(0, c_maxSqlLenInExceptions) + ": " + ex.getMessage().str());
   } catch(std::exception &se) {
     close();
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + sql + ": " + se.what());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      sql.substr(0, c_maxSqlLenInExceptions) + ": " + se.what());
   }
 }
 
@@ -130,8 +131,8 @@ void SqliteStmt::close() {
 //------------------------------------------------------------------------------
 sqlite3_stmt *SqliteStmt::get() const {
   if(nullptr == m_stmt) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() +
-      ": nullptr pointer");
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": nullptr pointer");
   }
   return m_stmt;
 }
@@ -170,8 +171,8 @@ void SqliteStmt::bindOptionalUint64(const std::string &paramName, const optional
       throw exception::Exception("sqlite3_bind_int64() failed");
     }
   } catch(exception::Exception &ex) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " +
-      ex.getMessage().str());
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + ex.getMessage().str());
   }
 }
 
@@ -206,8 +207,8 @@ void SqliteStmt::bindOptionalString(const std::string &paramName, const optional
       throw exception::Exception("sqlite3_bind_text() failed");
     }
   } catch(exception::Exception &ex) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " +
-      ex.getMessage().str()); 
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + ex.getMessage().str()); 
   }
 }
 
@@ -228,16 +229,16 @@ void SqliteStmt::executeNonQuery() {
 
   // Throw an exception if the call to sqlite3_step() failed
   if(SQLITE_DONE != stepRc && SQLITE_ROW != stepRc) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() + ": " +
-      Sqlite::rcToStr(stepRc));
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": " + Sqlite::rcToStr(stepRc));
   }
 
   m_nbAffectedRows = sqlite3_changes(m_conn.m_sqliteConn);
 
   // Throw an exception if the SQL statement returned a result set
   if(SQLITE_ROW == stepRc) {
-    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " + getSql() +
-      ": The SQL statment returned a result set");
+    throw exception::Exception(std::string(__FUNCTION__) + " failed for SQL statement " +
+      getSql().substr(0, c_maxSqlLenInExceptions) + ": The SQL statment returned a result set");
   }
 }
 
diff --git a/rdbms/Stmt.hpp b/rdbms/Stmt.hpp
index 8796d7221c..15dce6e950 100644
--- a/rdbms/Stmt.hpp
+++ b/rdbms/Stmt.hpp
@@ -177,6 +177,11 @@ protected:
    */
   AutocommitMode m_autoCommitMode;
 
+  /**
+   * The maximum length an SQL statement can have in exception error message.
+   */
+  const uint32_t c_maxSqlLenInExceptions = 80;
+
 }; // class Stmt
 
 } // namespace rdbms
-- 
GitLab