LoggingStatement.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Log;
  16. use Cake\Database\Statement\StatementDecorator;
  17. use Exception;
  18. /**
  19. * Statement decorator used to
  20. *
  21. * @internal
  22. */
  23. class LoggingStatement extends StatementDecorator
  24. {
  25. /**
  26. * Logger instance responsible for actually doing the logging task
  27. *
  28. * @var \Cake\Database\Log\QueryLogger|null
  29. */
  30. protected $_logger;
  31. /**
  32. * Holds bound params
  33. *
  34. * @var array
  35. */
  36. protected $_compiledParams = [];
  37. /**
  38. * Wrapper for the execute function to calculate time spent
  39. * and log the query afterwards.
  40. *
  41. * @param array|null $params List of values to be bound to query
  42. * @return bool True on success, false otherwise
  43. * @throws \Exception Re-throws any exception raised during query execution.
  44. */
  45. public function execute($params = null)
  46. {
  47. $t = microtime(true);
  48. $query = new LoggedQuery();
  49. try {
  50. $result = parent::execute($params);
  51. } catch (Exception $e) {
  52. $e->queryString = $this->queryString;
  53. $query->error = $e;
  54. $this->_log($query, $params, $t);
  55. throw $e;
  56. }
  57. $query->numRows = $this->rowCount();
  58. $this->_log($query, $params, $t);
  59. return $result;
  60. }
  61. /**
  62. * Copies the logging data to the passed LoggedQuery and sends it
  63. * to the logging system.
  64. *
  65. * @param \Cake\Database\Log\LoggedQuery $query The query to log.
  66. * @param array $params List of values to be bound to query.
  67. * @param float $startTime The microtime when the query was executed.
  68. * @return void
  69. */
  70. protected function _log($query, $params, $startTime)
  71. {
  72. $query->took = (int)round((microtime(true) - $startTime) * 1000, 0);
  73. $query->params = $params ?: $this->_compiledParams;
  74. $query->query = $this->queryString;
  75. $this->getLogger()->log($query);
  76. }
  77. /**
  78. * Wrapper for bindValue function to gather each parameter to be later used
  79. * in the logger function.
  80. *
  81. * @param string|int $column Name or param position to be bound
  82. * @param mixed $value The value to bind to variable in query
  83. * @param string|int|null $type PDO type or name of configured Type class
  84. * @return void
  85. */
  86. public function bindValue($column, $value, $type = 'string')
  87. {
  88. parent::bindValue($column, $value, $type);
  89. if ($type === null) {
  90. $type = 'string';
  91. }
  92. if (!ctype_digit($type)) {
  93. $value = $this->cast($value, $type)[0];
  94. }
  95. $this->_compiledParams[$column] = $value;
  96. }
  97. /**
  98. * Sets the logger object instance. When called with no arguments
  99. * it returns the currently setup logger instance
  100. *
  101. * @deprecated 3.5.0 Use getLogger() and setLogger() instead.
  102. * @param \Cake\Database\Log\QueryLogger|null $instance Logger object instance.
  103. * @return \Cake\Database\Log\QueryLogger|null Logger instance
  104. */
  105. public function logger($instance = null)
  106. {
  107. deprecationWarning(
  108. 'LoggingStatement::logger() is deprecated. ' .
  109. 'Use LoggingStatement::setLogger()/getLogger() instead.'
  110. );
  111. if ($instance === null) {
  112. return $this->getLogger();
  113. }
  114. return $this->_logger = $instance;
  115. }
  116. /**
  117. * Sets a logger
  118. *
  119. * @param \Cake\Database\Log\QueryLogger $logger Logger object
  120. * @return void
  121. */
  122. public function setLogger($logger)
  123. {
  124. $this->_logger = $logger;
  125. }
  126. /**
  127. * Gets the logger object
  128. *
  129. * @return \Cake\Database\Log\QueryLogger logger instance
  130. */
  131. public function getLogger()
  132. {
  133. return $this->_logger;
  134. }
  135. }