1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Schema\Visitor; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
7
|
|
|
use Doctrine\DBAL\Schema\SchemaException; |
8
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
10
|
|
|
use SplObjectStorage; |
11
|
|
|
use function strlen; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Gathers SQL statements that allow to completely drop the current schema. |
15
|
|
|
*/ |
16
|
|
|
class DropSchemaSqlCollector extends AbstractVisitor |
17
|
|
|
{ |
18
|
|
|
/** @var SplObjectStorage */ |
19
|
|
|
private $constraints; |
20
|
|
|
|
21
|
|
|
/** @var SplObjectStorage */ |
22
|
|
|
private $sequences; |
23
|
|
|
|
24
|
|
|
/** @var SplObjectStorage */ |
25
|
|
|
private $tables; |
26
|
|
|
|
27
|
|
|
/** @var AbstractPlatform */ |
28
|
|
|
private $platform; |
29
|
|
|
|
30
|
120 |
|
public function __construct(AbstractPlatform $platform) |
31
|
|
|
{ |
32
|
120 |
|
$this->platform = $platform; |
33
|
120 |
|
$this->clearQueries(); |
34
|
120 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
118 |
|
public function acceptTable(Table $table) |
40
|
|
|
{ |
41
|
118 |
|
$this->tables->attach($table); |
42
|
118 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
72 |
|
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
48
|
|
|
{ |
49
|
72 |
|
if (! $this->platform->supportsCreateDropForeignKeyConstraints()) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
72 |
|
if (strlen($fkConstraint->getName()) === 0) { |
54
|
47 |
|
throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint); |
55
|
|
|
} |
56
|
|
|
|
57
|
71 |
|
$this->constraints->attach($fkConstraint, $localTable); |
58
|
71 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
24 |
|
public function acceptSequence(Sequence $sequence) |
64
|
|
|
{ |
65
|
24 |
|
$this->sequences->attach($sequence); |
66
|
24 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
120 |
|
public function clearQueries() |
72
|
|
|
{ |
73
|
120 |
|
$this->constraints = new SplObjectStorage(); |
74
|
120 |
|
$this->sequences = new SplObjectStorage(); |
75
|
120 |
|
$this->tables = new SplObjectStorage(); |
76
|
120 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string[] |
80
|
|
|
*/ |
81
|
119 |
|
public function getQueries() |
82
|
|
|
{ |
83
|
119 |
|
$sql = []; |
84
|
|
|
|
85
|
|
|
/** @var ForeignKeyConstraint $fkConstraint */ |
86
|
119 |
|
foreach ($this->constraints as $fkConstraint) { |
87
|
71 |
|
$localTable = $this->constraints[$fkConstraint]; |
88
|
71 |
|
$sql[] = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @var Sequence $sequence */ |
92
|
119 |
|
foreach ($this->sequences as $sequence) { |
93
|
24 |
|
$sql[] = $this->platform->getDropSequenceSQL($sequence); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @var Table $table */ |
97
|
119 |
|
foreach ($this->tables as $table) { |
98
|
118 |
|
$sql[] = $this->platform->getDropTableSQL($table); |
99
|
|
|
} |
100
|
|
|
|
101
|
119 |
|
return $sql; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|