RunSqlCommand.php 3.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Tools\Console\Command;

Benjamin Morel's avatar
Benjamin Morel committed
22 23 24 25 26
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
27 28 29 30

/**
 * Task for executing arbitrary SQL that can come from a file or directly from
 * the command line.
31
 *
Benjamin Morel's avatar
Benjamin Morel committed
32 33 34 35 36 37
 * @link   www.doctrine-project.org
 * @since  2.0
 * @author Benjamin Eberlei <kontakt@beberlei.de>
 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author Jonathan Wage <jonwage@gmail.com>
 * @author Roman Borschel <roman@code-factory.org>
38
 */
Benjamin Morel's avatar
Benjamin Morel committed
39
class RunSqlCommand extends Command
40 41
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
42
     * {@inheritdoc}
43 44 45 46 47 48 49 50
     */
    protected function configure()
    {
        $this
        ->setName('dbal:run-sql')
        ->setDescription('Executes arbitrary SQL directly from the command line.')
        ->setDefinition(array(
            new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'),
51
            new InputOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of result set.', 7)
52 53 54 55 56 57 58 59
        ))
        ->setHelp(<<<EOT
Executes arbitrary SQL directly from the command line.
EOT
        );
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
60
     * {@inheritdoc}
61
     */
Benjamin Morel's avatar
Benjamin Morel committed
62
    protected function execute(InputInterface $input, OutputInterface $output)
63 64 65 66 67 68 69 70 71 72 73 74
    {
        $conn = $this->getHelper('db')->getConnection();

        if (($sql = $input->getArgument('sql')) === null) {
            throw new \RuntimeException("Argument 'SQL' is required in order to execute this command correctly.");
        }

        $depth = $input->getOption('depth');

        if ( ! is_numeric($depth)) {
            throw new \LogicException("Option 'depth' must contains an integer value");
        }
75

Tobias Schultze's avatar
Tobias Schultze committed
76 77
        if (stripos($sql, 'select') === 0) {
            $resultSet = $conn->fetchAll($sql);
78
        } else {
79
            $resultSet = $conn->executeUpdate($sql);
80 81
        }

82
        ob_start();
83
        \Doctrine\Common\Util\Debug::dump($resultSet, (int) $depth);
84
        $message = ob_get_clean();
85

86
        $output->write($message);
87
    }
88
}