Passed
Push — master ( 2856b4...cf0423 )
by Felipe
10:02 queued 05:52
created

Postgres80::_alterView()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 5
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.33
5
 */
6
7
namespace PHPPgAdmin\Database;
8
9
/**
10
 * @file
11
 * PostgreSQL 8.0 support
12
 *
13
 * Id: Postgres80.php,v 1.28 2007/12/12 04:11:10 xzilla Exp $
14
 * @package PHPPgAdmin
15
 */
16
class Postgres80 extends Postgres81
17
{
18
    public $major_version = 8.0;
19
    // Map of database encoding names to HTTP encoding names.  If a
20
    // database encoding does not appear in this list, then its HTTP
21
    // encoding name is the same as its database encoding name.
22
    public $codemap = [
23
        'ALT'        => 'CP866',
24
        'EUC_CN'     => 'GB2312',
25
        'EUC_JP'     => 'EUC-JP',
26
        'EUC_KR'     => 'EUC-KR',
27
        'EUC_TW'     => 'EUC-TW',
28
        'ISO_8859_5' => 'ISO-8859-5',
29
        'ISO_8859_6' => 'ISO-8859-6',
30
        'ISO_8859_7' => 'ISO-8859-7',
31
        'ISO_8859_8' => 'ISO-8859-8',
32
        'JOHAB'      => 'CP1361',
33
        'KOI8'       => 'KOI8-R',
34
        'LATIN1'     => 'ISO-8859-1',
35
        'LATIN2'     => 'ISO-8859-2',
36
        'LATIN3'     => 'ISO-8859-3',
37
        'LATIN4'     => 'ISO-8859-4',
38
        // The following encoding map is a known error in PostgreSQL < 7.2
39
        // See the constructor for Postgres72.
40
        'LATIN5'     => 'ISO-8859-5',
41
        'LATIN6'     => 'ISO-8859-10',
42
        'LATIN7'     => 'ISO-8859-13',
43
        'LATIN8'     => 'ISO-8859-14',
44
        'LATIN9'     => 'ISO-8859-15',
45
        'LATIN10'    => 'ISO-8859-16',
46
        'SQL_ASCII'  => 'US-ASCII',
47
        'TCVN'       => 'CP1258',
48
        'UNICODE'    => 'UTF-8',
49
        'WIN'        => 'CP1251',
50
        'WIN874'     => 'CP874',
51
        'WIN1256'    => 'CP1256',
52
    ];
53
54
    /**
55
     * Return all database available on the server.
56
     *
57
     * @param null $currentdatabase
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $currentdatabase is correct as it would always require null to be passed?
Loading history...
58
     *
59
     * @return \ADORecordSet A list of databases, sorted alphabetically
60
     */
61
    public function getDatabases($currentdatabase = null)
62
    {
63
        $conf        = $this->conf;
64
        $server_info = $this->server_info;
0 ignored issues
show
Bug Best Practice introduced by
The property server_info does not exist on PHPPgAdmin\Database\Postgres80. Did you maybe forget to declare it?
Loading history...
65
66
        if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser()) {
67
            $username = $server_info['username'];
68
            $this->clean($username);
69
            $clause = " AND pu.usename='{$username}'";
70
        } else {
71
            $clause = '';
72
        }
73
74
        if ($currentdatabase != null) {
75
            $this->clean($currentdatabase);
76
            $orderby = "ORDER BY pdb.datname = '{$currentdatabase}' DESC, pdb.datname";
77
        } else {
78
            $orderby = 'ORDER BY pdb.datname';
79
        }
80
81
        if (!$conf['show_system']) {
82
            $where = ' AND NOT pdb.datistemplate';
83
        } else {
84
            $where = ' AND pdb.datallowconn';
85
        }
86
87
        $sql = "SELECT pdb.datname AS datname,
88
                pu.usename AS datowner,
89
                pg_encoding_to_char(encoding) AS datencoding,
90
                (SELECT description FROM pg_description pd WHERE pdb.oid=pd.objoid) AS datcomment,
91
                (SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=pdb.dattablespace) AS tablespace
92
                FROM pg_database pdb, pg_user pu
93
    			WHERE pdb.datdba = pu.usesysid
94
    			{$where}
95
    			{$clause}
96
    			{$orderby}";
97
98
        return $this->selectSet($sql);
99
    }
100
101
    // Schema functions
102
103
    /**
104
     * Return all schemas in the current database.
105
     *
106
     * @return \ADORecordSet All schemas, sorted alphabetically
107
     */
108
    public function getSchemas()
109
    {
110
        $conf = $this->conf;
111
112
        if (!$conf['show_system']) {
113
            $where = "WHERE nspname NOT LIKE 'pg@_%' ESCAPE '@' AND nspname != 'information_schema'";
114
        } else {
115
            $where = "WHERE nspname !~ '^pg_t(emp_[0-9]+|oast)$'";
116
        }
117
118
        $sql = "
119
			SELECT pn.nspname, pu.usename AS nspowner,
120
				pg_catalog.obj_description(pn.oid, 'pg_namespace') AS nspcomment
121
			FROM pg_catalog.pg_namespace pn
122
				LEFT JOIN pg_catalog.pg_user pu ON (pn.nspowner = pu.usesysid)
123
			{$where}
124
			ORDER BY nspname";
125
126
        return $this->selectSet($sql);
127
    }
128
129
    /**
130
     * Return all information relating to a schema.
131
     *
132
     * @param string $schema The name of the schema
133
     *
134
     * @return \ADORecordSet Schema information
135
     */
136
    public function getSchemaByName($schema)
137
    {
138
        $this->clean($schema);
139
        $sql = "
140
			SELECT nspname, nspowner, u.usename AS ownername, nspacl,
141
				pg_catalog.obj_description(pn.oid, 'pg_namespace') as nspcomment
142
            FROM pg_catalog.pg_namespace pn
143
            	LEFT JOIN pg_shadow as u ON pn.nspowner = u.usesysid
144
			WHERE nspname='{$schema}'";
145
146
        return $this->selectSet($sql);
147
    }
148
149
    // Table functions
150
151
    /**
152
     * Changes a user's password.
153
     *
154
     * @param string $username The username
155
     * @param string $password The new password
156
     *
157
     * @return integer 0 if operation was successful
158
     */
159
    public function changePassword($username, $password)
160
    {
161
        $enc = $this->_encryptPassword($username, $password);
162
        $this->fieldClean($username);
163
        $this->clean($enc);
164
165
        $sql = "ALTER USER \"{$username}\" WITH ENCRYPTED PASSWORD '{$enc}'";
166
167
        return $this->execute($sql);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute($sql) returns the type ADORecordSet which is incompatible with the documented return type integer.
Loading history...
168
    }
169
170
    // View functions
171
172
    /**
173
     * Gets all information for an aggregate.
174
     *
175
     * @param string $name     The name of the aggregate
176
     * @param string $basetype The input data type of the aggregate
177
     *
178
     * @return \ADORecordSet A recordset
179
     */
180
    public function getAggregate($name, $basetype)
181
    {
182
        $c_schema = $this->_schema;
183
        $this->clean($c_schema);
184
        $this->clean($name);
185
        $this->clean($basetype);
186
187
        $sql = "
188
			SELECT p.proname,
189
				CASE p.proargtypes[0]
190
					WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN NULL
191
					ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
192
				END AS proargtypes, a.aggtransfn, format_type(a.aggtranstype, NULL) AS aggstype,
193
				a.aggfinalfn, a.agginitval, u.usename, pg_catalog.obj_description(p.oid, 'pg_proc') AS aggrcomment
194
			FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
195
			WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
196
				AND p.proisagg AND n.nspname='{$c_schema}'
197
				AND p.proname='{$name}'
198
				AND CASE p.proargtypes[0]
199
					WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN ''
200
					ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
201
				END ='{$basetype}'";
202
203
        return $this->selectSet($sql);
204
    }
205
206
    // Sequence functions
207
208
    public function hasAggregateSortOp()
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
209
    {
210
        return false;
211
    }
212
213
    // Role, User/group functions
214
215
    public function hasAlterTableSchema()
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
216
    {
217
        return false;
218
    }
219
220
    // Aggregate functions
221
222
    public function hasAutovacuum()
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
223
    {
224
        return false;
225
    }
226
227
    // Capabilities
228
229
    public function hasDisableTriggers()
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
230
    {
231
        return false;
232
    }
233
234
    public function hasFunctionAlterSchema()
235
    {
236
        return false;
237
    }
238
239
    public function hasPreparedXacts()
240
    {
241
        return false;
242
    }
243
244
    public function hasRoles()
245
    {
246
        return false;
247
    }
248
249
    public function hasAlterSequenceSchema()
250
    {
251
        return false;
252
    }
253
254
    public function hasServerAdminFuncs()
255
    {
256
        return false;
257
    }
258
259
    /**
260
     * Protected method which alter a table
261
     * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION.
262
     *
263
     * @param $tblrs      The table recordSet returned by getTable()
0 ignored issues
show
Bug introduced by
The type PHPPgAdmin\Database\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
264
     * @param $name       The new name for the table
265
     * @param $owner      The new owner for the table
266
     * @param $schema     The new schema for the table
267
     * @param $comment    The comment on the table
268
     * @param $tablespace The new tablespace for the table ('' means leave as is)
269
     *
270
     * @return int 0 success
271
     */
272
    protected function _alterTable($tblrs, $name, $owner, $schema, $comment, $tablespace)
1 ignored issue
show
Coding Style introduced by
Protected method name "Postgres80::_alterTable" must not be prefixed with an underscore
Loading history...
273
    {
274
        /* $schema not supported in pg80- */
275
276
        // Comment
277
        $status = $this->setComment('TABLE', '', $tblrs->fields['relname'], $comment);
278
        if ($status != 0) {
279
            return -4;
280
        }
281
282
        // Owner
283
        $this->fieldClean($owner);
284
        $status = $this->alterTableOwner($tblrs, $owner);
285
        if ($status != 0) {
286
            return -5;
287
        }
288
289
        // Tablespace
290
        $this->fieldClean($tablespace);
291
        $status = $this->alterTableTablespace($tblrs, $tablespace);
292
        if ($status != 0) {
293
            return -6;
294
        }
295
296
        // Rename
297
        $this->fieldClean($name);
298
        $status = $this->alterTableName($tblrs, $name);
299
        if ($status != 0) {
300
            return -3;
301
        }
302
303
        return 0;
304
    }
305
306
    /**
307
     * Protected method which alter a view
308
     * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION.
309
     *
310
     * @param $vwrs    The view recordSet returned by getView()
311
     * @param $name    The new name for the view
312
     * @param $owner   The new owner for the view
313
     * @param $schema
314
     * @param $comment The comment on the view
315
     *
316
     * @return int 0 success
317
     */
318
    protected function _alterView($vwrs, $name, $owner, $schema, $comment)
1 ignored issue
show
Coding Style introduced by
Protected method name "Postgres80::_alterView" must not be prefixed with an underscore
Loading history...
319
    {
320
        /* $schema not supported in pg80- */
321
        $this->fieldArrayClean($vwrs->fields);
322
323
        // Comment
324
        if ($this->setComment('VIEW', $vwrs->fields['relname'], '', $comment) != 0) {
325
            return -4;
326
        }
327
328
        // Owner
329
        $this->fieldClean($owner);
330
        $status = $this->alterViewOwner($vwrs, $owner);
331
        if ($status != 0) {
332
            return -5;
333
        }
334
335
        // Rename
336
        $this->fieldClean($name);
337
        $status = $this->alterViewName($vwrs, $name);
338
        if ($status != 0) {
339
            return -3;
340
        }
341
342
        return 0;
343
    }
344
345
    /**
346
     * Protected method which alter a sequence
347
     * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION.
348
     *
349
     * @param $seqrs        The sequence recordSet returned by getSequence()
350
     * @param $name         The new name for the sequence
351
     * @param $comment      The comment on the sequence
352
     * @param $owner        The new owner for the sequence
353
     * @param $schema       The new schema for the sequence
354
     * @param $increment    The increment
355
     * @param $minvalue     The min value
356
     * @param $maxvalue     The max value
357
     * @param $restartvalue The starting value
358
     * @param $cachevalue   The cache value
359
     * @param $cycledvalue  True if cycled, false otherwise
360
     * @param $startvalue   The sequence start value when issueing a restart
361
     *
362
     * @return int 0 success
363
     */
364
    protected function _alterSequence(
1 ignored issue
show
Coding Style introduced by
Protected method name "Postgres80::_alterSequence" must not be prefixed with an underscore
Loading history...
365
        $seqrs,
366
        $name,
367
        $comment,
368
        $owner,
369
        $schema,
370
        $increment,
371
        $minvalue,
372
        $maxvalue,
373
        $restartvalue,
374
        $cachevalue,
375
        $cycledvalue,
376
        $startvalue
377
    ) {
378
        /* $schema not supported in pg80- */
379
        $this->fieldArrayClean($seqrs->fields);
380
381
        // Comment
382
        $status = $this->setComment('SEQUENCE', $seqrs->fields['seqname'], '', $comment);
383
        if ($status != 0) {
384
            return -4;
385
        }
386
387
        // Owner
388
        $this->fieldClean($owner);
389
        $status = $this->alterSequenceOwner($seqrs, $owner);
390
        if ($status != 0) {
391
            return -5;
392
        }
393
394
        // Props
395
        $this->clean($increment);
396
        $this->clean($minvalue);
397
        $this->clean($maxvalue);
398
        $this->clean($restartvalue);
399
        $this->clean($cachevalue);
400
        $this->clean($cycledvalue);
401
        $this->clean($startvalue);
402
        $status = $this->alterSequenceProps(
403
            $seqrs,
404
            $increment,
405
            $minvalue,
406
            $maxvalue,
407
            $restartvalue,
408
            $cachevalue,
409
            $cycledvalue,
410
            null
411
        );
412
        if ($status != 0) {
413
            return -6;
414
        }
415
416
        // Rename
417
        $this->fieldClean($name);
418
        $status = $this->alterSequenceName($seqrs, $name);
419
        if ($status != 0) {
420
            return -3;
421
        }
422
423
        return 0;
424
    }
425
426
    /**
427
     * Return all tables in current database (and schema).
428
     *
429
     * @param bool|true $all True to fetch all tables, false for just in current schema
430
     *
431
     * @return \ADORecordSet All tables, sorted alphabetically
432
     */
433
    public function getTables($all = false)
434
    {
435
        $c_schema = $this->_schema;
436
        $this->clean($c_schema);
437
        if ($all) {
438
            // Exclude pg_catalog and information_schema tables
439
            $sql = "SELECT schemaname AS nspname, tablename AS relname, tableowner AS relowner
440
                    FROM pg_catalog.pg_tables
441
                    WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
442
                    ORDER BY schemaname, tablename";
443
        } else {
444
            $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
445
                        pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment,
446
                        reltuples::bigint
447
                    FROM pg_catalog.pg_class c
448
                    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
449
                    WHERE c.relkind = 'r'
450
                    AND nspname='{$c_schema}'
451
                    ORDER BY c.relname";
452
        }
453
454
        return $this->selectSet($sql);
455
    }
456
457
}
458