Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Postgres80.php « database « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: da45e25f0406762a8eba997bfa70639428cafe3c (plain)
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
<?php

/**
 * PHPPgAdmin v6.0.0-RC7
 */

namespace PHPPgAdmin\Database;

/**
 * @file
 * PostgreSQL 8.0 support
 *
 * Id: Postgres80.php,v 1.28 2007/12/12 04:11:10 xzilla Exp $
 *
 * @package PHPPgAdmin
 */
class Postgres80 extends Postgres81
{
    public $major_version = 8.0;
    // Map of database encoding names to HTTP encoding names.  If a
    // database encoding does not appear in this list, then its HTTP
    // encoding name is the same as its database encoding name.
    public $codemap = [
        'ALT'        => 'CP866',
        'EUC_CN'     => 'GB2312',
        'EUC_JP'     => 'EUC-JP',
        'EUC_KR'     => 'EUC-KR',
        'EUC_TW'     => 'EUC-TW',
        'ISO_8859_5' => 'ISO-8859-5',
        'ISO_8859_6' => 'ISO-8859-6',
        'ISO_8859_7' => 'ISO-8859-7',
        'ISO_8859_8' => 'ISO-8859-8',
        'JOHAB'      => 'CP1361',
        'KOI8'       => 'KOI8-R',
        'LATIN1'     => 'ISO-8859-1',
        'LATIN2'     => 'ISO-8859-2',
        'LATIN3'     => 'ISO-8859-3',
        'LATIN4'     => 'ISO-8859-4',
        // The following encoding map is a known error in PostgreSQL < 7.2
        // See the constructor for Postgres72.
        'LATIN5'     => 'ISO-8859-5',
        'LATIN6'     => 'ISO-8859-10',
        'LATIN7'     => 'ISO-8859-13',
        'LATIN8'     => 'ISO-8859-14',
        'LATIN9'     => 'ISO-8859-15',
        'LATIN10'    => 'ISO-8859-16',
        'SQL_ASCII'  => 'US-ASCII',
        'TCVN'       => 'CP1258',
        'UNICODE'    => 'UTF-8',
        'WIN'        => 'CP1251',
        'WIN874'     => 'CP874',
        'WIN1256'    => 'CP1256',
    ];

    /**
     * Return all database available on the server.
     *
     * @param null|string $currentdatabase
     *
     * @return \PHPPgAdmin\ADORecordSet A list of databases, sorted alphabetically
     */
    public function getDatabases($currentdatabase = null)
    {
        $conf        = $this->conf;
        $server_info = $this->server_info;

        if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser()) {
            $username = $server_info['username'];
            $this->clean($username);
            $clause = " AND pu.usename='{$username}'";
        } else {
            $clause = '';
        }

        if ($currentdatabase) {
            $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,
                pu.usename AS datowner,
                pg_encoding_to_char(encoding) AS datencoding,
                (SELECT description FROM pg_description pd WHERE pdb.oid=pd.objoid) AS datcomment,
                (SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=pdb.dattablespace) AS tablespace
                FROM pg_database pdb, pg_user pu
    			WHERE pdb.datdba = pu.usesysid
    			{$where}
    			{$clause}
    			{$orderby}";

        return $this->selectSet($sql);
    }

    // Schema functions

    /**
     * Return all schemas in the current database.
     *
     * @return \PHPPgAdmin\ADORecordSet All schemas, sorted alphabetically
     */
    public function getSchemas()
    {
        $conf = $this->conf;

        if (!$conf['show_system']) {
            $where = "WHERE nspname NOT LIKE 'pg@_%' ESCAPE '@' AND nspname != 'information_schema'";
        } else {
            $where = "WHERE nspname !~ '^pg_t(emp_[0-9]+|oast)$'";
        }

        $sql = "
			SELECT pn.nspname, pu.usename AS nspowner,
				pg_catalog.obj_description(pn.oid, 'pg_namespace') AS nspcomment
			FROM pg_catalog.pg_namespace pn
				LEFT JOIN pg_catalog.pg_user pu ON (pn.nspowner = pu.usesysid)
			{$where}
			ORDER BY nspname";

        return $this->selectSet($sql);
    }

    /**
     * Return all information relating to a schema.
     *
     * @param string $schema The name of the schema
     *
     * @return \PHPPgAdmin\ADORecordSet Schema information
     */
    public function getSchemaByName($schema)
    {
        $this->clean($schema);
        $sql = "
			SELECT nspname, nspowner, u.usename AS ownername, nspacl,
				pg_catalog.obj_description(pn.oid, 'pg_namespace') as nspcomment
            FROM pg_catalog.pg_namespace pn
            	LEFT JOIN pg_shadow as u ON pn.nspowner = u.usesysid
			WHERE nspname='{$schema}'";

        return $this->selectSet($sql);
    }

    // Table functions

    /**
     * Changes a user's password.
     *
     * @param string $username The username
     * @param string $password The new password
     *
     * @return int 0 if operation was successful
     */
    public function changePassword($username, $password)
    {
        $enc = $this->_encryptPassword($username, $password);
        $this->fieldClean($username);
        $this->clean($enc);

        $sql = "ALTER USER \"{$username}\" WITH ENCRYPTED PASSWORD '{$enc}'";

        return $this->execute($sql);
    }

    // View functions

    /**
     * Gets all information for an aggregate.
     *
     * @param string $name     The name of the aggregate
     * @param string $basetype The input data type of the aggregate
     *
     * @return \PHPPgAdmin\ADORecordSet A recordset
     */
    public function getAggregate($name, $basetype)
    {
        $c_schema = $this->_schema;
        $this->clean($c_schema);
        $this->clean($name);
        $this->clean($basetype);

        $sql = "
			SELECT p.proname,
				CASE p.proargtypes[0]
					WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN NULL
					ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
				END AS proargtypes, a.aggtransfn, format_type(a.aggtranstype, NULL) AS aggstype,
				a.aggfinalfn, a.agginitval, u.usename, pg_catalog.obj_description(p.oid, 'pg_proc') AS aggrcomment
			FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
			WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
				AND p.proisagg AND n.nspname='{$c_schema}'
				AND p.proname='{$name}'
				AND CASE p.proargtypes[0]
					WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN ''
					ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
				END ='{$basetype}'";

        return $this->selectSet($sql);
    }

    // Sequence functions

    public function hasAggregateSortOp()
    {
        return false;
    }

    // Role, User/group functions

    public function hasAlterTableSchema()
    {
        return false;
    }

    // Aggregate functions

    public function hasAutovacuum()
    {
        return false;
    }

    // Capabilities

    public function hasDisableTriggers()
    {
        return false;
    }

    public function hasFunctionAlterSchema()
    {
        return false;
    }

    public function hasPreparedXacts()
    {
        return false;
    }

    public function hasRoles()
    {
        return false;
    }

    public function hasAlterSequenceSchema()
    {
        return false;
    }

    public function hasServerAdminFuncs()
    {
        return false;
    }

    /**
     * Protected method which alter a table
     * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION.
     *
     * @param \PHPPgAdmin\ADORecordSet $tblrs      The table recordSet returned by getTable()
     * @param string                   $name       The new name for the table
     * @param string                   $owner      The new owner for the table
     * @param string                   $schema     The new schema for the table
     * @param string                   $comment    The comment on the table
     * @param string                   $tablespace The new tablespace for the table ('' means leave as is)
     *
     * @return int 0 success
     */
    protected function _alterTable($tblrs, $name, $owner, $schema, $comment, $tablespace)
    {
        /* $schema not supported in pg80- */

        // Comment
        $status = $this->setComment('TABLE', '', $tblrs->fields['relname'], $comment);
        if ($status != 0) {
            return -4;
        }

        // Owner
        $this->fieldClean($owner);
        $status = $this->alterTableOwner($tblrs, $owner);
        if ($status != 0) {
            return -5;
        }

        // Tablespace
        $this->fieldClean($tablespace);
        $status = $this->alterTableTablespace($tblrs, $tablespace);
        if ($status != 0) {
            return -6;
        }

        // Rename
        $this->fieldClean($name);
        $status = $this->alterTableName($tblrs, $name);
        if ($status != 0) {
            return -3;
        }

        return 0;
    }

    /**
     * Protected method which alter a view
     * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION.
     *
     * @param \PHPPgAdmin\ADORecordSet $vwrs    The view recordSet returned by getView()
     * @param string                   $name    The new name for the view
     * @param string                   $owner   The new owner for the view
     * @param string                   $schema  The view schema
     * @param string                   $comment The comment on the view
     *
     * @return int 0 success, otherwise, an error code
     */
    protected function _alterView($vwrs, $name, $owner, $schema, $comment)
    {
        $type = 'VIEW';
        if ($vwrs->fields['relkind'] === 'm') {
            $type = 'MATERIALIZED VIEW';
        }
        /* $schema not supported in pg80- */
        $this->fieldArrayClean($vwrs->fields);

        // Comment
        if ($this->setComment($type, $vwrs->fields['relname'], '', $comment) != 0) {
            return -4;
        }

        // Owner
        $this->fieldClean($owner);
        $status = $this->alterViewOwner($vwrs, $owner);
        if ($status != 0) {
            return -5;
        }

        // Rename
        $this->fieldClean($name);
        $status = $this->alterViewName($vwrs, $name);
        if ($status != 0) {
            return -3;
        }

        return 0;
    }

    /**
     * Protected method which alter a sequence
     * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION.
     *
     * @param \PHPPgAdmin\ADORecordSet $seqrs        The sequence recordSet returned by getSequence()
     * @param string                   $name         The new name for the sequence
     * @param string                   $comment      The comment on the sequence
     * @param string                   $owner        The new owner for the sequence
     * @param string                   $schema       The new schema for the sequence
     * @param int                      $increment    The increment
     * @param int                      $minvalue     The min value
     * @param int                      $maxvalue     The max value
     * @param int                      $restartvalue The starting value
     * @param int                      $cachevalue   The cache value
     * @param bool                     $cycledvalue  True if cycled, false otherwise
     * @param int                      $startvalue   The sequence start value when issueing a restart
     *
     * @return int 0 success
     */
    protected function _alterSequence(
        $seqrs,
        $name,
        $comment,
        $owner,
        $schema,
        $increment,
        $minvalue,
        $maxvalue,
        $restartvalue,
        $cachevalue,
        $cycledvalue,
        $startvalue
    ) {
        /* $schema not supported in pg80- */
        $this->fieldArrayClean($seqrs->fields);

        // Comment
        $status = $this->setComment('SEQUENCE', $seqrs->fields['seqname'], '', $comment);
        if ($status != 0) {
            return -4;
        }

        // Owner
        $this->fieldClean($owner);
        $status = $this->alterSequenceOwner($seqrs, $owner);
        if ($status != 0) {
            return -5;
        }

        // Props
        $this->clean($increment);
        $this->clean($minvalue);
        $this->clean($maxvalue);
        $this->clean($restartvalue);
        $this->clean($cachevalue);
        $this->clean($cycledvalue);
        $this->clean($startvalue);
        $status = $this->alterSequenceProps(
            $seqrs,
            $increment,
            $minvalue,
            $maxvalue,
            $restartvalue,
            $cachevalue,
            $cycledvalue,
            null
        );
        if ($status != 0) {
            return -6;
        }

        // Rename
        $this->fieldClean($name);
        $status = $this->alterSequenceName($seqrs, $name);
        if ($status != 0) {
            return -3;
        }

        return 0;
    }

    /**
     * Return all tables in current database (and schema).
     *
     * @return \PHPPgAdmin\ADORecordSet All tables, sorted alphabetically
     */
    public function getTables()
    {
        $c_schema = $this->_schema;
        $this->clean($c_schema);
        $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
                        pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment,
                        reltuples::bigint as reltuples,
                        null tablespace,
                        'N/A' as table_size
                    FROM pg_catalog.pg_class c
                    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
                    WHERE c.relkind = 'r'
                    AND nspname='{$c_schema}'
                    ORDER BY c.relname";

        return $this->selectSet($sql);
    }
}