| 148 |
lars |
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 Doctrine\Deprecations\Deprecation;
|
|
|
11 |
use SplObjectStorage;
|
|
|
12 |
|
|
|
13 |
use function assert;
|
|
|
14 |
use function strlen;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Gathers SQL statements that allow to completely drop the current schema.
|
|
|
18 |
*
|
|
|
19 |
* @deprecated Use {@link DropSchemaObjectsSQLBuilder} instead.
|
|
|
20 |
*/
|
|
|
21 |
class DropSchemaSqlCollector extends AbstractVisitor
|
|
|
22 |
{
|
|
|
23 |
private SplObjectStorage $constraints;
|
|
|
24 |
private SplObjectStorage $sequences;
|
|
|
25 |
private SplObjectStorage $tables;
|
|
|
26 |
private AbstractPlatform $platform;
|
|
|
27 |
|
|
|
28 |
public function __construct(AbstractPlatform $platform)
|
|
|
29 |
{
|
|
|
30 |
Deprecation::trigger(
|
|
|
31 |
'doctrine/dbal',
|
|
|
32 |
'https://github.com/doctrine/dbal/pull/5416',
|
|
|
33 |
'DropSchemaSqlCollector is deprecated. Use DropSchemaObjectsSQLBuilder instead.',
|
|
|
34 |
);
|
|
|
35 |
|
|
|
36 |
$this->platform = $platform;
|
|
|
37 |
$this->initializeQueries();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* {@inheritdoc}
|
|
|
42 |
*/
|
|
|
43 |
public function acceptTable(Table $table)
|
|
|
44 |
{
|
|
|
45 |
$this->tables->attach($table);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* {@inheritdoc}
|
|
|
50 |
*/
|
|
|
51 |
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
|
|
|
52 |
{
|
|
|
53 |
if (strlen($fkConstraint->getName()) === 0) {
|
|
|
54 |
throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
$this->constraints->attach($fkConstraint, $localTable);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* {@inheritdoc}
|
|
|
62 |
*/
|
|
|
63 |
public function acceptSequence(Sequence $sequence)
|
|
|
64 |
{
|
|
|
65 |
$this->sequences->attach($sequence);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/** @return void */
|
|
|
69 |
public function clearQueries()
|
|
|
70 |
{
|
|
|
71 |
$this->initializeQueries();
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/** @return string[] */
|
|
|
75 |
public function getQueries()
|
|
|
76 |
{
|
|
|
77 |
$sql = [];
|
|
|
78 |
|
|
|
79 |
foreach ($this->constraints as $fkConstraint) {
|
|
|
80 |
assert($fkConstraint instanceof ForeignKeyConstraint);
|
|
|
81 |
$localTable = $this->constraints[$fkConstraint];
|
|
|
82 |
$sql[] = $this->platform->getDropForeignKeySQL(
|
|
|
83 |
$fkConstraint->getQuotedName($this->platform),
|
|
|
84 |
$localTable->getQuotedName($this->platform),
|
|
|
85 |
);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
foreach ($this->sequences as $sequence) {
|
|
|
89 |
assert($sequence instanceof Sequence);
|
|
|
90 |
$sql[] = $this->platform->getDropSequenceSQL($sequence->getQuotedName($this->platform));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
foreach ($this->tables as $table) {
|
|
|
94 |
assert($table instanceof Table);
|
|
|
95 |
$sql[] = $this->platform->getDropTableSQL($table->getQuotedName($this->platform));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return $sql;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
private function initializeQueries(): void
|
|
|
102 |
{
|
|
|
103 |
$this->constraints = new SplObjectStorage();
|
|
|
104 |
$this->sequences = new SplObjectStorage();
|
|
|
105 |
$this->tables = new SplObjectStorage();
|
|
|
106 |
}
|
|
|
107 |
}
|