RunSqlCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Tools\Console\Command;
  20. use Symfony\Component\Console\Input\InputArgument,
  21. Symfony\Component\Console\Input\InputOption,
  22. Symfony\Component\Console;
  23. /**
  24. * Task for executing arbitrary SQL that can come from a file or directly from
  25. * the command line.
  26. *
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 2.0
  30. * @author Benjamin Eberlei <kontakt@beberlei.de>
  31. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  32. * @author Jonathan Wage <jonwage@gmail.com>
  33. * @author Roman Borschel <roman@code-factory.org>
  34. */
  35. class RunSqlCommand extends Console\Command\Command
  36. {
  37. /**
  38. * @see Console\Command\Command
  39. */
  40. protected function configure()
  41. {
  42. $this
  43. ->setName('dbal:run-sql')
  44. ->setDescription('Executes arbitrary SQL directly from the command line.')
  45. ->setDefinition(array(
  46. new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'),
  47. new InputOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of result set.', 7)
  48. ))
  49. ->setHelp(<<<EOT
  50. Executes arbitrary SQL directly from the command line.
  51. EOT
  52. );
  53. }
  54. /**
  55. * @see Console\Command\Command
  56. */
  57. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  58. {
  59. $conn = $this->getHelper('db')->getConnection();
  60. if (($sql = $input->getArgument('sql')) === null) {
  61. throw new \RuntimeException("Argument 'SQL' is required in order to execute this command correctly.");
  62. }
  63. $depth = $input->getOption('depth');
  64. if ( ! is_numeric($depth)) {
  65. throw new \LogicException("Option 'depth' must contains an integer value");
  66. }
  67. if (preg_match('/^select/i', $sql)) {
  68. $resultSet = $conn->fetchAll($sql);
  69. } else {
  70. $resultSet = $conn->executeUpdate($sql);
  71. }
  72. ob_start();
  73. \Doctrine\Common\Util\Debug::dump($resultSet, (int) $depth);
  74. $message = ob_get_clean();
  75. $output->write($message);
  76. }
  77. }