xref: /PHP-7.2/ext/pgsql/mysql_users.php (revision 902d39a3)
1<?php
2/*
3 * File: mysql_users.php
4 * Author: Yasuo Ohgaki <yohgaki@php.net>
5 *
6 * This file contains example user defined functions that does
7 * similar to MySQL functions. They can be implemented as module
8 * functions, but there won't be many users need them.
9 *
10 * Requires: PostgreSQL 7.2.x
11 */
12
13/*
14 * mysql_list_dbs()
15 *
16 * This function should be needed, since PostgreSQL connection
17 * binds database.
18 */
19function pg_list_dbs($db)
20{
21        assert(is_resource($db));
22        $query = '
23SELECT
24 d.datname as "Name",
25 u.usename as "Owner",
26 pg_encoding_to_char(d.encoding) as "Encoding"
27FROM
28 pg_database d LEFT JOIN pg_user u ON d.datdba = u.usesysid
29ORDER BY 1;
30';
31        return pg_query($db, $query);
32}
33
34
35/*
36 * mysql_list_tables()
37 */
38function pg_list_tables($db)
39{
40        assert(is_resource($db));
41        $query = "
42SELECT
43 c.relname as \"Name\",
44 CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as \"Type\",
45  u.usename as \"Owner\"
46FROM
47 pg_class c LEFT JOIN pg_user u ON c.relowner = u.usesysid
48WHERE
49 c.relkind IN ('r','v','S','')
50 AND c.relname !~ '^pg_'
51ORDER BY 1;
52";
53        return pg_query($db, $query);
54}
55
56/*
57 * mysql_list_fields()
58 *
59 * See also pg_meta_data(). It returns field definition as array.
60 */
61function pg_list_fields($db, $table)
62{
63        assert(is_resource($db));
64        $query = "
65SELECT
66 a.attname,
67 format_type(a.atttypid, a.atttypmod),
68 a.attnotnull,
69 a.atthasdef,
70 a.attnum
71FROM
72 pg_class c,
73 pg_attribute a
74WHERE
75 c.relname = '".$table."'
76 AND a.attnum > 0 AND a.attrelid = c.oid
77ORDER BY a.attnum;
78";
79        return pg_query($db, $query);
80}
81
82?>
83