2009-08-31 20:21:29 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools\Cli\Tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Task for executing arbitrary SQL that can come from a file or directly from
|
|
|
|
* the command line.
|
|
|
|
*
|
|
|
|
* @author robo
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
class RunSqlTask extends AbstractTask
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function extendedHelp()
|
|
|
|
{
|
2009-09-01 20:48:28 +04:00
|
|
|
$printer = $this->getPrinter();
|
|
|
|
|
|
|
|
$printer->write('Task: ')->writeln('run-sql', 'KEYWORD')
|
|
|
|
->write('Synopsis: ');
|
|
|
|
$this->_writeSynopsis($printer);
|
|
|
|
|
|
|
|
$printer->writeln('Description: Executes arbitrary SQL from a file or directly from the command line.')
|
|
|
|
->writeln('Options:')
|
|
|
|
->write('--sql=<SQL>', 'KEYWORD')
|
|
|
|
->writeln("\tThe SQL to execute.")
|
|
|
|
->write('--file=<path>', 'KEYWORD')
|
|
|
|
->writeln("\tThe path to the file with the SQL to execute.");
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function basicHelp()
|
|
|
|
{
|
2009-09-01 20:48:28 +04:00
|
|
|
$this->_writeSynopsis($this->getPrinter());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _writeSynopsis($printer)
|
|
|
|
{
|
|
|
|
$printer->write('run-sql', 'KEYWORD')
|
|
|
|
->writeln(' --file=<path> | --sql=<SQL>', 'INFO');
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the task.
|
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$args = $this->getArguments();
|
|
|
|
|
|
|
|
if (isset($args['file'])) {
|
|
|
|
//TODO
|
|
|
|
} else if (isset($args['sql'])) {
|
|
|
|
if (preg_match('/^select/i', $args['sql'])) {
|
|
|
|
$stmt = $this->_em->getConnection()->execute($args['sql']);
|
|
|
|
var_dump($stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC));
|
|
|
|
} else {
|
|
|
|
var_dump($this->_em->getConnection()->executeUpdate($args['sql']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|