@@ -14,138 +14,138 @@ |
||
14 | 14 | */ |
15 | 15 | class TDBMSchemaAnalyzer |
16 | 16 | { |
17 | - private $connection; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var Schema |
|
21 | - */ |
|
22 | - private $schema; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - private $cachePrefix; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var Cache |
|
31 | - */ |
|
32 | - private $cache; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var SchemaAnalyzer |
|
36 | - */ |
|
37 | - private $schemaAnalyzer; |
|
38 | - |
|
39 | - /** |
|
40 | - * @param Connection $connection The DBAL DB connection to use |
|
41 | - * @param Cache $cache A cache service to be used |
|
42 | - * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
|
43 | - * Will be automatically created if not passed |
|
44 | - */ |
|
45 | - public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer) |
|
46 | - { |
|
47 | - $this->connection = $connection; |
|
48 | - $this->cache = $cache; |
|
49 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Returns a unique ID for the current connection. Useful for namespacing cache entries in the current connection. |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function getCachePrefix() |
|
58 | - { |
|
59 | - if ($this->cachePrefix === null) { |
|
60 | - $this->cachePrefix = hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName()); |
|
61 | - } |
|
62 | - |
|
63 | - return $this->cachePrefix; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Returns the (cached) schema. |
|
68 | - * |
|
69 | - * @return Schema |
|
70 | - */ |
|
71 | - public function getSchema() |
|
72 | - { |
|
73 | - if ($this->schema === null) { |
|
74 | - $cacheKey = $this->getCachePrefix().'_schema'; |
|
75 | - if ($this->cache->contains($cacheKey)) { |
|
76 | - $this->schema = $this->cache->fetch($cacheKey); |
|
77 | - } else { |
|
78 | - $this->schema = $this->connection->getSchemaManager()->createSchema(); |
|
79 | - $this->cache->save($cacheKey, $this->schema); |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - return $this->schema; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns the list of pivot tables linked to table $tableName. |
|
88 | - * |
|
89 | - * @param string $tableName |
|
90 | - * |
|
91 | - * @return array|string[] |
|
92 | - */ |
|
93 | - public function getPivotTableLinkedToTable($tableName) |
|
94 | - { |
|
95 | - $cacheKey = $this->getCachePrefix().'_pivottables_link_'.$tableName; |
|
96 | - if ($this->cache->contains($cacheKey)) { |
|
97 | - return $this->cache->fetch($cacheKey); |
|
98 | - } |
|
99 | - |
|
100 | - $pivotTables = []; |
|
101 | - |
|
102 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
103 | - foreach ($junctionTables as $table) { |
|
104 | - $fks = $table->getForeignKeys(); |
|
105 | - foreach ($fks as $fk) { |
|
106 | - if ($fk->getForeignTableName() == $tableName) { |
|
107 | - $pivotTables[] = $table->getName(); |
|
108 | - break; |
|
109 | - } |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - $this->cache->save($cacheKey, $pivotTables); |
|
114 | - |
|
115 | - return $pivotTables; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Returns the list of foreign keys pointing to the table represented by this bean, excluding foreign keys |
|
120 | - * from junction tables and from inheritance. |
|
121 | - * |
|
122 | - * @return ForeignKeyConstraint[] |
|
123 | - */ |
|
124 | - public function getIncomingForeignKeys($tableName) |
|
125 | - { |
|
126 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
127 | - $junctionTableNames = array_map(function (Table $table) { |
|
128 | - return $table->getName(); |
|
129 | - }, $junctionTables); |
|
130 | - $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
131 | - |
|
132 | - $fks = []; |
|
133 | - foreach ($this->getSchema()->getTables() as $table) { |
|
134 | - foreach ($table->getForeignKeys() as $fk) { |
|
135 | - if ($fk->getForeignTableName() === $tableName) { |
|
136 | - if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
137 | - continue; |
|
138 | - } |
|
139 | - foreach ($childrenRelationships as $childFk) { |
|
140 | - if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
141 | - continue 2; |
|
142 | - } |
|
143 | - } |
|
144 | - $fks[] = $fk; |
|
145 | - } |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - return $fks; |
|
150 | - } |
|
17 | + private $connection; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var Schema |
|
21 | + */ |
|
22 | + private $schema; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + private $cachePrefix; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var Cache |
|
31 | + */ |
|
32 | + private $cache; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var SchemaAnalyzer |
|
36 | + */ |
|
37 | + private $schemaAnalyzer; |
|
38 | + |
|
39 | + /** |
|
40 | + * @param Connection $connection The DBAL DB connection to use |
|
41 | + * @param Cache $cache A cache service to be used |
|
42 | + * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
|
43 | + * Will be automatically created if not passed |
|
44 | + */ |
|
45 | + public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer) |
|
46 | + { |
|
47 | + $this->connection = $connection; |
|
48 | + $this->cache = $cache; |
|
49 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Returns a unique ID for the current connection. Useful for namespacing cache entries in the current connection. |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function getCachePrefix() |
|
58 | + { |
|
59 | + if ($this->cachePrefix === null) { |
|
60 | + $this->cachePrefix = hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName()); |
|
61 | + } |
|
62 | + |
|
63 | + return $this->cachePrefix; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Returns the (cached) schema. |
|
68 | + * |
|
69 | + * @return Schema |
|
70 | + */ |
|
71 | + public function getSchema() |
|
72 | + { |
|
73 | + if ($this->schema === null) { |
|
74 | + $cacheKey = $this->getCachePrefix().'_schema'; |
|
75 | + if ($this->cache->contains($cacheKey)) { |
|
76 | + $this->schema = $this->cache->fetch($cacheKey); |
|
77 | + } else { |
|
78 | + $this->schema = $this->connection->getSchemaManager()->createSchema(); |
|
79 | + $this->cache->save($cacheKey, $this->schema); |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + return $this->schema; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns the list of pivot tables linked to table $tableName. |
|
88 | + * |
|
89 | + * @param string $tableName |
|
90 | + * |
|
91 | + * @return array|string[] |
|
92 | + */ |
|
93 | + public function getPivotTableLinkedToTable($tableName) |
|
94 | + { |
|
95 | + $cacheKey = $this->getCachePrefix().'_pivottables_link_'.$tableName; |
|
96 | + if ($this->cache->contains($cacheKey)) { |
|
97 | + return $this->cache->fetch($cacheKey); |
|
98 | + } |
|
99 | + |
|
100 | + $pivotTables = []; |
|
101 | + |
|
102 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
103 | + foreach ($junctionTables as $table) { |
|
104 | + $fks = $table->getForeignKeys(); |
|
105 | + foreach ($fks as $fk) { |
|
106 | + if ($fk->getForeignTableName() == $tableName) { |
|
107 | + $pivotTables[] = $table->getName(); |
|
108 | + break; |
|
109 | + } |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + $this->cache->save($cacheKey, $pivotTables); |
|
114 | + |
|
115 | + return $pivotTables; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Returns the list of foreign keys pointing to the table represented by this bean, excluding foreign keys |
|
120 | + * from junction tables and from inheritance. |
|
121 | + * |
|
122 | + * @return ForeignKeyConstraint[] |
|
123 | + */ |
|
124 | + public function getIncomingForeignKeys($tableName) |
|
125 | + { |
|
126 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
127 | + $junctionTableNames = array_map(function (Table $table) { |
|
128 | + return $table->getName(); |
|
129 | + }, $junctionTables); |
|
130 | + $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
131 | + |
|
132 | + $fks = []; |
|
133 | + foreach ($this->getSchema()->getTables() as $table) { |
|
134 | + foreach ($table->getForeignKeys() as $fk) { |
|
135 | + if ($fk->getForeignTableName() === $tableName) { |
|
136 | + if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
137 | + continue; |
|
138 | + } |
|
139 | + foreach ($childrenRelationships as $childFk) { |
|
140 | + if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
141 | + continue 2; |
|
142 | + } |
|
143 | + } |
|
144 | + $fks[] = $fk; |
|
145 | + } |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + return $fks; |
|
150 | + } |
|
151 | 151 | } |
@@ -124,7 +124,7 @@ |
||
124 | 124 | public function getIncomingForeignKeys($tableName) |
125 | 125 | { |
126 | 126 | $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
127 | - $junctionTableNames = array_map(function (Table $table) { |
|
127 | + $junctionTableNames = array_map(function(Table $table) { |
|
128 | 128 | return $table->getName(); |
129 | 129 | }, $junctionTables); |
130 | 130 | $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
@@ -33,42 +33,42 @@ |
||
33 | 33 | */ |
34 | 34 | class TDBMObject extends AbstractTDBMObject |
35 | 35 | { |
36 | - public function getProperty($var, $tableName = null) |
|
37 | - { |
|
38 | - return $this->get($var, $tableName); |
|
39 | - } |
|
36 | + public function getProperty($var, $tableName = null) |
|
37 | + { |
|
38 | + return $this->get($var, $tableName); |
|
39 | + } |
|
40 | 40 | |
41 | - public function setProperty($var, $value, $tableName = null) |
|
42 | - { |
|
43 | - $this->set($var, $value, $tableName); |
|
44 | - } |
|
41 | + public function setProperty($var, $value, $tableName = null) |
|
42 | + { |
|
43 | + $this->set($var, $value, $tableName); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Specify data which should be serialized to JSON. |
|
48 | - * |
|
49 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
50 | - * |
|
51 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
52 | - * which is a value of any type other than a resource |
|
53 | - * |
|
54 | - * @since 5.4.0 |
|
55 | - */ |
|
56 | - public function jsonSerialize() |
|
57 | - { |
|
58 | - throw new TDBMException('Json serialization is only implemented for generated beans.'); |
|
59 | - } |
|
46 | + /** |
|
47 | + * Specify data which should be serialized to JSON. |
|
48 | + * |
|
49 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
50 | + * |
|
51 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
52 | + * which is a value of any type other than a resource |
|
53 | + * |
|
54 | + * @since 5.4.0 |
|
55 | + */ |
|
56 | + public function jsonSerialize() |
|
57 | + { |
|
58 | + throw new TDBMException('Json serialization is only implemented for generated beans.'); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Returns an array of used tables by this bean (from parent to child relationship). |
|
63 | - * |
|
64 | - * @return string[] |
|
65 | - */ |
|
66 | - protected function getUsedTables() |
|
67 | - { |
|
68 | - $tableNames = array_keys($this->dbRows); |
|
69 | - $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
70 | - $tableNames = array_reverse($tableNames); |
|
61 | + /** |
|
62 | + * Returns an array of used tables by this bean (from parent to child relationship). |
|
63 | + * |
|
64 | + * @return string[] |
|
65 | + */ |
|
66 | + protected function getUsedTables() |
|
67 | + { |
|
68 | + $tableNames = array_keys($this->dbRows); |
|
69 | + $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
70 | + $tableNames = array_reverse($tableNames); |
|
71 | 71 | |
72 | - return $tableNames; |
|
73 | - } |
|
72 | + return $tableNames; |
|
73 | + } |
|
74 | 74 | } |
@@ -29,267 +29,267 @@ |
||
29 | 29 | */ |
30 | 30 | class InnerResultIterator implements \Iterator, \Countable, \ArrayAccess |
31 | 31 | { |
32 | - /** |
|
33 | - * @var Statement |
|
34 | - */ |
|
35 | - protected $statement; |
|
36 | - |
|
37 | - protected $fetchStarted = false; |
|
38 | - private $objectStorage; |
|
39 | - private $className; |
|
40 | - |
|
41 | - private $tdbmService; |
|
42 | - private $magicSql; |
|
43 | - private $parameters; |
|
44 | - private $limit; |
|
45 | - private $offset; |
|
46 | - private $columnDescriptors; |
|
47 | - private $magicQuery; |
|
48 | - |
|
49 | - /** |
|
50 | - * The key of the current retrieved object. |
|
51 | - * |
|
52 | - * @var int |
|
53 | - */ |
|
54 | - protected $key = -1; |
|
55 | - |
|
56 | - protected $current = null; |
|
57 | - |
|
58 | - private $databasePlatform; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var LoggerInterface |
|
62 | - */ |
|
63 | - private $logger; |
|
64 | - |
|
65 | - public function __construct($magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, LoggerInterface $logger) |
|
66 | - { |
|
67 | - $this->magicSql = $magicSql; |
|
68 | - $this->objectStorage = $objectStorage; |
|
69 | - $this->className = $className; |
|
70 | - $this->tdbmService = $tdbmService; |
|
71 | - $this->parameters = $parameters; |
|
72 | - $this->limit = $limit; |
|
73 | - $this->offset = $offset; |
|
74 | - $this->columnDescriptors = $columnDescriptors; |
|
75 | - $this->magicQuery = $magicQuery; |
|
76 | - $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
77 | - $this->logger = $logger; |
|
78 | - } |
|
79 | - |
|
80 | - protected function executeQuery() |
|
81 | - { |
|
82 | - $sql = $this->magicQuery->build($this->magicSql, $this->parameters); |
|
83 | - $sql = $this->tdbmService->getConnection()->getDatabasePlatform()->modifyLimitQuery($sql, $this->limit, $this->offset); |
|
84 | - |
|
85 | - $this->logger->debug('Running SQL request: '.$sql); |
|
86 | - |
|
87 | - $this->statement = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters); |
|
88 | - |
|
89 | - $this->fetchStarted = true; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
|
94 | - * |
|
95 | - * @return int |
|
96 | - */ |
|
97 | - public function count() |
|
98 | - { |
|
99 | - if (!$this->fetchStarted) { |
|
100 | - $this->executeQuery(); |
|
101 | - } |
|
102 | - |
|
103 | - return $this->statement->rowCount(); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Fetches record at current cursor. |
|
108 | - * |
|
109 | - * @return AbstractTDBMObject|null |
|
110 | - */ |
|
111 | - public function current() |
|
112 | - { |
|
113 | - return $this->current; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns the current result's key. |
|
118 | - * |
|
119 | - * @return int |
|
120 | - */ |
|
121 | - public function key() |
|
122 | - { |
|
123 | - return $this->key; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Advances the cursor to the next result. |
|
128 | - * Casts the database result into one (or several) beans. |
|
129 | - */ |
|
130 | - public function next() |
|
131 | - { |
|
132 | - $row = $this->statement->fetch(\PDO::FETCH_NUM); |
|
133 | - if ($row) { |
|
134 | - |
|
135 | - // array<tablegroup, array<table, array<column, value>>> |
|
136 | - $beansData = []; |
|
137 | - foreach ($row as $i => $value) { |
|
138 | - $columnDescriptor = $this->columnDescriptors[$i]; |
|
139 | - |
|
140 | - if ($columnDescriptor['tableGroup'] === null) { |
|
141 | - // A column can have no tableGroup (if it comes from an ORDER BY expression) |
|
142 | - continue; |
|
143 | - } |
|
144 | - |
|
145 | - // Let's cast the value according to its type |
|
146 | - $value = $columnDescriptor['type']->convertToPHPValue($value, $this->databasePlatform); |
|
147 | - |
|
148 | - $beansData[$columnDescriptor['tableGroup']][$columnDescriptor['table']][$columnDescriptor['column']] = $value; |
|
149 | - } |
|
150 | - |
|
151 | - $reflectionClassCache = []; |
|
152 | - $firstBean = true; |
|
153 | - foreach ($beansData as $beanData) { |
|
154 | - |
|
155 | - // Let's find the bean class name associated to the bean. |
|
156 | - |
|
157 | - list($actualClassName, $mainBeanTableName, $tablesUsed) = $this->tdbmService->_getClassNameFromBeanData($beanData); |
|
158 | - |
|
159 | - if ($this->className !== null) { |
|
160 | - $actualClassName = $this->className; |
|
161 | - } |
|
162 | - |
|
163 | - // Let's filter out the beanData that is not used (because it belongs to a part of the hierarchy that is not fetched: |
|
164 | - foreach ($beanData as $tableName => $descriptors) { |
|
165 | - if (!in_array($tableName, $tablesUsed)) { |
|
166 | - unset($beanData[$tableName]); |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - // Must we create the bean? Let's see in the cache if we have a mapping DbRow? |
|
171 | - // Let's get the first object mapping a row: |
|
172 | - // We do this loop only for the first table |
|
173 | - |
|
174 | - $primaryKeys = $this->tdbmService->_getPrimaryKeysFromObjectData($mainBeanTableName, $beanData[$mainBeanTableName]); |
|
175 | - $hash = $this->tdbmService->getObjectHash($primaryKeys); |
|
176 | - |
|
177 | - if ($this->objectStorage->has($mainBeanTableName, $hash)) { |
|
178 | - $dbRow = $this->objectStorage->get($mainBeanTableName, $hash); |
|
179 | - $bean = $dbRow->getTDBMObject(); |
|
180 | - } else { |
|
181 | - // Let's construct the bean |
|
182 | - if (!isset($reflectionClassCache[$actualClassName])) { |
|
183 | - $reflectionClassCache[$actualClassName] = new \ReflectionClass($actualClassName); |
|
184 | - } |
|
185 | - // Let's bypass the constructor when creating the bean! |
|
186 | - $bean = $reflectionClassCache[$actualClassName]->newInstanceWithoutConstructor(); |
|
187 | - $bean->_constructFromData($beanData, $this->tdbmService); |
|
188 | - } |
|
189 | - |
|
190 | - // The first bean is the one containing the main table. |
|
191 | - if ($firstBean) { |
|
192 | - $firstBean = false; |
|
193 | - $this->current = $bean; |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - ++$this->key; |
|
198 | - } else { |
|
199 | - $this->current = null; |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Moves the cursor to the beginning of the result set. |
|
205 | - */ |
|
206 | - public function rewind() |
|
207 | - { |
|
208 | - $this->executeQuery(); |
|
209 | - $this->key = -1; |
|
210 | - $this->next(); |
|
211 | - } |
|
212 | - /** |
|
213 | - * Checks if the cursor is reading a valid result. |
|
214 | - * |
|
215 | - * @return bool |
|
216 | - */ |
|
217 | - public function valid() |
|
218 | - { |
|
219 | - return $this->current !== null; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Whether a offset exists. |
|
224 | - * |
|
225 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
226 | - * |
|
227 | - * @param mixed $offset <p> |
|
228 | - * An offset to check for. |
|
229 | - * </p> |
|
230 | - * |
|
231 | - * @return bool true on success or false on failure. |
|
232 | - * </p> |
|
233 | - * <p> |
|
234 | - * The return value will be casted to boolean if non-boolean was returned |
|
235 | - * |
|
236 | - * @since 5.0.0 |
|
237 | - */ |
|
238 | - public function offsetExists($offset) |
|
239 | - { |
|
240 | - throw new TDBMInvalidOperationException('You cannot access this result set via index because it was fetched in CURSOR mode. Use ARRAY_MODE instead.'); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Offset to retrieve. |
|
245 | - * |
|
246 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
247 | - * |
|
248 | - * @param mixed $offset <p> |
|
249 | - * The offset to retrieve. |
|
250 | - * </p> |
|
251 | - * |
|
252 | - * @return mixed Can return all value types |
|
253 | - * |
|
254 | - * @since 5.0.0 |
|
255 | - */ |
|
256 | - public function offsetGet($offset) |
|
257 | - { |
|
258 | - throw new TDBMInvalidOperationException('You cannot access this result set via index because it was fetched in CURSOR mode. Use ARRAY_MODE instead.'); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Offset to set. |
|
263 | - * |
|
264 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
265 | - * |
|
266 | - * @param mixed $offset <p> |
|
267 | - * The offset to assign the value to. |
|
268 | - * </p> |
|
269 | - * @param mixed $value <p> |
|
270 | - * The value to set. |
|
271 | - * </p> |
|
272 | - * |
|
273 | - * @since 5.0.0 |
|
274 | - */ |
|
275 | - public function offsetSet($offset, $value) |
|
276 | - { |
|
277 | - throw new TDBMInvalidOperationException('You can set values in a TDBM result set.'); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Offset to unset. |
|
282 | - * |
|
283 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
284 | - * |
|
285 | - * @param mixed $offset <p> |
|
286 | - * The offset to unset. |
|
287 | - * </p> |
|
288 | - * |
|
289 | - * @since 5.0.0 |
|
290 | - */ |
|
291 | - public function offsetUnset($offset) |
|
292 | - { |
|
293 | - throw new TDBMInvalidOperationException('You can unset values in a TDBM result set.'); |
|
294 | - } |
|
32 | + /** |
|
33 | + * @var Statement |
|
34 | + */ |
|
35 | + protected $statement; |
|
36 | + |
|
37 | + protected $fetchStarted = false; |
|
38 | + private $objectStorage; |
|
39 | + private $className; |
|
40 | + |
|
41 | + private $tdbmService; |
|
42 | + private $magicSql; |
|
43 | + private $parameters; |
|
44 | + private $limit; |
|
45 | + private $offset; |
|
46 | + private $columnDescriptors; |
|
47 | + private $magicQuery; |
|
48 | + |
|
49 | + /** |
|
50 | + * The key of the current retrieved object. |
|
51 | + * |
|
52 | + * @var int |
|
53 | + */ |
|
54 | + protected $key = -1; |
|
55 | + |
|
56 | + protected $current = null; |
|
57 | + |
|
58 | + private $databasePlatform; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var LoggerInterface |
|
62 | + */ |
|
63 | + private $logger; |
|
64 | + |
|
65 | + public function __construct($magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, LoggerInterface $logger) |
|
66 | + { |
|
67 | + $this->magicSql = $magicSql; |
|
68 | + $this->objectStorage = $objectStorage; |
|
69 | + $this->className = $className; |
|
70 | + $this->tdbmService = $tdbmService; |
|
71 | + $this->parameters = $parameters; |
|
72 | + $this->limit = $limit; |
|
73 | + $this->offset = $offset; |
|
74 | + $this->columnDescriptors = $columnDescriptors; |
|
75 | + $this->magicQuery = $magicQuery; |
|
76 | + $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
77 | + $this->logger = $logger; |
|
78 | + } |
|
79 | + |
|
80 | + protected function executeQuery() |
|
81 | + { |
|
82 | + $sql = $this->magicQuery->build($this->magicSql, $this->parameters); |
|
83 | + $sql = $this->tdbmService->getConnection()->getDatabasePlatform()->modifyLimitQuery($sql, $this->limit, $this->offset); |
|
84 | + |
|
85 | + $this->logger->debug('Running SQL request: '.$sql); |
|
86 | + |
|
87 | + $this->statement = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters); |
|
88 | + |
|
89 | + $this->fetchStarted = true; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
|
94 | + * |
|
95 | + * @return int |
|
96 | + */ |
|
97 | + public function count() |
|
98 | + { |
|
99 | + if (!$this->fetchStarted) { |
|
100 | + $this->executeQuery(); |
|
101 | + } |
|
102 | + |
|
103 | + return $this->statement->rowCount(); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Fetches record at current cursor. |
|
108 | + * |
|
109 | + * @return AbstractTDBMObject|null |
|
110 | + */ |
|
111 | + public function current() |
|
112 | + { |
|
113 | + return $this->current; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns the current result's key. |
|
118 | + * |
|
119 | + * @return int |
|
120 | + */ |
|
121 | + public function key() |
|
122 | + { |
|
123 | + return $this->key; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Advances the cursor to the next result. |
|
128 | + * Casts the database result into one (or several) beans. |
|
129 | + */ |
|
130 | + public function next() |
|
131 | + { |
|
132 | + $row = $this->statement->fetch(\PDO::FETCH_NUM); |
|
133 | + if ($row) { |
|
134 | + |
|
135 | + // array<tablegroup, array<table, array<column, value>>> |
|
136 | + $beansData = []; |
|
137 | + foreach ($row as $i => $value) { |
|
138 | + $columnDescriptor = $this->columnDescriptors[$i]; |
|
139 | + |
|
140 | + if ($columnDescriptor['tableGroup'] === null) { |
|
141 | + // A column can have no tableGroup (if it comes from an ORDER BY expression) |
|
142 | + continue; |
|
143 | + } |
|
144 | + |
|
145 | + // Let's cast the value according to its type |
|
146 | + $value = $columnDescriptor['type']->convertToPHPValue($value, $this->databasePlatform); |
|
147 | + |
|
148 | + $beansData[$columnDescriptor['tableGroup']][$columnDescriptor['table']][$columnDescriptor['column']] = $value; |
|
149 | + } |
|
150 | + |
|
151 | + $reflectionClassCache = []; |
|
152 | + $firstBean = true; |
|
153 | + foreach ($beansData as $beanData) { |
|
154 | + |
|
155 | + // Let's find the bean class name associated to the bean. |
|
156 | + |
|
157 | + list($actualClassName, $mainBeanTableName, $tablesUsed) = $this->tdbmService->_getClassNameFromBeanData($beanData); |
|
158 | + |
|
159 | + if ($this->className !== null) { |
|
160 | + $actualClassName = $this->className; |
|
161 | + } |
|
162 | + |
|
163 | + // Let's filter out the beanData that is not used (because it belongs to a part of the hierarchy that is not fetched: |
|
164 | + foreach ($beanData as $tableName => $descriptors) { |
|
165 | + if (!in_array($tableName, $tablesUsed)) { |
|
166 | + unset($beanData[$tableName]); |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + // Must we create the bean? Let's see in the cache if we have a mapping DbRow? |
|
171 | + // Let's get the first object mapping a row: |
|
172 | + // We do this loop only for the first table |
|
173 | + |
|
174 | + $primaryKeys = $this->tdbmService->_getPrimaryKeysFromObjectData($mainBeanTableName, $beanData[$mainBeanTableName]); |
|
175 | + $hash = $this->tdbmService->getObjectHash($primaryKeys); |
|
176 | + |
|
177 | + if ($this->objectStorage->has($mainBeanTableName, $hash)) { |
|
178 | + $dbRow = $this->objectStorage->get($mainBeanTableName, $hash); |
|
179 | + $bean = $dbRow->getTDBMObject(); |
|
180 | + } else { |
|
181 | + // Let's construct the bean |
|
182 | + if (!isset($reflectionClassCache[$actualClassName])) { |
|
183 | + $reflectionClassCache[$actualClassName] = new \ReflectionClass($actualClassName); |
|
184 | + } |
|
185 | + // Let's bypass the constructor when creating the bean! |
|
186 | + $bean = $reflectionClassCache[$actualClassName]->newInstanceWithoutConstructor(); |
|
187 | + $bean->_constructFromData($beanData, $this->tdbmService); |
|
188 | + } |
|
189 | + |
|
190 | + // The first bean is the one containing the main table. |
|
191 | + if ($firstBean) { |
|
192 | + $firstBean = false; |
|
193 | + $this->current = $bean; |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + ++$this->key; |
|
198 | + } else { |
|
199 | + $this->current = null; |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Moves the cursor to the beginning of the result set. |
|
205 | + */ |
|
206 | + public function rewind() |
|
207 | + { |
|
208 | + $this->executeQuery(); |
|
209 | + $this->key = -1; |
|
210 | + $this->next(); |
|
211 | + } |
|
212 | + /** |
|
213 | + * Checks if the cursor is reading a valid result. |
|
214 | + * |
|
215 | + * @return bool |
|
216 | + */ |
|
217 | + public function valid() |
|
218 | + { |
|
219 | + return $this->current !== null; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Whether a offset exists. |
|
224 | + * |
|
225 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
226 | + * |
|
227 | + * @param mixed $offset <p> |
|
228 | + * An offset to check for. |
|
229 | + * </p> |
|
230 | + * |
|
231 | + * @return bool true on success or false on failure. |
|
232 | + * </p> |
|
233 | + * <p> |
|
234 | + * The return value will be casted to boolean if non-boolean was returned |
|
235 | + * |
|
236 | + * @since 5.0.0 |
|
237 | + */ |
|
238 | + public function offsetExists($offset) |
|
239 | + { |
|
240 | + throw new TDBMInvalidOperationException('You cannot access this result set via index because it was fetched in CURSOR mode. Use ARRAY_MODE instead.'); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Offset to retrieve. |
|
245 | + * |
|
246 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
247 | + * |
|
248 | + * @param mixed $offset <p> |
|
249 | + * The offset to retrieve. |
|
250 | + * </p> |
|
251 | + * |
|
252 | + * @return mixed Can return all value types |
|
253 | + * |
|
254 | + * @since 5.0.0 |
|
255 | + */ |
|
256 | + public function offsetGet($offset) |
|
257 | + { |
|
258 | + throw new TDBMInvalidOperationException('You cannot access this result set via index because it was fetched in CURSOR mode. Use ARRAY_MODE instead.'); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Offset to set. |
|
263 | + * |
|
264 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
265 | + * |
|
266 | + * @param mixed $offset <p> |
|
267 | + * The offset to assign the value to. |
|
268 | + * </p> |
|
269 | + * @param mixed $value <p> |
|
270 | + * The value to set. |
|
271 | + * </p> |
|
272 | + * |
|
273 | + * @since 5.0.0 |
|
274 | + */ |
|
275 | + public function offsetSet($offset, $value) |
|
276 | + { |
|
277 | + throw new TDBMInvalidOperationException('You can set values in a TDBM result set.'); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Offset to unset. |
|
282 | + * |
|
283 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
284 | + * |
|
285 | + * @param mixed $offset <p> |
|
286 | + * The offset to unset. |
|
287 | + * </p> |
|
288 | + * |
|
289 | + * @since 5.0.0 |
|
290 | + */ |
|
291 | + public function offsetUnset($offset) |
|
292 | + { |
|
293 | + throw new TDBMInvalidOperationException('You can unset values in a TDBM result set.'); |
|
294 | + } |
|
295 | 295 | } |
@@ -12,122 +12,122 @@ |
||
12 | 12 | */ |
13 | 13 | class OrderByAnalyzer |
14 | 14 | { |
15 | - /** |
|
16 | - * The content of the cache variable. |
|
17 | - * |
|
18 | - * @var Cache |
|
19 | - */ |
|
20 | - private $cache; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $cachePrefix; |
|
26 | - |
|
27 | - /** |
|
28 | - * OrderByAnalyzer constructor. |
|
29 | - * |
|
30 | - * @param Cache $cache |
|
31 | - * @param string|null $cachePrefix |
|
32 | - */ |
|
33 | - public function __construct(Cache $cache, $cachePrefix = null) |
|
34 | - { |
|
35 | - $this->cache = $cache; |
|
36 | - $this->cachePrefix = $cachePrefix; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Returns an array for each sorted "column" in the form:. |
|
41 | - * |
|
42 | - * [ |
|
43 | - * [ |
|
44 | - * 'type' => 'colref', |
|
45 | - * 'table' => null, |
|
46 | - * 'column' => 'a', |
|
47 | - * 'direction' => 'ASC' |
|
48 | - * ], |
|
49 | - * [ |
|
50 | - * 'type' => 'expr', |
|
51 | - * 'expr' => 'RAND()', |
|
52 | - * 'direction' => 'DESC' |
|
53 | - * ] |
|
54 | - * ] |
|
55 | - * |
|
56 | - * @param string $orderBy |
|
57 | - * |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function analyzeOrderBy(string $orderBy) : array |
|
61 | - { |
|
62 | - $key = $this->cachePrefix.'_order_by_analysis_'.$orderBy; |
|
63 | - $results = $this->cache->fetch($key); |
|
64 | - if ($results !== false) { |
|
65 | - return $results; |
|
66 | - } |
|
67 | - $results = $this->analyzeOrderByNoCache($orderBy); |
|
68 | - $this->cache->save($key, $results); |
|
69 | - |
|
70 | - return $results; |
|
71 | - } |
|
72 | - |
|
73 | - private function analyzeOrderByNoCache(string $orderBy) : array |
|
74 | - { |
|
75 | - $sqlParser = new PHPSQLParser(); |
|
76 | - $sql = 'SELECT 1 FROM a ORDER BY '.$orderBy; |
|
77 | - $parsed = $sqlParser->parse($sql, true); |
|
78 | - |
|
79 | - $results = []; |
|
80 | - |
|
81 | - for ($i = 0, $count = count($parsed['ORDER']); $i < $count; ++$i) { |
|
82 | - $orderItem = $parsed['ORDER'][$i]; |
|
83 | - if ($orderItem['expr_type'] === 'colref') { |
|
84 | - $parts = $orderItem['no_quotes']['parts']; |
|
85 | - $columnName = array_pop($parts); |
|
86 | - if (!empty($parts)) { |
|
87 | - $tableName = array_pop($parts); |
|
88 | - } else { |
|
89 | - $tableName = null; |
|
90 | - } |
|
91 | - |
|
92 | - $results[] = [ |
|
93 | - 'type' => 'colref', |
|
94 | - 'table' => $tableName, |
|
95 | - 'column' => $columnName, |
|
96 | - 'direction' => $orderItem['direction'], |
|
97 | - ]; |
|
98 | - } else { |
|
99 | - $position = $orderItem['position']; |
|
100 | - if ($i + 1 < $count) { |
|
101 | - $nextPosition = $parsed['ORDER'][$i + 1]['position']; |
|
102 | - $str = substr($sql, $position, $nextPosition - $position); |
|
103 | - } else { |
|
104 | - $str = substr($sql, $position); |
|
105 | - } |
|
106 | - |
|
107 | - $str = trim($str, " \t\r\n,"); |
|
108 | - |
|
109 | - $results[] = [ |
|
110 | - 'type' => 'expr', |
|
111 | - 'expr' => $this->trimDirection($str), |
|
112 | - 'direction' => $orderItem['direction'], |
|
113 | - ]; |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - return $results; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Trims the ASC/DESC direction at the end of the string. |
|
122 | - * |
|
123 | - * @param string $sql |
|
124 | - * |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - private function trimDirection(string $sql) : string |
|
128 | - { |
|
129 | - preg_match('/^(.*)(\s+(DESC|ASC|))*$/Ui', $sql, $matches); |
|
130 | - |
|
131 | - return $matches[1]; |
|
132 | - } |
|
15 | + /** |
|
16 | + * The content of the cache variable. |
|
17 | + * |
|
18 | + * @var Cache |
|
19 | + */ |
|
20 | + private $cache; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $cachePrefix; |
|
26 | + |
|
27 | + /** |
|
28 | + * OrderByAnalyzer constructor. |
|
29 | + * |
|
30 | + * @param Cache $cache |
|
31 | + * @param string|null $cachePrefix |
|
32 | + */ |
|
33 | + public function __construct(Cache $cache, $cachePrefix = null) |
|
34 | + { |
|
35 | + $this->cache = $cache; |
|
36 | + $this->cachePrefix = $cachePrefix; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Returns an array for each sorted "column" in the form:. |
|
41 | + * |
|
42 | + * [ |
|
43 | + * [ |
|
44 | + * 'type' => 'colref', |
|
45 | + * 'table' => null, |
|
46 | + * 'column' => 'a', |
|
47 | + * 'direction' => 'ASC' |
|
48 | + * ], |
|
49 | + * [ |
|
50 | + * 'type' => 'expr', |
|
51 | + * 'expr' => 'RAND()', |
|
52 | + * 'direction' => 'DESC' |
|
53 | + * ] |
|
54 | + * ] |
|
55 | + * |
|
56 | + * @param string $orderBy |
|
57 | + * |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function analyzeOrderBy(string $orderBy) : array |
|
61 | + { |
|
62 | + $key = $this->cachePrefix.'_order_by_analysis_'.$orderBy; |
|
63 | + $results = $this->cache->fetch($key); |
|
64 | + if ($results !== false) { |
|
65 | + return $results; |
|
66 | + } |
|
67 | + $results = $this->analyzeOrderByNoCache($orderBy); |
|
68 | + $this->cache->save($key, $results); |
|
69 | + |
|
70 | + return $results; |
|
71 | + } |
|
72 | + |
|
73 | + private function analyzeOrderByNoCache(string $orderBy) : array |
|
74 | + { |
|
75 | + $sqlParser = new PHPSQLParser(); |
|
76 | + $sql = 'SELECT 1 FROM a ORDER BY '.$orderBy; |
|
77 | + $parsed = $sqlParser->parse($sql, true); |
|
78 | + |
|
79 | + $results = []; |
|
80 | + |
|
81 | + for ($i = 0, $count = count($parsed['ORDER']); $i < $count; ++$i) { |
|
82 | + $orderItem = $parsed['ORDER'][$i]; |
|
83 | + if ($orderItem['expr_type'] === 'colref') { |
|
84 | + $parts = $orderItem['no_quotes']['parts']; |
|
85 | + $columnName = array_pop($parts); |
|
86 | + if (!empty($parts)) { |
|
87 | + $tableName = array_pop($parts); |
|
88 | + } else { |
|
89 | + $tableName = null; |
|
90 | + } |
|
91 | + |
|
92 | + $results[] = [ |
|
93 | + 'type' => 'colref', |
|
94 | + 'table' => $tableName, |
|
95 | + 'column' => $columnName, |
|
96 | + 'direction' => $orderItem['direction'], |
|
97 | + ]; |
|
98 | + } else { |
|
99 | + $position = $orderItem['position']; |
|
100 | + if ($i + 1 < $count) { |
|
101 | + $nextPosition = $parsed['ORDER'][$i + 1]['position']; |
|
102 | + $str = substr($sql, $position, $nextPosition - $position); |
|
103 | + } else { |
|
104 | + $str = substr($sql, $position); |
|
105 | + } |
|
106 | + |
|
107 | + $str = trim($str, " \t\r\n,"); |
|
108 | + |
|
109 | + $results[] = [ |
|
110 | + 'type' => 'expr', |
|
111 | + 'expr' => $this->trimDirection($str), |
|
112 | + 'direction' => $orderItem['direction'], |
|
113 | + ]; |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + return $results; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Trims the ASC/DESC direction at the end of the string. |
|
122 | + * |
|
123 | + * @param string $sql |
|
124 | + * |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + private function trimDirection(string $sql) : string |
|
128 | + { |
|
129 | + preg_match('/^(.*)(\s+(DESC|ASC|))*$/Ui', $sql, $matches); |
|
130 | + |
|
131 | + return $matches[1]; |
|
132 | + } |
|
133 | 133 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | |
79 | 79 | $results = []; |
80 | 80 | |
81 | - for ($i = 0, $count = count($parsed['ORDER']); $i < $count; ++$i) { |
|
81 | + for ($i = 0, $count = count($parsed['ORDER']); $i<$count; ++$i) { |
|
82 | 82 | $orderItem = $parsed['ORDER'][$i]; |
83 | 83 | if ($orderItem['expr_type'] === 'colref') { |
84 | 84 | $parts = $orderItem['no_quotes']['parts']; |
@@ -97,9 +97,9 @@ discard block |
||
97 | 97 | ]; |
98 | 98 | } else { |
99 | 99 | $position = $orderItem['position']; |
100 | - if ($i + 1 < $count) { |
|
101 | - $nextPosition = $parsed['ORDER'][$i + 1]['position']; |
|
102 | - $str = substr($sql, $position, $nextPosition - $position); |
|
100 | + if ($i+1<$count) { |
|
101 | + $nextPosition = $parsed['ORDER'][$i+1]['position']; |
|
102 | + $str = substr($sql, $position, $nextPosition-$position); |
|
103 | 103 | } else { |
104 | 104 | $str = substr($sql, $position); |
105 | 105 | } |
@@ -20,24 +20,24 @@ |
||
20 | 20 | */ |
21 | 21 | class UncheckedOrderBy |
22 | 22 | { |
23 | - /** |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - private $orderBy; |
|
23 | + /** |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + private $orderBy; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @param $orderBy |
|
30 | - */ |
|
31 | - public function __construct(string $orderBy) |
|
32 | - { |
|
33 | - $this->orderBy = $orderBy; |
|
34 | - } |
|
28 | + /** |
|
29 | + * @param $orderBy |
|
30 | + */ |
|
31 | + public function __construct(string $orderBy) |
|
32 | + { |
|
33 | + $this->orderBy = $orderBy; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function getOrderBy() : string |
|
40 | - { |
|
41 | - return $this->orderBy; |
|
42 | - } |
|
36 | + /** |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function getOrderBy() : string |
|
40 | + { |
|
41 | + return $this->orderBy; |
|
42 | + } |
|
43 | 43 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')'; |
40 | 40 | |
41 | 41 | $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
42 | - $pkColumnNames = array_map(function ($pkColumn) { |
|
42 | + $pkColumnNames = array_map(function($pkColumn) { |
|
43 | 43 | return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
44 | 44 | }, $pkColumnNames); |
45 | 45 |
@@ -11,75 +11,75 @@ |
||
11 | 11 | */ |
12 | 12 | class FindObjectsQueryFactory extends AbstractQueryFactory |
13 | 13 | { |
14 | - private $mainTable; |
|
15 | - private $additionalTablesFetch; |
|
16 | - private $filterString; |
|
17 | - private $orderBy; |
|
18 | - |
|
19 | - private $magicSql; |
|
20 | - private $magicSqlCount; |
|
21 | - private $columnDescList; |
|
22 | - |
|
23 | - public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer) |
|
24 | - { |
|
25 | - parent::__construct($tdbmService, $schema, $orderByAnalyzer); |
|
26 | - $this->mainTable = $mainTable; |
|
27 | - $this->additionalTablesFetch = $additionalTablesFetch; |
|
28 | - $this->filterString = $filterString; |
|
29 | - $this->orderBy = $orderBy; |
|
30 | - } |
|
31 | - |
|
32 | - private function compute() |
|
33 | - { |
|
34 | - list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy); |
|
35 | - |
|
36 | - $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')'; |
|
37 | - |
|
38 | - $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
39 | - $pkColumnNames = array_map(function ($pkColumn) { |
|
40 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
41 | - }, $pkColumnNames); |
|
42 | - |
|
43 | - $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')'; |
|
44 | - |
|
45 | - if (!empty($this->filterString)) { |
|
46 | - $sql .= ' WHERE '.$this->filterString; |
|
47 | - $countSql .= ' WHERE '.$this->filterString; |
|
48 | - } |
|
49 | - |
|
50 | - if (!empty($orderString)) { |
|
51 | - $sql .= ' ORDER BY '.$orderString; |
|
52 | - } |
|
53 | - |
|
54 | - $this->magicSql = $sql; |
|
55 | - $this->magicSqlCount = $countSql; |
|
56 | - $this->columnDescList = $columnDescList; |
|
57 | - } |
|
58 | - |
|
59 | - public function getMagicSql() : string |
|
60 | - { |
|
61 | - if ($this->magicSql === null) { |
|
62 | - $this->compute(); |
|
63 | - } |
|
64 | - |
|
65 | - return $this->magicSql; |
|
66 | - } |
|
67 | - |
|
68 | - public function getMagicSqlCount() : string |
|
69 | - { |
|
70 | - if ($this->magicSqlCount === null) { |
|
71 | - $this->compute(); |
|
72 | - } |
|
73 | - |
|
74 | - return $this->magicSqlCount; |
|
75 | - } |
|
76 | - |
|
77 | - public function getColumnDescriptors() : array |
|
78 | - { |
|
79 | - if ($this->columnDescList === null) { |
|
80 | - $this->compute(); |
|
81 | - } |
|
82 | - |
|
83 | - return $this->columnDescList; |
|
84 | - } |
|
14 | + private $mainTable; |
|
15 | + private $additionalTablesFetch; |
|
16 | + private $filterString; |
|
17 | + private $orderBy; |
|
18 | + |
|
19 | + private $magicSql; |
|
20 | + private $magicSqlCount; |
|
21 | + private $columnDescList; |
|
22 | + |
|
23 | + public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer) |
|
24 | + { |
|
25 | + parent::__construct($tdbmService, $schema, $orderByAnalyzer); |
|
26 | + $this->mainTable = $mainTable; |
|
27 | + $this->additionalTablesFetch = $additionalTablesFetch; |
|
28 | + $this->filterString = $filterString; |
|
29 | + $this->orderBy = $orderBy; |
|
30 | + } |
|
31 | + |
|
32 | + private function compute() |
|
33 | + { |
|
34 | + list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy); |
|
35 | + |
|
36 | + $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')'; |
|
37 | + |
|
38 | + $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
39 | + $pkColumnNames = array_map(function ($pkColumn) { |
|
40 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
41 | + }, $pkColumnNames); |
|
42 | + |
|
43 | + $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')'; |
|
44 | + |
|
45 | + if (!empty($this->filterString)) { |
|
46 | + $sql .= ' WHERE '.$this->filterString; |
|
47 | + $countSql .= ' WHERE '.$this->filterString; |
|
48 | + } |
|
49 | + |
|
50 | + if (!empty($orderString)) { |
|
51 | + $sql .= ' ORDER BY '.$orderString; |
|
52 | + } |
|
53 | + |
|
54 | + $this->magicSql = $sql; |
|
55 | + $this->magicSqlCount = $countSql; |
|
56 | + $this->columnDescList = $columnDescList; |
|
57 | + } |
|
58 | + |
|
59 | + public function getMagicSql() : string |
|
60 | + { |
|
61 | + if ($this->magicSql === null) { |
|
62 | + $this->compute(); |
|
63 | + } |
|
64 | + |
|
65 | + return $this->magicSql; |
|
66 | + } |
|
67 | + |
|
68 | + public function getMagicSqlCount() : string |
|
69 | + { |
|
70 | + if ($this->magicSqlCount === null) { |
|
71 | + $this->compute(); |
|
72 | + } |
|
73 | + |
|
74 | + return $this->magicSqlCount; |
|
75 | + } |
|
76 | + |
|
77 | + public function getColumnDescriptors() : array |
|
78 | + { |
|
79 | + if ($this->columnDescList === null) { |
|
80 | + $this->compute(); |
|
81 | + } |
|
82 | + |
|
83 | + return $this->columnDescList; |
|
84 | + } |
|
85 | 85 | } |
@@ -63,17 +63,17 @@ discard block |
||
63 | 63 | ]; |
64 | 64 | } |
65 | 65 | |
66 | - $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) { |
|
66 | + $sql = 'SELECT DISTINCT '.implode(', ', array_map(function($columnDesc) { |
|
67 | 67 | return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']); |
68 | 68 | }, $columnDescList)).' FROM '.$this->from; |
69 | 69 | |
70 | - if (count($allFetchedTables) > 1) { |
|
70 | + if (count($allFetchedTables)>1) { |
|
71 | 71 | list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
72 | 72 | } |
73 | 73 | |
74 | 74 | // Let's compute the COUNT. |
75 | 75 | $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
76 | - $pkColumnNames = array_map(function ($pkColumn) { |
|
76 | + $pkColumnNames = array_map(function($pkColumn) { |
|
77 | 77 | return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
78 | 78 | }, $pkColumnNames); |
79 | 79 | |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | */ |
132 | 132 | private function getParentRelationshipForeignKeys($tableName) |
133 | 133 | { |
134 | - return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { |
|
134 | + return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function() use ($tableName) { |
|
135 | 135 | return $this->getParentRelationshipForeignKeysWithoutCache($tableName); |
136 | 136 | }); |
137 | 137 | } |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | */ |
161 | 161 | private function getChildrenRelationshipForeignKeys(string $tableName) : array |
162 | 162 | { |
163 | - return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { |
|
163 | + return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function() use ($tableName) { |
|
164 | 164 | return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName); |
165 | 165 | }); |
166 | 166 | } |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | $children = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
176 | 176 | |
177 | 177 | if (!empty($children)) { |
178 | - $fksTables = array_map(function (ForeignKeyConstraint $fk) { |
|
178 | + $fksTables = array_map(function(ForeignKeyConstraint $fk) { |
|
179 | 179 | return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName()); |
180 | 180 | }, $children); |
181 | 181 |
@@ -15,219 +15,219 @@ |
||
15 | 15 | */ |
16 | 16 | class FindObjectsFromSqlQueryFactory extends AbstractQueryFactory |
17 | 17 | { |
18 | - private $mainTable; |
|
19 | - private $from; |
|
20 | - private $filterString; |
|
21 | - private $orderBy; |
|
22 | - private $cache; |
|
23 | - private $cachePrefix; |
|
24 | - |
|
25 | - private $magicSql; |
|
26 | - private $magicSqlCount; |
|
27 | - private $columnDescList; |
|
28 | - |
|
29 | - public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix) |
|
30 | - { |
|
31 | - parent::__construct($tdbmService, $schema, $orderByAnalyzer); |
|
32 | - $this->mainTable = $mainTable; |
|
33 | - $this->from = $from; |
|
34 | - $this->filterString = $filterString; |
|
35 | - $this->orderBy = $orderBy; |
|
36 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
37 | - $this->cache = $cache; |
|
38 | - $this->cachePrefix = $cachePrefix; |
|
39 | - } |
|
40 | - |
|
41 | - private function compute() |
|
42 | - { |
|
43 | - $connection = $this->tdbmService->getConnection(); |
|
44 | - |
|
45 | - $columnsList = null; |
|
46 | - |
|
47 | - $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
|
48 | - |
|
49 | - $columnDescList = []; |
|
50 | - |
|
51 | - $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
52 | - |
|
53 | - foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) { |
|
54 | - $columnName = $column->getName(); |
|
55 | - $columnDescList[] = [ |
|
56 | - 'as' => $columnName, |
|
57 | - 'table' => $this->mainTable, |
|
58 | - 'column' => $columnName, |
|
59 | - 'type' => $column->getType(), |
|
60 | - 'tableGroup' => $tableGroupName, |
|
61 | - ]; |
|
62 | - } |
|
63 | - |
|
64 | - $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) { |
|
65 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']); |
|
66 | - }, $columnDescList)).' FROM '.$this->from; |
|
67 | - |
|
68 | - if (count($allFetchedTables) > 1) { |
|
69 | - list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
|
70 | - } |
|
71 | - |
|
72 | - // Let's compute the COUNT. |
|
73 | - $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
74 | - $pkColumnNames = array_map(function ($pkColumn) { |
|
75 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
76 | - }, $pkColumnNames); |
|
77 | - |
|
78 | - $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; |
|
79 | - |
|
80 | - if (!empty($this->filterString)) { |
|
81 | - $sql .= ' WHERE '.$this->filterString; |
|
82 | - $countSql .= ' WHERE '.$this->filterString; |
|
83 | - } |
|
84 | - |
|
85 | - if (!empty($orderString)) { |
|
86 | - $sql .= ' ORDER BY '.$orderString; |
|
87 | - } |
|
88 | - |
|
89 | - if (stripos($countSql, 'GROUP BY') !== false) { |
|
90 | - throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); |
|
91 | - } |
|
92 | - |
|
93 | - if ($columnsList !== null) { |
|
94 | - $joinSql = ''; |
|
95 | - $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable); |
|
96 | - foreach ($parentFks as $fk) { |
|
97 | - $joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)', |
|
98 | - $connection->quoteIdentifier($fk->getForeignTableName()), |
|
99 | - $connection->quoteIdentifier($fk->getLocalTableName()), |
|
100 | - $connection->quoteIdentifier($fk->getLocalColumns()[0]), |
|
101 | - $connection->quoteIdentifier($fk->getForeignTableName()), |
|
102 | - $connection->quoteIdentifier($fk->getForeignColumns()[0]) |
|
103 | - ); |
|
104 | - } |
|
105 | - |
|
106 | - $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable); |
|
107 | - foreach ($childrenFks as $fk) { |
|
108 | - $joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)', |
|
109 | - $connection->quoteIdentifier($fk->getLocalTableName()), |
|
110 | - $connection->quoteIdentifier($fk->getForeignTableName()), |
|
111 | - $connection->quoteIdentifier($fk->getForeignColumns()[0]), |
|
112 | - $connection->quoteIdentifier($fk->getLocalTableName()), |
|
113 | - $connection->quoteIdentifier($fk->getLocalColumns()[0]) |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - $sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql; |
|
118 | - } |
|
119 | - |
|
120 | - $this->magicSql = $sql; |
|
121 | - $this->magicSqlCount = $countSql; |
|
122 | - $this->columnDescList = $columnDescList; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $tableName |
|
127 | - * |
|
128 | - * @return ForeignKeyConstraint[] |
|
129 | - */ |
|
130 | - private function getParentRelationshipForeignKeys($tableName) |
|
131 | - { |
|
132 | - return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { |
|
133 | - return $this->getParentRelationshipForeignKeysWithoutCache($tableName); |
|
134 | - }); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string $tableName |
|
139 | - * |
|
140 | - * @return ForeignKeyConstraint[] |
|
141 | - */ |
|
142 | - private function getParentRelationshipForeignKeysWithoutCache($tableName) |
|
143 | - { |
|
144 | - $parentFks = []; |
|
145 | - $currentTable = $tableName; |
|
146 | - while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) { |
|
147 | - $currentTable = $currentFk->getForeignTableName(); |
|
148 | - $parentFks[] = $currentFk; |
|
149 | - } |
|
150 | - |
|
151 | - return $parentFks; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @param string $tableName |
|
156 | - * |
|
157 | - * @return ForeignKeyConstraint[] |
|
158 | - */ |
|
159 | - private function getChildrenRelationshipForeignKeys(string $tableName) : array |
|
160 | - { |
|
161 | - return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { |
|
162 | - return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName); |
|
163 | - }); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param string $tableName |
|
168 | - * |
|
169 | - * @return ForeignKeyConstraint[] |
|
170 | - */ |
|
171 | - private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array |
|
172 | - { |
|
173 | - $children = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
174 | - |
|
175 | - if (!empty($children)) { |
|
176 | - $fksTables = array_map(function (ForeignKeyConstraint $fk) { |
|
177 | - return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName()); |
|
178 | - }, $children); |
|
179 | - |
|
180 | - $fks = array_merge($children, call_user_func_array('array_merge', $fksTables)); |
|
181 | - |
|
182 | - return $fks; |
|
183 | - } else { |
|
184 | - return []; |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Returns an item from cache or computes it using $closure and puts it in cache. |
|
190 | - * |
|
191 | - * @param string $key |
|
192 | - * @param callable $closure |
|
193 | - * |
|
194 | - * @return mixed |
|
195 | - */ |
|
196 | - protected function fromCache(string $key, callable $closure) |
|
197 | - { |
|
198 | - $item = $this->cache->fetch($key); |
|
199 | - if ($item === false) { |
|
200 | - $item = $closure(); |
|
201 | - $this->cache->save($key, $item); |
|
202 | - } |
|
203 | - |
|
204 | - return $item; |
|
205 | - } |
|
206 | - |
|
207 | - public function getMagicSql() : string |
|
208 | - { |
|
209 | - if ($this->magicSql === null) { |
|
210 | - $this->compute(); |
|
211 | - } |
|
212 | - |
|
213 | - return $this->magicSql; |
|
214 | - } |
|
215 | - |
|
216 | - public function getMagicSqlCount() : string |
|
217 | - { |
|
218 | - if ($this->magicSqlCount === null) { |
|
219 | - $this->compute(); |
|
220 | - } |
|
221 | - |
|
222 | - return $this->magicSqlCount; |
|
223 | - } |
|
224 | - |
|
225 | - public function getColumnDescriptors() : array |
|
226 | - { |
|
227 | - if ($this->columnDescList === null) { |
|
228 | - $this->compute(); |
|
229 | - } |
|
230 | - |
|
231 | - return $this->columnDescList; |
|
232 | - } |
|
18 | + private $mainTable; |
|
19 | + private $from; |
|
20 | + private $filterString; |
|
21 | + private $orderBy; |
|
22 | + private $cache; |
|
23 | + private $cachePrefix; |
|
24 | + |
|
25 | + private $magicSql; |
|
26 | + private $magicSqlCount; |
|
27 | + private $columnDescList; |
|
28 | + |
|
29 | + public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix) |
|
30 | + { |
|
31 | + parent::__construct($tdbmService, $schema, $orderByAnalyzer); |
|
32 | + $this->mainTable = $mainTable; |
|
33 | + $this->from = $from; |
|
34 | + $this->filterString = $filterString; |
|
35 | + $this->orderBy = $orderBy; |
|
36 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
37 | + $this->cache = $cache; |
|
38 | + $this->cachePrefix = $cachePrefix; |
|
39 | + } |
|
40 | + |
|
41 | + private function compute() |
|
42 | + { |
|
43 | + $connection = $this->tdbmService->getConnection(); |
|
44 | + |
|
45 | + $columnsList = null; |
|
46 | + |
|
47 | + $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
|
48 | + |
|
49 | + $columnDescList = []; |
|
50 | + |
|
51 | + $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
52 | + |
|
53 | + foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) { |
|
54 | + $columnName = $column->getName(); |
|
55 | + $columnDescList[] = [ |
|
56 | + 'as' => $columnName, |
|
57 | + 'table' => $this->mainTable, |
|
58 | + 'column' => $columnName, |
|
59 | + 'type' => $column->getType(), |
|
60 | + 'tableGroup' => $tableGroupName, |
|
61 | + ]; |
|
62 | + } |
|
63 | + |
|
64 | + $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) { |
|
65 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']); |
|
66 | + }, $columnDescList)).' FROM '.$this->from; |
|
67 | + |
|
68 | + if (count($allFetchedTables) > 1) { |
|
69 | + list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
|
70 | + } |
|
71 | + |
|
72 | + // Let's compute the COUNT. |
|
73 | + $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
74 | + $pkColumnNames = array_map(function ($pkColumn) { |
|
75 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
76 | + }, $pkColumnNames); |
|
77 | + |
|
78 | + $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; |
|
79 | + |
|
80 | + if (!empty($this->filterString)) { |
|
81 | + $sql .= ' WHERE '.$this->filterString; |
|
82 | + $countSql .= ' WHERE '.$this->filterString; |
|
83 | + } |
|
84 | + |
|
85 | + if (!empty($orderString)) { |
|
86 | + $sql .= ' ORDER BY '.$orderString; |
|
87 | + } |
|
88 | + |
|
89 | + if (stripos($countSql, 'GROUP BY') !== false) { |
|
90 | + throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); |
|
91 | + } |
|
92 | + |
|
93 | + if ($columnsList !== null) { |
|
94 | + $joinSql = ''; |
|
95 | + $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable); |
|
96 | + foreach ($parentFks as $fk) { |
|
97 | + $joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)', |
|
98 | + $connection->quoteIdentifier($fk->getForeignTableName()), |
|
99 | + $connection->quoteIdentifier($fk->getLocalTableName()), |
|
100 | + $connection->quoteIdentifier($fk->getLocalColumns()[0]), |
|
101 | + $connection->quoteIdentifier($fk->getForeignTableName()), |
|
102 | + $connection->quoteIdentifier($fk->getForeignColumns()[0]) |
|
103 | + ); |
|
104 | + } |
|
105 | + |
|
106 | + $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable); |
|
107 | + foreach ($childrenFks as $fk) { |
|
108 | + $joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)', |
|
109 | + $connection->quoteIdentifier($fk->getLocalTableName()), |
|
110 | + $connection->quoteIdentifier($fk->getForeignTableName()), |
|
111 | + $connection->quoteIdentifier($fk->getForeignColumns()[0]), |
|
112 | + $connection->quoteIdentifier($fk->getLocalTableName()), |
|
113 | + $connection->quoteIdentifier($fk->getLocalColumns()[0]) |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + $sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql; |
|
118 | + } |
|
119 | + |
|
120 | + $this->magicSql = $sql; |
|
121 | + $this->magicSqlCount = $countSql; |
|
122 | + $this->columnDescList = $columnDescList; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $tableName |
|
127 | + * |
|
128 | + * @return ForeignKeyConstraint[] |
|
129 | + */ |
|
130 | + private function getParentRelationshipForeignKeys($tableName) |
|
131 | + { |
|
132 | + return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { |
|
133 | + return $this->getParentRelationshipForeignKeysWithoutCache($tableName); |
|
134 | + }); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string $tableName |
|
139 | + * |
|
140 | + * @return ForeignKeyConstraint[] |
|
141 | + */ |
|
142 | + private function getParentRelationshipForeignKeysWithoutCache($tableName) |
|
143 | + { |
|
144 | + $parentFks = []; |
|
145 | + $currentTable = $tableName; |
|
146 | + while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) { |
|
147 | + $currentTable = $currentFk->getForeignTableName(); |
|
148 | + $parentFks[] = $currentFk; |
|
149 | + } |
|
150 | + |
|
151 | + return $parentFks; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @param string $tableName |
|
156 | + * |
|
157 | + * @return ForeignKeyConstraint[] |
|
158 | + */ |
|
159 | + private function getChildrenRelationshipForeignKeys(string $tableName) : array |
|
160 | + { |
|
161 | + return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { |
|
162 | + return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName); |
|
163 | + }); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param string $tableName |
|
168 | + * |
|
169 | + * @return ForeignKeyConstraint[] |
|
170 | + */ |
|
171 | + private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array |
|
172 | + { |
|
173 | + $children = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
174 | + |
|
175 | + if (!empty($children)) { |
|
176 | + $fksTables = array_map(function (ForeignKeyConstraint $fk) { |
|
177 | + return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName()); |
|
178 | + }, $children); |
|
179 | + |
|
180 | + $fks = array_merge($children, call_user_func_array('array_merge', $fksTables)); |
|
181 | + |
|
182 | + return $fks; |
|
183 | + } else { |
|
184 | + return []; |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Returns an item from cache or computes it using $closure and puts it in cache. |
|
190 | + * |
|
191 | + * @param string $key |
|
192 | + * @param callable $closure |
|
193 | + * |
|
194 | + * @return mixed |
|
195 | + */ |
|
196 | + protected function fromCache(string $key, callable $closure) |
|
197 | + { |
|
198 | + $item = $this->cache->fetch($key); |
|
199 | + if ($item === false) { |
|
200 | + $item = $closure(); |
|
201 | + $this->cache->save($key, $item); |
|
202 | + } |
|
203 | + |
|
204 | + return $item; |
|
205 | + } |
|
206 | + |
|
207 | + public function getMagicSql() : string |
|
208 | + { |
|
209 | + if ($this->magicSql === null) { |
|
210 | + $this->compute(); |
|
211 | + } |
|
212 | + |
|
213 | + return $this->magicSql; |
|
214 | + } |
|
215 | + |
|
216 | + public function getMagicSqlCount() : string |
|
217 | + { |
|
218 | + if ($this->magicSqlCount === null) { |
|
219 | + $this->compute(); |
|
220 | + } |
|
221 | + |
|
222 | + return $this->magicSqlCount; |
|
223 | + } |
|
224 | + |
|
225 | + public function getColumnDescriptors() : array |
|
226 | + { |
|
227 | + if ($this->columnDescList === null) { |
|
228 | + $this->compute(); |
|
229 | + } |
|
230 | + |
|
231 | + return $this->columnDescList; |
|
232 | + } |
|
233 | 233 | } |
@@ -7,9 +7,9 @@ |
||
7 | 7 | */ |
8 | 8 | interface QueryFactory |
9 | 9 | { |
10 | - public function getMagicSql() : string; |
|
10 | + public function getMagicSql() : string; |
|
11 | 11 | |
12 | - public function getMagicSqlCount() : string; |
|
12 | + public function getMagicSqlCount() : string; |
|
13 | 13 | |
14 | - public function getColumnDescriptors() : array; |
|
14 | + public function getColumnDescriptors() : array; |
|
15 | 15 | } |
@@ -10,146 +10,146 @@ |
||
10 | 10 | |
11 | 11 | abstract class AbstractQueryFactory implements QueryFactory |
12 | 12 | { |
13 | - /** |
|
14 | - * @var TDBMService |
|
15 | - */ |
|
16 | - protected $tdbmService; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var Schema |
|
20 | - */ |
|
21 | - protected $schema; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var OrderByAnalyzer |
|
25 | - */ |
|
26 | - protected $orderByAnalyzer; |
|
27 | - |
|
28 | - /** |
|
29 | - * @param TDBMService $tdbmService |
|
30 | - */ |
|
31 | - public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer) |
|
32 | - { |
|
33 | - $this->tdbmService = $tdbmService; |
|
34 | - $this->schema = $schema; |
|
35 | - $this->orderByAnalyzer = $orderByAnalyzer; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Returns the column list that must be fetched for the SQL request. |
|
40 | - * |
|
41 | - * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause. |
|
42 | - * |
|
43 | - * @param string $mainTable |
|
44 | - * @param array $additionalTablesFetch |
|
45 | - * @param string|UncheckedOrderBy|null $orderBy |
|
46 | - * |
|
47 | - * @return array |
|
48 | - * |
|
49 | - * @throws \Doctrine\DBAL\Schema\SchemaException |
|
50 | - */ |
|
51 | - protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null) |
|
52 | - { |
|
53 | - // From the table name and the additional tables we want to fetch, let's build a list of all tables |
|
54 | - // that must be part of the select columns. |
|
55 | - |
|
56 | - $connection = $this->tdbmService->getConnection(); |
|
57 | - |
|
58 | - $tableGroups = []; |
|
59 | - $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable); |
|
60 | - $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
61 | - foreach ($allFetchedTables as $table) { |
|
62 | - $tableGroups[$table] = $tableGroupName; |
|
63 | - } |
|
64 | - |
|
65 | - $columnsList = []; |
|
66 | - $columnDescList = []; |
|
67 | - $sortColumn = 0; |
|
68 | - $reconstructedOrderBy = null; |
|
69 | - |
|
70 | - // Now, let's deal with "order by columns" |
|
71 | - if ($orderBy !== null) { |
|
72 | - if ($orderBy instanceof UncheckedOrderBy) { |
|
73 | - $securedOrderBy = false; |
|
74 | - $orderBy = $orderBy->getOrderBy(); |
|
75 | - $reconstructedOrderBy = $orderBy; |
|
76 | - } else { |
|
77 | - $securedOrderBy = true; |
|
78 | - $reconstructedOrderBys = []; |
|
79 | - } |
|
80 | - $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy); |
|
81 | - |
|
82 | - // If we sort by a column, there is a high chance we will fetch the bean containing this column. |
|
83 | - // Hence, we should add the table to the $additionalTablesFetch |
|
84 | - foreach ($orderByColumns as $orderByColumn) { |
|
85 | - if ($orderByColumn['type'] === 'colref') { |
|
86 | - if ($orderByColumn['table'] !== null) { |
|
87 | - $additionalTablesFetch[] = $orderByColumn['table']; |
|
88 | - } |
|
89 | - if ($securedOrderBy) { |
|
90 | - $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $orderByColumn['table'].'.' : '').$orderByColumn['column'].' '.$orderByColumn['direction']; |
|
91 | - } |
|
92 | - } elseif ($orderByColumn['type'] === 'expr') { |
|
93 | - $sortColumnName = 'sort_column_'.$sortColumn; |
|
94 | - $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName; |
|
95 | - $columnDescList[] = [ |
|
96 | - 'tableGroup' => null, |
|
97 | - ]; |
|
98 | - ++$sortColumn; |
|
99 | - |
|
100 | - if ($securedOrderBy) { |
|
101 | - throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")'); |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - if ($reconstructedOrderBy === null) { |
|
107 | - $reconstructedOrderBy = implode(', ', $reconstructedOrderBys); |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - foreach ($additionalTablesFetch as $additionalTable) { |
|
112 | - $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); |
|
113 | - $tableGroupName = $this->getTableGroupName($relatedTables); |
|
114 | - foreach ($relatedTables as $table) { |
|
115 | - $tableGroups[$table] = $tableGroupName; |
|
116 | - } |
|
117 | - $allFetchedTables = array_merge($allFetchedTables, $relatedTables); |
|
118 | - } |
|
119 | - |
|
120 | - // Let's remove any duplicate |
|
121 | - $allFetchedTables = array_flip(array_flip($allFetchedTables)); |
|
122 | - |
|
123 | - // Now, let's build the column list |
|
124 | - foreach ($allFetchedTables as $table) { |
|
125 | - foreach ($this->schema->getTable($table)->getColumns() as $column) { |
|
126 | - $columnName = $column->getName(); |
|
127 | - $columnDescList[] = [ |
|
128 | - 'as' => $table.'____'.$columnName, |
|
129 | - 'table' => $table, |
|
130 | - 'column' => $columnName, |
|
131 | - 'type' => $column->getType(), |
|
132 | - 'tableGroup' => $tableGroups[$table], |
|
133 | - ]; |
|
134 | - $columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '. |
|
135 | - $connection->quoteIdentifier($table.'____'.$columnName); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - return [$columnDescList, $columnsList, $reconstructedOrderBy]; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Returns an identifier for the group of tables passed in parameter. |
|
144 | - * |
|
145 | - * @param string[] $relatedTables |
|
146 | - * |
|
147 | - * @return string |
|
148 | - */ |
|
149 | - protected function getTableGroupName(array $relatedTables) |
|
150 | - { |
|
151 | - sort($relatedTables); |
|
152 | - |
|
153 | - return implode('_``_', $relatedTables); |
|
154 | - } |
|
13 | + /** |
|
14 | + * @var TDBMService |
|
15 | + */ |
|
16 | + protected $tdbmService; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var Schema |
|
20 | + */ |
|
21 | + protected $schema; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var OrderByAnalyzer |
|
25 | + */ |
|
26 | + protected $orderByAnalyzer; |
|
27 | + |
|
28 | + /** |
|
29 | + * @param TDBMService $tdbmService |
|
30 | + */ |
|
31 | + public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer) |
|
32 | + { |
|
33 | + $this->tdbmService = $tdbmService; |
|
34 | + $this->schema = $schema; |
|
35 | + $this->orderByAnalyzer = $orderByAnalyzer; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Returns the column list that must be fetched for the SQL request. |
|
40 | + * |
|
41 | + * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause. |
|
42 | + * |
|
43 | + * @param string $mainTable |
|
44 | + * @param array $additionalTablesFetch |
|
45 | + * @param string|UncheckedOrderBy|null $orderBy |
|
46 | + * |
|
47 | + * @return array |
|
48 | + * |
|
49 | + * @throws \Doctrine\DBAL\Schema\SchemaException |
|
50 | + */ |
|
51 | + protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null) |
|
52 | + { |
|
53 | + // From the table name and the additional tables we want to fetch, let's build a list of all tables |
|
54 | + // that must be part of the select columns. |
|
55 | + |
|
56 | + $connection = $this->tdbmService->getConnection(); |
|
57 | + |
|
58 | + $tableGroups = []; |
|
59 | + $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable); |
|
60 | + $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
61 | + foreach ($allFetchedTables as $table) { |
|
62 | + $tableGroups[$table] = $tableGroupName; |
|
63 | + } |
|
64 | + |
|
65 | + $columnsList = []; |
|
66 | + $columnDescList = []; |
|
67 | + $sortColumn = 0; |
|
68 | + $reconstructedOrderBy = null; |
|
69 | + |
|
70 | + // Now, let's deal with "order by columns" |
|
71 | + if ($orderBy !== null) { |
|
72 | + if ($orderBy instanceof UncheckedOrderBy) { |
|
73 | + $securedOrderBy = false; |
|
74 | + $orderBy = $orderBy->getOrderBy(); |
|
75 | + $reconstructedOrderBy = $orderBy; |
|
76 | + } else { |
|
77 | + $securedOrderBy = true; |
|
78 | + $reconstructedOrderBys = []; |
|
79 | + } |
|
80 | + $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy); |
|
81 | + |
|
82 | + // If we sort by a column, there is a high chance we will fetch the bean containing this column. |
|
83 | + // Hence, we should add the table to the $additionalTablesFetch |
|
84 | + foreach ($orderByColumns as $orderByColumn) { |
|
85 | + if ($orderByColumn['type'] === 'colref') { |
|
86 | + if ($orderByColumn['table'] !== null) { |
|
87 | + $additionalTablesFetch[] = $orderByColumn['table']; |
|
88 | + } |
|
89 | + if ($securedOrderBy) { |
|
90 | + $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $orderByColumn['table'].'.' : '').$orderByColumn['column'].' '.$orderByColumn['direction']; |
|
91 | + } |
|
92 | + } elseif ($orderByColumn['type'] === 'expr') { |
|
93 | + $sortColumnName = 'sort_column_'.$sortColumn; |
|
94 | + $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName; |
|
95 | + $columnDescList[] = [ |
|
96 | + 'tableGroup' => null, |
|
97 | + ]; |
|
98 | + ++$sortColumn; |
|
99 | + |
|
100 | + if ($securedOrderBy) { |
|
101 | + throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")'); |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + if ($reconstructedOrderBy === null) { |
|
107 | + $reconstructedOrderBy = implode(', ', $reconstructedOrderBys); |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + foreach ($additionalTablesFetch as $additionalTable) { |
|
112 | + $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); |
|
113 | + $tableGroupName = $this->getTableGroupName($relatedTables); |
|
114 | + foreach ($relatedTables as $table) { |
|
115 | + $tableGroups[$table] = $tableGroupName; |
|
116 | + } |
|
117 | + $allFetchedTables = array_merge($allFetchedTables, $relatedTables); |
|
118 | + } |
|
119 | + |
|
120 | + // Let's remove any duplicate |
|
121 | + $allFetchedTables = array_flip(array_flip($allFetchedTables)); |
|
122 | + |
|
123 | + // Now, let's build the column list |
|
124 | + foreach ($allFetchedTables as $table) { |
|
125 | + foreach ($this->schema->getTable($table)->getColumns() as $column) { |
|
126 | + $columnName = $column->getName(); |
|
127 | + $columnDescList[] = [ |
|
128 | + 'as' => $table.'____'.$columnName, |
|
129 | + 'table' => $table, |
|
130 | + 'column' => $columnName, |
|
131 | + 'type' => $column->getType(), |
|
132 | + 'tableGroup' => $tableGroups[$table], |
|
133 | + ]; |
|
134 | + $columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '. |
|
135 | + $connection->quoteIdentifier($table.'____'.$columnName); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + return [$columnDescList, $columnsList, $reconstructedOrderBy]; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Returns an identifier for the group of tables passed in parameter. |
|
144 | + * |
|
145 | + * @param string[] $relatedTables |
|
146 | + * |
|
147 | + * @return string |
|
148 | + */ |
|
149 | + protected function getTableGroupName(array $relatedTables) |
|
150 | + { |
|
151 | + sort($relatedTables); |
|
152 | + |
|
153 | + return implode('_``_', $relatedTables); |
|
154 | + } |
|
155 | 155 | } |