@@ -14,136 +14,136 @@ |
||
| 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) { return $table->getName(); }, $junctionTables); |
|
| 128 | - $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
| 129 | - |
|
| 130 | - $fks = []; |
|
| 131 | - foreach ($this->getSchema()->getTables() as $table) { |
|
| 132 | - foreach ($table->getForeignKeys() as $fk) { |
|
| 133 | - if ($fk->getForeignTableName() === $tableName) { |
|
| 134 | - if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
| 135 | - continue; |
|
| 136 | - } |
|
| 137 | - foreach ($childrenRelationships as $childFk) { |
|
| 138 | - if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
| 139 | - continue 2; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - $fks[] = $fk; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return $fks; |
|
| 148 | - } |
|
| 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) { return $table->getName(); }, $junctionTables); |
|
| 128 | + $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
| 129 | + |
|
| 130 | + $fks = []; |
|
| 131 | + foreach ($this->getSchema()->getTables() as $table) { |
|
| 132 | + foreach ($table->getForeignKeys() as $fk) { |
|
| 133 | + if ($fk->getForeignTableName() === $tableName) { |
|
| 134 | + if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
| 135 | + continue; |
|
| 136 | + } |
|
| 137 | + foreach ($childrenRelationships as $childFk) { |
|
| 138 | + if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
| 139 | + continue 2; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + $fks[] = $fk; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return $fks; |
|
| 148 | + } |
|
| 149 | 149 | } |