DbDataCollector.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB;
  8. use Doctrine\DBAL\Types\ConversionException;
  9. use Doctrine\DBAL\Types\Type;
  10. use OC\AppFramework\Http\Request;
  11. use OCP\AppFramework\Http\Response;
  12. class DbDataCollector extends \OCP\DataCollector\AbstractDataCollector {
  13. protected ?BacktraceDebugStack $debugStack = null;
  14. private Connection $connection;
  15. /**
  16. * DbDataCollector constructor.
  17. */
  18. public function __construct(Connection $connection) {
  19. $this->connection = $connection;
  20. }
  21. public function setDebugStack(BacktraceDebugStack $debugStack, $name = 'default'): void {
  22. $this->debugStack = $debugStack;
  23. }
  24. /**
  25. * @inheritDoc
  26. */
  27. public function collect(Request $request, Response $response, ?\Throwable $exception = null): void {
  28. $queries = $this->sanitizeQueries($this->debugStack->queries);
  29. $this->data = [
  30. 'queries' => $queries,
  31. ];
  32. }
  33. public function getName(): string {
  34. return 'db';
  35. }
  36. public function getQueries(): array {
  37. return $this->data['queries'];
  38. }
  39. private function sanitizeQueries(array $queries): array {
  40. foreach ($queries as $i => $query) {
  41. $queries[$i] = $this->sanitizeQuery($query);
  42. }
  43. return $queries;
  44. }
  45. private function sanitizeQuery(array $query): array {
  46. $query['explainable'] = true;
  47. $query['runnable'] = true;
  48. if ($query['params'] === null) {
  49. $query['params'] = [];
  50. }
  51. if (!\is_array($query['params'])) {
  52. $query['params'] = [$query['params']];
  53. }
  54. if (!\is_array($query['types'])) {
  55. $query['types'] = [];
  56. }
  57. foreach ($query['params'] as $j => $param) {
  58. $e = null;
  59. if (isset($query['types'][$j])) {
  60. // Transform the param according to the type
  61. $type = $query['types'][$j];
  62. if (\is_string($type)) {
  63. $type = Type::getType($type);
  64. }
  65. if ($type instanceof Type) {
  66. $query['types'][$j] = $type->getBindingType();
  67. try {
  68. $param = $type->convertToDatabaseValue($param, $this->connection->getDatabasePlatform());
  69. } catch (\TypeError $e) {
  70. } catch (ConversionException $e) {
  71. }
  72. }
  73. }
  74. [$query['params'][$j], $explainable, $runnable] = $this->sanitizeParam($param, $e);
  75. if (!$explainable) {
  76. $query['explainable'] = false;
  77. }
  78. if (!$runnable) {
  79. $query['runnable'] = false;
  80. }
  81. }
  82. return $query;
  83. }
  84. /**
  85. * Sanitizes a param.
  86. *
  87. * The return value is an array with the sanitized value and a boolean
  88. * indicating if the original value was kept (allowing to use the sanitized
  89. * value to explain the query).
  90. */
  91. private function sanitizeParam($var, ?\Throwable $error): array {
  92. if (\is_object($var)) {
  93. return [$o = new ObjectParameter($var, $error), false, $o->isStringable() && !$error];
  94. }
  95. if ($error) {
  96. return ['⚠ '.$error->getMessage(), false, false];
  97. }
  98. if (\is_array($var)) {
  99. $a = [];
  100. $explainable = $runnable = true;
  101. foreach ($var as $k => $v) {
  102. [$value, $e, $r] = $this->sanitizeParam($v, null);
  103. $explainable = $explainable && $e;
  104. $runnable = $runnable && $r;
  105. $a[$k] = $value;
  106. }
  107. return [$a, $explainable, $runnable];
  108. }
  109. if (\is_resource($var)) {
  110. return [sprintf('/* Resource(%s) */', get_resource_type($var)), false, false];
  111. }
  112. return [$var, true, true];
  113. }
  114. }