Completed
Push — master ( bd5921...d9293e )
by Christian
17:54 queued 08:59
created

QueryCommand::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Database;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use N98\Util\Exec;
10
11
class QueryCommand extends AbstractDatabaseCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('db:query')
17
            ->addArgument('query', InputArgument::OPTIONAL, 'SQL query')
18
            ->addOption('only-command', null, InputOption::VALUE_NONE, 'Print only mysql command. Do not execute')
19
            ->setDescription('Executes an SQL query on the database defined in local.xml')
20
        ;
21
22
        $help = <<<HELP
23
Executes an SQL query on the current configured database. Wrap your SQL in
24
single or double quotes.
25
26
If your query produces a result (e.g. a SELECT statement), the output of the
27
mysql cli tool will be returned.
28
29
* Requires MySQL CLI tools installed on your system.
30
31
HELP;
32
        $this->setHelp($help);
33
    }
34
35
    /**
36
     * @return bool
37
     */
38
    public function isEnabled()
39
    {
40
        return Exec::allowed();
41
    }
42
43
    /**
44
     * Returns the query string with escaped ' characters so it can be used
45
     * within the mysql -e argument.
46
     *
47
     * The -e argument is enclosed by single quotes. As you can't escape
48
     * the single quote within the single quote, you have to end the quote,
49
     * then escape the single quote character and reopen the quote.
50
     *
51
     * @param string $query
52
     * @return string
53
     */
54
    protected function getEscapedSql($query)
55
    {
56
        return str_replace("'", "'\''", $query);
57
    }
58
59
    /**
60
     * @param InputInterface  $input
61
     * @param OutputInterface $output
62
     *
63
     * @return int|void
64
     */
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $this->detectDbSettings($output);
68
69
        $query = $this->getOrAskForArgument('query', $input, $output, 'SQL Query');
70
71
        /** @var $helper \N98\Util\Console\Helper\DatabaseHelper */
72
        $helper = $this->getHelper('database');
73
        $exec   = sprintf('mysql %s -e %s', $helper->getMysqlClientToolConnectionString(), escapeshellarg($query));
74
75
        if ($input->getOption('only-command')) {
76
            $output->writeln($exec);
77
        } else {
78
            Exec::run($exec, $commandOutput, $returnValue);
79
            $output->writeln($commandOutput);
80
            if ($returnValue > 0) {
81
                $output->writeln('<error>' . $commandOutput . '</error>');
82
            }
83
        }
84
    }
85
}
86