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.
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>.
21 namespace Doctrine\DBAL\Tools\Console\Command;
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;
30 class ReservedWordsCommand extends Command
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',
42 * If you want to add or replace a keywords list use this command
45 * @param string $class
47 public function setKeywordListClass($name, $class)
49 $this->keywordListClasses[$name] = $class;
53 * @see Console\Command\Command
55 protected function configure()
58 ->setName('dbal:reserved-words')
59 ->setDescription('Checks if the current database contains identifiers that are reserved.')
60 ->setDefinition(array(
62 'list', 'l', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Keyword-List name.'
66 Checks if the current database contains tables and columns
67 with names that are identifiers in this dialect or in other SQL dialects.
69 By default SQLite, MySQL, PostgreSQL, MsSQL and Oracle
72 <info>%command.full_name%</info>
74 If you want to check against specific dialects you can
75 pass them to the command:
77 <info>%command.full_name% mysql pgsql</info>
79 The following keyword lists are currently shipped with Doctrine:
86 * db2 (Not checked by default)
92 * @see Console\Command\Command
94 protected function execute(InputInterface $input, OutputInterface $output)
96 /* @var $conn \Doctrine\DBAL\Connection */
97 $conn = $this->getHelper('db')->getConnection();
99 $keywordLists = (array)$input->getOption('list');
100 if ( ! $keywordLists) {
101 $keywordLists = array('mysql', 'pgsql', 'sqlite', 'oracle', 'mssql');
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))
112 $class = $this->keywordListClasses[$keywordList];
113 $keywords[] = new $class;
116 $output->write('Checking keyword violations for <comment>' . implode(", ", $keywordLists) . "</comment>...", true);
118 /* @var $schema \Doctrine\DBAL\Schema\Schema */
119 $schema = $conn->getSchemaManager()->createSchema();
120 $visitor = new ReservedKeywordsValidator($keywords);
121 $schema->visit($visitor);
123 $violations = $visitor->getViolations();
124 if (count($violations) == 0) {
125 $output->write("No reserved keywords violations have been found!", true);
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);