1
|
|
|
<?php |
2
|
|
|
namespace Mouf\Database\TDBM; |
3
|
|
|
use Doctrine\Common\Cache\Cache; |
4
|
|
|
use Doctrine\Common\Cache\VoidCache; |
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Schema\Column; |
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
10
|
|
|
use Mouf\Database\TDBM\Utils\AbstractBeanPropertyDescriptor; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class is used to analyze the schema and return valuable information / hints. |
14
|
|
|
*/ |
15
|
|
|
class TDBMSchemaAnalyzer |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private $connection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Schema |
22
|
|
|
*/ |
23
|
|
|
private $schema; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $cachePrefix; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Cache |
32
|
|
|
*/ |
33
|
|
|
private $cache; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var SchemaAnalyzer |
37
|
|
|
*/ |
38
|
|
|
private $schemaAnalyzer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Connection $connection The DBAL DB connection to use |
42
|
|
|
* @param Cache $cache A cache service to be used |
43
|
|
|
* @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
44
|
|
|
* Will be automatically created if not passed. |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer) { |
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
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getCachePrefix() { |
57
|
|
|
if ($this->cachePrefix === null) { |
58
|
|
|
$this->cachePrefix = hash('md4', $this->connection->getHost()."-".$this->connection->getPort()."-".$this->connection->getDatabase()."-".$this->connection->getDriver()->getName()); |
59
|
|
|
} |
60
|
|
|
return $this->cachePrefix; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the (cached) schema. |
65
|
|
|
* |
66
|
|
|
* @return Schema |
67
|
|
|
*/ |
68
|
|
|
public function getSchema() { |
69
|
|
|
if ($this->schema === null) { |
70
|
|
|
$cacheKey = $this->getCachePrefix().'_schema'; |
71
|
|
|
if ($this->cache->contains($cacheKey)) { |
72
|
|
|
$this->schema = $this->cache->fetch($cacheKey); |
73
|
|
|
} else { |
74
|
|
|
$this->schema = $this->connection->getSchemaManager()->createSchema(); |
75
|
|
|
$this->cache->save($cacheKey, $this->schema); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return $this->schema; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the list of pivot tables linked to table $tableName |
83
|
|
|
* @param string $tableName |
84
|
|
|
* @return array|string[] |
85
|
|
|
*/ |
86
|
|
|
public function getPivotTableLinkedToTable($tableName) { |
87
|
|
|
$cacheKey = $this->getCachePrefix().'_pivottables_link'; |
88
|
|
|
if ($this->cache->contains($cacheKey)) { |
89
|
|
|
return $this->cache->fetch($cacheKey); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$pivotTables = []; |
93
|
|
|
|
94
|
|
|
$junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
95
|
|
|
foreach ($junctionTables as $table) { |
96
|
|
|
$fks = $table->getForeignKeys(); |
97
|
|
|
foreach ($fks as $fk) { |
98
|
|
|
if ($fk->getForeignTableName() == $tableName) { |
99
|
|
|
$pivotTables[] = $table->getName(); |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->cache->save($cacheKey, $pivotTables); |
106
|
|
|
return $pivotTables; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|