Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ability to sort queries by alphabetical order #1669

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
'memory_usage' => false, // Show queries memory usage
'soft_limit' => 100, // After the soft limit, no parameters/backtrace are captured
'hard_limit' => 500, // After the hard limit, queries are ignored
'sort_condition' => 'execution_order', // Options (execution_order, alphabetical_order)
],
'mail' => [
'timeline' => false, // Add mails to the timeline
Expand Down
19 changes: 19 additions & 0 deletions src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class QueryCollector extends PDOCollector
protected $explainTypes = ['SELECT']; // ['SELECT', 'INSERT', 'UPDATE', 'DELETE']; for MySQL 5.6.3+
protected $showHints = false;
protected $showCopyButton = false;
protected $sortCondition = 'execution_order';
protected $reflection = [];
protected $backtraceExcludePaths = [
'/vendor/laravel/framework/src/Illuminate/Support',
Expand Down Expand Up @@ -87,6 +88,16 @@ public function setShowCopyButton($enabled = true)
$this->showCopyButton = $enabled;
}

/**
* Determine how the queries are sorted.
*
* @param string $sortCondition
*/
public function setSortCondition($sortCondition = 'execution_order')
{
$this->sortCondition = $sortCondition;
}

/**
* Enable/disable finding the source
*
Expand Down Expand Up @@ -194,6 +205,14 @@ public function addQuery($query)
'show_copy' => $this->showCopyButton,
];

if ($this->sortCondition === 'alphabetical_order') {
usort($this->queries, function (array $queryOne, array $queryTwo) {
$queryOneTableName = substr($queryOne['query'], stripos($queryOne['query'], "from") + 4);
$queryTwoTableName = substr($queryTwo['query'], stripos($queryTwo['query'], "from") + 4);
return (int)($queryOneTableName > $queryTwoTableName);
});
}

if ($this->timeCollector !== null) {
$this->timeCollector->addMeasure(Str::limit($sql, 100), $startTime, $endTime, [], 'db');
}
Expand Down
4 changes: 4 additions & 0 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ function (\Illuminate\Log\Events\MessageLogged $log) use ($logger) {
$queryCollector->setShowCopyButton(true);
}

if ($sort = $config->get('debugbar.options.db.sort_condition', 'execution_order')) {
$queryCollector->setSortCondition($sort);
}

$this->addCollector($queryCollector);

try {
Expand Down
32 changes: 32 additions & 0 deletions tests/DataCollector/QueryCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,36 @@ public function testFindingCorrectPathForView()
);
});
}

public function testWillSortByAlphabeticalOrderOfTableName()
{
debugbar()->boot();

/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
$collector = debugbar()->getCollector('queries');
$collector->setSortCondition('alphabetical_order');
$collector->addQuery(new QueryExecuted(
"SELECT a FROM c WHERE b = ?",
['1'],
0,
$this->app['db']->connection()
));
$collector->addQuery(new QueryExecuted(
"SELECT b FROM a WHERE c = ?",
['2'],
0,
$this->app['db']->connection()
));
$collector->addQuery(new QueryExecuted(
"SELECT a FROM b WHERE c = ?",
['3'],
0,
$this->app['db']->connection()
));

$statements = $collector->collect()['statements'];
$this->assertEquals("SELECT b FROM a WHERE c = '2'", $statements[0]['sql']);
$this->assertEquals("SELECT a FROM b WHERE c = '3'", $statements[1]['sql']);
$this->assertEquals("SELECT a FROM c WHERE b = '1'", $statements[2]['sql']);
}
}
Loading