Replace placeholders by actual values in Benchmark
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 27 Jan 2021 11:39:51 +0000 (12:39 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 27 Jan 2021 11:45:43 +0000 (12:45 +0100)
In most cases this allows one to simply copy the query to easily edit it within
a MySQL shell. The code (intentionally) does not handle single quotes
correctly. It also truncates the parameter after 100 characters and handles at
most 30 parameters.

wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php

index 571353ad3e517491e1b2e34d04810d27f463529c..1038406376efde008e29cc3ccc8f9c887468185f 100644 (file)
@@ -94,7 +94,21 @@ class PreparedStatement
 
         try {
             if (WCF::benchmarkIsEnabled()) {
-                Benchmark::getInstance()->start($this->query, Benchmark::TYPE_SQL_QUERY);
+                $benchmarkParameters = \array_slice($parameters, 0, 30);
+                Benchmark::getInstance()->start(
+                    \preg_replace_callback(
+                        '/\?/',
+                        static function ($matches) use (&$benchmarkParameters) {
+                            if (empty($benchmarkParameters)) {
+                                return $matches[0];
+                            }
+
+                            return "'" . \substr(\array_shift($benchmarkParameters), 0, 100) . "'";
+                        },
+                        $this->query
+                    ),
+                    Benchmark::TYPE_SQL_QUERY
+                );
             }
 
             $result = $this->pdoStatement->execute($parameters);