Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Tools / Console / Command / ReservedWordsCommand.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20
21 namespace Doctrine\DBAL\Tools\Console\Command;
22
23 use Symfony\Component\Console\Input\InputArgument,
24     Symfony\Component\Console\Input\InputOption,
25     Symfony\Component\Console\Command\Command,
26     Symfony\Component\Console\Input\InputInterface,
27     Symfony\Component\Console\Output\OutputInterface;
28 use Doctrine\DBAL\Platforms\Keywords\ReservedKeywordsValidator;
29
30 class ReservedWordsCommand extends Command
31 {
32     private $keywordListClasses = array(
33         'mysql'     => 'Doctrine\DBAL\Platforms\Keywords\MySQLKeywords',
34         'mssql'     => 'Doctrine\DBAL\Platforms\Keywords\MsSQLKeywords',
35         'sqlite'    => 'Doctrine\DBAL\Platforms\Keywords\SQLiteKeywords',
36         'pgsql'     => 'Doctrine\DBAL\Platforms\Keywords\PostgreSQLKeywords',
37         'oracle'    => 'Doctrine\DBAL\Platforms\Keywords\OracleKeywords',
38         'db2'       => 'Doctrine\DBAL\Platforms\Keywords\DB2Keywords',
39     );
40
41     /**
42      * If you want to add or replace a keywords list use this command
43      *
44      * @param string $name
45      * @param string $class
46      */
47     public function setKeywordListClass($name, $class)
48     {
49         $this->keywordListClasses[$name] = $class;
50     }
51
52     /**
53      * @see Console\Command\Command
54      */
55     protected function configure()
56     {
57         $this
58         ->setName('dbal:reserved-words')
59         ->setDescription('Checks if the current database contains identifiers that are reserved.')
60         ->setDefinition(array(
61             new InputOption(
62                 'list', 'l', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Keyword-List name.'
63             )
64         ))
65         ->setHelp(<<<EOT
66 Checks if the current database contains tables and columns
67 with names that are identifiers in this dialect or in other SQL dialects.
68
69 By default SQLite, MySQL, PostgreSQL, MsSQL and Oracle
70 keywords are checked:
71
72     <info>%command.full_name%</info>
73
74 If you want to check against specific dialects you can
75 pass them to the command:
76
77     <info>%command.full_name% mysql pgsql</info>
78
79 The following keyword lists are currently shipped with Doctrine:
80
81     * mysql
82     * pgsql
83     * sqlite
84     * oracle
85     * mssql
86     * db2 (Not checked by default)
87 EOT
88         );
89     }
90
91     /**
92      * @see Console\Command\Command
93      */
94     protected function execute(InputInterface $input, OutputInterface $output)
95     {
96         /* @var $conn \Doctrine\DBAL\Connection */
97         $conn = $this->getHelper('db')->getConnection();
98
99         $keywordLists = (array)$input->getOption('list');
100         if ( ! $keywordLists) {
101             $keywordLists = array('mysql', 'pgsql', 'sqlite', 'oracle', 'mssql');
102         }
103
104         $keywords = array();
105         foreach ($keywordLists as $keywordList) {
106             if (!isset($this->keywordListClasses[$keywordList])) {
107                 throw new \InvalidArgumentException(
108                     "There exists no keyword list with name '" . $keywordList . "'. ".
109                     "Known lists: " . implode(", ", array_keys($this->keywordListClasses))
110                 );
111             }
112             $class = $this->keywordListClasses[$keywordList];
113             $keywords[] = new $class;
114         }
115
116         $output->write('Checking keyword violations for <comment>' . implode(", ", $keywordLists) . "</comment>...", true);
117
118         /* @var $schema \Doctrine\DBAL\Schema\Schema */
119         $schema = $conn->getSchemaManager()->createSchema();
120         $visitor = new ReservedKeywordsValidator($keywords);
121         $schema->visit($visitor);
122
123         $violations = $visitor->getViolations();
124         if (count($violations) == 0) {
125             $output->write("No reserved keywords violations have been found!", true);
126         } else {
127             $output->write('There are <error>' . count($violations) . '</error> reserved keyword violations in your database schema:', true);
128             foreach ($violations as $violation) {
129                 $output->write('  - ' . $violation, true);
130             }
131         }
132     }
133 }