1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
<?php
/**
* PHPPgAdmin v6.0.0-beta.50
*/
namespace PHPPgAdmin\Database\Traits;
/**
* Common trait for tables manipulation.
*/
trait DatabaseTrait
{
/**
* Returns the current default_with_oids setting.
*
* @return string default_with_oids setting
*/
public function getDefaultWithOid()
{
$sql = 'SHOW default_with_oids';
return $this->selectField($sql, 'default_with_oids');
}
/**
* Determines whether or not a user is a super user.
*
* @param string $username The username of the user
*
* @return bool true if is a super user, false otherwise
*/
public function isSuperUser($username = '')
{
$this->clean($username);
if (empty($username)) {
$val = pg_parameter_status($this->conn->_connectionID, 'is_superuser');
if ($val !== false) {
return $val == 'on';
}
}
$sql = "SELECT usesuper FROM pg_user WHERE usename='{$username}'";
$usesuper = $this->selectField($sql, 'usesuper');
if ($usesuper == -1) {
return false;
}
return $usesuper == 't';
}
/**
* Analyze a database.
*
* @param string $table (optional) The table to analyze
*
* @return bool 0 if successful
*/
public function analyzeDB($table = '')
{
if ($table != '') {
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($table);
$sql = "ANALYZE \"{$f_schema}\".\"{$table}\"";
} else {
$sql = 'ANALYZE';
}
return $this->execute($sql);
}
/**
* Return all information about a particular database.
*
* @param string $database The name of the database to retrieve
*
* @return \PHPPgAdmin\ADORecordSet The database info
*/
public function getDatabase($database)
{
$this->clean($database);
$sql = "SELECT * FROM pg_database WHERE datname='{$database}'";
return $this->selectSet($sql);
}
/**
* Return all database available on the server.
*
* @param null|string $currentdatabase database name that should be on top of the resultset
*
* @return \PHPPgAdmin\ADORecordSet A list of databases, sorted alphabetically
*/
public function getDatabases($currentdatabase = null)
{
$conf = $this->conf;
$server_info = $this->server_info;
//$this->prtrace('server_info', $server_info);
if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser()) {
$username = $server_info['username'];
$this->clean($username);
$clause = " AND pr.rolname='{$username}'";
} else {
$clause = '';
}
if (isset($server_info['useonlydefaultdb']) && $server_info['useonlydefaultdb']) {
$currentdatabase = $server_info['defaultdb'];
$clause .= " AND pdb.datname = '{$currentdatabase}' ";
}
if (isset($server_info['hiddendbs']) && $server_info['hiddendbs']) {
$hiddendbs = $server_info['hiddendbs'];
$not_in = "('".implode("','", $hiddendbs)."')";
$clause .= " AND pdb.datname NOT IN {$not_in} ";
}
if ($currentdatabase != null) {
$this->clean($currentdatabase);
$orderby = "ORDER BY pdb.datname = '{$currentdatabase}' DESC, pdb.datname";
} else {
$orderby = 'ORDER BY pdb.datname';
}
if (!$conf['show_system']) {
$where = ' AND NOT pdb.datistemplate';
} else {
$where = ' AND pdb.datallowconn';
}
$sql = "
SELECT pdb.datname AS datname,
pr.rolname AS datowner,
pg_encoding_to_char(encoding) AS datencoding,
(SELECT description FROM pg_catalog.pg_shdescription pd WHERE pdb.oid=pd.objoid AND pd.classoid='pg_database'::regclass) AS datcomment,
(SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=pdb.dattablespace) AS tablespace,
CASE WHEN pg_catalog.has_database_privilege(current_user, pdb.oid, 'CONNECT')
THEN pg_catalog.pg_database_size(pdb.oid)
ELSE -1 -- set this magic value, which we will convert to no access later
END as dbsize,
pdb.datcollate,
pdb.datctype
FROM pg_catalog.pg_database pdb
LEFT JOIN pg_catalog.pg_roles pr ON (pdb.datdba = pr.oid)
WHERE true
{$where}
{$clause}
{$orderby}";
return $this->selectSet($sql);
}
/**
* Return the database comment of a db from the shared description table.
*
* @param string $database the name of the database to get the comment for
*
* @return \PHPPgAdmin\ADORecordSet recordset of the db comment info
*/
public function getDatabaseComment($database)
{
$this->clean($database);
$sql = "SELECT description
FROM pg_catalog.pg_database
JOIN pg_catalog.pg_shdescription
ON (oid=objoid AND classoid='pg_database'::regclass)
WHERE pg_database.datname = '{$database}' ";
return $this->selectSet($sql);
}
/**
* Return the database owner of a db.
*
* @param string $database the name of the database to get the owner for
*
* @return \PHPPgAdmin\ADORecordSet recordset of the db owner info
*/
public function getDatabaseOwner($database)
{
$this->clean($database);
$sql = "SELECT usename FROM pg_user, pg_database WHERE pg_user.usesysid = pg_database.datdba AND pg_database.datname = '{$database}' ";
return $this->selectSet($sql);
}
// Help functions
// Database functions
/**
* Returns the current database encoding.
*
* @return string The encoding. eg. SQL_ASCII, UTF-8, etc.
*/
public function getDatabaseEncoding()
{
return pg_parameter_status($this->conn->_connectionID, 'server_encoding');
}
/**
* Creates a database.
*
* @param string $database The name of the database to create
* @param string $encoding Encoding of the database
* @param string $tablespace (optional) The tablespace name
* @param string $comment
* @param string $template
* @param string $lc_collate
* @param string $lc_ctype
*
* @return int 0 success
*/
public function createDatabase(
$database,
$encoding,
$tablespace = '',
$comment = '',
$template = 'template1',
$lc_collate = '',
$lc_ctype = ''
) {
$this->fieldClean($database);
$this->clean($encoding);
$this->fieldClean($tablespace);
$this->fieldClean($template);
$this->clean($lc_collate);
$this->clean($lc_ctype);
$sql = "CREATE DATABASE \"{$database}\" WITH TEMPLATE=\"{$template}\"";
if ($encoding != '') {
$sql .= " ENCODING='{$encoding}'";
}
if ($lc_collate != '') {
$sql .= " LC_COLLATE='{$lc_collate}'";
}
if ($lc_ctype != '') {
$sql .= " LC_CTYPE='{$lc_ctype}'";
}
if ($tablespace != '' && $this->hasTablespaces()) {
$sql .= " TABLESPACE \"{$tablespace}\"";
}
$status = $this->execute($sql);
if ($status != 0) {
return -1;
}
if ($comment != '' && $this->hasSharedComments()) {
$status = $this->setComment('DATABASE', $database, '', $comment);
if ($status != 0) {
return -2;
}
}
return 0;
}
/**
* Drops a database.
*
* @param string $database The name of the database to drop
*
* @return int 0 if operation was successful
*/
public function dropDatabase($database)
{
$this->fieldClean($database);
$sql = "DROP DATABASE \"{$database}\"";
return $this->execute($sql);
}
/**
* Alters a database
* the multiple return vals are for postgres 8+ which support more functionality in alter database.
*
* @param string $dbName The name of the database
* @param string $newName new name for the database
* @param string $newOwner The new owner for the database
* @param string $comment
*
* @return bool|int 0 success
*/
public function alterDatabase($dbName, $newName, $newOwner = '', $comment = '')
{
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
if ($dbName != $newName) {
$status = $this->alterDatabaseRename($dbName, $newName);
if ($status != 0) {
$this->rollbackTransaction();
return -3;
}
$dbName = $newName;
}
if ($newOwner != '') {
$status = $this->alterDatabaseOwner($newName, $newOwner);
if ($status != 0) {
$this->rollbackTransaction();
return -2;
}
}
$this->fieldClean($dbName);
$status = $this->setComment('DATABASE', $dbName, '', $comment);
if ($status != 0) {
$this->rollbackTransaction();
return -4;
}
return $this->endTransaction();
}
/**
* Renames a database, note that this operation cannot be
* performed on a database that is currently being connected to.
*
* @param string $oldName name of database to rename
* @param string $newName new name of database
*
* @return int 0 on success
*/
public function alterDatabaseRename($oldName, $newName)
{
$this->fieldClean($oldName);
$this->fieldClean($newName);
if ($oldName != $newName) {
$sql = "ALTER DATABASE \"{$oldName}\" RENAME TO \"{$newName}\"";
return $this->execute($sql);
}
return 0;
}
/**
* Changes ownership of a database
* This can only be done by a superuser or the owner of the database.
*
* @param string $dbName database to change ownership of
* @param string $newOwner user that will own the database
*
* @return int 0 on success
*/
public function alterDatabaseOwner($dbName, $newOwner)
{
$this->fieldClean($dbName);
$this->fieldClean($newOwner);
$sql = "ALTER DATABASE \"{$dbName}\" OWNER TO \"{$newOwner}\"";
return $this->execute($sql);
}
/**
* Returns prepared transactions information.
*
* @param null|string $database (optional) Find only prepared transactions executed in a specific database
*
* @return \PHPPgAdmin\ADORecordSet A recordset
*/
public function getPreparedXacts($database = null)
{
if ($database === null) {
$sql = 'SELECT * FROM pg_prepared_xacts';
} else {
$this->clean($database);
$sql = "SELECT transaction, gid, prepared, owner FROM pg_prepared_xacts
WHERE database='{$database}' ORDER BY owner";
}
return $this->selectSet($sql);
}
/**
* Returns all available process information.
*
* @param null|string $database (optional) Find only connections to specified database
*
* @return \PHPPgAdmin\ADORecordSet A recordset
*/
public function getProcesses($database = null)
{
if ($database === null) {
$sql = "SELECT datname, usename, pid, waiting, state_change as query_start,
case when state='idle in transaction' then '<IDLE> in transaction' when state = 'idle' then '<IDLE>' else query end as query
FROM pg_catalog.pg_stat_activity
ORDER BY datname, usename, pid";
} else {
$this->clean($database);
$sql = "SELECT datname, usename, pid, waiting, state_change as query_start,
case when state='idle in transaction' then '<IDLE> in transaction' when state = 'idle' then '<IDLE>' else query end as query
FROM pg_catalog.pg_stat_activity
WHERE datname='{$database}'
ORDER BY usename, pid";
}
return $this->selectSet($sql);
}
// interfaces Statistics collector functions
/**
* Returns table locks information in the current database.
*
* @return \PHPPgAdmin\ADORecordSet A recordset
*/
public function getLocks()
{
$conf = $this->conf;
if (!$conf['show_system']) {
$where = 'AND pn.nspname NOT LIKE $$pg\_%$$';
} else {
$where = "AND nspname !~ '^pg_t(emp_[0-9]+|oast)$'";
}
$sql = "
SELECT
pn.nspname, pc.relname AS tablename, pl.pid, pl.mode, pl.granted, pl.virtualtransaction,
(select transactionid from pg_catalog.pg_locks l2 where l2.locktype='transactionid'
and l2.mode='ExclusiveLock' and l2.virtualtransaction=pl.virtualtransaction) as transaction
FROM
pg_catalog.pg_locks pl,
pg_catalog.pg_class pc,
pg_catalog.pg_namespace pn
WHERE
pl.relation = pc.oid AND pc.relnamespace=pn.oid
{$where}
ORDER BY pid,nspname,tablename";
return $this->selectSet($sql);
}
/**
* Sends a cancel or kill command to a process.
*
* @param int $pid The ID of the backend process
* @param string $signal 'CANCEL' or 'KILL'
*
* @return int 0 success
*/
public function sendSignal($pid, $signal)
{
// Clean
$pid = (int) $pid;
if ($signal == 'CANCEL') {
$sql = "SELECT pg_catalog.pg_cancel_backend({$pid}) AS val";
} elseif ($signal == 'KILL') {
$sql = "SELECT pg_catalog.pg_terminate_backend({$pid}) AS val";
} else {
return -1;
}
// Execute the query
$val = $this->selectField($sql, 'val');
if ($val === 'f') {
return -1;
}
if ($val === 't') {
return 0;
}
return -1;
}
/**
* Vacuums a database.
*
* @param string $table The table to vacuum
* @param bool $analyze If true, also does analyze
* @param bool $full If true, selects "full" vacuum
* @param bool $freeze If true, selects aggressive "freezing" of tuples
*
* @return bool 0 if successful
*/
public function vacuumDB($table = '', $analyze = false, $full = false, $freeze = false)
{
$sql = 'VACUUM';
if ($full) {
$sql .= ' FULL';
}
if ($freeze) {
$sql .= ' FREEZE';
}
if ($analyze) {
$sql .= ' ANALYZE';
}
if ($table != '') {
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($table);
$sql .= " \"{$f_schema}\".\"{$table}\"";
}
$status = $this->execute($sql);
return [$status, $sql];
}
/**
* Returns all autovacuum global configuration.
*
* @return array associative array array( param => value, ...)
*/
public function getAutovacuum()
{
$_defaults = $this->selectSet(
"SELECT name, setting
FROM pg_catalog.pg_settings
WHERE
name = 'autovacuum'
OR name = 'autovacuum_vacuum_threshold'
OR name = 'autovacuum_vacuum_scale_factor'
OR name = 'autovacuum_analyze_threshold'
OR name = 'autovacuum_analyze_scale_factor'
OR name = 'autovacuum_vacuum_cost_delay'
OR name = 'autovacuum_vacuum_cost_limit'
OR name = 'vacuum_freeze_min_age'
OR name = 'autovacuum_freeze_max_age'
"
);
$ret = [];
while (!$_defaults->EOF) {
$ret[$_defaults->fields['name']] = $_defaults->fields['setting'];
$_defaults->moveNext();
}
return $ret;
}
/**
* Returns all available variable information.
*
* @return \PHPPgAdmin\ADORecordSet A recordset
*/
public function getVariables()
{
$sql = 'SHOW ALL';
return $this->selectSet($sql);
}
abstract public function fieldClean(&$str);
abstract public function beginTransaction();
abstract public function rollbackTransaction();
abstract public function endTransaction();
abstract public function execute($sql);
abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);
abstract public function selectSet($sql);
abstract public function clean(&$str);
abstract public function phpBool($parameter);
abstract public function hasCreateTableLikeWithConstraints();
abstract public function hasCreateTableLikeWithIndexes();
abstract public function hasTablespaces();
abstract public function delete($table, $conditions, $schema = '');
abstract public function fieldArrayClean(&$arr);
abstract public function hasCreateFieldWithConstraints();
abstract public function getAttributeNames($table, $atts);
abstract public function hasSharedComments();
abstract public function selectField($sql, $field);
}
|