From a32be680aa8e9f79c9f65c545f298963f0916aff Mon Sep 17 00:00:00 2001
From: Prasad <prasad@vtiger.com>
Date: Wed, 8 May 2024 13:34:48 +0530
Subject: [PATCH] Fixes #1877: Improved calls to mem-cost function based on
 need in pquery

---
 include/database/PearDatabase.php | 16 ++++++++++++++--
 modules/Vtiger/helpers/Logger.php | 14 ++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/include/database/PearDatabase.php b/include/database/PearDatabase.php
index 254fe9abc..f81d7911e 100644
--- a/include/database/PearDatabase.php
+++ b/include/database/PearDatabase.php
@@ -357,8 +357,20 @@ class PearDatabase{
 		$this->executeSetNamesUTF8SQL();
 
 		$sql_start_time = microtime(true);
-		$params = $this->flatten_array($params);
-		if (is_array($params) && count($params) > 0) {
+
+		// flatten only when params has array inside.
+		$flatten_params = false;
+		foreach ($params as $val) {
+			if (is_array($val)) {
+				$flatten_params = true;
+				break;
+			}
+		}
+		if ($flatten_params) {
+			$params = $this->flatten_array($params);
+		}
+
+		if ($log->isDebugEnabled() && is_array($params) && count($params) > 0) {
 			$log->debug('Prepared sql query parameters : [' . implode(",", $params) . ']');
 		}
 
diff --git a/modules/Vtiger/helpers/Logger.php b/modules/Vtiger/helpers/Logger.php
index f9a61ef77..0198dc142 100644
--- a/modules/Vtiger/helpers/Logger.php
+++ b/modules/Vtiger/helpers/Logger.php
@@ -76,6 +76,10 @@ class Logger {
     public function warn($message) {}
     public function error($message) {}
 
+    public function isDebugEnabled() {
+        return self::$logLevel == 100;
+    }
+
 }
 
 // Define extended version of Monolog Logger to support functions
@@ -83,6 +87,16 @@ class MonologLoggerEx extends MonologLogger {
     function fatal($message, $context = array()) {
         $this->error($message, $context);
     }
+    function isDebugEnabled() {
+        $debugLevel = false;
+        foreach ($this->getHandlers() as $handler) {
+            if ($handler->getLogLevel() == static::DEBUG) {
+                $debugLevel = true;
+                break;
+            }
+        }
+        return $debugLevel;
+    }
 }
 
 // Define a custom log formatter
-- 
GitLab