ExecuteCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace LimeSoda\Database;
4
5
use N98\Magento\Command\Database\AbstractDatabaseCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class ExecuteCommand extends AbstractDatabaseCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('db:execute')
17
            ->addArgument('statement', InputArgument::REQUIRED, 'SQL query')
18
            ->addOption('only-command', null, InputOption::VALUE_NONE, 'Print only mysql command. Do not execute')
19
            ->setDescription('Executes query on database defined in local.xml');
20
    }
21
22
    /**
23
     * @param \Symfony\Component\Console\Input\InputInterface $input
24
     * @param \Symfony\Component\Console\Output\OutputInterface $output
25
     * @return int|void
26
     */
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $this->detectDbSettings($output);
30
31
        $sql = $input->getArgument('statement');
32
33
        // dump data for all other tables
34
        $exec = 'mysql ' . $this->getMysqlClientToolConnectionString() . " -e '" . $sql . "'";
35
36
        if ($input->getOption('only-command')) {
37
            $output->writeln($exec);
38
        } else {
39
            exec($exec, $commandOutput, $returnValue);
40
            if ($returnValue > 0) {
41
                $output->writeln('<error>' . implode(PHP_EOL, $commandOutput) . '</error>');
42
            }
43
        }
44
    }
45
46
}