DbDataCollector.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. *
  6. * @author Carl Schwan <carl@carlschwan.eu>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\DB;
  25. use Doctrine\DBAL\Types\ConversionException;
  26. use Doctrine\DBAL\Types\Type;
  27. use OC\AppFramework\Http\Request;
  28. use OCP\AppFramework\Http\Response;
  29. class DbDataCollector extends \OCP\DataCollector\AbstractDataCollector {
  30. protected ?BacktraceDebugStack $debugStack = null;
  31. private Connection $connection;
  32. /**
  33. * DbDataCollector constructor.
  34. */
  35. public function __construct(Connection $connection) {
  36. $this->connection = $connection;
  37. }
  38. public function setDebugStack(BacktraceDebugStack $debugStack, $name = 'default'): void {
  39. $this->debugStack = $debugStack;
  40. }
  41. /**
  42. * @inheritDoc
  43. */
  44. public function collect(Request $request, Response $response, ?\Throwable $exception = null): void {
  45. $queries = $this->sanitizeQueries($this->debugStack->queries);
  46. $this->data = [
  47. 'queries' => $queries,
  48. ];
  49. }
  50. public function getName(): string {
  51. return 'db';
  52. }
  53. public function getQueries(): array {
  54. return $this->data['queries'];
  55. }
  56. private function sanitizeQueries(array $queries): array {
  57. foreach ($queries as $i => $query) {
  58. $queries[$i] = $this->sanitizeQuery($query);
  59. }
  60. return $queries;
  61. }
  62. private function sanitizeQuery(array $query): array {
  63. $query['explainable'] = true;
  64. $query['runnable'] = true;
  65. if ($query['params'] === null) {
  66. $query['params'] = [];
  67. }
  68. if (!\is_array($query['params'])) {
  69. $query['params'] = [$query['params']];
  70. }
  71. if (!\is_array($query['types'])) {
  72. $query['types'] = [];
  73. }
  74. foreach ($query['params'] as $j => $param) {
  75. $e = null;
  76. if (isset($query['types'][$j])) {
  77. // Transform the param according to the type
  78. $type = $query['types'][$j];
  79. if (\is_string($type)) {
  80. $type = Type::getType($type);
  81. }
  82. if ($type instanceof Type) {
  83. $query['types'][$j] = $type->getBindingType();
  84. try {
  85. $param = $type->convertToDatabaseValue($param, $this->connection->getDatabasePlatform());
  86. } catch (\TypeError $e) {
  87. } catch (ConversionException $e) {
  88. }
  89. }
  90. }
  91. [$query['params'][$j], $explainable, $runnable] = $this->sanitizeParam($param, $e);
  92. if (!$explainable) {
  93. $query['explainable'] = false;
  94. }
  95. if (!$runnable) {
  96. $query['runnable'] = false;
  97. }
  98. }
  99. return $query;
  100. }
  101. /**
  102. * Sanitizes a param.
  103. *
  104. * The return value is an array with the sanitized value and a boolean
  105. * indicating if the original value was kept (allowing to use the sanitized
  106. * value to explain the query).
  107. */
  108. private function sanitizeParam($var, ?\Throwable $error): array {
  109. if (\is_object($var)) {
  110. return [$o = new ObjectParameter($var, $error), false, $o->isStringable() && !$error];
  111. }
  112. if ($error) {
  113. return ['⚠ '.$error->getMessage(), false, false];
  114. }
  115. if (\is_array($var)) {
  116. $a = [];
  117. $explainable = $runnable = true;
  118. foreach ($var as $k => $v) {
  119. [$value, $e, $r] = $this->sanitizeParam($v, null);
  120. $explainable = $explainable && $e;
  121. $runnable = $runnable && $r;
  122. $a[$k] = $value;
  123. }
  124. return [$a, $explainable, $runnable];
  125. }
  126. if (\is_resource($var)) {
  127. return [sprintf('/* Resource(%s) */', get_resource_type($var)), false, false];
  128. }
  129. return [$var, true, true];
  130. }
  131. }