Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from moufmouf/bugfix/sort_on_external_col
Adds an order by clause analyzer to be able to include external order by columns or expressions in order by columns.
- Loading branch information
Showing
8 changed files
with
293 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace Mouf\Database\TDBM; | ||
|
||
use Doctrine\Common\Cache\Cache; | ||
use PHPSQLParser\PHPSQLParser; | ||
|
||
/** | ||
* Class in charge of analyzing order by clauses. | ||
* | ||
* Analyzing those clauses are important because an order by clause must be pushed into the select clause too. | ||
*/ | ||
class OrderByAnalyzer | ||
{ | ||
/** | ||
* The content of the cache variable. | ||
* | ||
* @var Cache | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $cachePrefix; | ||
|
||
/** | ||
* OrderByAnalyzer constructor. | ||
* | ||
* @param Cache $cache | ||
* @param string|null $cachePrefix | ||
*/ | ||
public function __construct(Cache $cache, $cachePrefix = null) | ||
{ | ||
$this->cache = $cache; | ||
$this->cachePrefix = $cachePrefix; | ||
} | ||
|
||
/** | ||
* Returns an array for each sorted "column" in the form:. | ||
* | ||
* [ | ||
* [ | ||
* 'type' => 'colref', | ||
* 'table' => null, | ||
* 'column' => 'a', | ||
* 'direction' => 'ASC' | ||
* ], | ||
* [ | ||
* 'type' => 'expr', | ||
* 'expr' => 'RAND()', | ||
* 'direction' => 'DESC' | ||
* ] | ||
* ] | ||
* | ||
* @param string $orderBy | ||
* | ||
* @return array | ||
*/ | ||
public function analyzeOrderBy(string $orderBy) : array | ||
{ | ||
$key = $this->cachePrefix.'_order_by_analysis_'.$orderBy; | ||
$results = $this->cache->fetch($key); | ||
if ($results !== false) { | ||
return $results; | ||
} | ||
$results = $this->analyzeOrderByNoCache($orderBy); | ||
$this->cache->save($key, $results); | ||
|
||
return $results; | ||
} | ||
|
||
private function analyzeOrderByNoCache(string $orderBy) : array | ||
{ | ||
$sqlParser = new PHPSQLParser(); | ||
$sql = 'SELECT 1 FROM a ORDER BY '.$orderBy; | ||
$parsed = $sqlParser->parse($sql, true); | ||
|
||
$results = []; | ||
|
||
for ($i = 0, $count = count($parsed['ORDER']); $i < $count; ++$i) { | ||
$orderItem = $parsed['ORDER'][$i]; | ||
if ($orderItem['expr_type'] === 'colref') { | ||
$parts = $orderItem['no_quotes']['parts']; | ||
$columnName = array_pop($parts); | ||
if (!empty($parts)) { | ||
$tableName = array_pop($parts); | ||
} else { | ||
$tableName = null; | ||
} | ||
|
||
$results[] = [ | ||
'type' => 'colref', | ||
'table' => $tableName, | ||
'column' => $columnName, | ||
'direction' => $orderItem['direction'], | ||
]; | ||
} else { | ||
$position = $orderItem['position']; | ||
if ($i + 1 < $count) { | ||
$nextPosition = $parsed['ORDER'][$i + 1]['position']; | ||
$str = substr($sql, $position, $nextPosition - $position); | ||
} else { | ||
$str = substr($sql, $position); | ||
} | ||
|
||
$str = trim($str, " \t\r\n,"); | ||
|
||
$results[] = [ | ||
'type' => 'expr', | ||
'expr' => $this->trimDirection($str), | ||
'direction' => $orderItem['direction'], | ||
]; | ||
} | ||
} | ||
|
||
return $results; | ||
} | ||
|
||
/** | ||
* Trims the ASC/DESC direction at the end of the string. | ||
* | ||
* @param string $sql | ||
* | ||
* @return string | ||
*/ | ||
private function trimDirection(string $sql) : string | ||
{ | ||
preg_match('/^(.*)(\s+(DESC|ASC|))*$/Ui', $sql, $matches); | ||
|
||
return $matches[1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Mouf\Database\TDBM; | ||
|
||
use Doctrine\Common\Cache\ArrayCache; | ||
use Doctrine\Common\Cache\VoidCache; | ||
|
||
class OrderByAnalyzerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testAnalyzeOrderBy() | ||
{ | ||
$analyzer = new OrderByAnalyzer(new VoidCache(), ''); | ||
$results = $analyzer->analyzeOrderBy('`a`, b desc, rand() DESC, masc, mytable.mycol'); | ||
|
||
$this->assertCount(5, $results); | ||
$this->assertEquals([ | ||
'type' => 'colref', | ||
'table' => null, | ||
'column' => 'a', | ||
'direction' => 'ASC', | ||
], $results[0]); | ||
$this->assertEquals([ | ||
'type' => 'colref', | ||
'table' => null, | ||
'column' => 'b', | ||
'direction' => 'DESC', | ||
], $results[1]); | ||
$this->assertEquals([ | ||
'type' => 'expr', | ||
'expr' => 'rand()', | ||
'direction' => 'DESC', | ||
], $results[2]); | ||
$this->assertEquals([ | ||
'type' => 'colref', | ||
'table' => null, | ||
'column' => 'masc', | ||
'direction' => 'ASC', | ||
], $results[3]); | ||
$this->assertEquals([ | ||
'type' => 'colref', | ||
'table' => 'mytable', | ||
'column' => 'mycol', | ||
'direction' => 'ASC', | ||
], $results[4]); | ||
} | ||
|
||
public function testExprWithAsc() | ||
{ | ||
$analyzer = new OrderByAnalyzer(new VoidCache(), ''); | ||
$results = $analyzer->analyzeOrderBy('foodesc + barasc'); | ||
|
||
$this->assertCount(1, $results); | ||
$this->assertEquals([ | ||
'type' => 'expr', | ||
'expr' => 'foodesc + barasc', | ||
'direction' => 'ASC', | ||
], $results[0]); | ||
} | ||
|
||
public function testCache() | ||
{ | ||
$analyzer = new OrderByAnalyzer(new ArrayCache(), ''); | ||
$results = $analyzer->analyzeOrderBy('foo'); | ||
$results2 = $analyzer->analyzeOrderBy('foo'); | ||
// For code coverage purpose | ||
} | ||
} |
Oops, something went wrong.