1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Database\Connections; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Row; |
6
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\DBALException; |
9
|
|
|
use Doctrine\DBAL\DriverManager; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PostgreSqlConnector |
14
|
|
|
* @package Coco\SourceWatcher\Core\Database\Connections |
15
|
|
|
* |
16
|
|
|
* Following the definition from: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-pgsql |
17
|
|
|
*/ |
18
|
|
|
class PostgreSqlConnector extends Connector |
19
|
|
|
{ |
20
|
|
|
protected string $charset; |
21
|
|
|
protected string $defaultDatabaseName; |
22
|
|
|
protected string $sslMode; |
23
|
|
|
protected string $sslRootCert; |
24
|
|
|
protected string $sslCert; |
25
|
|
|
protected string $sslKey; |
26
|
|
|
protected string $sslCrl; |
27
|
|
|
protected string $applicationName; |
28
|
|
|
|
29
|
|
|
public function __construct () |
30
|
|
|
{ |
31
|
|
|
$this->driver = "pdo_pgsql"; |
32
|
|
|
|
33
|
|
|
$this->port = 5432; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function connect () : Connection |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
return DriverManager::getConnection( $this->getConnectionParameters() ); |
40
|
|
|
} catch ( DBALException $dbalException ) { |
41
|
|
|
throw new SourceWatcherException( "Something went wrong trying to get a connection: " . $dbalException->getMessage() ); |
42
|
|
|
} catch ( Exception $exception ) { |
43
|
|
|
throw new SourceWatcherException( "Something unexpected went wrong trying to get a connection: " . $exception->getMessage() ); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function getConnectionParameters () : array |
48
|
|
|
{ |
49
|
|
|
$this->connectionParameters = array(); |
50
|
|
|
|
51
|
|
|
$this->connectionParameters["driver"] = $this->driver; |
52
|
|
|
$this->connectionParameters["user"] = $this->user; |
53
|
|
|
$this->connectionParameters["password"] = $this->password; |
54
|
|
|
$this->connectionParameters["host"] = $this->host; |
55
|
|
|
$this->connectionParameters["port"] = $this->port; |
56
|
|
|
$this->connectionParameters["dbname"] = $this->dbName; |
57
|
|
|
|
58
|
|
|
$this->connectionParameters["charset"] = $this->charset; |
59
|
|
|
$this->connectionParameters["default_dbname"] = $this->defaultDatabaseName; |
60
|
|
|
$this->connectionParameters["sslmode"] = $this->sslMode; |
61
|
|
|
$this->connectionParameters["sslrootcert"] = $this->sslRootCert; |
62
|
|
|
$this->connectionParameters["sslcert"] = $this->sslCert; |
63
|
|
|
$this->connectionParameters["sslkey"] = $this->sslKey; |
64
|
|
|
$this->connectionParameters["sslcrl"] = $this->sslCrl; |
65
|
|
|
$this->connectionParameters["application_name"] = $this->applicationName; |
66
|
|
|
|
67
|
|
|
return $this->connectionParameters; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function insert ( Row $row ) : void |
71
|
|
|
{ |
72
|
|
|
// TODO: Implement insert() method. |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|