-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathOrderByAnalyzer.php
More file actions
138 lines (122 loc) · 3.56 KB
/
OrderByAnalyzer.php
File metadata and controls
138 lines (122 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace TheCodingMachine\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|null
*/
private $cachePrefix;
/**
* OrderByAnalyzer constructor.
*
* @param Cache $cache
* @param string|null $cachePrefix
*/
public function __construct(Cache $cache, ?string $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;
}
/**
* @param string $orderBy
* @return array[]
*/
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('/^((.|\n)*)(\s+(DESC|ASC))?$/Ui', $sql, $matches);
return $matches[1];
}
}