Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement initial port support for PostgreSQL driver #19466

Merged
merged 7 commits into from Sep 22, 2018
38 changes: 37 additions & 1 deletion libraries/joomla/database/driver/postgresql.php
Expand Up @@ -85,6 +85,7 @@ public function __construct($options)
$options['user'] = (isset($options['user'])) ? $options['user'] : '';
$options['password'] = (isset($options['password'])) ? $options['password'] : '';
$options['database'] = (isset($options['database'])) ? $options['database'] : '';
$options['port'] = (isset($options['port'])) ? $options['port'] : null;

// Finalize initialization
parent::__construct($options);
Expand All @@ -111,12 +112,47 @@ public function connect()
throw new JDatabaseExceptionUnsupported('The pgsql extension for PHP is not installed or enabled.');
}

/*
* pg_connect() takes the port as separate argument. Therefore, we
* have to extract it from the host string (if provided).
*/

// Check for empty port
if (!$this->options['port'])
{
// Port is empty or not set via options, check for port annotation (:) in the host string
$tmp = substr(strstr($this->options['host'], ':'), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do explode(':', $this->options['host']). Then $tmp[0] will be the domain and $tmp[1] will be the port if provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used the code from here: https://github.com/joomla-framework/database/blob/master/src/Postgresql/PostgresqlDriver.php so i think we should fix it in the source first can you send a PR with the needed changes?


if (!empty($tmp))
{
// Get the port number
if (is_numeric($tmp))
{
$this->options['port'] = $tmp;
}

// Extract the host name
$this->options['host'] = substr($this->options['host'], 0, strlen($this->options['host']) - (strlen($tmp) + 1));

// This will take care of the following notation: ":5432"
if ($this->options['host'] === '')
{
$this->options['host'] = 'localhost';
}
}
// No port annotation (:) found, setting port to default PostgreSQL port 5432
else
{
$this->options['port'] = '5432';
}
}

// Build the DSN for the connection.
$dsn = '';

if (!empty($this->options['host']))
{
$dsn .= "host={$this->options['host']} ";
$dsn .= "host={$this->options['host']} port={$this->options['port']} ";
}

$dsn .= "dbname={$this->options['database']} user={$this->options['user']} password={$this->options['password']}";
Expand Down