Skip to content

Instantly share code, notes, and snippets.

@wilr
Created April 10, 2016 22:57
Show Gist options
  • Save wilr/d67a4ed67dcb74b5a4ffa3da58a0241d to your computer and use it in GitHub Desktop.
Save wilr/d67a4ed67dcb74b5a4ffa3da58a0241d to your computer and use it in GitHub Desktop.
Better MySQL showqueries debugging in SilverStripe 3.2+
diff --git a/model/connect/Database.php b/model/connect/Database.php
index cfdd628..53b236f 100644
--- a/model/connect/Database.php
+++ b/model/connect/Database.php
@@ -132,7 +132,8 @@ abstract class SS_Database {
$sql,
function($sql) use($connector, $parameters, $errorLevel) {
return $connector->preparedQuery($sql, $parameters, $errorLevel);
- }
+ },
+ $parameters
);
}
@@ -167,12 +168,24 @@ abstract class SS_Database {
* @param callable $callback Callback to execute code
* @return mixed Result of query
*/
- protected function benchmarkQuery($sql, $callback) {
+ protected function benchmarkQuery($sql, $callback, $parameters = array()) {
if (isset($_REQUEST['showqueries']) && Director::isDev()) {
$starttime = microtime(true);
+ $sql = str_replace('"', '', $sql);
+
+ if($parameters) {
+ $fixedSQL = preg_replace(array(
+ '/\?/'
+ ), array_map(function($v) {
+ return '"'. trim($v, '"') . '"';
+ }, $parameters), $sql);
+ } else {
+ $fixedSQL = $sql;
+ }
+
$result = $callback($sql);
$endtime = round(microtime(true) - $starttime, 4);
- Debug::message("\n$sql\n{$endtime}s\n", false);
+ Debug::message("\n$fixedSQL\n{$endtime}s\n", false);
return $result;
} else {
return $callback($sql);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment