Test Failed
Branch develop (db5506)
by Felipe
03:46
created

Postgres83::getDatabases()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 38
Code Lines 22

Duplication

Lines 38
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 8
nop 1
dl 38
loc 38
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
    namespace PHPPgAdmin\Database;
4
5
/**
6
 * PostgreSQL 8.3 support
7
 *
8
 * $Id: Postgres82.php,v 1.10 2007/12/28 16:21:25 ioguix Exp $
9
 */
10
11
    class Postgres83 extends Postgres84
12
    {
13
        public $major_version = 8.3;
14
15
        // List of all legal privileges that can be applied to different types
16
        // of objects.
17
        public $privlist = [
18
            'table'      => ['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'REFERENCES', 'TRIGGER', 'ALL PRIVILEGES'],
19
            'view'       => ['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'REFERENCES', 'TRIGGER', 'ALL PRIVILEGES'],
20
            'sequence'   => ['SELECT', 'UPDATE', 'ALL PRIVILEGES'],
21
            'database'   => ['CREATE', 'TEMPORARY', 'CONNECT', 'ALL PRIVILEGES'],
22
            'function'   => ['EXECUTE', 'ALL PRIVILEGES'],
23
            'language'   => ['USAGE', 'ALL PRIVILEGES'],
24
            'schema'     => ['CREATE', 'USAGE', 'ALL PRIVILEGES'],
25
            'tablespace' => ['CREATE', 'ALL PRIVILEGES'],
26
        ];
27
        // List of characters in acl lists and the privileges they
28
        // refer to.
29
        public $privmap = [
30
            'r' => 'SELECT',
31
            'w' => 'UPDATE',
32
            'a' => 'INSERT',
33
            'd' => 'DELETE',
34
            'R' => 'RULE',
35
            'x' => 'REFERENCES',
36
            't' => 'TRIGGER',
37
            'X' => 'EXECUTE',
38
            'U' => 'USAGE',
39
            'C' => 'CREATE',
40
            'T' => 'TEMPORARY',
41
            'c' => 'CONNECT',
42
        ];
43
44
        // Databse functions
45
46
        /**
47
         * Return all database available on the server
48
         *
49
         * @param $currentdatabase database name that should be on top of the resultset
50
         *
51
         * @return A list of databases, sorted alphabetically
52
         */
53 View Code Duplication
        public function getDatabases($currentdatabase = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
        {
55
            $conf        = $this->conf;
56
            $server_info = $this->server_info;
57
58
            if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser()) {
59
                $username = $server_info['username'];
60
                $this->clean($username);
61
                $clause = " AND pr.rolname='{$username}'";
62
            } else {
63
                $clause = '';
64
            }
65
66
            if ($currentdatabase != null) {
67
                $this->clean($currentdatabase);
68
                $orderby = "ORDER BY pdb.datname = '{$currentdatabase}' DESC, pdb.datname";
69
            } else {
70
                $orderby = 'ORDER BY pdb.datname';
71
            }
72
73
            if (!$conf['show_system']) {
74
                $where = ' AND NOT pdb.datistemplate';
75
            } else {
76
                $where = ' AND pdb.datallowconn';
77
            }
78
79
            $sql = "
80
			SELECT pdb.datname AS datname, pr.rolname AS datowner, pg_encoding_to_char(encoding) AS datencoding,
81
				(SELECT description FROM pg_catalog.pg_shdescription pd WHERE pdb.oid=pd.objoid AND pd.classoid='pg_database'::regclass) AS datcomment,
82
				(SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=pdb.dattablespace) AS tablespace,
83
				pg_catalog.pg_database_size(pdb.oid) as dbsize
84
			FROM pg_catalog.pg_database pdb LEFT JOIN pg_catalog.pg_roles pr ON (pdb.datdba = pr.oid)
85
			WHERE true
86
				{$where}
87
				{$clause}
88
			{$orderby}";
89
90
            return $this->selectSet($sql);
91
        }
92
93
        // Administration functions
94
95
        /**
96
         * Returns all available autovacuum per table information.
97
         *
98
         * @param string $table
99
         * @return \PHPPgAdmin\Database\A recordset
100
         */
101
        public function getTableAutovacuum($table = '')
102
        {
103
            $sql = '';
104
105
            if ($table !== '') {
106
                $this->clean($table);
107
                $c_schema = $this->_schema;
108
                $this->clean($c_schema);
109
110
                $sql = "
111
				SELECT vacrelid, nspname, relname,
112
					CASE enabled
113
						WHEN 't' THEN 'on'
114
						ELSE 'off'
115
					END AS autovacuum_enabled, vac_base_thresh AS autovacuum_vacuum_threshold,
116
					vac_scale_factor AS autovacuum_vacuum_scale_factor, anl_base_thresh AS autovacuum_analyze_threshold,
117
					anl_scale_factor AS autovacuum_analyze_scale_factor, vac_cost_delay AS autovacuum_vacuum_cost_delay,
118
					vac_cost_limit AS autovacuum_vacuum_cost_limit
119
				FROM pg_autovacuum AS a
120
					join pg_class AS c on (c.oid=a.vacrelid)
121
					join pg_namespace AS n on (n.oid=c.relnamespace)
122
				WHERE c.relname = '{$table}' AND n.nspname = '{$c_schema}'
123
				ORDER BY nspname, relname
124
			";
125
            } else {
126
                $sql = "
127
				SELECT vacrelid, nspname, relname,
128
					CASE enabled
129
						WHEN 't' THEN 'on'
130
						ELSE 'off'
131
					END AS autovacuum_enabled, vac_base_thresh AS autovacuum_vacuum_threshold,
132
					vac_scale_factor AS autovacuum_vacuum_scale_factor, anl_base_thresh AS autovacuum_analyze_threshold,
133
					anl_scale_factor AS autovacuum_analyze_scale_factor, vac_cost_delay AS autovacuum_vacuum_cost_delay,
134
					vac_cost_limit AS autovacuum_vacuum_cost_limit
135
				FROM pg_autovacuum AS a
136
					join pg_class AS c on (c.oid=a.vacrelid)
137
					join pg_namespace AS n on (n.oid=c.relnamespace)
138
				ORDER BY nspname, relname
139
			";
140
            }
141
142
            return $this->selectSet($sql);
143
        }
144
145
        public function saveAutovacuum(
0 ignored issues
show
Coding Style introduced by
saveAutovacuum uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
146
            $table,
147
            $vacenabled,
148
            $vacthreshold,
149
            $vacscalefactor,
150
            $anathresold,
151
            $anascalefactor,
152
            $vaccostdelay,
153
            $vaccostlimit
154
        ) {
155
            $defaults = $this->getAutovacuum();
156
            $c_schema = $this->_schema;
157
            $this->clean($c_schema);
158
            $this->clean($table);
159
160
            $rs = $this->selectSet("
161
			SELECT c.oid
162
			FROM pg_catalog.pg_class AS c
163
				LEFT JOIN pg_catalog.pg_namespace AS n ON (n.oid=c.relnamespace)
164
			WHERE
165
				c.relname = '{$table}' AND n.nspname = '{$c_schema}'
166
		");
167
168
            if ($rs->EOF) {
169
                return -1;
170
            }
171
172
            $toid = $rs->fields('oid');
173
            unset($rs);
174
175
            if (empty($_POST['autovacuum_vacuum_threshold'])) {
176
                $_POST['autovacuum_vacuum_threshold'] = $defaults['autovacuum_vacuum_threshold'];
177
            }
178
179
            if (empty($_POST['autovacuum_vacuum_scale_factor'])) {
180
                $_POST['autovacuum_vacuum_scale_factor'] = $defaults['autovacuum_vacuum_scale_factor'];
181
            }
182
183
            if (empty($_POST['autovacuum_analyze_threshold'])) {
184
                $_POST['autovacuum_analyze_threshold'] = $defaults['autovacuum_analyze_threshold'];
185
            }
186
187
            if (empty($_POST['autovacuum_analyze_scale_factor'])) {
188
                $_POST['autovacuum_analyze_scale_factor'] = $defaults['autovacuum_analyze_scale_factor'];
189
            }
190
191
            if (empty($_POST['autovacuum_vacuum_cost_delay'])) {
192
                $_POST['autovacuum_vacuum_cost_delay'] = $defaults['autovacuum_vacuum_cost_delay'];
193
            }
194
195
            if (empty($_POST['autovacuum_vacuum_cost_limit'])) {
196
                $_POST['autovacuum_vacuum_cost_limit'] = $defaults['autovacuum_vacuum_cost_limit'];
197
            }
198
199
            if (empty($_POST['vacuum_freeze_min_age'])) {
200
                $_POST['vacuum_freeze_min_age'] = $defaults['vacuum_freeze_min_age'];
201
            }
202
203
            if (empty($_POST['autovacuum_freeze_max_age'])) {
204
                $_POST['autovacuum_freeze_max_age'] = $defaults['autovacuum_freeze_max_age'];
205
            }
206
207
            $rs = $this->selectSet("SELECT vacrelid
208
			FROM \"pg_catalog\".\"pg_autovacuum\"
209
			WHERE vacrelid = {$toid};");
210
211
            $status = -1; // ini
212
            if ($rs->recordCount() and ($rs->fields['vacrelid'] == $toid)) {
213
                // table exists in pg_autovacuum, UPDATE
214
                $sql    = sprintf("UPDATE \"pg_catalog\".\"pg_autovacuum\" SET
215
						enabled = '%s',
216
						vac_base_thresh = %s,
217
						vac_scale_factor = %s,
218
						anl_base_thresh = %s,
219
						anl_scale_factor = %s,
220
						vac_cost_delay = %s,
221
						vac_cost_limit = %s,
222
						freeze_min_age = %s,
223
						freeze_max_age = %s
224
					WHERE vacrelid = {$toid};
225
				",
226
                    ($_POST['autovacuum_enabled'] == 'on') ? 't' : 'f',
227
                    $_POST['autovacuum_vacuum_threshold'],
228
                    $_POST['autovacuum_vacuum_scale_factor'],
229
                    $_POST['autovacuum_analyze_threshold'],
230
                    $_POST['autovacuum_analyze_scale_factor'],
231
                    $_POST['autovacuum_vacuum_cost_delay'],
232
                    $_POST['autovacuum_vacuum_cost_limit'],
233
                    $_POST['vacuum_freeze_min_age'],
234
                    $_POST['autovacuum_freeze_max_age']
235
                );
236
                $status = $this->execute($sql);
237
            } else {
238
                // table doesn't exists in pg_autovacuum, INSERT
239
                $sql    = sprintf("INSERT INTO \"pg_catalog\".\"pg_autovacuum\"
240
				VALUES (%s, '%s', %s, %s, %s, %s, %s, %s, %s, %s )",
241
                    $toid,
242
                    ($_POST['autovacuum_enabled'] == 'on') ? 't' : 'f',
243
                    $_POST['autovacuum_vacuum_threshold'],
244
                    $_POST['autovacuum_vacuum_scale_factor'],
245
                    $_POST['autovacuum_analyze_threshold'],
246
                    $_POST['autovacuum_analyze_scale_factor'],
247
                    $_POST['autovacuum_vacuum_cost_delay'],
248
                    $_POST['autovacuum_vacuum_cost_limit'],
249
                    $_POST['vacuum_freeze_min_age'],
250
                    $_POST['autovacuum_freeze_max_age']
251
                );
252
                $status = $this->execute($sql);
253
            }
254
255
            return $status;
256
        }
257
258
        public function dropAutovacuum($table)
259
        {
260
            $c_schema = $this->_schema;
261
            $this->clean($c_schema);
262
            $this->clean($table);
263
264
            $rs = $this->selectSet("
265
			SELECT c.oid
266
			FROM pg_catalog.pg_class AS c
267
				LEFT JOIN pg_catalog.pg_namespace AS n ON (n.oid=c.relnamespace)
268
			WHERE
269
				c.relname = '{$table}' AND n.nspname = '{$c_schema}'
270
		");
271
272
            return $this->deleteRow('pg_autovacuum', ['vacrelid' => $rs->fields['oid']], 'pg_catalog');
273
        }
274
275
        // Sequence functions
276
277
        /**
278
         * Alter a sequence's properties
279
         *
280
         * @param $seqrs        The sequence RecordSet returned by getSequence()
281
         * @param $increment    The sequence incremental value
282
         * @param $minvalue     The sequence minimum value
283
         * @param $maxvalue     The sequence maximum value
284
         * @param $restartvalue The sequence current value
285
         * @param $cachevalue   The sequence cache value
286
         * @param $cycledvalue  Sequence can cycle ?
287
         * @param $startvalue   The sequence start value when issueing a restart (ignored)
288
         * @return int|\PHPPgAdmin\Database\A 0 success
289
         */
290
        public function alterSequenceProps(
291
            $seqrs,
292
            $increment,
293
            $minvalue,
294
            $maxvalue,
295
            $restartvalue,
296
            $cachevalue,
297
            $cycledvalue,
298
            $startvalue
299
        ) {
300
            $sql = '';
301
            /* vars are cleaned in _alterSequence */
302
            if (!empty($increment) && ($increment != $seqrs->fields['increment_by'])) {
303
                $sql .= " INCREMENT {$increment}";
304
            }
305
306
            if (!empty($minvalue) && ($minvalue != $seqrs->fields['min_value'])) {
307
                $sql .= " MINVALUE {$minvalue}";
308
            }
309
310
            if (!empty($maxvalue) && ($maxvalue != $seqrs->fields['max_value'])) {
311
                $sql .= " MAXVALUE {$maxvalue}";
312
            }
313
314
            if (!empty($restartvalue) && ($restartvalue != $seqrs->fields['last_value'])) {
315
                $sql .= " RESTART {$restartvalue}";
316
            }
317
318
            if (!empty($cachevalue) && ($cachevalue != $seqrs->fields['cache_value'])) {
319
                $sql .= " CACHE {$cachevalue}";
320
            }
321
322
            // toggle cycle yes/no
323
            if (!is_null($cycledvalue)) {
324
                $sql .= (!$cycledvalue ? ' NO ' : '') . ' CYCLE';
325
            }
326
327
            if ($sql != '') {
328
                $f_schema = $this->_schema;
329
                $this->fieldClean($f_schema);
330
                $sql = "ALTER SEQUENCE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" {$sql}";
331
332
                return $this->execute($sql);
333
            }
334
335
            return 0;
336
        }
337
338
        /**
339
         * Alter a sequence's owner
340
         *
341
         * @param $seqrs The sequence RecordSet returned by getSequence()
342
         * @param $owner
343
         * @return int|\PHPPgAdmin\Database\A 0 success
344
         * @internal param \PHPPgAdmin\Database\The $name new owner for the sequence
345
         */
346 View Code Duplication
        public function alterSequenceOwner($seqrs, $owner)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
347
        {
348
            // If owner has been changed, then do the alteration.  We are
349
            // careful to avoid this generally as changing owner is a
350
            // superuser only function.
351
            /* vars are cleaned in _alterSequence */
352
            if (!empty($owner) && ($seqrs->fields['seqowner'] != $owner)) {
353
                $f_schema = $this->_schema;
354
                $this->fieldClean($f_schema);
355
                $sql = "ALTER TABLE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" OWNER TO \"{$owner}\"";
356
357
                return $this->execute($sql);
358
            }
359
360
            return 0;
361
        }
362
363
        // Function functions
364
365
        /**
366
         * Returns all details for a particular function
367
         *
368
         * @param $function_oid
369
         * @return \PHPPgAdmin\Database\Function info
370
         * @internal param \PHPPgAdmin\Database\The $func name of the function to retrieve
371
         */
372
        public function getFunction($function_oid)
373
        {
374
            $this->clean($function_oid);
375
376
            $sql = "
377
			SELECT
378
				pc.oid AS prooid, proname, pg_catalog.pg_get_userbyid(proowner) AS proowner,
379
				nspname as proschema, lanname as prolanguage, procost, prorows,
380
				pg_catalog.format_type(prorettype, NULL) as proresult, prosrc,
381
				probin, proretset, proisstrict, provolatile, prosecdef,
382
				pg_catalog.oidvectortypes(pc.proargtypes) AS proarguments,
383
				proargnames AS proargnames,
384
				pg_catalog.obj_description(pc.oid, 'pg_proc') AS procomment,
385
				proconfig
386
			FROM
387
				pg_catalog.pg_proc pc, pg_catalog.pg_language pl,
388
				pg_catalog.pg_namespace pn
389
			WHERE
390
				pc.oid = '{$function_oid}'::oid AND pc.prolang = pl.oid
391
				AND pc.pronamespace = pn.oid
392
			";
393
394
            return $this->selectSet($sql);
395
        }
396
397
        // Capabilities
398
        public function hasQueryKill()
399
        {
400
            return false;
401
        }
402
403
        public function hasDatabaseCollation()
404
        {
405
            return false;
406
        }
407
408
        public function hasAlterSequenceStart()
409
        {
410
            return false;
411
        }
412
    }
413