From dfc7c7a3078b33700c18b43ba52bf43dcba3c02a Mon Sep 17 00:00:00 2001 From: ms20 Date: Wed, 21 Jun 2017 15:35:19 +0000 Subject: [PATCH 1/3] Update DBService.php of all modules except the Report one to reuse existing MySQL database connection. --- licensing/admin/classes/common/DBService.php | 13 +++++++++--- management/admin/classes/common/DBService.php | 10 ++++++++-- .../admin/classes/common/DBService.php | 12 ++++++++--- resources/admin/classes/common/DBService.php | 20 ++++++++++++------- usage/admin/classes/common/DBService.php | 7 +++++++ 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/licensing/admin/classes/common/DBService.php b/licensing/admin/classes/common/DBService.php index 5b6c9f0fb..577546811 100644 --- a/licensing/admin/classes/common/DBService.php +++ b/licensing/admin/classes/common/DBService.php @@ -23,11 +23,12 @@ class DBService extends Object { protected $db; protected $config; protected $error; + private static $currDBH;//used to hold current DB connection for reuse. protected function init(NamedArguments $arguments) { parent::init($arguments); $this->config = new Configuration; - $this->connect(); + $this->connect(); } protected function dealloc() { @@ -36,19 +37,25 @@ protected function dealloc() { } protected function checkForError() { - if ($this->error = mysqli_error($this->db)) { + $this->error = mysqli_error($this->db); + if ($this->error) { throw new Exception(_("There was a problem with the database: ") . $this->error); } } protected function connect() { + if(empty(self::$currDBH)){ $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; $databaseName = $this->config->database->name; $this->db = mysqli_connect($host, $username, $password, $databaseName); - $this->checkForError(); + $this->checkForError(); mysqli_set_charset($this->db, 'utf8'); + self::$currDBH=$this->db; + } else { + $this->db=self::$currDBH; + } } protected function disconnect() { diff --git a/management/admin/classes/common/DBService.php b/management/admin/classes/common/DBService.php index 71effe4dc..a01fd42c8 100644 --- a/management/admin/classes/common/DBService.php +++ b/management/admin/classes/common/DBService.php @@ -23,6 +23,7 @@ class DBService extends Object { protected $db; protected $config; protected $error; + private static $currDBH; protected function init(NamedArguments $arguments) { parent::init($arguments); @@ -42,6 +43,7 @@ protected function checkForError() { } protected function connect() { + if(empty(self::$currDBH)){ $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; @@ -52,8 +54,12 @@ protected function connect() { $this->db->select_db($databaseName); $this->db->set_charset('utf8'); $this->checkForError(); - } - + mysqli_set_charset($this->db, 'utf8'); + self::$currDBH=$this->db; + } else { + $this->db=self::$currDBH; + } + } protected function disconnect() { //mysqli_close($this->db); } diff --git a/organizations/admin/classes/common/DBService.php b/organizations/admin/classes/common/DBService.php index e6753802a..5ffa5746c 100644 --- a/organizations/admin/classes/common/DBService.php +++ b/organizations/admin/classes/common/DBService.php @@ -22,6 +22,7 @@ class DBService extends Object { protected $db; protected $config; protected $error; + private static $currDBH;//used to hold current DB connection for reuse. protected function init(NamedArguments $arguments) { parent::init($arguments); @@ -41,16 +42,21 @@ protected function checkForError() { } protected function connect() { + if(empty(self::$currDBH)){ $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; $this->db = new mysqli($host, $username, $password); - $this->checkForError(); - $this->db->set_charset('utf8'); - + $this->checkForError(); + $this->db->set_charset('utf8'); $databaseName = $this->config->database->name; $this->db->select_db($databaseName); $this->checkForError(); + mysqli_set_charset($this->db, 'utf8'); + self::$currDBH=$this->db; + } else { + $this->db=self::$currDBH; + } } protected function disconnect() { diff --git a/resources/admin/classes/common/DBService.php b/resources/admin/classes/common/DBService.php index 4cbc77ffb..0ef1b01e8 100644 --- a/resources/admin/classes/common/DBService.php +++ b/resources/admin/classes/common/DBService.php @@ -22,6 +22,7 @@ class DBService extends Object { protected $db; protected $config; protected $error; + private static $currDBH;//used to hold current DB connection for reuse. protected function init(NamedArguments $arguments) { parent::init($arguments); @@ -41,17 +42,22 @@ protected function checkForError() { } protected function connect() { + if(empty(self::$currDBH)){ $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; $this->db = new mysqli($host, $username, $password); - $this->checkForError(); - $this->db->set_charset('utf8'); - - $databaseName = $this->config->database->name; - $this->db->select_db($databaseName); - $this->checkForError(); - } + $this->checkForError(); + $this->db->set_charset('utf8'); + $databaseName = $this->config->database->name; + $this->db->select_db($databaseName); + $this->checkForError(); + mysqli_set_charset($this->db, 'utf8'); + self::$currDBH=$this->db; + } else { + $this->db=self::$currDBH; + } + } protected function disconnect() { //mysqli_close($this->db); diff --git a/usage/admin/classes/common/DBService.php b/usage/admin/classes/common/DBService.php index c6dd66328..d92396ec2 100755 --- a/usage/admin/classes/common/DBService.php +++ b/usage/admin/classes/common/DBService.php @@ -22,6 +22,7 @@ class DBService extends Object { protected $db; protected $config; protected $error; + private static $currDBH;//used to hold current DB connection for reuse. protected function init(NamedArguments $arguments) { parent::init($arguments); @@ -41,12 +42,18 @@ protected function checkForError() { } protected function connect() { + if(empty(self::$currDBH)){ $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; $databaseName = $this->config->database->name; $this->db = mysqli_connect($host, $username, $password, $databaseName); $this->checkForError(); + mysqli_set_charset($this->db, 'utf8'); + self::$currDBH=$this->db; + } else { + $this->db=self::$currDBH; + } } protected function disconnect() { From 690c62abaf4dfca87c6a9e90b2ddde312b84e2bb Mon Sep 17 00:00:00 2001 From: ms20 Date: Fri, 23 Jun 2017 15:48:50 +0000 Subject: [PATCH 2/3] replace DBService.php of Usage with the one of Licensing. --- usage/admin/classes/common/DBService.php | 35 ++++++++++++++++++------ 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/usage/admin/classes/common/DBService.php b/usage/admin/classes/common/DBService.php index d92396ec2..577546811 100755 --- a/usage/admin/classes/common/DBService.php +++ b/usage/admin/classes/common/DBService.php @@ -1,7 +1,7 @@ config = new Configuration; - $this->connect(); + $this->connect(); } protected function dealloc() { @@ -36,7 +37,8 @@ protected function dealloc() { } protected function checkForError() { - if ($this->error = mysqli_error($this->db)) { + $this->error = mysqli_error($this->db); + if ($this->error) { throw new Exception(_("There was a problem with the database: ") . $this->error); } } @@ -48,7 +50,7 @@ protected function connect() { $password = $this->config->database->password; $databaseName = $this->config->database->name; $this->db = mysqli_connect($host, $username, $password, $databaseName); - $this->checkForError(); + $this->checkForError(); mysqli_set_charset($this->db, 'utf8'); self::$currDBH=$this->db; } else { @@ -57,20 +59,24 @@ protected function connect() { } protected function disconnect() { - $this->db->close(); + //mysqli_close($this->db); } - public function escapeString($value) { - return mysqli_real_escape_string($this->db, $value); - } + public function escapeString($value) { + return mysqli_real_escape_string($this->db, $value); + } public function getDatabase() { return $this->db; } public function processQuery($sql, $type = NULL) { - + //echo $sql. "
"; + $query_start = microtime(true); $result = mysqli_query($this->db, $sql); + $query_end = microtime(true); + $this->log($sql, $query_end - $query_start); + $this->checkForError(); $data = array(); @@ -156,6 +162,17 @@ private static function refValues($arr){ return $refs; } + public function log($sql, $query_time) { + $threshold = $this->config->database->logQueryThreshold; + if ($this->config->database->logQueries == "Y" && (!$threshold || $query_time >= $threshold)) { + $util = new Utility(); + $log_path = $util->getModulePath()."/log"; + $log_file = $log_path."/database.log"; + $log_string = date("c")."\n".$_SERVER['REQUEST_URI']."\n".$sql."\nQuery completed in ".sprintf("%.3f", round($query_time, 3))." seconds"; + error_log($log_string."\n\n", 3, $log_file); + } + } + } ?> From 9d161a145ff5e31d57111d93b6679c80407ad18c Mon Sep 17 00:00:00 2001 From: ms20 Date: Mon, 26 Jun 2017 20:42:09 +0000 Subject: [PATCH 3/3] Make the DBService.php files of all modules except the one of the Report identical --- auth/admin/classes/common/DBService.php | 45 ++++++------- licensing/admin/classes/common/DBService.php | 29 +++------ management/admin/classes/common/DBService.php | 64 ++++++++----------- resources/admin/classes/common/DBService.php | 38 +++++------ usage/admin/classes/common/DBService.php | 29 +++------ 5 files changed, 86 insertions(+), 119 deletions(-) diff --git a/auth/admin/classes/common/DBService.php b/auth/admin/classes/common/DBService.php index 1cf8f559c..01d1b089a 100644 --- a/auth/admin/classes/common/DBService.php +++ b/auth/admin/classes/common/DBService.php @@ -1,8 +1,7 @@ config = new Configuration; - $this->connect(); + $this->connect(); } protected function dealloc() { @@ -35,48 +36,48 @@ protected function dealloc() { } protected function checkForError() { - if ($this->error = mysqli_error($this->db)) { + $this->error = mysqli_error($this->db); + if ($this->error) { throw new Exception(_("There was a problem with the database: ") . $this->error); } } protected function connect() { + if(empty(self::$currDBH)){ $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; $databaseName = $this->config->database->name; $this->db = mysqli_connect($host, $username, $password, $databaseName); - $this->checkForError(); - $this->db->set_charset('utf8'); + $this->checkForError(); + mysqli_set_charset($this->db, 'utf8'); + self::$currDBH=$this->db; + } else { + $this->db=self::$currDBH; + } } protected function disconnect() { - //$this->db->close(); - } - - public function changeDB($databaseName) { - //$databaseName='coral_reporting_pprd'; - $this->db->select_db($databaseName); - $this->checkForError(); + //mysqli_close($this->db); } public function escapeString($value) { - return $this->db->escape_string($value); + return $this->db->real_escape_string($value); } public function getDatabase() { return $this->db; } - public function query($sql) { + public function query($sql) { $result = $this->db->query($sql); $this->checkForError(); return $result; - } + } public function processQuery($sql, $type = NULL) { - //echo $sql. "
"; - $result = $this->db->query($sql); + //echo $sql. "
"; + $result = mysqli_query($this->db, $sql); $this->checkForError(); $data = array(); @@ -85,16 +86,16 @@ public function processQuery($sql, $type = NULL) { if ($type == 'assoc') { $resultType = MYSQLI_ASSOC; } - while ($row = $result->fetch_array($resultType)) { - if ($this->db->affected_rows > 1) { + while ($row = mysqli_fetch_array($result, $resultType)) { + if (mysqli_affected_rows($this->db) > 1) { array_push($data, $row); } else { $data = $row; } } - $result->free(); + mysqli_free_result($result); } else if ($result) { - $data = $this->db->insert_id; + $data = mysqli_insert_id($this->db); } return $data; diff --git a/licensing/admin/classes/common/DBService.php b/licensing/admin/classes/common/DBService.php index 577546811..01d1b089a 100644 --- a/licensing/admin/classes/common/DBService.php +++ b/licensing/admin/classes/common/DBService.php @@ -1,8 +1,7 @@ db, $value); + return $this->db->real_escape_string($value); } public function getDatabase() { return $this->db; } + public function query($sql) { + $result = $this->db->query($sql); + $this->checkForError(); + return $result; + } + public function processQuery($sql, $type = NULL) { //echo $sql. "
"; - $query_start = microtime(true); - $result = mysqli_query($this->db, $sql); - $query_end = microtime(true); - $this->log($sql, $query_end - $query_start); - + $result = mysqli_query($this->db, $sql); $this->checkForError(); $data = array(); @@ -161,18 +162,6 @@ private static function refValues($arr){ } return $refs; } - - public function log($sql, $query_time) { - $threshold = $this->config->database->logQueryThreshold; - if ($this->config->database->logQueries == "Y" && (!$threshold || $query_time >= $threshold)) { - $util = new Utility(); - $log_path = $util->getModulePath()."/log"; - $log_file = $log_path."/database.log"; - $log_string = date("c")."\n".$_SERVER['REQUEST_URI']."\n".$sql."\nQuery completed in ".sprintf("%.3f", round($query_time, 3))." seconds"; - error_log($log_string."\n\n", 3, $log_file); - } - } - } ?> diff --git a/management/admin/classes/common/DBService.php b/management/admin/classes/common/DBService.php index a01fd42c8..01d1b089a 100644 --- a/management/admin/classes/common/DBService.php +++ b/management/admin/classes/common/DBService.php @@ -1,8 +1,7 @@ config = new Configuration; - $this->connect(); + $this->connect(); } protected function dealloc() { @@ -37,7 +36,8 @@ protected function dealloc() { } protected function checkForError() { - if ($this->error = $this->db->error) { + $this->error = mysqli_error($this->db); + if ($this->error) { throw new Exception(_("There was a problem with the database: ") . $this->error); } } @@ -47,39 +47,37 @@ protected function connect() { $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; - $this->db = new mysqli($host, $username, $password); - $this->checkForError(); - $databaseName = $this->config->database->name; - $this->db->select_db($databaseName); - $this->db->set_charset('utf8'); - $this->checkForError(); + $this->db = mysqli_connect($host, $username, $password, $databaseName); + $this->checkForError(); mysqli_set_charset($this->db, 'utf8'); self::$currDBH=$this->db; - } else { + } else { $this->db=self::$currDBH; } - } + } + protected function disconnect() { //mysqli_close($this->db); } public function escapeString($value) { - return $this->db->real_escape_string($value); + return $this->db->real_escape_string($value); } - public function query($sql) { - $result = $this->db->query($sql); - $this->checkForError(); - return $result; + public function getDatabase() { + return $this->db; } - public function processQuery($sql, $type = NULL) { - $query_start = microtime(true); - $result = $this->db->query($sql); - $query_end = microtime(true); - $this->log($sql, $query_end - $query_start); + public function query($sql) { + $result = $this->db->query($sql); + $this->checkForError(); + return $result; + } + public function processQuery($sql, $type = NULL) { + //echo $sql. "
"; + $result = mysqli_query($this->db, $sql); $this->checkForError(); $data = array(); @@ -88,16 +86,16 @@ public function processQuery($sql, $type = NULL) { if ($type == 'assoc') { $resultType = MYSQLI_ASSOC; } - while ($row = $result->fetch_array($resultType)) { - if ($this->db->affected_rows > 1) { + while ($row = mysqli_fetch_array($result, $resultType)) { + if (mysqli_affected_rows($this->db) > 1) { array_push($data, $row); } else { $data = $row; } } - $result->free(); + mysqli_free_result($result); } else if ($result) { - $data = $this->db->insert_id; + $data = mysqli_insert_id($this->db); } return $data; @@ -164,18 +162,6 @@ private static function refValues($arr){ } return $refs; } - - public function log($sql, $query_time) { - $threshold = $this->config->database->logQueryThreshold; - if ($this->config->database->logQueries == "Y" && (!$threshold || $query_time >= $threshold)) { - $util = new Utility(); - $log_path = $util->getModulePath()."/log"; - $log_file = $log_path."/database.log"; - $log_string = date("c")."\n".$_SERVER['REQUEST_URI']."\n".$sql."\nQuery completed in ".sprintf("%.3f", round($query_time, 3))." seconds"; - error_log($log_string."\n\n", 3, $log_file); - } - } - } ?> diff --git a/resources/admin/classes/common/DBService.php b/resources/admin/classes/common/DBService.php index 0ef1b01e8..01d1b089a 100644 --- a/resources/admin/classes/common/DBService.php +++ b/resources/admin/classes/common/DBService.php @@ -1,8 +1,7 @@ config = new Configuration; - $this->connect(); + $this->connect(); } protected function dealloc() { @@ -36,7 +36,8 @@ protected function dealloc() { } protected function checkForError() { - if ($this->error = $this->db->error) { + $this->error = mysqli_error($this->db); + if ($this->error) { throw new Exception(_("There was a problem with the database: ") . $this->error); } } @@ -46,18 +47,15 @@ protected function connect() { $host = $this->config->database->host; $username = $this->config->database->username; $password = $this->config->database->password; - $this->db = new mysqli($host, $username, $password); - $this->checkForError(); - $this->db->set_charset('utf8'); - $databaseName = $this->config->database->name; - $this->db->select_db($databaseName); + $databaseName = $this->config->database->name; + $this->db = mysqli_connect($host, $username, $password, $databaseName); $this->checkForError(); mysqli_set_charset($this->db, 'utf8'); self::$currDBH=$this->db; } else { $this->db=self::$currDBH; - } - } + } + } protected function disconnect() { //mysqli_close($this->db); @@ -67,15 +65,19 @@ public function escapeString($value) { return $this->db->real_escape_string($value); } - public function query($sql) { + public function getDatabase() { + return $this->db; + } + + public function query($sql) { $result = $this->db->query($sql); $this->checkForError(); return $result; } public function processQuery($sql, $type = NULL) { - //echo $sql. "\n\n"; - $result = $this->db->query($sql); + //echo $sql. "
"; + $result = mysqli_query($this->db, $sql); $this->checkForError(); $data = array(); @@ -84,16 +86,16 @@ public function processQuery($sql, $type = NULL) { if ($type == 'assoc') { $resultType = MYSQLI_ASSOC; } - while ($row = $result->fetch_array($resultType)) { - if ($this->db->affected_rows > 1) { + while ($row = mysqli_fetch_array($result, $resultType)) { + if (mysqli_affected_rows($this->db) > 1) { array_push($data, $row); } else { $data = $row; } } - $result->free(); + mysqli_free_result($result); } else if ($result) { - $data = $this->db->insert_id; + $data = mysqli_insert_id($this->db); } return $data; diff --git a/usage/admin/classes/common/DBService.php b/usage/admin/classes/common/DBService.php index 577546811..01d1b089a 100755 --- a/usage/admin/classes/common/DBService.php +++ b/usage/admin/classes/common/DBService.php @@ -1,8 +1,7 @@ db, $value); + return $this->db->real_escape_string($value); } public function getDatabase() { return $this->db; } + public function query($sql) { + $result = $this->db->query($sql); + $this->checkForError(); + return $result; + } + public function processQuery($sql, $type = NULL) { //echo $sql. "
"; - $query_start = microtime(true); - $result = mysqli_query($this->db, $sql); - $query_end = microtime(true); - $this->log($sql, $query_end - $query_start); - + $result = mysqli_query($this->db, $sql); $this->checkForError(); $data = array(); @@ -161,18 +162,6 @@ private static function refValues($arr){ } return $refs; } - - public function log($sql, $query_time) { - $threshold = $this->config->database->logQueryThreshold; - if ($this->config->database->logQueries == "Y" && (!$threshold || $query_time >= $threshold)) { - $util = new Utility(); - $log_path = $util->getModulePath()."/log"; - $log_file = $log_path."/database.log"; - $log_string = date("c")."\n".$_SERVER['REQUEST_URI']."\n".$sql."\nQuery completed in ".sprintf("%.3f", round($query_time, 3))." seconds"; - error_log($log_string."\n\n", 3, $log_file); - } - } - } ?>