1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-beta.47 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Traits; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Common trait for tables manipulation. |
11
|
|
|
*/ |
12
|
|
|
trait SchemaTrait |
13
|
|
|
{ |
14
|
|
|
// Schema functons |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Return all schemas in the current database. |
18
|
|
|
* |
19
|
|
|
* @return \PHPPgAdmin\ADORecordSet All schemas, sorted alphabetically |
20
|
|
|
*/ |
21
|
|
|
public function getSchemas() |
22
|
|
|
{ |
23
|
|
|
$conf = $this->conf; |
24
|
|
|
|
25
|
|
|
if (!$conf['show_system']) { |
26
|
|
|
$where = "WHERE nspname NOT LIKE 'pg@_%' ESCAPE '@' AND nspname != 'information_schema'"; |
27
|
|
|
} else { |
28
|
|
|
$where = "WHERE nspname !~ '^pg_t(emp_[0-9]+|oast)$'"; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$sql = " |
32
|
|
|
SELECT pn.nspname, |
33
|
|
|
pu.rolname AS nspowner, |
34
|
|
|
pg_catalog.obj_description(pn.oid, 'pg_namespace') AS nspcomment, |
35
|
|
|
pg_size_pretty(SUM(pg_total_relation_size(pg_class.oid))) as schema_size |
36
|
|
|
FROM pg_catalog.pg_namespace pn |
37
|
|
|
LEFT JOIN pg_catalog.pg_class ON relnamespace = pn.oid |
38
|
|
|
LEFT JOIN pg_catalog.pg_roles pu ON (pn.nspowner = pu.oid) |
39
|
|
|
{$where} |
40
|
|
|
GROUP BY pn.nspname, pu.rolname, pg_catalog.obj_description(pn.oid, 'pg_namespace') |
41
|
|
|
ORDER BY nspname"; |
42
|
|
|
|
43
|
|
|
return $this->selectSet($sql); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sets the current working schema. Will also set Class variable. |
48
|
|
|
* |
49
|
|
|
* @param string $schema The the name of the schema to work in |
50
|
|
|
* |
51
|
|
|
* @return int 0 if operation was successful |
52
|
|
|
*/ |
53
|
|
|
public function setSchema($schema) |
54
|
|
|
{ |
55
|
|
|
// Get the current schema search path, including 'pg_catalog'. |
56
|
|
|
$search_path = $this->getSearchPath(); |
57
|
|
|
// Prepend $schema to search path |
58
|
|
|
array_unshift($search_path, $schema); |
59
|
|
|
$status = $this->setSearchPath($search_path); |
60
|
|
|
if ($status == 0) { |
61
|
|
|
$this->_schema = $schema; |
62
|
|
|
|
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $status; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the current schema search path. |
71
|
|
|
* |
72
|
|
|
* @return array array of schema names |
73
|
|
|
*/ |
74
|
|
|
public function getSearchPath() |
75
|
|
|
{ |
76
|
|
|
$sql = 'SELECT current_schemas(false) AS search_path'; |
77
|
|
|
|
78
|
|
|
return $this->phpArray($this->selectField($sql, 'search_path')); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sets the current schema search path. |
83
|
|
|
* |
84
|
|
|
* @param mixed $paths An array of schemas in required search order |
85
|
|
|
* |
86
|
|
|
* @return int 0 if operation was successful |
87
|
|
|
*/ |
88
|
|
|
public function setSearchPath($paths) |
89
|
|
|
{ |
90
|
|
|
if (!is_array($paths)) { |
91
|
|
|
return -1; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (sizeof($paths) == 0) { |
95
|
|
|
return -2; |
96
|
|
|
} |
97
|
|
|
if (sizeof($paths) == 1 && $paths[0] == '') { |
98
|
|
|
// Need to handle empty paths in some cases |
99
|
|
|
$paths[0] = 'pg_catalog'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Loop over all the paths to check that none are empty |
103
|
|
|
$temp = []; |
104
|
|
|
foreach ($paths as $schema) { |
105
|
|
|
if ($schema != '') { |
106
|
|
|
$temp[] = $schema; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
$this->fieldArrayClean($temp); |
110
|
|
|
|
111
|
|
|
$sql = 'SET SEARCH_PATH TO "'.implode('","', $temp).'"'; |
112
|
|
|
|
113
|
|
|
return $this->execute($sql); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Creates a new schema. |
118
|
|
|
* |
119
|
|
|
* @param string $schemaname The name of the schema to create |
120
|
|
|
* @param string $authorization (optional) The username to create the schema for |
121
|
|
|
* @param string $comment (optional) If omitted, defaults to nothing |
122
|
|
|
* |
123
|
|
|
* @return bool|int 0 success |
124
|
|
|
*/ |
125
|
|
|
public function createSchema($schemaname, $authorization = '', $comment = '') |
126
|
|
|
{ |
127
|
|
|
$this->fieldClean($schemaname); |
128
|
|
|
$this->fieldClean($authorization); |
129
|
|
|
|
130
|
|
|
$sql = "CREATE SCHEMA \"{$schemaname}\""; |
131
|
|
|
if ($authorization != '') { |
132
|
|
|
$sql .= " AUTHORIZATION \"{$authorization}\""; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($comment != '') { |
136
|
|
|
$status = $this->beginTransaction(); |
137
|
|
|
if ($status != 0) { |
138
|
|
|
return -1; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Create the new schema |
143
|
|
|
$status = $this->execute($sql); |
144
|
|
|
if ($status != 0) { |
145
|
|
|
$this->rollbackTransaction(); |
146
|
|
|
|
147
|
|
|
return -1; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Set the comment |
151
|
|
|
if ($comment != '') { |
152
|
|
|
$status = $this->setComment('SCHEMA', $schemaname, '', $comment); |
153
|
|
|
if ($status != 0) { |
154
|
|
|
$this->rollbackTransaction(); |
155
|
|
|
|
156
|
|
|
return -1; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->endTransaction(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return 0; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Updates a schema. |
167
|
|
|
* |
168
|
|
|
* @param string $schemaname The name of the schema to drop |
169
|
|
|
* @param string $comment The new comment for this schema |
170
|
|
|
* @param string $name new name for this schema |
171
|
|
|
* @param string $owner The new owner for this schema |
172
|
|
|
* |
173
|
|
|
* @return bool|int 0 success |
174
|
|
|
*/ |
175
|
|
|
public function updateSchema($schemaname, $comment, $name, $owner) |
176
|
|
|
{ |
177
|
|
|
$this->fieldClean($schemaname); |
178
|
|
|
$this->fieldClean($name); |
179
|
|
|
$this->fieldClean($owner); |
180
|
|
|
|
181
|
|
|
$status = $this->beginTransaction(); |
182
|
|
|
if ($status != 0) { |
183
|
|
|
$this->rollbackTransaction(); |
184
|
|
|
|
185
|
|
|
return -1; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$status = $this->setComment('SCHEMA', $schemaname, '', $comment); |
189
|
|
|
if ($status != 0) { |
190
|
|
|
$this->rollbackTransaction(); |
191
|
|
|
|
192
|
|
|
return -1; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$schema_rs = $this->getSchemaByName($schemaname); |
196
|
|
|
/* Only if the owner change */ |
197
|
|
|
if ($schema_rs->fields['ownername'] != $owner) { |
198
|
|
|
$sql = "ALTER SCHEMA \"{$schemaname}\" OWNER TO \"{$owner}\""; |
199
|
|
|
$status = $this->execute($sql); |
200
|
|
|
if ($status != 0) { |
201
|
|
|
$this->rollbackTransaction(); |
202
|
|
|
|
203
|
|
|
return -1; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// Only if the name has changed |
208
|
|
|
if ($name != $schemaname) { |
209
|
|
|
$sql = "ALTER SCHEMA \"{$schemaname}\" RENAME TO \"{$name}\""; |
210
|
|
|
$status = $this->execute($sql); |
211
|
|
|
if ($status != 0) { |
212
|
|
|
$this->rollbackTransaction(); |
213
|
|
|
|
214
|
|
|
return -1; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this->endTransaction(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Return all information relating to a schema. |
223
|
|
|
* |
224
|
|
|
* @param string $schema The name of the schema |
225
|
|
|
* |
226
|
|
|
* @return \PHPPgAdmin\ADORecordSet Schema information |
227
|
|
|
*/ |
228
|
|
|
public function getSchemaByName($schema) |
229
|
|
|
{ |
230
|
|
|
$this->clean($schema); |
231
|
|
|
$sql = " |
232
|
|
|
SELECT nspname, nspowner, r.rolname AS ownername, nspacl, |
233
|
|
|
pg_catalog.obj_description(pn.oid, 'pg_namespace') as nspcomment |
234
|
|
|
FROM pg_catalog.pg_namespace pn |
235
|
|
|
LEFT JOIN pg_roles as r ON pn.nspowner = r.oid |
236
|
|
|
WHERE nspname='{$schema}'"; |
237
|
|
|
|
238
|
|
|
return $this->selectSet($sql); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// Table functions |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Drops a schema. |
245
|
|
|
* |
246
|
|
|
* @param string $schemaname The name of the schema to drop |
247
|
|
|
* @param bool $cascade True to cascade drop, false to restrict |
248
|
|
|
* |
249
|
|
|
* @return int 0 if operation was successful |
250
|
|
|
*/ |
251
|
|
|
public function dropSchema($schemaname, $cascade) |
252
|
|
|
{ |
253
|
|
|
$this->fieldClean($schemaname); |
254
|
|
|
|
255
|
|
|
$sql = "DROP SCHEMA \"{$schemaname}\""; |
256
|
|
|
if ($cascade) { |
257
|
|
|
$sql .= ' CASCADE'; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this->execute($sql); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
abstract public function fieldClean(&$str); |
264
|
|
|
|
265
|
|
|
abstract public function beginTransaction(); |
266
|
|
|
|
267
|
|
|
abstract public function rollbackTransaction(); |
268
|
|
|
|
269
|
|
|
abstract public function endTransaction(); |
270
|
|
|
|
271
|
|
|
abstract public function execute($sql); |
272
|
|
|
|
273
|
|
|
abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
274
|
|
|
|
275
|
|
|
abstract public function selectSet($sql); |
276
|
|
|
|
277
|
|
|
abstract public function clean(&$str); |
278
|
|
|
|
279
|
|
|
abstract public function phpBool($parameter); |
280
|
|
|
|
281
|
|
|
abstract public function hasCreateTableLikeWithConstraints(); |
282
|
|
|
|
283
|
|
|
abstract public function hasCreateTableLikeWithIndexes(); |
284
|
|
|
|
285
|
|
|
abstract public function hasTablespaces(); |
286
|
|
|
|
287
|
|
|
abstract public function delete($table, $conditions, $schema = ''); |
288
|
|
|
|
289
|
|
|
abstract public function fieldArrayClean(&$arr); |
290
|
|
|
|
291
|
|
|
abstract public function hasCreateFieldWithConstraints(); |
292
|
|
|
|
293
|
|
|
abstract public function getAttributeNames($table, $atts); |
294
|
|
|
} |
295
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.