- 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
This commit is contained in:
Paolo
2018-11-21 14:59:31 +01:00
parent 494c721b02
commit 08af558e8f
2 changed files with 85 additions and 53 deletions
+40 -14
View File
@@ -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());
}
}
+45 -39
View File
@@ -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;
}
}