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
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
|
|
public function open(string $database, string $schema = '') |
27
|
|
|
{ |
28
|
|
|
$server = str_replace(":", "' port='", addcslashes($this->driver->options('server'), "'\\")); |
29
|
|
|
$options = $this->driver->options(); |
30
|
|
|
$username = addcslashes($options['username'], "'\\"); |
31
|
|
|
$password = addcslashes($options['password'], "'\\"); |
32
|
|
|
$database = ($database) ? addcslashes($database, "'\\") : "postgres"; |
33
|
|
|
|
34
|
|
|
$connString = "host='$server' user='$username' password='$password' dbname='$database'"; |
35
|
|
|
$this->client = pg_connect($connString, PGSQL_CONNECT_FORCE_NEW); |
36
|
|
|
// if (!$this->client && $database != "") { |
37
|
|
|
// // try to connect directly with database for performance |
38
|
|
|
// $this->_database = false; |
39
|
|
|
// $this->client = pg_connect("{$this->_string} dbname='postgres'", PGSQL_CONNECT_FORCE_NEW); |
40
|
|
|
// } |
41
|
|
|
|
42
|
|
|
if (!$this->client) { |
43
|
|
|
$this->driver->setError($this->utils->trans->lang('Unable to connect to database server.')); |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($this->driver->minVersion(9, 0)) { |
48
|
|
|
if (pg_query($this->client, "SET application_name = 'Jaxon DbAdmin'") === false) { |
49
|
|
|
$this->driver->setError(pg_last_error($this->client)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
if (($schema)) { |
53
|
|
|
if (pg_query($this->client, "SET search_path TO " . $this->driver->escapeId($schema)) === false) { |
54
|
|
|
$this->driver->setError(pg_last_error($this->client)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
pg_set_client_encoding($this->client, "UTF8"); |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function serverInfo() |
65
|
|
|
{ |
66
|
|
|
$version = pg_version($this->client); |
67
|
|
|
return $version["server"]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function quote(string $string) |
74
|
|
|
{ |
75
|
|
|
return "'" . pg_escape_string($this->client, $string) . "'"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function value($value, TableFieldEntity $field) |
82
|
|
|
{ |
83
|
|
|
$type = $field->type; |
84
|
|
|
return ($type == "bytea" && $value !== null ? pg_unescape_bytea($value) : $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
|
|
public function quoteBinary(string $string) |
91
|
|
|
{ |
92
|
|
|
return "'" . pg_escape_bytea($this->client, $string) . "'"; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
|
|
public function close() |
99
|
|
|
{ |
100
|
|
|
// $this->client = pg_connect("{$this->_string} dbname='postgres'"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
|
|
public function query(string $query, bool $unbuffered = false) |
107
|
|
|
{ |
108
|
|
|
$result = pg_query($this->client, $query); |
109
|
|
|
$this->driver->setError(); |
110
|
|
|
if ($result === false) { |
111
|
|
|
$this->driver->setError(pg_last_error($this->client)); |
112
|
|
|
$statement = false; |
113
|
|
|
} elseif (!pg_num_fields($result)) { |
114
|
|
|
$this->setAffectedRows(pg_affected_rows($result)); |
115
|
|
|
$statement = true; |
116
|
|
|
} else { |
117
|
|
|
$statement = new Statement($result); |
118
|
|
|
} |
119
|
|
|
if ($this->timeout) { |
120
|
|
|
$this->timeout = 0; |
121
|
|
|
$this->query("RESET statement_timeout"); |
122
|
|
|
} |
123
|
|
|
return $statement; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
|
|
public function multiQuery(string $query) |
130
|
|
|
{ |
131
|
|
|
$this->statement = $this->driver->execute($query); |
132
|
|
|
return $this->statement !== false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function storedResult() |
139
|
|
|
{ |
140
|
|
|
return $this->statement; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
*/ |
146
|
|
|
public function nextResult() |
147
|
|
|
{ |
148
|
|
|
// PgSQL extension doesn't support multiple results |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritDoc |
154
|
|
|
*/ |
155
|
|
|
public function result(string $query, int $field = -1) |
156
|
|
|
{ |
157
|
|
|
if ($field < 0) { |
158
|
|
|
$field = $this->defaultField(); |
159
|
|
|
} |
160
|
|
|
$result = $this->driver->execute($query); |
161
|
|
|
if ($result === null || !$result->rowCount()) { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
// return pg_fetch_result($result->result, 0, $field); |
165
|
|
|
$row = $result->fetchRow(); |
166
|
|
|
return is_array($row) && count($row) > $field ? $row[$field] : null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
|
|
public function warnings() |
173
|
|
|
{ |
174
|
|
|
// second parameter is available since PHP 7.1.0 |
175
|
|
|
return $this->utils->str->html(pg_last_notice($this->client)); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|