Failed Conditions
Pull Request — 2.10.x (#4011)
by
unknown
03:07
created

RunSqlCommand::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0185
1
<?php
2
3
namespace Doctrine\DBAL\Tools\Console\Command;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
7
use Doctrine\DBAL\Tools\Dumper;
8
use Exception;
9
use LogicException;
10
use RuntimeException;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use function assert;
17
use function is_numeric;
18
use function is_string;
19
use function stripos;
20
use function trigger_error;
21
use const E_USER_DEPRECATED;
22
23
/**
24
 * Task for executing arbitrary SQL that can come from a file or directly from
25
 * the command line.
26
 */
27
class RunSqlCommand extends Command
28
{
29
    /** @var ConnectionProvider|null */
30
    private $connectionProvider;
31
32 120
    public function __construct(?ConnectionProvider $connectionProvider = null)
33
    {
34 120
        parent::__construct();
35 120
        $this->connectionProvider = $connectionProvider;
36 120
        if ($connectionProvider !== null) {
37
            return;
38
        }
39
40 120
        @trigger_error('Not passing a connection provider as the first constructor argument is deprecated', E_USER_DEPRECATED);
41 120
    }
42
43
    /** @return void */
44 120
    protected function configure()
45
    {
46
        $this
47 120
        ->setName('dbal:run-sql')
48 120
        ->setDescription('Executes arbitrary SQL directly from the command line.')
49 120
        ->setDefinition([
50 120
            new InputOption('connection', null, InputOption::VALUE_REQUIRED, 'The named database connection'),
51 120
            new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'),
52 120
            new InputOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of result set.', 7),
53 120
            new InputOption('force-fetch', null, InputOption::VALUE_NONE, 'Forces fetching the result.'),
54
        ])
55 120
        ->setHelp(<<<EOT
56 120
Executes arbitrary SQL directly from the command line.
57
EOT
58
        );
59 120
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 120
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66 120
        $conn = $this->getConnection($input);
67
68 120
        $sql = $input->getArgument('sql');
69
70 120
        if ($sql === null) {
71 116
            throw new RuntimeException("Argument 'SQL' is required in order to execute this command correctly.");
72
        }
73
74 96
        assert(is_string($sql));
75
76 96
        $depth = $input->getOption('depth');
77
78 96
        if (! is_numeric($depth)) {
79 93
            throw new LogicException("Option 'depth' must contains an integer value");
80
        }
81
82 72
        if (stripos($sql, 'select') === 0 || $input->getOption('force-fetch')) {
83 71
            $resultSet = $conn->fetchAll($sql);
84
        } else {
85 47
            $resultSet = $conn->executeUpdate($sql);
86
        }
87
88 72
        $output->write(Dumper::dump($resultSet, (int) $depth));
89
90 72
        return 0;
91
    }
92
93 120
    private function getConnection(InputInterface $input) : Connection
94
    {
95 120
        $connectionName = $input->getOption('connection');
96 120
        assert(is_string($connectionName) || $connectionName === null);
97
98 120
        if ($this->connectionProvider === null) {
99 120
            if ($connectionName !== null) {
100
                throw new Exception('Specifying a connection is only supported when a ConnectionProvider is used.');
101
            }
102
103 120
            return $this->getHelper('db')->getConnection();
104
        }
105
106
        if ($connectionName !== null) {
107
            return $this->connectionProvider->getConnection($connectionName);
108
        }
109
110
        return $this->connectionProvider->getDefaultConnection();
111
    }
112
}
113