From 08af558e8f67d7785e4b35f51448e42d0ac459ae Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 21 Nov 2018 14:59:31 +0100 Subject: [PATCH] - Better code in application/libraries/LogLib.php - Added private property debugMode to application/core/DB_Model.php - application/core/DB_Model.php now loads Loglib - Added private method _logLastQuery to application/core/DB_Model.php - Placed _logLastQuery in DB_Model methods: insert, update, delete, loadWhere, loadTree, execQuery - DB_Model method load now calls method loadWhere --- application/core/DB_Model.php | 54 ++++++++++++++------ application/libraries/LogLib.php | 84 +++++++++++++++++--------------- 2 files changed, 85 insertions(+), 53 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 6273cb962..b636c3fe5 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -26,6 +26,8 @@ class DB_Model extends FHC_Model private $executedQueryMetaData; private $executedQueryListFields; + private $debugMode; + /** * Constructor */ @@ -42,6 +44,10 @@ class DB_Model extends FHC_Model // Loads the UDF library $this->load->library('UDFLib'); + // Loads the logs library + $this->load->library('LogLib'); + + $this->debugMode = isset($this->db->db_debug) && $this->db->db_debug === true; } // ------------------------------------------------------------------------------------------ @@ -62,7 +68,11 @@ class DB_Model extends FHC_Model if (isError($validate = $this->_manageUDFs($data, $this->dbTable))) return $validate; // DB-INSERT - if ($this->db->insert($this->dbTable, $data)) + $insert = $this->db->insert($this->dbTable, $data); + + $this->_logLastQuery(); + + if ($insert) { // If the table has a primary key that uses a sequence if ($this->hasSequence === true) @@ -126,7 +136,11 @@ class DB_Model extends FHC_Model $this->db->where($tmpId); // DB-UPDATE - if ($this->db->update($this->dbTable, $data)) + $update = $this->db->update($this->dbTable, $data); + + $this->_logLastQuery(); + + if ($update) { return success($id); } @@ -164,7 +178,11 @@ class DB_Model extends FHC_Model } // DB-DELETE - if ($this->db->delete($this->dbTable, $tmpId)) + $delete = $this->db->delete($this->dbTable, $tmpId); + + $this->_logLastQuery(); + + if ($delete) { return success($id); } @@ -201,15 +219,7 @@ class DB_Model extends FHC_Model $tmpId = array($this->pk => $id); } - // DB-SELECT - if ($result = $this->db->get_where($this->dbTable, $tmpId)) - { - return success($this->_toPhp($result)); - } - else - { - return error($this->db->error(), FHC_DB_ERROR); - } + return $this->loadWhere($tmpId); } /** @@ -223,7 +233,11 @@ class DB_Model extends FHC_Model if (is_null($this->dbTable)) return error(FHC_MODEL_ERROR, FHC_NODBTABLE); // Execute query - if ($result = $this->db->get_where($this->dbTable, $where)) + $result = $this->db->get_where($this->dbTable, $where); + + $this->_logLastQuery(); + + if ($result) { return success($this->_toPhp($result)); } @@ -292,6 +306,9 @@ class DB_Model extends FHC_Model // Execute the query $resultDB = $this->db->get_where($this->dbTable, $where); + + $this->_logLastQuery(); + // If everything went ok... if ($resultDB) { @@ -603,7 +620,6 @@ class DB_Model extends FHC_Model // Workaround to get metadata from this table $result = $this->db->query(sprintf(DB_Model::QUERY_LIST_FIELDS, $this->dbTable)); - if (is_object($result)) { $listFields = $result->list_fields(); @@ -736,6 +752,8 @@ class DB_Model extends FHC_Model $resultDB = $this->db->query($query); } + $this->_logLastQuery(); + // If no errors occurred if ($resultDB) { @@ -940,4 +958,12 @@ class DB_Model extends FHC_Model return $this->execQuery($query, array(strtolower($schema), strtolower($table))); } + + /** + * + */ + private function _logLastQuery() + { + if ($this->debugMode) $this->loglib->logDebug($this->db->last_query()); + } } diff --git a/application/libraries/LogLib.php b/application/libraries/LogLib.php index a42132644..cb0541003 100644 --- a/application/libraries/LogLib.php +++ b/application/libraries/LogLib.php @@ -16,27 +16,48 @@ class LogLib const CLASS_POSTFIX = '->'; const LINE_SEPARATOR = ':'; + // -------------------------------------------------------------------------------------------------------------- + // Public methods + /** - * format + * logDebug */ - private function format($class, $function, $line) + public function logDebug($message) { - $formatted = LogLib::CALLER_PREFIX; - - if (!is_null($class) && $class != '') - { - $formatted .= $class.LogLib::CLASS_POSTFIX; - } - - $formatted .= $function.LogLib::LINE_SEPARATOR.$line.LogLib::CALLER_POSTFIX.' '; - - return $formatted; + $this->_log(LogLib::DEBUG, $message); } /** - * getCaller + * logInfo */ - private function getCaller() + public function logInfo($message) + { + $this->_log(LogLib::INFO, $message); + } + + /** + * logError + */ + public function logError($message) + { + $this->_log(LogLib::ERROR, $message); + } + + // -------------------------------------------------------------------------------------------------------------- + // Private methods + + /** + * log + */ + private function _log($level, $message) + { + log_message($level, $this->_getCaller().$message); + } + + /** + * _getCaller + */ + private function _getCaller() { $classIndex = 3; $functionIndex = 3; @@ -60,38 +81,23 @@ class LogLib $line = $backtrace_arr[$lineIndex]['line']; } - return $this->format($class, $function, $line); + return $this->_format($class, $function, $line); } /** - * log + * format */ - private function log($level, $message) + private function _format($class, $function, $line) { - log_message($level, $this->getCaller().$message); - } + $formatted = LogLib::CALLER_PREFIX; - /** - * logDebug - */ - public function logDebug($message) - { - $this->log(LogLib::DEBUG, $message); - } + if (!is_null($class) && $class != '') + { + $formatted .= $class.LogLib::CLASS_POSTFIX; + } - /** - * logInfo - */ - public function logInfo($message) - { - $this->log(LogLib::INFO, $message); - } + $formatted .= $function.LogLib::LINE_SEPARATOR.$line.LogLib::CALLER_POSTFIX.' '; - /** - * logError - */ - public function logError($message) - { - $this->log(LogLib::ERROR, $message); + return $formatted; } }