@@ -15,168 +15,168 @@ |
||
15 | 15 | */ |
16 | 16 | class FindObjectsFromSqlQueryFactory extends AbstractQueryFactory |
17 | 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 | - $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 | - 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 | - 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 | - 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 | - 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 | - } |
|
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 | + $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 | + 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 | + 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 | + 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 | + 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 | 182 | } |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | |
47 | 47 | // Let's compute the COUNT. |
48 | 48 | $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
49 | - $pkColumnNames = array_map(function ($pkColumn) { |
|
49 | + $pkColumnNames = array_map(function($pkColumn) { |
|
50 | 50 | return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
51 | 51 | }, $pkColumnNames); |
52 | 52 | |
53 | 53 | $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; |
54 | 54 | |
55 | 55 | // Add joins on inherited tables if necessary |
56 | - if (count($allFetchedTables) > 1) { |
|
56 | + if (count($allFetchedTables)>1) { |
|
57 | 57 | $joinSql = ''; |
58 | 58 | $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable); |
59 | 59 | foreach ($parentFks as $fk) { |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | private function getParentRelationshipForeignKeys($tableName) |
107 | 107 | { |
108 | - return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { |
|
108 | + return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function() use ($tableName) { |
|
109 | 109 | return $this->getParentRelationshipForeignKeysWithoutCache($tableName); |
110 | 110 | }); |
111 | 111 | } |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | */ |
135 | 135 | private function getChildrenRelationshipForeignKeys(string $tableName) : array |
136 | 136 | { |
137 | - return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { |
|
137 | + return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function() use ($tableName) { |
|
138 | 138 | return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName); |
139 | 139 | }); |
140 | 140 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | $children = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
150 | 150 | |
151 | 151 | if (!empty($children)) { |
152 | - $fksTables = array_map(function (ForeignKeyConstraint $fk) { |
|
152 | + $fksTables = array_map(function(ForeignKeyConstraint $fk) { |
|
153 | 153 | return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName()); |
154 | 154 | }, $children); |
155 | 155 |