|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Database\Connections; |
|
4
|
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
|
6
|
|
|
use Doctrine\DBAL\Connection; |
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
|
8
|
|
|
use Doctrine\DBAL\DriverManager; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PostgreSqlConnector |
|
12
|
|
|
* @package Coco\SourceWatcher\Core\Database\Connections |
|
13
|
|
|
* |
|
14
|
|
|
* Following the definition from: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-pgsql |
|
15
|
|
|
*/ |
|
16
|
|
|
class PostgreSqlConnector extends ClientServerDatabaseConnector |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected string $charset = ""; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected string $defaultDatabaseName = ""; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected string $sslMode = ""; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected string $sslRootCert = ""; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected string $sslCert = ""; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected string $sslKey = ""; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected string $sslCrl = ""; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected string $applicationName = ""; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* PostgreSqlConnector constructor. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct () |
|
62
|
|
|
{ |
|
63
|
|
|
$this->driver = "pdo_pgsql"; |
|
64
|
|
|
|
|
65
|
|
|
$this->port = 5432; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getCharset () : string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->charset; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $charset |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setCharset ( string $charset ) : void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->charset = $charset; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return Connection |
|
86
|
|
|
* @throws SourceWatcherException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function connect () : Connection |
|
89
|
|
|
{ |
|
90
|
|
|
try { |
|
91
|
|
|
return DriverManager::getConnection( $this->getConnectionParameters() ); |
|
92
|
|
|
} catch ( DBALException $dbalException ) { |
|
93
|
|
|
throw new SourceWatcherException( "Something went wrong trying to get a connection: " . $dbalException->getMessage() ); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getConnectionParameters () : array |
|
101
|
|
|
{ |
|
102
|
|
|
$this->connectionParameters = array(); |
|
103
|
|
|
|
|
104
|
|
|
$this->connectionParameters["driver"] = $this->driver; |
|
105
|
|
|
$this->connectionParameters["user"] = $this->user; |
|
106
|
|
|
$this->connectionParameters["password"] = $this->password; |
|
107
|
|
|
$this->connectionParameters["host"] = $this->host; |
|
108
|
|
|
$this->connectionParameters["port"] = $this->port; |
|
109
|
|
|
$this->connectionParameters["dbname"] = $this->dbName; |
|
110
|
|
|
|
|
111
|
|
|
$this->connectionParameters["charset"] = $this->charset; |
|
112
|
|
|
$this->connectionParameters["default_dbname"] = $this->defaultDatabaseName; |
|
113
|
|
|
$this->connectionParameters["sslmode"] = $this->sslMode; |
|
114
|
|
|
$this->connectionParameters["sslrootcert"] = $this->sslRootCert; |
|
115
|
|
|
$this->connectionParameters["sslcert"] = $this->sslCert; |
|
116
|
|
|
$this->connectionParameters["sslkey"] = $this->sslKey; |
|
117
|
|
|
$this->connectionParameters["sslcrl"] = $this->sslCrl; |
|
118
|
|
|
$this->connectionParameters["application_name"] = $this->applicationName; |
|
119
|
|
|
|
|
120
|
|
|
return $this->connectionParameters; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|