Skip to content

Commit

Permalink
Implement initial port support for PostgreSQL driver (#19466)
Browse files Browse the repository at this point in the history
* implement initial port support for PostgreSQL driver

* cast to integer

* implement auto port detection from the framework

* Finally do the things i have promised to do at 27 Jan 2018

* povided vs provided
  • Loading branch information
zero-24 authored and Michael Babker committed Sep 22, 2018
1 parent 549ff03 commit 2b707d5
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion libraries/joomla/database/driver/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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 @@ -112,12 +113,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);

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

0 comments on commit 2b707d5

Please sign in to comment.