Вход Регистрация
Файл: vendor/doctrine/dbal/lib/Doctrine/DBAL/Tools/Console/Command/ReservedWordsCommand.php
Строк: 333
<?php

namespace DoctrineDBALToolsConsoleCommand;

use 
DoctrineDBALConnection;
use 
DoctrineDBALPlatformsKeywordsDB2Keywords;
use 
DoctrineDBALPlatformsKeywordsKeywordList;
use 
DoctrineDBALPlatformsKeywordsMySQL57Keywords;
use 
DoctrineDBALPlatformsKeywordsMySQL80Keywords;
use 
DoctrineDBALPlatformsKeywordsMySQLKeywords;
use 
DoctrineDBALPlatformsKeywordsOracleKeywords;
use 
DoctrineDBALPlatformsKeywordsPostgreSQL91Keywords;
use 
DoctrineDBALPlatformsKeywordsPostgreSQL92Keywords;
use 
DoctrineDBALPlatformsKeywordsPostgreSQLKeywords;
use 
DoctrineDBALPlatformsKeywordsReservedKeywordsValidator;
use 
DoctrineDBALPlatformsKeywordsSQLAnywhere11Keywords;
use 
DoctrineDBALPlatformsKeywordsSQLAnywhere12Keywords;
use 
DoctrineDBALPlatformsKeywordsSQLAnywhere16Keywords;
use 
DoctrineDBALPlatformsKeywordsSQLAnywhereKeywords;
use 
DoctrineDBALPlatformsKeywordsSQLiteKeywords;
use 
DoctrineDBALPlatformsKeywordsSQLServer2005Keywords;
use 
DoctrineDBALPlatformsKeywordsSQLServer2008Keywords;
use 
DoctrineDBALPlatformsKeywordsSQLServer2012Keywords;
use 
DoctrineDBALPlatformsKeywordsSQLServerKeywords;
use 
DoctrineDBALToolsConsoleConnectionProvider;
use 
DoctrineDeprecationsDeprecation;
use 
Exception;
use 
InvalidArgumentException;
use 
SymfonyComponentConsoleCommandCommand;
use 
SymfonyComponentConsoleInputInputInterface;
use 
SymfonyComponentConsoleInputInputOption;
use 
SymfonyComponentConsoleOutputOutputInterface;

use function 
array_keys;
use function 
assert;
use function 
count;
use function 
implode;
use function 
is_array;
use function 
is_string;

class 
ReservedWordsCommand extends Command
{
    
/** @var array<string,class-string<KeywordList>> */
    
private $keywordListClasses = [
        
'mysql'         => MySQLKeywords::class,
        
'mysql57'       => MySQL57Keywords::class,
        
'mysql80'       => MySQL80Keywords::class,
        
'sqlserver'     => SQLServerKeywords::class,
        
'sqlserver2005' => SQLServer2005Keywords::class,
        
'sqlserver2008' => SQLServer2008Keywords::class,
        
'sqlserver2012' => SQLServer2012Keywords::class,
        
'sqlite'        => SQLiteKeywords::class,
        
'pgsql'         => PostgreSQLKeywords::class,
        
'pgsql91'       => PostgreSQL91Keywords::class,
        
'pgsql92'       => PostgreSQL92Keywords::class,
        
'oracle'        => OracleKeywords::class,
        
'db2'           => DB2Keywords::class,
        
'sqlanywhere'   => SQLAnywhereKeywords::class,
        
'sqlanywhere11' => SQLAnywhere11Keywords::class,
        
'sqlanywhere12' => SQLAnywhere12Keywords::class,
        
'sqlanywhere16' => SQLAnywhere16Keywords::class,
    ];

    
/** @var ConnectionProvider|null */
    
private $connectionProvider;

    public function 
__construct(?ConnectionProvider $connectionProvider null)
    {
        
parent::__construct();
        
$this->connectionProvider $connectionProvider;
        if (
$connectionProvider !== null) {
            return;
        }

        
Deprecation::trigger(
            
'doctrine/dbal',
            
'https://github.com/doctrine/dbal/pull/3956',
            
'Not passing a connection provider as the first constructor argument is deprecated'
        
);
    }

    
/**
     * If you want to add or replace a keywords list use this command.
     *
     * @param string                    $name
     * @param class-string<KeywordList> $class
     *
     * @return void
     */
    
public function setKeywordListClass($name$class)
    {
        
$this->keywordListClasses[$name] = $class;
    }

    
/** @return void */
    
protected function configure()
    {
        
$this
        
->setName('dbal:reserved-words')
        ->
setDescription('Checks if the current database contains identifiers that are reserved.')
        ->
setDefinition([
            new 
InputOption('connection'nullInputOption::VALUE_REQUIRED'The named database connection'),
            new 
InputOption(
                
'list',
                
'l',
                
InputOption::VALUE_OPTIONAL InputOption::VALUE_IS_ARRAY,
                
'Keyword-List name.'
            
),
        ])
        ->
setHelp(<<<EOT
Checks if the current database contains tables and columns
with names that are identifiers in this dialect or in other SQL dialects.

By default SQLite, MySQL, PostgreSQL, Microsoft SQL Server, Oracle
and SQL Anywhere keywords are checked:

    <info>%command.full_name%</info>

If you want to check against specific dialects you can
pass them to the command:

    <info>%command.full_name% -l mysql -l pgsql</info>

The following keyword lists are currently shipped with Doctrine:

    * mysql
    * mysql57
    * mysql80
    * pgsql
    * pgsql92
    * sqlite
    * oracle
    * sqlserver
    * sqlserver2005
    * sqlserver2008
    * sqlserver2012
    * sqlanywhere
    * sqlanywhere11
    * sqlanywhere12
    * sqlanywhere16
    * db2 (Not checked by default)
EOT
        );
    }

    
/**
     * {@inheritdoc}
     *
     * @return int
     */
    
protected function execute(InputInterface $inputOutputInterface $output)
    {
        
$conn $this->getConnection($input);

        
$keywordLists $input->getOption('list');

        if (
is_string($keywordLists)) {
            
$keywordLists = [$keywordLists];
        } elseif (! 
is_array($keywordLists)) {
            
$keywordLists = [];
        }

        if (! 
$keywordLists) {
            
$keywordLists = [
                
'mysql',
                
'mysql57',
                
'mysql80',
                
'pgsql',
                
'pgsql92',
                
'sqlite',
                
'oracle',
                
'sqlserver',
                
'sqlserver2005',
                
'sqlserver2008',
                
'sqlserver2012',
                
'sqlanywhere',
                
'sqlanywhere11',
                
'sqlanywhere12',
                
'sqlanywhere16',
            ];
        }

        
$keywords = [];
        foreach (
$keywordLists as $keywordList) {
            if (! isset(
$this->keywordListClasses[$keywordList])) {
                throw new 
InvalidArgumentException(
                    
"There exists no keyword list with name '" $keywordList "'. " .
                    
'Known lists: ' implode(', 'array_keys($this->keywordListClasses))
                );
            }

            
$class      $this->keywordListClasses[$keywordList];
            
$keywords[] = new $class();
        }

        
$output->write(
            
'Checking keyword violations for <comment>' implode(', '$keywordLists) . '</comment>...',
            
true
        
);

        
$schema  $conn->getSchemaManager()->createSchema();
        
$visitor = new ReservedKeywordsValidator($keywords);
        
$schema->visit($visitor);

        
$violations $visitor->getViolations();
        if (
count($violations) !== 0) {
            
$output->write(
                
'There are <error>' count($violations) . '</error> reserved keyword violations'
                    
' in your database schema:',
                
true
            
);

            foreach (
$violations as $violation) {
                
$output->write('  - ' $violationtrue);
            }

            return 
1;
        }

        
$output->write('No reserved keywords violations have been found!'true);

        return 
0;
    }

    private function 
getConnection(InputInterface $input): Connection
    
{
        
$connectionName $input->getOption('connection');
        
assert(is_string($connectionName) || $connectionName === null);

        if (
$this->connectionProvider === null) {
            if (
$connectionName !== null) {
                throw new 
Exception('Specifying a connection is only supported when a ConnectionProvider is used.');
            }

            return 
$this->getHelper('db')->getConnection();
        }

        if (
$connectionName !== null) {
            return 
$this->connectionProvider->getConnection($connectionName);
        }

        return 
$this->connectionProvider->getDefaultConnection();
    }
}
Онлайн: 0
Реклама