|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class PostgreSQLDriver |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource PostgreSQLDriver.php |
|
6
|
|
|
* @created 21.02.2016 |
|
7
|
|
|
* @package chillerlan\Database\Drivers\PostgreSQL |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2016 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Drivers; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Database\DBException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class PostgreSQLDriver extends DBDriverAbstract{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Holds the database resource object |
|
24
|
|
|
* |
|
25
|
|
|
* @var resource |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $db; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Establishes a database connection and returns the connection object |
|
31
|
|
|
* |
|
32
|
|
|
* @return \chillerlan\Database\Drivers\DBDriverInterface |
|
33
|
|
|
* @throws \chillerlan\Database\DBException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function connect():DBDriverInterface{ |
|
36
|
|
|
|
|
37
|
|
|
if(gettype($this->db) === 'resource'){ |
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// i am an ugly duckling. fix me please. |
|
42
|
|
|
|
|
43
|
|
|
$options = [ |
|
44
|
|
|
'--client_encoding='.$this->options->pgsql_charset, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
$conn_str = [ |
|
48
|
|
|
'host=\''.$this->options->host.'\'', |
|
49
|
|
|
'port=\''.(int)$this->options->port.'\'', |
|
50
|
|
|
'dbname=\''.$this->options->database.'\'', |
|
51
|
|
|
'user=\''.$this->options->username.'\'', |
|
52
|
|
|
'password=\''.$this->options->password.'\'', |
|
53
|
|
|
'options=\''.implode(' ', $options).'\'', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
if(!$this->db = pg_connect(implode(' ', $conn_str))){ |
|
57
|
|
|
throw new DBException('Could not connect to the database.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Closes a database connection |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function disconnect():bool{ |
|
69
|
|
|
return pg_close($this->db); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns info about the used php client |
|
74
|
|
|
* |
|
75
|
|
|
* @return string php's database client string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getClientInfo():string{ |
|
78
|
|
|
$ver = pg_version($this->db); |
|
79
|
|
|
|
|
80
|
|
|
return 'PostgreSQL '.$ver['client'].' ('.$ver['client_encoding'].')'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns info about the database server |
|
85
|
|
|
* |
|
86
|
|
|
* @return string database's serverinfo string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getServerInfo():string{ |
|
89
|
|
|
$ver = pg_version($this->db); |
|
90
|
|
|
|
|
91
|
|
|
return 'PostgreSQL '.$ver['server'].' ('.$ver['server_encoding'].', date style: '.$ver['DateStyle'].', time zone: '.$ver['TimeZone'].')'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $data |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function __escape($data){ |
|
100
|
|
|
return pg_escape_string($this->db, $data); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $result |
|
105
|
|
|
* @param string|null $index |
|
106
|
|
|
* @param bool $assoc |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool|\chillerlan\Database\DBResult |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function __getResult($result, string $index = null, bool $assoc = true){ |
|
111
|
|
|
|
|
112
|
|
|
if(is_bool($result)){ |
|
113
|
|
|
return $result; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$r = $this->getResult($assoc ? 'pg_fetch_assoc' : 'pg_fetch_row', [$result], $index, $assoc); |
|
117
|
|
|
|
|
118
|
|
|
pg_free_result($result); |
|
119
|
|
|
|
|
120
|
|
|
return $r; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $sql |
|
125
|
|
|
* @param string|null $index |
|
126
|
|
|
* @param bool $assoc |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool|\chillerlan\Database\DBResult |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function __raw(string $sql, string $index = null, bool $assoc = true){ |
|
131
|
|
|
return $this->__getResult(pg_query($sql), $index, $assoc); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $sql |
|
136
|
|
|
* @param array $values |
|
137
|
|
|
* @param string|null $index |
|
138
|
|
|
* @param bool $assoc |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool|\chillerlan\Database\DBResult |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function __prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
|
143
|
|
|
pg_prepare($this->db, '', $sql); |
|
144
|
|
|
|
|
145
|
|
|
return $this->__getResult(pg_execute($this->db, '', $values), $index, $assoc); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $sql |
|
150
|
|
|
* @param array $values |
|
151
|
|
|
* |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function __multi(string $sql, array $values){ |
|
155
|
|
|
pg_prepare($this->db, '', $sql); |
|
156
|
|
|
|
|
157
|
|
|
foreach($values as $row){ |
|
158
|
|
|
pg_execute($this->db, '', $row); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $sql |
|
166
|
|
|
* @param array $data |
|
167
|
|
|
* @param $callback |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function __multi_callback(string $sql, array $data, $callback){ |
|
172
|
|
|
pg_prepare($this->db, '', $sql); |
|
173
|
|
|
|
|
174
|
|
|
foreach($data as $row){ |
|
175
|
|
|
pg_execute($this->db, '', call_user_func_array($callback, [$row])); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|