1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Command; |
4
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Forked\ShellExecutor; |
6
|
|
|
use Db3v4l\Core\SqlExecutor\Forked\NativeClient; |
7
|
|
|
use Db3v4l\Service\DatabaseConfigurationManager; |
8
|
|
|
use Db3v4l\Service\SqlExecutorFactory; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
12
|
|
|
|
13
|
|
|
class SqlShell extends BaseCommand |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
protected static $defaultName = 'db3v4l:sql:shell'; |
16
|
|
|
|
17
|
|
|
/** @var DatabaseConfigurationManager $dbConfigurationManager */ |
|
|
|
|
18
|
|
|
protected $dbConfigurationManager; |
19
|
|
|
/** @var SqlExecutorFactory $executorFactory */ |
|
|
|
|
20
|
|
|
protected $executorFactory; |
21
|
|
|
|
22
|
|
|
public function __construct( |
|
|
|
|
23
|
|
|
DatabaseConfigurationManager $dbConfigurationManager, |
24
|
|
|
SqlExecutorFactory $executorFactory) |
|
|
|
|
25
|
|
|
{ |
|
|
|
|
26
|
|
|
$this->dbConfigurationManager = $dbConfigurationManager; |
27
|
|
|
$this->executorFactory = $executorFactory; |
28
|
|
|
|
29
|
|
|
parent::__construct(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function configure() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this |
35
|
|
|
->setDescription('Connects to one of the configured database instances, using the appropriate native sql client') |
36
|
|
|
->addOption('instance', 'i', InputOption::VALUE_REQUIRED, 'The instance to connect to', null) |
37
|
|
|
->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'The name of an existing database to use') |
|
|
|
|
38
|
|
|
; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$instanceName = $input->getOption('instance'); |
44
|
|
|
$dbName = $input->getOption('database'); |
45
|
|
|
|
46
|
|
|
if ($instanceName == null) { |
47
|
|
|
throw new \Exception("Please provide an instance name"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$dbConnectionSpec = $this->dbConfigurationManager->getInstanceConfiguration($instanceName); |
51
|
|
|
if ($dbName != '') { |
52
|
|
|
$dbConnectionSpec['dbname'] = $dbName; |
53
|
|
|
} |
54
|
|
|
$executor = $this->executorFactory->createForkedExecutor($instanceName, $dbConnectionSpec, NativeClient::EXECUTION_STRATEGY, false); |
55
|
|
|
|
56
|
|
|
if (! $executor instanceof ShellExecutor) { |
57
|
|
|
throw new \Exception("Can not start an interactive shell for databases of type '{$dbConnectionSpec['vendor']}'"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$process = $executor->getExecuteShellProcess(); |
61
|
|
|
|
62
|
|
|
$process->setTty(true); |
63
|
|
|
$process->run(); |
64
|
|
|
|
65
|
|
|
return $process->getExitCode(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|