Skip to content

Commit

Permalink
Enable to show the explanation for a query in the symfony debug bar
Browse files Browse the repository at this point in the history
Add a module to run explain
Add a link to show the explain

Require the following config in databases.yml :
dev:
  propel:
    param:
      debug:
        realmemoryusage: true
        details:
          connection: { enabled: true }
  • Loading branch information
cedriclombardot committed Mar 16, 2012
1 parent 8709ee5 commit 22273c9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
propel_debug_pannel:
url: /propel-debug-pannel/:connection/:base64_query
param: { module: sfPropelORMExplain, action: index }
5 changes: 5 additions & 0 deletions config/sfPropelORMPluginConfiguration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function initialize()
if (sfConfig::get('sf_web_debug'))
{
$this->dispatcher->connect('debug.web.load_panels', array('sfWebDebugPanelPropel', 'listenToAddPanelEvent'));

$modules = sfConfig::get('sf_enabled_modules', array());
$modules[] = 'sfPropelORMExplain';

sfConfig::set('sf_enabled_modules', $modules);
}

if (sfConfig::get('sf_test'))
Expand Down
58 changes: 56 additions & 2 deletions lib/debug/sfWebDebugPanelPropel.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,42 @@ public function getPanelContent()
<h3>Propel Version: '.Propel::VERSION.'</h3>
<ol>'.implode("\n", $this->getSqlLogs()).'</ol>
</div>
<script type="text/javascript">
function sfWebDebbugGetXMLHttpRequest() {
var xhr = null;
if (window.XMLHttpRequest || window.ActiveXObject) {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhr = new XMLHttpRequest();
}
} else {
alert("Your browser do no support XMLHTTPRequest");
return null;
}
return xhr;
}
function sfWebDebbugDoExplain(url, area)
{
var xhr = sfWebDebbugGetXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
document.getElementById(area).innerHTML = xhr.responseText;
}
};
xhr.open("GET", url, true);
xhr.send(null)
}
</script>
';
}

Expand All @@ -80,14 +116,15 @@ protected function getSqlLogs()
$threshold = $config->getParameter('debugpdo.logging.details.slow.threshold', DebugPDO::DEFAULT_SLOW_THRESHOLD);

$html = array();
foreach ($this->webDebug->getLogger()->getLogs() as $log)
foreach ($this->webDebug->getLogger()->getLogs() as $i => $log)
{
if ('sfPropelLogger' != $log['type'])
{
continue;
}

$details = array();
$explainlink = '';
$slowQuery = false;

$parts = explode($outerGlue, $log['message']);
Expand All @@ -111,10 +148,25 @@ protected function getSqlLogs()
}
}
}

// Find connection for explain
if ('connection' == $match[1])
{
$connection = $match[2];
}
}
}
// all stuff that has not been eaten by the loop should be the query string
$query = join($outerGlue, $parts);

if(isset($connection))
{
$explainlink = ', <a onclick="sfWebDebbugDoExplain(\''.sfContext::getInstance()->getRouting()->generate('propel_debug_pannel', array(
'connection' => $connection,
'base64_query' => base64_encode($query),
)).'\', \'explain_'.$i.'\')">Explain the query</a>';
}

if ($query == "SET NAMES 'utf8'")
{
// This is the initialization query that occurs on every request.
Expand All @@ -127,11 +179,13 @@ protected function getSqlLogs()
$html[] = sprintf('
<li class="%s">
<p class="sfWebDebugDatabaseQuery">%s</p>
<div class="sfWebDebugDatabaseLogInfo">%s%s</div>
<div class="sfWebDebugDatabaseLogInfo">%s%s%s</div>
<div class="sfWebDebugDatabaseExplain" id="explain_'.$i.'"></div>
</li>',
$slowQuery ? 'sfWebDebugWarning' : '',
$query,
implode(', ', $details),
$explainlink,
$backtrace
);
}
Expand Down
24 changes: 24 additions & 0 deletions modules/sfPropelORMExplain/actions/actions.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class sfPropelORMExplainActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
$this->setLayout(false);
sfConfig::set('sf_web_debug', false);

// Open the connection
$con = \Propel::getConnection($request->getParameter('connection'));

// Get the adapter
$db = \Propel::getDB($request->getParameter('connection'));

try {
$this->query = base64_decode($request->getParameter('base64_query'));
$stmt = $db->doExplainPlan($con, $this->query);
$this->results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (Exception $e) {
$this->getResponse()->setContent('<div class="error">This query cannot be explained.</div>');
}
}
}
16 changes: 16 additions & 0 deletions modules/sfPropelORMExplain/templates/indexSuccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Explanation</h2>

<table>
<tr>
<?php foreach($results[0] as $label =>$val): ?>
<th><?php echo $label; ?></th>
<?php endforeach; ?>
</tr>
<?php foreach($results as $row): ?>
<tr>
<?php foreach($row as $item): ?>
<td><?php echo $item; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

0 comments on commit 22273c9

Please sign in to comment.