protected function execute(InputInterface $input, OutputInterface $output)
{
$this->detectDbSettings($output);
$settings = array();
foreach ($this->dbSettings as $key => $value) {
$settings[$key] = (string) $value;
}
$isSocketConnect = $this->dbSettings->isSocketConnect();
// note: there is no need to specify the default port neither for PDO, nor JDBC nor CLI.
$portOrDefault = isset($this->dbSettings['port']) ? $this->dbSettings['port'] : 3306;
$pdoConnectionString = '';
if ($isSocketConnect) {
$pdoConnectionString = sprintf('mysql:unix_socket=%s;dbname=%s', $this->dbSettings['unix_socket'], $this->dbSettings['dbname']);
} else {
$pdoConnectionString = sprintf('mysql:host=%s;port=%s;dbname=%s', $this->dbSettings['host'], $portOrDefault, $this->dbSettings['dbname']);
}
$settings['PDO-Connection-String'] = $pdoConnectionString;
$jdbcConnectionString = '';
if ($isSocketConnect) {
// isn't supported according to this post: http://stackoverflow.com/a/18493673/145829
$jdbcConnectionString = 'Connecting using JDBC through a unix socket isn\'t supported!';
} else {
$jdbcConnectionString = sprintf('jdbc:mysql://%s:%s/%s?username=%s&password=%s', $this->dbSettings['host'], $portOrDefault, $this->dbSettings['dbname'], $this->dbSettings['username'], $this->dbSettings['password']);
}
$settings['JDBC-Connection-String'] = $jdbcConnectionString;
$mysqlCliString = 'mysql ' . $this->getHelper('database')->getMysqlClientToolConnectionString();
$settings['MySQL-Cli-String'] = $mysqlCliString;
$rows = array();
foreach ($settings as $settingName => $settingValue) {
$rows[] = array($settingName, $settingValue);
}
if (($settingArgument = $input->getArgument('setting')) !== null) {
if (!isset($settings[$settingArgument])) {
throw new InvalidArgumentException('Unknown setting: ' . $settingArgument);
}
$output->writeln((string) $settings[$settingArgument]);
} else {
$this->getHelper('table')->setHeaders(array('Name', 'Value'))->renderByFormat($output, $rows, $input->getOption('format'));
}
}