@@ -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,191 +15,191 @@  | 
                                                    ||
| 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 | - $columnDescList = [];  | 
                                                        |
| 44 | -  | 
                                                        |
| 45 | - $tableGroupName = $this->getTableGroupName($allFetchedTables);  | 
                                                        |
| 46 | -  | 
                                                        |
| 47 | -        foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) { | 
                                                        |
| 48 | - $columnName = $column->getName();  | 
                                                        |
| 49 | - $columnDescList[] = [  | 
                                                        |
| 50 | - 'as' => $columnName,  | 
                                                        |
| 51 | - 'table' => $this->mainTable,  | 
                                                        |
| 52 | - 'column' => $columnName,  | 
                                                        |
| 53 | - 'type' => $column->getType(),  | 
                                                        |
| 54 | - 'tableGroup' => $tableGroupName,  | 
                                                        |
| 55 | - ];  | 
                                                        |
| 56 | - }  | 
                                                        |
| 57 | -  | 
                                                        |
| 58 | -        $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) { | 
                                                        |
| 59 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);  | 
                                                        |
| 60 | - }, $columnDescList)).' FROM '.$this->from;  | 
                                                        |
| 61 | -  | 
                                                        |
| 62 | -        if (count($allFetchedTables) > 1) { | 
                                                        |
| 63 | - list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);  | 
                                                        |
| 64 | -        } elseif ($this->orderBy) { | 
                                                        |
| 65 | - list($columnDescListNotUse, $columnsListNotUse, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);  | 
                                                        |
| 66 | - }  | 
                                                        |
| 67 | -  | 
                                                        |
| 68 | - // Let's compute the COUNT.  | 
                                                        |
| 69 | - $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();  | 
                                                        |
| 70 | -        $pkColumnNames = array_map(function ($pkColumn) { | 
                                                        |
| 71 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);  | 
                                                        |
| 72 | - }, $pkColumnNames);  | 
                                                        |
| 73 | -  | 
                                                        |
| 74 | -        $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; | 
                                                        |
| 75 | -  | 
                                                        |
| 76 | -        if (!empty($this->filterString)) { | 
                                                        |
| 77 | - $sql .= ' WHERE '.$this->filterString;  | 
                                                        |
| 78 | - $countSql .= ' WHERE '.$this->filterString;  | 
                                                        |
| 79 | - }  | 
                                                        |
| 80 | -  | 
                                                        |
| 81 | -        if (!empty($orderString)) { | 
                                                        |
| 82 | - $sql .= ' ORDER BY '.$orderString;  | 
                                                        |
| 83 | - }  | 
                                                        |
| 84 | -  | 
                                                        |
| 85 | -        if (stripos($countSql, 'GROUP BY') !== false) { | 
                                                        |
| 86 | -            throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); | 
                                                        |
| 87 | - }  | 
                                                        |
| 88 | -  | 
                                                        |
| 89 | -        if ($columnsList !== null) { | 
                                                        |
| 90 | - $joinSql = '';  | 
                                                        |
| 91 | - $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable);  | 
                                                        |
| 92 | -            foreach ($parentFks as $fk) { | 
                                                        |
| 93 | -                $joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)', | 
                                                        |
| 94 | - $connection->quoteIdentifier($fk->getForeignTableName()),  | 
                                                        |
| 95 | - $connection->quoteIdentifier($fk->getLocalTableName()),  | 
                                                        |
| 96 | - $connection->quoteIdentifier($fk->getLocalColumns()[0]),  | 
                                                        |
| 97 | - $connection->quoteIdentifier($fk->getForeignTableName()),  | 
                                                        |
| 98 | - $connection->quoteIdentifier($fk->getForeignColumns()[0])  | 
                                                        |
| 99 | - );  | 
                                                        |
| 100 | - }  | 
                                                        |
| 101 | -  | 
                                                        |
| 102 | - $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable);  | 
                                                        |
| 103 | -            foreach ($childrenFks as $fk) { | 
                                                        |
| 104 | -                $joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)', | 
                                                        |
| 105 | - $connection->quoteIdentifier($fk->getLocalTableName()),  | 
                                                        |
| 106 | - $connection->quoteIdentifier($fk->getForeignTableName()),  | 
                                                        |
| 107 | - $connection->quoteIdentifier($fk->getForeignColumns()[0]),  | 
                                                        |
| 108 | - $connection->quoteIdentifier($fk->getLocalTableName()),  | 
                                                        |
| 109 | - $connection->quoteIdentifier($fk->getLocalColumns()[0])  | 
                                                        |
| 110 | - );  | 
                                                        |
| 111 | - }  | 
                                                        |
| 112 | -  | 
                                                        |
| 113 | -            $sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql; | 
                                                        |
| 114 | -            if (!empty($orderString)) { | 
                                                        |
| 115 | - $sql .= ' ORDER BY '.$orderString;  | 
                                                        |
| 116 | - }  | 
                                                        |
| 117 | - }  | 
                                                        |
| 118 | -  | 
                                                        |
| 119 | - $this->magicSql = $sql;  | 
                                                        |
| 120 | - $this->magicSqlCount = $countSql;  | 
                                                        |
| 121 | - $this->columnDescList = $columnDescList;  | 
                                                        |
| 122 | - }  | 
                                                        |
| 123 | -  | 
                                                        |
| 124 | - /**  | 
                                                        |
| 125 | - * @param string $tableName  | 
                                                        |
| 126 | - *  | 
                                                        |
| 127 | - * @return ForeignKeyConstraint[]  | 
                                                        |
| 128 | - */  | 
                                                        |
| 129 | - private function getParentRelationshipForeignKeys($tableName)  | 
                                                        |
| 130 | -    { | 
                                                        |
| 131 | -        return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { | 
                                                        |
| 132 | - return $this->getParentRelationshipForeignKeysWithoutCache($tableName);  | 
                                                        |
| 133 | - });  | 
                                                        |
| 134 | - }  | 
                                                        |
| 135 | -  | 
                                                        |
| 136 | - /**  | 
                                                        |
| 137 | - * @param string $tableName  | 
                                                        |
| 138 | - *  | 
                                                        |
| 139 | - * @return ForeignKeyConstraint[]  | 
                                                        |
| 140 | - */  | 
                                                        |
| 141 | - private function getParentRelationshipForeignKeysWithoutCache($tableName)  | 
                                                        |
| 142 | -    { | 
                                                        |
| 143 | - $parentFks = [];  | 
                                                        |
| 144 | - $currentTable = $tableName;  | 
                                                        |
| 145 | -        while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) { | 
                                                        |
| 146 | - $currentTable = $currentFk->getForeignTableName();  | 
                                                        |
| 147 | - $parentFks[] = $currentFk;  | 
                                                        |
| 148 | - }  | 
                                                        |
| 149 | -  | 
                                                        |
| 150 | - return $parentFks;  | 
                                                        |
| 151 | - }  | 
                                                        |
| 152 | -  | 
                                                        |
| 153 | - /**  | 
                                                        |
| 154 | - * @param string $tableName  | 
                                                        |
| 155 | - *  | 
                                                        |
| 156 | - * @return ForeignKeyConstraint[]  | 
                                                        |
| 157 | - */  | 
                                                        |
| 158 | - private function getChildrenRelationshipForeignKeys(string $tableName) : array  | 
                                                        |
| 159 | -    { | 
                                                        |
| 160 | -        return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { | 
                                                        |
| 161 | - return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName);  | 
                                                        |
| 162 | - });  | 
                                                        |
| 163 | - }  | 
                                                        |
| 164 | -  | 
                                                        |
| 165 | - /**  | 
                                                        |
| 166 | - * @param string $tableName  | 
                                                        |
| 167 | - *  | 
                                                        |
| 168 | - * @return ForeignKeyConstraint[]  | 
                                                        |
| 169 | - */  | 
                                                        |
| 170 | - private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array  | 
                                                        |
| 171 | -    { | 
                                                        |
| 172 | - $children = $this->schemaAnalyzer->getChildrenRelationships($tableName);  | 
                                                        |
| 173 | -  | 
                                                        |
| 174 | -        if (!empty($children)) { | 
                                                        |
| 175 | -            $fksTables = array_map(function (ForeignKeyConstraint $fk) { | 
                                                        |
| 176 | - return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());  | 
                                                        |
| 177 | - }, $children);  | 
                                                        |
| 178 | -  | 
                                                        |
| 179 | -            $fks = array_merge($children, call_user_func_array('array_merge', $fksTables)); | 
                                                        |
| 180 | -  | 
                                                        |
| 181 | - return $fks;  | 
                                                        |
| 182 | -        } else { | 
                                                        |
| 183 | - return [];  | 
                                                        |
| 184 | - }  | 
                                                        |
| 185 | - }  | 
                                                        |
| 186 | -  | 
                                                        |
| 187 | - /**  | 
                                                        |
| 188 | - * Returns an item from cache or computes it using $closure and puts it in cache.  | 
                                                        |
| 189 | - *  | 
                                                        |
| 190 | - * @param string $key  | 
                                                        |
| 191 | - * @param callable $closure  | 
                                                        |
| 192 | - *  | 
                                                        |
| 193 | - * @return mixed  | 
                                                        |
| 194 | - */  | 
                                                        |
| 195 | - protected function fromCache(string $key, callable $closure)  | 
                                                        |
| 196 | -    { | 
                                                        |
| 197 | - $item = $this->cache->fetch($key);  | 
                                                        |
| 198 | -        if ($item === false) { | 
                                                        |
| 199 | - $item = $closure();  | 
                                                        |
| 200 | - $this->cache->save($key, $item);  | 
                                                        |
| 201 | - }  | 
                                                        |
| 202 | -  | 
                                                        |
| 203 | - return $item;  | 
                                                        |
| 204 | - }  | 
                                                        |
| 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 | + $columnDescList = [];  | 
                                                        |
| 44 | +  | 
                                                        |
| 45 | + $tableGroupName = $this->getTableGroupName($allFetchedTables);  | 
                                                        |
| 46 | +  | 
                                                        |
| 47 | +		foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) { | 
                                                        |
| 48 | + $columnName = $column->getName();  | 
                                                        |
| 49 | + $columnDescList[] = [  | 
                                                        |
| 50 | + 'as' => $columnName,  | 
                                                        |
| 51 | + 'table' => $this->mainTable,  | 
                                                        |
| 52 | + 'column' => $columnName,  | 
                                                        |
| 53 | + 'type' => $column->getType(),  | 
                                                        |
| 54 | + 'tableGroup' => $tableGroupName,  | 
                                                        |
| 55 | + ];  | 
                                                        |
| 56 | + }  | 
                                                        |
| 57 | +  | 
                                                        |
| 58 | +		$sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) { | 
                                                        |
| 59 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);  | 
                                                        |
| 60 | + }, $columnDescList)).' FROM '.$this->from;  | 
                                                        |
| 61 | +  | 
                                                        |
| 62 | +		if (count($allFetchedTables) > 1) { | 
                                                        |
| 63 | + list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);  | 
                                                        |
| 64 | +		} elseif ($this->orderBy) { | 
                                                        |
| 65 | + list($columnDescListNotUse, $columnsListNotUse, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);  | 
                                                        |
| 66 | + }  | 
                                                        |
| 67 | +  | 
                                                        |
| 68 | + // Let's compute the COUNT.  | 
                                                        |
| 69 | + $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();  | 
                                                        |
| 70 | +		$pkColumnNames = array_map(function ($pkColumn) { | 
                                                        |
| 71 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);  | 
                                                        |
| 72 | + }, $pkColumnNames);  | 
                                                        |
| 73 | +  | 
                                                        |
| 74 | +		$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; | 
                                                        |
| 75 | +  | 
                                                        |
| 76 | +		if (!empty($this->filterString)) { | 
                                                        |
| 77 | + $sql .= ' WHERE '.$this->filterString;  | 
                                                        |
| 78 | + $countSql .= ' WHERE '.$this->filterString;  | 
                                                        |
| 79 | + }  | 
                                                        |
| 80 | +  | 
                                                        |
| 81 | +		if (!empty($orderString)) { | 
                                                        |
| 82 | + $sql .= ' ORDER BY '.$orderString;  | 
                                                        |
| 83 | + }  | 
                                                        |
| 84 | +  | 
                                                        |
| 85 | +		if (stripos($countSql, 'GROUP BY') !== false) { | 
                                                        |
| 86 | +			throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); | 
                                                        |
| 87 | + }  | 
                                                        |
| 88 | +  | 
                                                        |
| 89 | +		if ($columnsList !== null) { | 
                                                        |
| 90 | + $joinSql = '';  | 
                                                        |
| 91 | + $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable);  | 
                                                        |
| 92 | +			foreach ($parentFks as $fk) { | 
                                                        |
| 93 | +				$joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)', | 
                                                        |
| 94 | + $connection->quoteIdentifier($fk->getForeignTableName()),  | 
                                                        |
| 95 | + $connection->quoteIdentifier($fk->getLocalTableName()),  | 
                                                        |
| 96 | + $connection->quoteIdentifier($fk->getLocalColumns()[0]),  | 
                                                        |
| 97 | + $connection->quoteIdentifier($fk->getForeignTableName()),  | 
                                                        |
| 98 | + $connection->quoteIdentifier($fk->getForeignColumns()[0])  | 
                                                        |
| 99 | + );  | 
                                                        |
| 100 | + }  | 
                                                        |
| 101 | +  | 
                                                        |
| 102 | + $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable);  | 
                                                        |
| 103 | +			foreach ($childrenFks as $fk) { | 
                                                        |
| 104 | +				$joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)', | 
                                                        |
| 105 | + $connection->quoteIdentifier($fk->getLocalTableName()),  | 
                                                        |
| 106 | + $connection->quoteIdentifier($fk->getForeignTableName()),  | 
                                                        |
| 107 | + $connection->quoteIdentifier($fk->getForeignColumns()[0]),  | 
                                                        |
| 108 | + $connection->quoteIdentifier($fk->getLocalTableName()),  | 
                                                        |
| 109 | + $connection->quoteIdentifier($fk->getLocalColumns()[0])  | 
                                                        |
| 110 | + );  | 
                                                        |
| 111 | + }  | 
                                                        |
| 112 | +  | 
                                                        |
| 113 | +			$sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql; | 
                                                        |
| 114 | +			if (!empty($orderString)) { | 
                                                        |
| 115 | + $sql .= ' ORDER BY '.$orderString;  | 
                                                        |
| 116 | + }  | 
                                                        |
| 117 | + }  | 
                                                        |
| 118 | +  | 
                                                        |
| 119 | + $this->magicSql = $sql;  | 
                                                        |
| 120 | + $this->magicSqlCount = $countSql;  | 
                                                        |
| 121 | + $this->columnDescList = $columnDescList;  | 
                                                        |
| 122 | + }  | 
                                                        |
| 123 | +  | 
                                                        |
| 124 | + /**  | 
                                                        |
| 125 | + * @param string $tableName  | 
                                                        |
| 126 | + *  | 
                                                        |
| 127 | + * @return ForeignKeyConstraint[]  | 
                                                        |
| 128 | + */  | 
                                                        |
| 129 | + private function getParentRelationshipForeignKeys($tableName)  | 
                                                        |
| 130 | +	{ | 
                                                        |
| 131 | +		return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { | 
                                                        |
| 132 | + return $this->getParentRelationshipForeignKeysWithoutCache($tableName);  | 
                                                        |
| 133 | + });  | 
                                                        |
| 134 | + }  | 
                                                        |
| 135 | +  | 
                                                        |
| 136 | + /**  | 
                                                        |
| 137 | + * @param string $tableName  | 
                                                        |
| 138 | + *  | 
                                                        |
| 139 | + * @return ForeignKeyConstraint[]  | 
                                                        |
| 140 | + */  | 
                                                        |
| 141 | + private function getParentRelationshipForeignKeysWithoutCache($tableName)  | 
                                                        |
| 142 | +	{ | 
                                                        |
| 143 | + $parentFks = [];  | 
                                                        |
| 144 | + $currentTable = $tableName;  | 
                                                        |
| 145 | +		while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) { | 
                                                        |
| 146 | + $currentTable = $currentFk->getForeignTableName();  | 
                                                        |
| 147 | + $parentFks[] = $currentFk;  | 
                                                        |
| 148 | + }  | 
                                                        |
| 149 | +  | 
                                                        |
| 150 | + return $parentFks;  | 
                                                        |
| 151 | + }  | 
                                                        |
| 152 | +  | 
                                                        |
| 153 | + /**  | 
                                                        |
| 154 | + * @param string $tableName  | 
                                                        |
| 155 | + *  | 
                                                        |
| 156 | + * @return ForeignKeyConstraint[]  | 
                                                        |
| 157 | + */  | 
                                                        |
| 158 | + private function getChildrenRelationshipForeignKeys(string $tableName) : array  | 
                                                        |
| 159 | +	{ | 
                                                        |
| 160 | +		return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { | 
                                                        |
| 161 | + return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName);  | 
                                                        |
| 162 | + });  | 
                                                        |
| 163 | + }  | 
                                                        |
| 164 | +  | 
                                                        |
| 165 | + /**  | 
                                                        |
| 166 | + * @param string $tableName  | 
                                                        |
| 167 | + *  | 
                                                        |
| 168 | + * @return ForeignKeyConstraint[]  | 
                                                        |
| 169 | + */  | 
                                                        |
| 170 | + private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array  | 
                                                        |
| 171 | +	{ | 
                                                        |
| 172 | + $children = $this->schemaAnalyzer->getChildrenRelationships($tableName);  | 
                                                        |
| 173 | +  | 
                                                        |
| 174 | +		if (!empty($children)) { | 
                                                        |
| 175 | +			$fksTables = array_map(function (ForeignKeyConstraint $fk) { | 
                                                        |
| 176 | + return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());  | 
                                                        |
| 177 | + }, $children);  | 
                                                        |
| 178 | +  | 
                                                        |
| 179 | +			$fks = array_merge($children, call_user_func_array('array_merge', $fksTables)); | 
                                                        |
| 180 | +  | 
                                                        |
| 181 | + return $fks;  | 
                                                        |
| 182 | +		} else { | 
                                                        |
| 183 | + return [];  | 
                                                        |
| 184 | + }  | 
                                                        |
| 185 | + }  | 
                                                        |
| 186 | +  | 
                                                        |
| 187 | + /**  | 
                                                        |
| 188 | + * Returns an item from cache or computes it using $closure and puts it in cache.  | 
                                                        |
| 189 | + *  | 
                                                        |
| 190 | + * @param string $key  | 
                                                        |
| 191 | + * @param callable $closure  | 
                                                        |
| 192 | + *  | 
                                                        |
| 193 | + * @return mixed  | 
                                                        |
| 194 | + */  | 
                                                        |
| 195 | + protected function fromCache(string $key, callable $closure)  | 
                                                        |
| 196 | +	{ | 
                                                        |
| 197 | + $item = $this->cache->fetch($key);  | 
                                                        |
| 198 | +		if ($item === false) { | 
                                                        |
| 199 | + $item = $closure();  | 
                                                        |
| 200 | + $this->cache->save($key, $item);  | 
                                                        |
| 201 | + }  | 
                                                        |
| 202 | +  | 
                                                        |
| 203 | + return $item;  | 
                                                        |
| 204 | + }  | 
                                                        |
| 205 | 205 | }  |