protected function execute(InputInterface $input, OutputInterface $output)
{
$this->validateInput($input);
if (!$input->getArgument('query') && $this->runningViaMulti) {
throw new \InvalidArgumentException('The query argument is required when running via "multi"');
}
$sshOptions = '';
$sshUrl = $this->getSelectedEnvironment()->getSshUrl($this->selectApp($input));
$util = new RelationshipsUtil($this->stdErr);
$database = $util->chooseDatabase($sshUrl, $input);
if (empty($database)) {
return 1;
}
switch ($database['scheme']) {
case 'pgsql':
$sqlCommand = "psql postgresql://{$database['username']}:{$database['password']}@{$database['host']}/{$database['path']}";
$queryOption = ' -c ';
break;
default:
$sqlCommand = "mysql --no-auto-rehash --database={$database['path']}" . " --host={$database['host']} --port={$database['port']}" . " --user={$database['username']} --password={$database['password']}";
$queryOption = ' --execute ';
break;
}
$query = $input->getArgument('query');
if ($query) {
$sqlCommand .= $queryOption . escapeshellarg($query) . ' 2>&1';
}
// Switch on pseudo-tty allocation when there is a local tty.
if ($this->isTerminal($output)) {
$sshOptions .= ' -t';
}
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
$sshOptions .= ' -vv';
} elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
$sshOptions .= ' -v';
} elseif ($output->getVerbosity() <= OutputInterface::VERBOSITY_VERBOSE) {
$sshOptions .= ' -q';
}
$command = 'ssh' . $sshOptions . ' ' . escapeshellarg($sshUrl) . ' ' . escapeshellarg($sqlCommand);
return $this->getHelper('shell')->executeSimple($command);
}