QueryAnalyzer.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace Gedmo\Tool\Logging\DBAL;
  3. use Doctrine\DBAL\Logging\SQLLogger;
  4. use Doctrine\DBAL\Types\Type;
  5. use Doctrine\DBAL\Platforms\AbstractPlatform;
  6. /**
  7. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  8. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  9. */
  10. class QueryAnalyzer implements SQLLogger
  11. {
  12. /**
  13. * Used database platform
  14. *
  15. * @var AbstractPlatform
  16. */
  17. protected $platform;
  18. /**
  19. * Start time of currently executed query
  20. *
  21. * @var integer
  22. */
  23. private $queryStartTime = null;
  24. /**
  25. * Total execution time of all queries
  26. *
  27. * @var integer
  28. */
  29. private $totalExecutionTime = 0;
  30. /**
  31. * List of queries executed
  32. *
  33. * @var array
  34. */
  35. private $queries = array();
  36. /**
  37. * Query execution times indexed
  38. * in same order as queries
  39. *
  40. * @var array
  41. */
  42. private $queryExecutionTimes = array();
  43. /**
  44. * Initialize log listener with database
  45. * platform, which is needed for parameter
  46. * conversion
  47. *
  48. * @param AbstractPlatform $platform
  49. */
  50. public function __construct(AbstractPlatform $platform)
  51. {
  52. $this->platform = $platform;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function startQuery($sql, array $params = null, array $types = null)
  58. {
  59. $this->queryStartTime = microtime(true);
  60. $this->queries[] = $this->generateSql($sql, $params, $types);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function stopQuery()
  66. {
  67. $ms = round(microtime(true) - $this->queryStartTime, 4) * 1000;
  68. $this->queryExecutionTimes[] = $ms;
  69. $this->totalExecutionTime += $ms;
  70. }
  71. /**
  72. * Clean all collected data
  73. *
  74. * @return QueryAnalyzer
  75. */
  76. public function cleanUp()
  77. {
  78. $this->queries = array();
  79. $this->queryExecutionTimes = array();
  80. $this->totalExecutionTime = 0;
  81. return $this;
  82. }
  83. /**
  84. * Dump the statistics of executed queries
  85. *
  86. * @param boolean $dumpOnlySql
  87. * @return void
  88. */
  89. public function getOutput($dumpOnlySql = false)
  90. {
  91. $output = '';
  92. if (!$dumpOnlySql) {
  93. $output .= 'Platform: ' . $this->platform->getName() . PHP_EOL;
  94. $output .= 'Executed queries: ' . count($this->queries) . ', total time: ' . $this->totalExecutionTime . ' ms' . PHP_EOL;
  95. }
  96. foreach ($this->queries as $index => $sql) {
  97. if (!$dumpOnlySql) {
  98. $output .= 'Query(' . ($index+1) . ') - ' . $this->queryExecutionTimes[$index] . ' ms' . PHP_EOL;
  99. }
  100. $output .= $sql . ';' . PHP_EOL;
  101. }
  102. $output .= PHP_EOL;
  103. return $output;
  104. }
  105. /**
  106. * Index of the slowest query executed
  107. *
  108. * @return integer
  109. */
  110. public function getSlowestQueryIndex()
  111. {
  112. $index = 0;
  113. $slowest = 0;
  114. foreach ($this->queryExecutionTimes as $i => $time) {
  115. if ($time > $slowest) {
  116. $slowest = $time;
  117. $index = $i;
  118. }
  119. }
  120. return $index;
  121. }
  122. /**
  123. * Get total execution time of queries
  124. *
  125. * @return integer
  126. */
  127. public function getTotalExecutionTime()
  128. {
  129. return $this->totalExecutionTime;
  130. }
  131. /**
  132. * Get all queries
  133. *
  134. * @return array
  135. */
  136. public function getExecutedQueries()
  137. {
  138. return $this->queries;
  139. }
  140. /**
  141. * Get number of executed queries
  142. *
  143. * @return integer
  144. */
  145. public function getNumExecutedQueries()
  146. {
  147. return count($this->queries);
  148. }
  149. /**
  150. * Get all query execution times
  151. *
  152. * @return array
  153. */
  154. public function getExecutionTimes()
  155. {
  156. return $this->queryExecutionTimes;
  157. }
  158. /**
  159. * Create the SQL with mapped parameters
  160. *
  161. * @param string $sql
  162. * @param array $params
  163. * @param array $types
  164. * @return string sql
  165. */
  166. private function generateSql($sql, $params, $types)
  167. {
  168. if (!count($params)) {
  169. return $sql;
  170. }
  171. $converted = $this->getConvertedParams($params, $types);
  172. if (is_int(key($params))) {
  173. $index = key($converted);
  174. $sql = preg_replace_callback('@\?@sm', function($match) use (&$index, $converted) {
  175. return $converted[$index++];
  176. }, $sql);
  177. } else {
  178. foreach ($converted as $key => $value) {
  179. $sql = str_replace(':' . $key, $value, $sql);
  180. }
  181. }
  182. return $sql;
  183. }
  184. /**
  185. * Get the converted parameter list
  186. *
  187. * @param array $params
  188. * @param array $types
  189. * @return array
  190. */
  191. private function getConvertedParams($params, $types)
  192. {
  193. $result = array();
  194. foreach ($params as $position => $value) {
  195. if (isset($types[$position])) {
  196. $type = $types[$position];
  197. if (is_string($type)) {
  198. $type = Type::getType($type);
  199. }
  200. if ($type instanceof Type) {
  201. $value = $type->convertToDatabaseValue($value, $this->platform);
  202. }
  203. } else {
  204. if (is_object($value) && $value instanceof \DateTime) {
  205. $value = $value->format($this->platform->getDateTimeFormatString());
  206. } elseif (!is_null($value)) {
  207. $type = Type::getType(gettype($value));
  208. $value = $type->convertToDatabaseValue($value, $this->platform);
  209. }
  210. }
  211. if (is_string($value)) {
  212. $value = "'{$value}'";
  213. } elseif (is_null($value)) {
  214. $value = 'NULL';
  215. }
  216. $result[$position] = $value;
  217. }
  218. return $result;
  219. }
  220. }