Skip to content

Instantly share code, notes, and snippets.

@codebymikey
Last active November 10, 2021 11:34
Show Gist options
  • Select an option

  • Save codebymikey/861fca170650b75dac20d352a116f457 to your computer and use it in GitHub Desktop.

Select an option

Save codebymikey/861fca170650b75dac20d352a116f457 to your computer and use it in GitHub Desktop.
Drupal 8 log queries
Index: core/lib/Drupal/Core/Database/Query/Select.php
Description: Log RAW SELECT queries in Drupal 9.
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/core/lib/Drupal/Core/Database/Query/Select.php b/core/lib/Drupal/Core/Database/Query/Select.php
--- a//core/lib/Drupal/Core/Database/Query/Select.php
+++ b//core/lib/Drupal/Core/Database/Query/Select.php (date 1636458764911)
@@ -509,7 +509,33 @@
}
$args = $this->getArguments();
- return $this->connection->query((string) $this, $args, $this->queryOptions);
+ $sql = (string) $this;
+
+ if (\Drupal\Core\Site\Settings::get('log_queries', TRUE)) {
+ if (!isset($GLOBALS['zm_query_logs'])) {
+ $GLOBALS['zm_query_logs'] = [];
+ }
+ $connection = Database::getConnection();
+
+ $result = $connection->prefixTables($sql);
+ $quoted = [];
+ foreach ((array) $this->arguments() as $key => $data) {
+ if (substr($key, -2) === '[]') {
+ $values = [];
+ foreach (array_values($data) as $value) {
+ $values[] = $value === NULL ? 'NULL' : $connection->quote($value);
+ }
+ $quoted[$key] = implode(', ', $values);
+ }
+ else {
+ $quoted[$key] = $data === NULL ? 'NULL' : $connection->quote($data);
+ }
+ }
+ $result = $connection->quoteIdentifiers(strtr($result, $quoted));
+ $GLOBALS['zm_query_logs'][] = $result;
+ }
+
+ return $this->connection->query($sql, $args, $this->queryOptions);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment