Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Tools\Console\Command;
4
 
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Exception;
7
use Doctrine\DBAL\Platforms\Keywords\DB2Keywords;
8
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
9
use Doctrine\DBAL\Platforms\Keywords\MariaDb102Keywords;
10
use Doctrine\DBAL\Platforms\Keywords\MySQL57Keywords;
11
use Doctrine\DBAL\Platforms\Keywords\MySQL80Keywords;
12
use Doctrine\DBAL\Platforms\Keywords\MySQLKeywords;
13
use Doctrine\DBAL\Platforms\Keywords\OracleKeywords;
14
use Doctrine\DBAL\Platforms\Keywords\PostgreSQL100Keywords;
15
use Doctrine\DBAL\Platforms\Keywords\PostgreSQL94Keywords;
16
use Doctrine\DBAL\Platforms\Keywords\ReservedKeywordsValidator;
17
use Doctrine\DBAL\Platforms\Keywords\SQLiteKeywords;
18
use Doctrine\DBAL\Platforms\Keywords\SQLServer2012Keywords;
19
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
20
use Doctrine\Deprecations\Deprecation;
21
use InvalidArgumentException;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
 
27
use function array_keys;
28
use function assert;
29
use function count;
30
use function implode;
31
use function is_array;
32
use function is_string;
33
 
34
/** @deprecated Use database documentation instead. */
35
class ReservedWordsCommand extends Command
36
{
37
    /** @var array<string,KeywordList> */
38
    private array $keywordLists;
39
 
40
    private ConnectionProvider $connectionProvider;
41
 
42
    public function __construct(ConnectionProvider $connectionProvider)
43
    {
44
        Deprecation::triggerIfCalledFromOutside(
45
            'doctrine/dbal',
46
            'https://github.com/doctrine/dbal/pull/5431',
47
            'ReservedWordsCommand is deprecated. Use database documentation instead.',
48
        );
49
 
50
        parent::__construct();
51
 
52
        $this->connectionProvider = $connectionProvider;
53
 
54
        $this->keywordLists = [
55
            'db2'        => new DB2Keywords(),
56
            'mariadb102' => new MariaDb102Keywords(),
57
            'mysql'      => new MySQLKeywords(),
58
            'mysql57'    => new MySQL57Keywords(),
59
            'mysql80'    => new MySQL80Keywords(),
60
            'oracle'     => new OracleKeywords(),
61
            'pgsql'      => new PostgreSQL94Keywords(),
62
            'pgsql100'   => new PostgreSQL100Keywords(),
63
            'sqlite'     => new SQLiteKeywords(),
64
            'sqlserver'  => new SQLServer2012Keywords(),
65
        ];
66
    }
67
 
68
    /**
69
     * Add or replace a keyword list.
70
     */
71
    public function setKeywordList(string $name, KeywordList $keywordList): void
72
    {
73
        $this->keywordLists[$name] = $keywordList;
74
    }
75
 
76
    /**
77
     * If you want to add or replace a keywords list use this command.
78
     *
79
     * @param string                    $name
80
     * @param class-string<KeywordList> $class
81
     *
82
     * @return void
83
     */
84
    public function setKeywordListClass($name, $class)
85
    {
86
        Deprecation::trigger(
87
            'doctrine/dbal',
88
            'https://github.com/doctrine/dbal/issues/4510',
89
            'ReservedWordsCommand::setKeywordListClass() is deprecated,'
90
                . ' use ReservedWordsCommand::setKeywordList() instead.',
91
        );
92
 
93
        $this->keywordLists[$name] = new $class();
94
    }
95
 
96
    /** @return void */
97
    protected function configure()
98
    {
99
        $this
100
        ->setName('dbal:reserved-words')
101
        ->setDescription('Checks if the current database contains identifiers that are reserved.')
102
        ->setDefinition([
103
            new InputOption('connection', null, InputOption::VALUE_REQUIRED, 'The named database connection'),
104
            new InputOption(
105
                'list',
106
                'l',
107
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
108
                'Keyword-List name.',
109
            ),
110
        ])
111
        ->setHelp(<<<'EOT'
112
Checks if the current database contains tables and columns
113
with names that are identifiers in this dialect or in other SQL dialects.
114
 
115
By default all supported platform keywords are checked:
116
 
117
    <info>%command.full_name%</info>
118
 
119
If you want to check against specific dialects you can
120
pass them to the command:
121
 
122
    <info>%command.full_name% -l mysql -l pgsql</info>
123
 
124
The following keyword lists are currently shipped with Doctrine:
125
 
126
    * db2
127
    * mariadb102
128
    * mysql
129
    * mysql57
130
    * mysql80
131
    * oracle
132
    * pgsql
133
    * pgsql100
134
    * sqlite
135
    * sqlserver
136
EOT);
137
    }
138
 
139
    /**
140
     * {@inheritdoc}
141
     *
142
     * @return int
143
     *
144
     * @throws Exception
145
     */
146
    protected function execute(InputInterface $input, OutputInterface $output)
147
    {
148
        $output->writeln(
149
            '<comment>The <info>dbal:reserved-words</info> command is deprecated.</comment>'
150
                . ' Use the documentation on the used database platform(s) instead.',
151
        );
152
        $output->writeln('');
153
 
154
        $conn = $this->getConnection($input);
155
 
156
        $keywordLists = $input->getOption('list');
157
 
158
        if (is_string($keywordLists)) {
159
            $keywordLists = [$keywordLists];
160
        } elseif (! is_array($keywordLists)) {
161
            $keywordLists = [];
162
        }
163
 
164
        if (count($keywordLists) === 0) {
165
            $keywordLists = array_keys($this->keywordLists);
166
        }
167
 
168
        $keywords = [];
169
        foreach ($keywordLists as $keywordList) {
170
            if (! isset($this->keywordLists[$keywordList])) {
171
                throw new InvalidArgumentException(
172
                    "There exists no keyword list with name '" . $keywordList . "'. " .
173
                    'Known lists: ' . implode(', ', array_keys($this->keywordLists)),
174
                );
175
            }
176
 
177
            $keywords[] = $this->keywordLists[$keywordList];
178
        }
179
 
180
        $output->write(
181
            'Checking keyword violations for <comment>' . implode(', ', $keywordLists) . '</comment>...',
182
            true,
183
        );
184
 
185
        $schema  = $conn->getSchemaManager()->introspectSchema();
186
        $visitor = new ReservedKeywordsValidator($keywords);
187
        $schema->visit($visitor);
188
 
189
        $violations = $visitor->getViolations();
190
        if (count($violations) !== 0) {
191
            $output->write(
192
                'There are <error>' . count($violations) . '</error> reserved keyword violations'
193
                . ' in your database schema:',
194
                true,
195
            );
196
 
197
            foreach ($violations as $violation) {
198
                $output->write('  - ' . $violation, true);
199
            }
200
 
201
            return 1;
202
        }
203
 
204
        $output->write('No reserved keywords violations have been found!', true);
205
 
206
        return 0;
207
    }
208
 
209
    private function getConnection(InputInterface $input): Connection
210
    {
211
        $connectionName = $input->getOption('connection');
212
        assert(is_string($connectionName) || $connectionName === null);
213
 
214
        if ($connectionName !== null) {
215
            return $this->connectionProvider->getConnection($connectionName);
216
        }
217
 
218
        return $this->connectionProvider->getDefaultConnection();
219
    }
220
}