1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Database\TDBM\QueryFactory; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
8
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
9
|
|
|
use Mouf\Database\TDBM\OrderByAnalyzer; |
10
|
|
|
use Mouf\Database\TDBM\TDBMException; |
11
|
|
|
use Mouf\Database\TDBM\TDBMService; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This class is in charge of creating the MagicQuery SQL based on parameters passed to findObjectsFromSql method. |
15
|
|
|
*/ |
16
|
|
|
class FindObjectsFromSqlQueryFactory extends AbstractQueryFactory |
17
|
|
|
{ |
18
|
|
|
private $mainTable; |
19
|
|
|
private $from; |
20
|
|
|
private $filterString; |
21
|
|
|
private $cache; |
22
|
|
|
private $cachePrefix; |
23
|
|
|
|
24
|
|
|
public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy); |
27
|
|
|
$this->mainTable = $mainTable; |
28
|
|
|
$this->from = $from; |
29
|
|
|
$this->filterString = $filterString; |
30
|
|
|
$this->schemaAnalyzer = $schemaAnalyzer; |
|
|
|
|
31
|
|
|
$this->cache = $cache; |
32
|
|
|
$this->cachePrefix = $cachePrefix; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function compute() |
36
|
|
|
{ |
37
|
|
|
$connection = $this->tdbmService->getConnection(); |
38
|
|
|
|
39
|
|
|
$columnsList = null; |
40
|
|
|
|
41
|
|
|
$allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
42
|
|
|
|
43
|
|
|
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
44
|
|
|
|
45
|
|
|
$sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM '.$this->from; |
46
|
|
|
|
47
|
|
|
// Let's compute the COUNT. |
48
|
|
|
$pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
49
|
|
View Code Duplication |
$pkColumnNames = array_map(function ($pkColumn) { |
|
|
|
|
50
|
|
|
return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
51
|
|
|
}, $pkColumnNames); |
52
|
|
|
|
53
|
|
|
$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; |
54
|
|
|
|
55
|
|
|
// Add joins on inherited tables if necessary |
56
|
|
|
if (count($allFetchedTables) > 1) { |
57
|
|
|
$joinSql = ''; |
58
|
|
|
$parentFks = $this->getParentRelationshipForeignKeys($this->mainTable); |
59
|
|
View Code Duplication |
foreach ($parentFks as $fk) { |
|
|
|
|
60
|
|
|
$joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)', |
61
|
|
|
$connection->quoteIdentifier($fk->getForeignTableName()), |
62
|
|
|
$connection->quoteIdentifier($fk->getLocalTableName()), |
63
|
|
|
$connection->quoteIdentifier($fk->getLocalColumns()[0]), |
64
|
|
|
$connection->quoteIdentifier($fk->getForeignTableName()), |
65
|
|
|
$connection->quoteIdentifier($fk->getForeignColumns()[0]) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable); |
70
|
|
View Code Duplication |
foreach ($childrenFks as $fk) { |
|
|
|
|
71
|
|
|
$joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)', |
72
|
|
|
$connection->quoteIdentifier($fk->getLocalTableName()), |
73
|
|
|
$connection->quoteIdentifier($fk->getForeignTableName()), |
74
|
|
|
$connection->quoteIdentifier($fk->getForeignColumns()[0]), |
75
|
|
|
$connection->quoteIdentifier($fk->getLocalTableName()), |
76
|
|
|
$connection->quoteIdentifier($fk->getLocalColumns()[0]) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$sql .= $joinSql; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
if (!empty($this->filterString)) { |
|
|
|
|
84
|
|
|
$sql .= ' WHERE '.$this->filterString; |
85
|
|
|
$countSql .= ' WHERE '.$this->filterString; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!empty($orderString)) { |
89
|
|
|
$sql .= ' ORDER BY '.$orderString; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (stripos($countSql, 'GROUP BY') !== false) { |
93
|
|
|
throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->magicSql = $sql; |
97
|
|
|
$this->magicSqlCount = $countSql; |
98
|
|
|
$this->columnDescList = $columnDescList; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $tableName |
103
|
|
|
* |
104
|
|
|
* @return ForeignKeyConstraint[] |
105
|
|
|
*/ |
106
|
|
|
private function getParentRelationshipForeignKeys($tableName) |
107
|
|
|
{ |
108
|
|
|
return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { |
109
|
|
|
return $this->getParentRelationshipForeignKeysWithoutCache($tableName); |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $tableName |
115
|
|
|
* |
116
|
|
|
* @return ForeignKeyConstraint[] |
117
|
|
|
*/ |
118
|
|
|
private function getParentRelationshipForeignKeysWithoutCache($tableName) |
119
|
|
|
{ |
120
|
|
|
$parentFks = []; |
121
|
|
|
$currentTable = $tableName; |
122
|
|
|
while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) { |
|
|
|
|
123
|
|
|
$currentTable = $currentFk->getForeignTableName(); |
124
|
|
|
$parentFks[] = $currentFk; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $parentFks; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $tableName |
132
|
|
|
* |
133
|
|
|
* @return ForeignKeyConstraint[] |
134
|
|
|
*/ |
135
|
|
|
private function getChildrenRelationshipForeignKeys(string $tableName) : array |
136
|
|
|
{ |
137
|
|
|
return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { |
138
|
|
|
return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $tableName |
144
|
|
|
* |
145
|
|
|
* @return ForeignKeyConstraint[] |
146
|
|
|
*/ |
147
|
|
|
private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array |
148
|
|
|
{ |
149
|
|
|
$children = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
|
|
|
150
|
|
|
|
151
|
|
|
if (!empty($children)) { |
152
|
|
|
$fksTables = array_map(function (ForeignKeyConstraint $fk) { |
153
|
|
|
return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName()); |
154
|
|
|
}, $children); |
155
|
|
|
|
156
|
|
|
$fks = array_merge($children, call_user_func_array('array_merge', $fksTables)); |
157
|
|
|
|
158
|
|
|
return $fks; |
159
|
|
|
} else { |
160
|
|
|
return []; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns an item from cache or computes it using $closure and puts it in cache. |
166
|
|
|
* |
167
|
|
|
* @param string $key |
168
|
|
|
* @param callable $closure |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
protected function fromCache(string $key, callable $closure) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$item = $this->cache->fetch($key); |
175
|
|
|
if ($item === false) { |
176
|
|
|
$item = $closure(); |
177
|
|
|
$this->cache->save($key, $item); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $item; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.