mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
Refine pg connection strings
This commit is contained in:
parent
796315c00c
commit
edfae438fa
6 changed files with 94 additions and 15 deletions
|
@ -35,8 +35,10 @@ class Conf {
|
||||||
public $dbPostgreSQLPort = 5432;
|
public $dbPostgreSQLPort = 5432;
|
||||||
/** @var string Database name on PostgreSQL database server (if using PostgreSQL) */
|
/** @var string Database name on PostgreSQL database server (if using PostgreSQL) */
|
||||||
public $dbPostgreSQLDb = "arsse";
|
public $dbPostgreSQLDb = "arsse";
|
||||||
/** @var string Schema name on PostgreSQL database server (if using PostgreSQL) */
|
/** @var string Schema name in PostgreSQL database (if using PostgreSQL) */
|
||||||
public $dbPostgreSQLSchema = "";
|
public $dbPostgreSQLSchema = "";
|
||||||
|
/** @var string Service file entry to use (if using PostgreSQL); if using a service entry all above parameters except schema are ignored */
|
||||||
|
public $dbPostgreSQLService = "";
|
||||||
|
|
||||||
/** @var string Class of the user management driver in use (Internal by default) */
|
/** @var string Class of the user management driver in use (Internal by default) */
|
||||||
public $userDriver = User\Internal\Driver::class;
|
public $userDriver = User\Internal\Driver::class;
|
||||||
|
|
|
@ -14,7 +14,7 @@ use JKingWeb\Arsse\Db\ExceptionTimeout;
|
||||||
|
|
||||||
class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
|
class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
|
||||||
|
|
||||||
public function __construct(string $user = null, string $pass = null, string $db = null, string $host = null, int $port = null, string $schema = null) {
|
public function __construct(string $user = null, string $pass = null, string $db = null, string $host = null, int $port = null, string $schema = null, string $service = null) {
|
||||||
// check to make sure required extension is loaded
|
// check to make sure required extension is loaded
|
||||||
if (!static::requirementsMet()) {
|
if (!static::requirementsMet()) {
|
||||||
throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore
|
throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore
|
||||||
|
@ -25,7 +25,8 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
|
||||||
$host = $host ?? Arsse::$conf->dbPostgreSQLHost;
|
$host = $host ?? Arsse::$conf->dbPostgreSQLHost;
|
||||||
$port = $port ?? Arsse::$conf->dbPostgreSQLPort;
|
$port = $port ?? Arsse::$conf->dbPostgreSQLPort;
|
||||||
$schema = $schema ?? Arsse::$conf->dbPostgreSQLSchema;
|
$schema = $schema ?? Arsse::$conf->dbPostgreSQLSchema;
|
||||||
$this->makeConnection($user, $pass, $db, $host, $port);
|
$service = $service ?? Arsse::$conf->dbPostgreSQLService;
|
||||||
|
$this->makeConnection($user, $pass, $db, $host, $port, $service);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function requirementsMet(): bool {
|
public static function requirementsMet(): bool {
|
||||||
|
@ -38,20 +39,38 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
|
||||||
throw new \Exception;
|
throw new \Exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port): string {
|
public static function makeConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port, string $service): string {
|
||||||
$out = ['dbname' => $db];
|
$base = [
|
||||||
if ($host != "") {
|
'client_encoding' => "UTF8",
|
||||||
$out['host'] = $host;
|
'application_name' => "arsse",
|
||||||
$out['port'] = (string) $port;
|
];
|
||||||
}
|
$out = [];
|
||||||
if (!$pdo) {
|
if ($service != "") {
|
||||||
$out['user'] = $user;
|
$out['service'] = $service;
|
||||||
$out['password'] = $pass;
|
} else {
|
||||||
|
if ($host != "") {
|
||||||
|
$out['host'] = $host;
|
||||||
|
}
|
||||||
|
if ($port != 5432 && !($host != "" && $host[0] == "/")) {
|
||||||
|
$out['port'] = (string) $port;
|
||||||
|
}
|
||||||
|
if ($db != "") {
|
||||||
|
$out['dbname'] = $db;
|
||||||
|
}
|
||||||
|
if (!$pdo) {
|
||||||
|
$out['user'] = $user;
|
||||||
|
if ($pass != "") {
|
||||||
|
$out['password'] = $pass;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
ksort($out);
|
||||||
|
ksort($base);
|
||||||
|
$out = array_merge($out, $base);
|
||||||
$out = array_map(function($v, $k) {
|
$out = array_map(function($v, $k) {
|
||||||
return "$k='".str_replace("'", "\\'", str_replace("\\", "\\\\", $v))."'";
|
return "$k='".str_replace("'", "\\'", str_replace("\\", "\\\\", $v))."'";
|
||||||
}, $out, array_keys($out));
|
}, $out, array_keys($out));
|
||||||
return implode(($pdo ? ";" : " "), $out);
|
return implode(" ", $out);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct() {
|
public function __destruct() {
|
||||||
|
|
|
@ -20,8 +20,8 @@ class PDODriver extends Driver {
|
||||||
return class_exists("PDO") && in_array("pgsql", \PDO::getAvailableDrivers());
|
return class_exists("PDO") && in_array("pgsql", \PDO::getAvailableDrivers());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeConnection(string $user, string $pass, string $db, string $host, int $port) {
|
protected function makeConnection(string $user, string $pass, string $db, string $host, int $port, string $service) {
|
||||||
$dsn = $this->makeconnectionString(true, $user, $pass, $db, $host, $port);
|
$dsn = $this->makeconnectionString(true, $user, $pass, $db, $host, $port, $service);
|
||||||
$this->db = new \PDO("pgsql:$dsn", $user, $pass);
|
$this->db = new \PDO("pgsql:$dsn", $user, $pass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,8 @@ return [
|
||||||
|
|
||||||
'Driver.Db.SQLite3.Name' => 'SQLite 3',
|
'Driver.Db.SQLite3.Name' => 'SQLite 3',
|
||||||
'Driver.Db.SQLite3PDO.Name' => 'SQLite 3 (PDO)',
|
'Driver.Db.SQLite3PDO.Name' => 'SQLite 3 (PDO)',
|
||||||
|
'Driver.Db.PostgreSQL.Name' => 'PostgreSQL',
|
||||||
|
'Driver.Db.PostgreSQLPDO.Name' => 'PostgreSQL (PDO)',
|
||||||
'Driver.Service.Curl.Name' => 'HTTP (curl)',
|
'Driver.Service.Curl.Name' => 'HTTP (curl)',
|
||||||
'Driver.Service.Internal.Name' => 'Internal',
|
'Driver.Service.Internal.Name' => 'Internal',
|
||||||
'Driver.User.Internal.Name' => 'Internal',
|
'Driver.User.Internal.Name' => 'Internal',
|
||||||
|
|
54
tests/cases/Db/PostgreSQL/TestDriver.php
Normal file
54
tests/cases/Db/PostgreSQL/TestDriver.php
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
/** @license MIT
|
||||||
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
||||||
|
* See LICENSE and AUTHORS files for details */
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace JKingWeb\Arsse\TestCase\Db\PostgreSQL;
|
||||||
|
|
||||||
|
use JKingWeb\Arsse\Db\PostgreSQL\Driver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Driver<extended> */
|
||||||
|
class TestDriver extends \JKingWeb\Arsse\Test\AbstractTest {
|
||||||
|
/** @dataProvider provideConnectionStrings */
|
||||||
|
public function testGenerateConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port, string $service, string $exp) {
|
||||||
|
$postfix = "application_name='arsse' client_encoding='UTF8'";
|
||||||
|
$act = Driver::makeConnectionString($pdo, $user, $pass, $db, $host, $port, $service);
|
||||||
|
if ($act==$postfix) {
|
||||||
|
$this->assertSame($exp, "");
|
||||||
|
} else {
|
||||||
|
$test = substr($act, 0, strlen($act) - (strlen($postfix) + 1) );
|
||||||
|
$check = substr($act, strlen($test) + 1);
|
||||||
|
$this->assertSame($postfix, $check);
|
||||||
|
$this->assertSame($exp, $test);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideConnectionStrings() {
|
||||||
|
return [
|
||||||
|
[false, "arsse", "secret", "arsse", "", 5432, "", "dbname='arsse' password='secret' user='arsse'"],
|
||||||
|
[false, "arsse", "p word", "arsse", "", 5432, "", "dbname='arsse' password='p word' user='arsse'"],
|
||||||
|
[false, "arsse", "p'word", "arsse", "", 5432, "", "dbname='arsse' password='p\\'word' user='arsse'"],
|
||||||
|
[false, "arsse user", "secret", "arsse db", "", 5432, "", "dbname='arsse db' password='secret' user='arsse user'"],
|
||||||
|
[false, "arsse", "secret", "", "", 5432, "", "password='secret' user='arsse'"],
|
||||||
|
[false, "arsse", "secret", "arsse", "localhost", 5432, "", "dbname='arsse' host='localhost' password='secret' user='arsse'"],
|
||||||
|
[false, "arsse", "secret", "arsse", "", 9999, "", "dbname='arsse' password='secret' port='9999' user='arsse'"],
|
||||||
|
[false, "arsse", "secret", "arsse", "localhost", 9999, "", "dbname='arsse' host='localhost' password='secret' port='9999' user='arsse'"],
|
||||||
|
[false, "arsse", "secret", "arsse", "/socket", 9999, "", "dbname='arsse' host='/socket' password='secret' user='arsse'"],
|
||||||
|
[false, "T'Pau of Vulcan", "", "", "", 5432, "", "user='T\\'Pau of Vulcan'"],
|
||||||
|
[false, "T'Pau of Vulcan", "superman", "datumbase", "somehost", 2112, "arsse", "service='arsse'"],
|
||||||
|
[true, "arsse", "secret", "arsse", "", 5432, "", "dbname='arsse'"],
|
||||||
|
[true, "arsse", "p word", "arsse", "", 5432, "", "dbname='arsse'"],
|
||||||
|
[true, "arsse", "p'word", "arsse", "", 5432, "", "dbname='arsse'"],
|
||||||
|
[true, "arsse user", "secret", "arsse db", "", 5432, "", "dbname='arsse db'"],
|
||||||
|
[true, "arsse", "secret", "", "", 5432, "", ""],
|
||||||
|
[true, "arsse", "secret", "arsse", "localhost", 5432, "", "dbname='arsse' host='localhost'"],
|
||||||
|
[true, "arsse", "secret", "arsse", "", 9999, "", "dbname='arsse' port='9999'"],
|
||||||
|
[true, "arsse", "secret", "arsse", "localhost", 9999, "", "dbname='arsse' host='localhost' port='9999'"],
|
||||||
|
[true, "arsse", "secret", "arsse", "/socket", 9999, "", "dbname='arsse' host='/socket'"],
|
||||||
|
[true, "T'Pau of Vulcan", "", "", "", 5432, "", ""],
|
||||||
|
[true, "T'Pau of Vulcan", "superman", "datumbase", "somehost", 2112, "arsse", "service='arsse'"],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -55,6 +55,8 @@
|
||||||
<file>cases/Db/SQLite3PDO/TestCreation.php</file>
|
<file>cases/Db/SQLite3PDO/TestCreation.php</file>
|
||||||
<file>cases/Db/SQLite3PDO/TestDriver.php</file>
|
<file>cases/Db/SQLite3PDO/TestDriver.php</file>
|
||||||
<file>cases/Db/SQLite3PDO/TestUpdate.php</file>
|
<file>cases/Db/SQLite3PDO/TestUpdate.php</file>
|
||||||
|
|
||||||
|
<file>cases/Db/PostgreSQL/TestDriver.php</file>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
<testsuite name="Database functions">
|
<testsuite name="Database functions">
|
||||||
<file>cases/Db/SQLite3/Database/TestMiscellany.php</file>
|
<file>cases/Db/SQLite3/Database/TestMiscellany.php</file>
|
||||||
|
|
Loading…
Reference in a new issue