|
| 1 | +<?php |
| 2 | +namespace Mouf\Database\SchemaAnalyzer; |
| 3 | +use Doctrine\DBAL\Schema\Schema; |
| 4 | +use Doctrine\DBAL\Schema\Table; |
| 5 | +use Fhaculty\Graph\Graph; |
| 6 | + |
| 7 | +/** |
| 8 | + * This class can analyze a database model. |
| 9 | + * In this class you will find: |
| 10 | + * |
| 11 | + * - Functions to automatically detect **junction tables** |
| 12 | + * - Functions to compute the shortest path between 2 tables based on the relationships stored in the schema. |
| 13 | + */ |
| 14 | +class SchemaAnalyzer |
| 15 | +{ |
| 16 | + private static $WEIGHT_FK = 1; |
| 17 | + private static $WEIGHT_JOINTURE_TABLE = 1.5; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var Schema |
| 21 | + */ |
| 22 | + private $schema; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param Schema $schema |
| 26 | + */ |
| 27 | + public function __construct(Schema $schema) |
| 28 | + { |
| 29 | + $this->schema = $schema; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Detect all junctions tables in the schema. |
| 34 | + * A table is a junction table if: |
| 35 | + * - it has exactly 2 foreign keys |
| 36 | + * - it has only 2 columns (or 3 columns if the third one is an autoincremented primary key) |
| 37 | + * |
| 38 | + * |
| 39 | + * @return Table[] |
| 40 | + */ |
| 41 | + public function detectJunctionTables() { |
| 42 | + return array_filter($this->schema->getTables(), [$this, "isJunctionTable"]); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns true if $table is a junction table. |
| 47 | + * I.e: |
| 48 | + * - it must have exactly 2 foreign keys |
| 49 | + * - it must have only 2 columns (or 3 columns if the third one is an autoincremented primary key) |
| 50 | + * |
| 51 | + * @param Table $table |
| 52 | + * @return bool |
| 53 | + */ |
| 54 | + private function isJunctionTable(Table $table) { |
| 55 | + $foreignKeys = $table->getForeignKeys(); |
| 56 | + if (count($foreignKeys) != 2) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + $columns = $table->getColumns(); |
| 61 | + if (count($columns) < 2 || count($columns) > 3) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + $pkColumns = $table->getPrimaryKeyColumns(); |
| 66 | + |
| 67 | + if (count($pkColumns) == 1 && count($columns) == 2) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + if (count($pkColumns) != 1 && count($columns) == 3) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + $fkColumnNames = []; |
| 76 | + foreach ($foreignKeys as $foreignKey) { |
| 77 | + $fkColumns = $foreignKey->getColumns(); |
| 78 | + if (count($fkColumns) != 1) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + $fkColumnNames[$fkColumns[0]] = true; |
| 82 | + } |
| 83 | + |
| 84 | + // Let's check that the third column (the ID is NOT a foreign key) |
| 85 | + if (count($columns) == 3 && isset($fkColumnNames[$pkColumns[0]])) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the shortest path between 2 tables. |
| 94 | + * |
| 95 | + * @param $fromTable |
| 96 | + * @param $toTable |
| 97 | + */ |
| 98 | + public function getShortestPath($fromTable, $toTable) { |
| 99 | + $graph = $this->buildSchemaGraph(); |
| 100 | + // TODO |
| 101 | + } |
| 102 | + |
| 103 | + public function buildSchemaGraph() { |
| 104 | + $graph = new Graph(); |
| 105 | + |
| 106 | + // First, let's create all the vertex |
| 107 | + foreach ($this->schema->getTables() as $table) { |
| 108 | + $graph->createVertex($table->getName()); |
| 109 | + } |
| 110 | + |
| 111 | + // Then, let's create all the edges |
| 112 | + foreach ($this->schema->getTables() as $table) { |
| 113 | + foreach ($table->getForeignKeys() as $fk) { |
| 114 | + // Create an undirected edge, with weight = 1 |
| 115 | + $edge = $graph->getVertex($table->getName())->createEdge($graph->getVertex($fk->getForeignTableName())); |
| 116 | + $edge->setWeight(self::$WEIGHT_FK); |
| 117 | + $edge->getAttributeBag()->setAttribute("fk", $fk); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Finally, let's add virtual edges for the junction tables |
| 122 | + foreach ($this->detectJunctionTables() as $junctionTable) { |
| 123 | + $tables = []; |
| 124 | + foreach ($junctionTable->getForeignKeys() as $fk) { |
| 125 | + $tables[] = $fk->getForeignTableName(); |
| 126 | + } |
| 127 | + |
| 128 | + $edge = $graph->getVertex($tables[0]->getName())->createEdge($graph->getVertex($tables[1]->getName())); |
| 129 | + $edge->setWeight(self::$WEIGHT_JOINTURE_TABLE); |
| 130 | + $edge->getAttributeBag()->setAttribute("junction", $junctionTable); |
| 131 | + } |
| 132 | + |
| 133 | + return $graph; |
| 134 | + } |
| 135 | +} |
0 commit comments