1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\PgSql\Db\PgSql; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Db\Connection as AbstractConnection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* PostgreSQL driver to be used with the pgsql PHP extension. |
10
|
|
|
*/ |
11
|
|
|
class Connection extends AbstractConnection |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var mixed |
15
|
|
|
*/ |
16
|
|
|
public $result; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
public $timeout; |
22
|
|
|
|
23
|
|
|
public function _error($errno, $error) |
24
|
|
|
{ |
25
|
|
|
if ($this->util->iniBool("html_errors")) { |
26
|
|
|
$error = html_entity_decode(strip_tags($error)); |
27
|
|
|
} |
28
|
|
|
$error = preg_replace('~^[^:]*: ~', '', $error); |
29
|
|
|
$this->driver->setError($error); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritDoc |
34
|
|
|
*/ |
35
|
|
|
public function open(string $database, string $schema = '') |
36
|
|
|
{ |
37
|
|
|
$server = str_replace(":", "' port='", addcslashes($this->driver->options('server'), "'\\")); |
38
|
|
|
$options = $this->driver->options(); |
39
|
|
|
$username = addcslashes($options['username'], "'\\"); |
40
|
|
|
$password = addcslashes($options['password'], "'\\"); |
41
|
|
|
$database = ($database) ? addcslashes($database, "'\\") : "postgres"; |
42
|
|
|
|
43
|
|
|
set_error_handler(array($this, '_error')); |
44
|
|
|
$connString = "host='$server' user='$username' password='$password' dbname='$database'"; |
45
|
|
|
$this->client = pg_connect($connString, PGSQL_CONNECT_FORCE_NEW); |
46
|
|
|
// if (!$this->client && $database != "") { |
47
|
|
|
// // try to connect directly with database for performance |
48
|
|
|
// $this->_database = false; |
49
|
|
|
// $this->client = pg_connect("{$this->_string} dbname='postgres'", PGSQL_CONNECT_FORCE_NEW); |
50
|
|
|
// } |
51
|
|
|
restore_error_handler(); |
52
|
|
|
|
53
|
|
|
if (!$this->client) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($this->driver->minVersion(9, 0)) { |
58
|
|
|
if (pg_query($this->client, "SET application_name = 'Adminer'") === false) { |
59
|
|
|
$this->driver->setError(pg_last_error($this->client)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
if (($schema)) { |
63
|
|
|
if (pg_query($this->client, "SET search_path TO " . $this->driver->escapeId($schema)) === false) { |
64
|
|
|
$this->driver->setError(pg_last_error($this->client)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
pg_set_client_encoding($this->client, "UTF8"); |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public function serverInfo() |
75
|
|
|
{ |
76
|
|
|
$version = pg_version($this->client); |
77
|
|
|
return $version["server"]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
|
|
public function quote(string $string) |
84
|
|
|
{ |
85
|
|
|
return "'" . pg_escape_string($this->client, $string) . "'"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function value($value, TableFieldEntity $field) |
92
|
|
|
{ |
93
|
|
|
$type = $field->type; |
94
|
|
|
return ($type == "bytea" && $value !== null ? pg_unescape_bytea($value) : $value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
100
|
|
|
public function quoteBinary(string $string) |
101
|
|
|
{ |
102
|
|
|
return "'" . pg_escape_bytea($this->client, $string) . "'"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public function close() |
109
|
|
|
{ |
110
|
|
|
// $this->client = pg_connect("{$this->_string} dbname='postgres'"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function query(string $query, bool $unbuffered = false) |
117
|
|
|
{ |
118
|
|
|
$result = pg_query($this->client, $query); |
119
|
|
|
$this->driver->setError(); |
120
|
|
|
if ($result === false) { |
121
|
|
|
$this->driver->setError(pg_last_error($this->client)); |
122
|
|
|
$statement = false; |
123
|
|
|
} elseif (!pg_num_fields($result)) { |
124
|
|
|
$this->driver->setAffectedRows(pg_affected_rows($result)); |
125
|
|
|
$statement = true; |
126
|
|
|
} else { |
127
|
|
|
$statement = new Statement($result); |
128
|
|
|
} |
129
|
|
|
if ($this->timeout) { |
130
|
|
|
$this->timeout = 0; |
131
|
|
|
$this->query("RESET statement_timeout"); |
132
|
|
|
} |
133
|
|
|
return $statement; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
|
|
public function multiQuery($query) |
140
|
|
|
{ |
141
|
|
|
$this->statement = $this->query($query); |
|
|
|
|
142
|
|
|
return !(!$this->statement); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritDoc |
147
|
|
|
*/ |
148
|
|
|
public function storedResult() |
149
|
|
|
{ |
150
|
|
|
return $this->statement; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritDoc |
155
|
|
|
*/ |
156
|
|
|
public function nextResult() |
157
|
|
|
{ |
158
|
|
|
// PgSQL extension doesn't support multiple results |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
|
|
public function result(string $query, int $field = -1) |
166
|
|
|
{ |
167
|
|
|
if ($field < 0) { |
168
|
|
|
$field = $this->defaultField(); |
169
|
|
|
} |
170
|
|
|
$result = $this->query($query); |
171
|
|
|
if ($result === null || !$result->rowCount()) { |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
// return pg_fetch_result($result->result, 0, $field); |
175
|
|
|
$row = $result->fetchRow(); |
176
|
|
|
return is_array($row) && count($row) > $field ? $row[$field] : null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritDoc |
181
|
|
|
*/ |
182
|
|
|
public function warnings() |
183
|
|
|
{ |
184
|
|
|
// second parameter is available since PHP 7.1.0 |
185
|
|
|
return $this->util->html(pg_last_notice($this->client)); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|