diff options
Diffstat (limited to 'src/classes/Connection.php')
-rw-r--r-- | src/classes/Connection.php | 150 |
1 files changed, 79 insertions, 71 deletions
diff --git a/src/classes/Connection.php b/src/classes/Connection.php index 1fdf7c0a..c63ffe21 100644 --- a/src/classes/Connection.php +++ b/src/classes/Connection.php @@ -6,8 +6,6 @@ namespace PHPPgAdmin; -use ADODB2_postgres; - /** * @file * Class to represent a database connection @@ -21,8 +19,11 @@ class Connection public $conn; public $platform = 'UNKNOWN'; - /** @var string */ - private $pgVersion; + + /** + * @var string + */ + public $driver; protected $container; @@ -42,11 +43,18 @@ class Connection '9.1' => 'Postgres91', '9.0' => 'Postgres90', ]; -private $adodb_driver='postgres9'; // or pdo + + /** + * @var string + */ + private $pgVersion; + + private $adodb_driver = 'postgres9'; + + // or pdo // The backend platform. Set to UNKNOWN by default. private $_connection_result; - /** @var string */ - public $driver; + /** * Creates a new connection. Will actually make a database connection. * @@ -73,8 +81,6 @@ private $adodb_driver='postgres9'; // or pdo $this->conn->setFetchMode($fetchMode); // PDO Approach - - /*try { $this->_connection_result = $this->conn->connect($pghost, $user, $password, $database); @@ -83,67 +89,17 @@ private $adodb_driver='postgres9'; // or pdo $this->prtrace(['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); */ try { - $connectionResult = $this->adodb_driver === 'pdo' ? - $this->getPDOConnection($host,$port,$sslmode,$database,$user,$password,$fetchMode): - $this->getPG9Connection($host,$port,$sslmode,$database,$user,$password,$fetchMode); + $connectionResult = 'pdo' === $this->adodb_driver ? + $this->getPDOConnection($host, $port, $sslmode, $database, $user, $password, $fetchMode) : + $this->getPG9Connection($host, $port, $sslmode, $database, $user, $password, $fetchMode); //$this->prtrace($this->conn); } catch (\Exception $e) { //dump($dsnString, $this->adodb_driver); - $this->prtrace($e->getMessage(), array_slice($e->getTrace(), 0, 10)); - } - } - - private function getPG9Connection( - string $host,int $port=5432,string $sslmode='prefer', - ?string $database, - - ?string $user, - - ?string $password, - int $fetchMode = \ADODB_FETCH_ASSOC) : \ADODB_postgres9{ - $this->conn = ADONewConnection('postgres9'); - $this->conn->setFetchMode($fetchMode); - // Ignore host if null - if (null === $host || '' === $host) { - if (null !== $port && '' !== $port) { - $pghost = ':' . $port; - } else { - $pghost = ''; - } - } else { - $pghost = "{$host}:{$port}"; + $this->prtrace($e->getMessage(), \array_slice($e->getTrace(), 0, 10)); } - - // Add sslmode to $pghost as needed - if (('disable' === $sslmode) || ('allow' === $sslmode) || ('prefer' === $sslmode) || ('require' === $sslmode)) { - $pghost .= ':' . $sslmode; - } elseif ('legacy' === $sslmode) { - $pghost .= ' requiressl=1'; - } - - - $this->conn->connect($pghost, $user, $password, $database); - - return $this->conn; - } - private function getPDOConnection( - string $host,int $port=5432,string $sslmode='prefer', - ?string $database, - - ?string $user, - ?string $password, - int $fetchMode = \ADODB_FETCH_ASSOC) : \ADODB_pdo { - $this->conn = ADONewConnection('pdo'); - $this->conn->setFetchMode($fetchMode); - $dsnString = sprintf('pgsql:host=%s;port=%d;dbname=%s;sslmode=%s;application_name=PHPPgAdmin6', $host, $port, $database, $sslmode); - $this->conn->connect($dsnString, $user, $password) ; - return $this->conn; - - } - public function getConnectionResult() { return $this->_connection_result; @@ -153,6 +109,7 @@ private $adodb_driver='postgres9'; // or pdo { return $this->pgVersion; } + /** * Gets the name of the correct database driver to use. As a side effect, * sets the platform. @@ -163,11 +120,11 @@ private $adodb_driver='postgres9'; // or pdo */ public function getDriver(&$description) { - if(!$this->conn->IsConnected()) { + if (!$this->conn->IsConnected()) { return null; } - $serverInfo=$this->conn->ServerInfo(); - $this->pgVersion =$serverInfo['version']; + $serverInfo = $this->conn->ServerInfo(); + $this->pgVersion = $serverInfo['version']; $description = "PostgreSQL {$this->pgVersion}"; $version_parts = \explode('.', $this->pgVersion); @@ -177,10 +134,11 @@ private $adodb_driver='postgres9'; // or pdo } else { $major_version = \implode('.', [$version_parts[0], $version_parts[1]]); } - + // if major version is less than 9 return null, we don't support it - if (9 > (float) $major_version) { + if (9 > (float) $major_version) { $this->driver = null; + return null; } @@ -191,7 +149,6 @@ private $adodb_driver='postgres9'; // or pdo $this->driver = $this->version_dictionary[$major_version]; } - // If unknown version, then default to latest driver return $this->driver; } @@ -203,8 +160,59 @@ private $adodb_driver='postgres9'; // or pdo */ public function getLastError() { - return $this->conn->ErrorMsg(); - + } + + private function getPG9Connection( + string $host, + int $port, + string $sslmode, + ?string $database, + ?string $user, + ?string $password, + int $fetchMode = \ADODB_FETCH_ASSOC + ): \ADODB_postgres9 + { + $this->conn = ADONewConnection('postgres9'); + $this->conn->setFetchMode($fetchMode); + // Ignore host if null + if (null === $host || '' === $host) { + if (null !== $port && '' !== $port) { + $pghost = ':' . $port; + } else { + $pghost = ''; + } + } else { + $pghost = "{$host}:{$port}"; + } + + // Add sslmode to $pghost as needed + if (('disable' === $sslmode) || ('allow' === $sslmode) || ('prefer' === $sslmode) || ('require' === $sslmode)) { + $pghost .= ':' . $sslmode; + } elseif ('legacy' === $sslmode) { + $pghost .= ' requiressl=1'; + } + + $this->conn->connect($pghost, $user, $password, $database); + + return $this->conn; + } + + private function getPDOConnection( + string $host, + int $port, + string $sslmode, + ?string $database, + ?string $user, + ?string $password, + int $fetchMode = \ADODB_FETCH_ASSOC + ): \ADODB_pdo + { + $this->conn = ADONewConnection('pdo'); + $this->conn->setFetchMode($fetchMode); + $dsnString = \sprintf('pgsql:host=%s;port=%d;dbname=%s;sslmode=%s;application_name=PHPPgAdmin6', $host, $port, $database, $sslmode); + $this->conn->connect($dsnString, $user, $password); + + return $this->conn; } } |