1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-beta.43 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Traits; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Common trait for views manipulation. |
11
|
|
|
*/ |
12
|
|
|
trait ViewTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Returns a list of all views in the database. |
16
|
|
|
* |
17
|
|
|
* @return \PHPPgAdmin\ADORecordSet All views |
18
|
|
|
*/ |
19
|
|
|
public function getViews() |
20
|
|
|
{ |
21
|
|
|
$c_schema = $this->_schema; |
22
|
|
|
$this->clean($c_schema); |
23
|
|
|
$sql = " |
24
|
|
|
SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner, |
25
|
|
|
pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment |
26
|
|
|
FROM pg_catalog.pg_class c |
27
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) |
28
|
|
|
WHERE (n.nspname='{$c_schema}') AND (c.relkind = 'v'::\"char\") |
29
|
|
|
ORDER BY relname"; |
30
|
|
|
|
31
|
|
|
return $this->selectSet($sql); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns a list of all materialized views in the database. |
36
|
|
|
* |
37
|
|
|
* @return \PHPPgAdmin\ADORecordSet All materialized views |
38
|
|
|
*/ |
39
|
|
|
public function getMaterializedViews() |
40
|
|
|
{ |
41
|
|
|
$c_schema = $this->_schema; |
42
|
|
|
$this->clean($c_schema); |
43
|
|
|
$sql = " |
44
|
|
|
SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner, |
45
|
|
|
pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment |
46
|
|
|
FROM pg_catalog.pg_class c |
47
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) |
48
|
|
|
WHERE (n.nspname='{$c_schema}') AND (c.relkind = 'm'::\"char\") |
49
|
|
|
ORDER BY relname"; |
50
|
|
|
|
51
|
|
|
return $this->selectSet($sql); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Updates a view. |
56
|
|
|
* |
57
|
|
|
* @param string $viewname The name fo the view to update |
58
|
|
|
* @param string $definition The new definition for the view |
59
|
|
|
* @param string $comment |
60
|
|
|
* |
61
|
|
|
* @return bool|int 0 success |
62
|
|
|
*/ |
63
|
|
|
public function setView($viewname, $definition, $comment) |
64
|
|
|
{ |
65
|
|
|
return $this->createView($viewname, $definition, true, $comment); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Creates a new view. |
70
|
|
|
* |
71
|
|
|
* @param string $viewname The name of the view to create |
72
|
|
|
* @param string $definition The definition for the new view |
73
|
|
|
* @param bool $replace True to replace the view, false otherwise |
74
|
|
|
* @param string $comment |
75
|
|
|
* |
76
|
|
|
* @return bool|int 0 success |
77
|
|
|
*/ |
78
|
|
|
public function createView($viewname, $definition, $replace, $comment) |
79
|
|
|
{ |
80
|
|
|
$status = $this->beginTransaction(); |
81
|
|
|
if ($status != 0) { |
82
|
|
|
return -1; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$f_schema = $this->_schema; |
86
|
|
|
$this->fieldClean($f_schema); |
87
|
|
|
$this->fieldClean($viewname); |
88
|
|
|
|
89
|
|
|
// Note: $definition not cleaned |
90
|
|
|
|
91
|
|
|
$sql = 'CREATE '; |
92
|
|
|
if ($replace) { |
93
|
|
|
$sql .= 'OR REPLACE '; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$sql .= "VIEW \"{$f_schema}\".\"{$viewname}\" AS {$definition}"; |
97
|
|
|
|
98
|
|
|
$status = $this->execute($sql); |
99
|
|
|
if ($status) { |
100
|
|
|
$this->rollbackTransaction(); |
101
|
|
|
|
102
|
|
|
return -1; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($comment != '') { |
106
|
|
|
$status = $this->setComment('VIEW', $viewname, '', $comment); |
107
|
|
|
if ($status) { |
108
|
|
|
$this->rollbackTransaction(); |
109
|
|
|
|
110
|
|
|
return -1; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->endTransaction(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Alter view properties. |
119
|
|
|
* |
120
|
|
|
* @param string $view The name of the view |
121
|
|
|
* @param string $name The new name for the view |
122
|
|
|
* @param string $owner The new owner for the view |
123
|
|
|
* @param string $schema The new schema for the view |
124
|
|
|
* @param string $comment The comment on the view |
125
|
|
|
* |
126
|
|
|
* @return bool|int 0 success |
127
|
|
|
*/ |
128
|
|
|
public function alterView($view, $name, $owner, $schema, $comment) |
129
|
|
|
{ |
130
|
|
|
$data = $this->getView($view); |
131
|
|
|
if ($data->recordCount() != 1) { |
|
|
|
|
132
|
|
|
return -2; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$status = $this->beginTransaction(); |
136
|
|
|
if ($status != 0) { |
137
|
|
|
$this->rollbackTransaction(); |
138
|
|
|
|
139
|
|
|
return -1; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$status = $this->_alterView($data, $name, $owner, $schema, $comment); |
143
|
|
|
|
144
|
|
|
if ($status != 0) { |
145
|
|
|
$this->rollbackTransaction(); |
146
|
|
|
|
147
|
|
|
return $status; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->endTransaction(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns all details for a particular view. |
155
|
|
|
* |
156
|
|
|
* @param string $view The name of the view to retrieve |
157
|
|
|
* |
158
|
|
|
* @return \PHPPgAdmin\ADORecordSet View info |
159
|
|
|
*/ |
160
|
|
|
public function getView($view) |
161
|
|
|
{ |
162
|
|
|
$c_schema = $this->_schema; |
163
|
|
|
$this->clean($c_schema); |
164
|
|
|
$this->clean($view); |
165
|
|
|
|
166
|
|
|
$sql = " |
167
|
|
|
SELECT c.relname, n.nspname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner, |
168
|
|
|
pg_catalog.pg_get_viewdef(c.oid, true) AS vwdefinition, |
169
|
|
|
pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment |
170
|
|
|
FROM pg_catalog.pg_class c |
171
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) |
172
|
|
|
WHERE (c.relname = '{$view}') AND n.nspname='{$c_schema}'"; |
173
|
|
|
|
174
|
|
|
return $this->selectSet($sql); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Protected method which alter a view |
179
|
|
|
* SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION. |
180
|
|
|
* |
181
|
|
|
* @param \PHPPgAdmin\ADORecordSet $vwrs The view recordSet returned by getView() |
182
|
|
|
* @param string $name The new name for the view |
183
|
|
|
* @param string $owner The new owner for the view |
184
|
|
|
* @param string $schema Schema name |
185
|
|
|
* @param string $comment The comment on the view |
186
|
|
|
* |
187
|
|
|
* @return int 0 success |
188
|
|
|
*/ |
189
|
|
|
protected function _alterView($vwrs, $name, $owner, $schema, $comment) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$this->fieldArrayClean($vwrs->fields); |
|
|
|
|
192
|
|
|
|
193
|
|
|
// Comment |
194
|
|
|
if ($this->setComment('VIEW', $vwrs->fields['relname'], '', $comment) != 0) { |
195
|
|
|
return -4; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Owner |
199
|
|
|
$this->fieldClean($owner); |
200
|
|
|
$status = $this->alterViewOwner($vwrs, $owner); |
201
|
|
|
if ($status != 0) { |
202
|
|
|
return -5; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Rename |
206
|
|
|
$this->fieldClean($name); |
207
|
|
|
$status = $this->alterViewName($vwrs, $name); |
208
|
|
|
if ($status != 0) { |
209
|
|
|
return -3; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// Schema |
213
|
|
|
$this->fieldClean($schema); |
214
|
|
|
$status = $this->alterViewSchema($vwrs, $schema); |
215
|
|
|
if ($status != 0) { |
216
|
|
|
return -6; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return 0; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Alter a view's owner. |
224
|
|
|
* |
225
|
|
|
* @param \PHPPgAdmin\ADORecordSet $vwrs The view recordSet returned by getView() |
226
|
|
|
* @param null|string $owner |
227
|
|
|
* |
228
|
|
|
* @return int 0 if operation was successful |
229
|
|
|
* |
230
|
|
|
* @internal param $name new view's owner |
231
|
|
|
*/ |
232
|
|
|
public function alterViewOwner($vwrs, $owner = null) |
233
|
|
|
{ |
234
|
|
|
/* $vwrs and $owner are cleaned in _alterView */ |
235
|
|
|
if ((!empty($owner)) && ($vwrs->fields['relowner'] != $owner)) { |
236
|
|
|
$f_schema = $this->_schema; |
237
|
|
|
$this->fieldClean($f_schema); |
238
|
|
|
// If owner has been changed, then do the alteration. We are |
239
|
|
|
// careful to avoid this generally as changing owner is a |
240
|
|
|
// superuser only function. |
241
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$vwrs->fields['relname']}\" OWNER TO \"{$owner}\""; |
242
|
|
|
|
243
|
|
|
return $this->execute($sql); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return 0; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Rename a view. |
251
|
|
|
* |
252
|
|
|
* @param \PHPPgAdmin\ADORecordSet $vwrs The view recordSet returned by getView() |
253
|
|
|
* @param string $name The new view's name |
254
|
|
|
* |
255
|
|
|
* @return int 0 if operation was successful |
256
|
|
|
*/ |
257
|
|
|
public function alterViewName($vwrs, $name) |
258
|
|
|
{ |
259
|
|
|
// Rename (only if name has changed) |
260
|
|
|
/* $vwrs and $name are cleaned in _alterView */ |
261
|
|
|
if (!empty($name) && ($name != $vwrs->fields['relname'])) { |
262
|
|
|
$f_schema = $this->_schema; |
263
|
|
|
$this->fieldClean($f_schema); |
264
|
|
|
$sql = "ALTER VIEW \"{$f_schema}\".\"{$vwrs->fields['relname']}\" RENAME TO \"{$name}\""; |
265
|
|
|
$status = $this->execute($sql); |
266
|
|
|
if ($status == 0) { |
267
|
|
|
$vwrs->fields['relname'] = $name; |
268
|
|
|
} else { |
269
|
|
|
return $status; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return 0; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Alter a view's schema. |
278
|
|
|
* |
279
|
|
|
* @param \PHPPgAdmin\ADORecordSet $vwrs The view recordSet returned by getView() |
280
|
|
|
* @param string $schema |
281
|
|
|
* |
282
|
|
|
* @return int 0 if operation was successful |
283
|
|
|
* |
284
|
|
|
* @internal param The $name new view's schema |
285
|
|
|
*/ |
286
|
|
|
public function alterViewSchema($vwrs, $schema) |
287
|
|
|
{ |
288
|
|
|
/* $vwrs and $schema are cleaned in _alterView */ |
289
|
|
|
if (!empty($schema) && ($vwrs->fields['nspname'] != $schema)) { |
290
|
|
|
$f_schema = $this->_schema; |
291
|
|
|
$this->fieldClean($f_schema); |
292
|
|
|
// If tablespace has been changed, then do the alteration. We |
293
|
|
|
// don't want to do this unnecessarily. |
294
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$vwrs->fields['relname']}\" SET SCHEMA \"{$schema}\""; |
295
|
|
|
|
296
|
|
|
return $this->execute($sql); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return 0; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Drops a view. |
304
|
|
|
* |
305
|
|
|
* @param string $viewname The name of the view to drop |
306
|
|
|
* @param string $cascade True to cascade drop, false to restrict |
307
|
|
|
* |
308
|
|
|
* @return int 0 if operation was successful |
309
|
|
|
*/ |
310
|
|
|
public function dropView($viewname, $cascade) |
311
|
|
|
{ |
312
|
|
|
$f_schema = $this->_schema; |
313
|
|
|
$this->fieldClean($f_schema); |
314
|
|
|
$this->fieldClean($viewname); |
315
|
|
|
|
316
|
|
|
$sql = "DROP VIEW \"{$f_schema}\".\"{$viewname}\""; |
317
|
|
|
if ($cascade) { |
318
|
|
|
$sql .= ' CASCADE'; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return $this->execute($sql); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
abstract public function fieldClean(&$str); |
325
|
|
|
|
326
|
|
|
abstract public function beginTransaction(); |
327
|
|
|
|
328
|
|
|
abstract public function rollbackTransaction(); |
329
|
|
|
|
330
|
|
|
abstract public function endTransaction(); |
331
|
|
|
|
332
|
|
|
abstract public function execute($sql); |
333
|
|
|
|
334
|
|
|
abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
335
|
|
|
|
336
|
|
|
abstract public function selectSet($sql); |
337
|
|
|
|
338
|
|
|
abstract public function clean(&$str); |
339
|
|
|
} |
340
|
|
|
|