ImportCommand.php 4.62 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
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
26 27 28 29 30

/**
 * Task for executing arbitrary SQL that can come from a file or directly from
 * the command line.
 *
Benjamin Morel's avatar
Benjamin Morel committed
31 32 33 34 35 36
 * @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>
37
 */
Benjamin Morel's avatar
Benjamin Morel committed
38
class ImportCommand extends Command
39 40
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
41
     * {@inheritdoc}
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
     */
    protected function configure()
    {
        $this
        ->setName('dbal:import')
        ->setDescription('Import SQL file(s) directly to Database.')
        ->setDefinition(array(
            new InputArgument(
                'file', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'File path(s) of SQL to be executed.'
            )
        ))
        ->setHelp(<<<EOT
Import SQL file(s) directly to Database.
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
    {
        $conn = $this->getHelper('db')->getConnection();

66
        if (($fileNames = $input->getArgument('file')) !== null) {
67
            foreach ((array) $fileNames as $fileName) {
68
                $filePath = realpath($fileName);
Steve Müller's avatar
Steve Müller committed
69

70 71 72 73
                // Phar compatibility.
                if (false === $filePath) {
                    $filePath = $fileName;
                }
Steve Müller's avatar
Steve Müller committed
74

75
                if ( ! file_exists($filePath)) {
76
                    throw new \InvalidArgumentException(
77
                        sprintf("SQL file '<info>%s</info>' does not exist.", $filePath)
78
                    );
Steve Müller's avatar
Steve Müller committed
79
                } elseif ( ! is_readable($filePath)) {
80
                    throw new \InvalidArgumentException(
81
                        sprintf("SQL file '<info>%s</info>' does not have read permissions.", $filePath)
82 83 84
                    );
                }

85 86
                $output->write(sprintf("Processing file '<info>%s</info>'... ", $filePath));
                $sql = file_get_contents($filePath);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

                if ($conn instanceof \Doctrine\DBAL\Driver\PDOConnection) {
                    // PDO Drivers
                    try {
                        $lines = 0;

                        $stmt = $conn->prepare($sql);
                        $stmt->execute();

                        do {
                            // Required due to "MySQL has gone away!" issue
                            $stmt->fetch();
                            $stmt->closeCursor();

                            $lines++;
                        } while ($stmt->nextRowset());

                        $output->write(sprintf('%d statements executed!', $lines) . PHP_EOL);
                    } catch (\PDOException $e) {
                        $output->write('error!' . PHP_EOL);

                        throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
                    }
                } else {
                    // Non-PDO Drivers (ie. OCI8 driver)
                    $stmt = $conn->prepare($sql);
                    $rs = $stmt->execute();

                    if ($rs) {
116
                        $output->writeln('OK!' . PHP_EOL);
117 118 119 120 121 122 123 124 125 126 127 128 129
                    } else {
                        $error = $stmt->errorInfo();

                        $output->write('error!' . PHP_EOL);

                        throw new \RuntimeException($error[2], $error[0]);
                    }

                    $stmt->closeCursor();
                }
            }
        }
    }
130
}