Back to index

php5  5.3.10
mysql_users.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * File: mysql_users.php
00004  * Author: Yasuo Ohgaki <yohgaki@php.net>
00005  * 
00006  * This file contains example user defined functions that does
00007  * similar to MySQL functions. They can be implemented as module
00008  * functions, but there won't be many users need them.
00009  *
00010  * Requires: PostgreSQL 7.2.x
00011  */
00012 
00013 /*
00014  * mysql_list_dbs()
00015  * 
00016  * This function should be needed, since PostgreSQL connection 
00017  * binds database.
00018  */
00019 function pg_list_dbs($db) 
00020 {
00021         assert(is_resource($db));
00022         $query = '
00023 SELECT
00024  d.datname as "Name",
00025  u.usename as "Owner",
00026  pg_encoding_to_char(d.encoding) as "Encoding"
00027 FROM 
00028  pg_database d LEFT JOIN pg_user u ON d.datdba = u.usesysid
00029 ORDER BY 1;
00030 ';
00031         return pg_query($db, $query);
00032 }
00033 
00034 
00035 /*
00036  * mysql_list_tables()
00037  */
00038 function pg_list_tables($db) 
00039 {
00040         assert(is_resource($db));
00041         $query = "
00042 SELECT
00043  c.relname as \"Name\",
00044  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\",
00045   u.usename as \"Owner\"
00046 FROM
00047  pg_class c LEFT JOIN pg_user u ON c.relowner = u.usesysid
00048 WHERE
00049  c.relkind IN ('r','v','S','')
00050  AND c.relname !~ '^pg_'
00051 ORDER BY 1;
00052 ";
00053         return pg_query($db, $query);
00054 }
00055 
00056 /*
00057  * mysql_list_fields()
00058  *
00059  * See also pg_meta_data(). It returns field defintion as array.
00060  */
00061 function pg_list_fields($db, $table) 
00062 {
00063         assert(is_resource($db));
00064         $query = "
00065 SELECT
00066  a.attname,
00067  format_type(a.atttypid, a.atttypmod),
00068  a.attnotnull,
00069  a.atthasdef,
00070  a.attnum
00071 FROM
00072  pg_class c,
00073  pg_attribute a
00074 WHERE
00075  c.relname = '".$table."'
00076  AND a.attnum > 0 AND a.attrelid = c.oid
00077 ORDER BY a.attnum;
00078 ";
00079         return pg_query($db, $query);
00080 }
00081 
00082 ?>