mysql_users.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * mysql_list_dbs()
  14. *
  15. * This function should be needed, since PostgreSQL connection
  16. * binds database.
  17. */
  18. function pg_list_dbs($db)
  19. {
  20. assert(is_resource($db));
  21. $query = '
  22. SELECT
  23. d.datname as "Name",
  24. u.usename as "Owner",
  25. pg_encoding_to_char(d.encoding) as "Encoding"
  26. FROM
  27. pg_database d LEFT JOIN pg_user u ON d.datdba = u.usesysid
  28. ORDER BY 1;
  29. ';
  30. return pg_query($db, $query);
  31. }
  32. /*
  33. * mysql_list_tables()
  34. */
  35. function pg_list_tables($db)
  36. {
  37. assert(is_resource($db));
  38. $query = "
  39. SELECT
  40. c.relname as \"Name\",
  41. 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\",
  42. u.usename as \"Owner\"
  43. FROM
  44. pg_class c LEFT JOIN pg_user u ON c.relowner = u.usesysid
  45. WHERE
  46. c.relkind IN ('r','v','S','')
  47. AND c.relname !~ '^pg_'
  48. ORDER BY 1;
  49. ";
  50. return pg_query($db, $query);
  51. }
  52. /*
  53. * mysql_list_fields()
  54. *
  55. * See also pg_meta_data(). It returns field definition as array.
  56. */
  57. function pg_list_fields($db, $table)
  58. {
  59. assert(is_resource($db));
  60. $query = "
  61. SELECT
  62. a.attname,
  63. format_type(a.atttypid, a.atttypmod),
  64. a.attnotnull,
  65. a.atthasdef,
  66. a.attnum
  67. FROM
  68. pg_class c,
  69. pg_attribute a
  70. WHERE
  71. c.relname = '".$table."'
  72. AND a.attnum > 0 AND a.attrelid = c.oid
  73. ORDER BY a.attnum;
  74. ";
  75. return pg_query($db, $query);
  76. }
  77. ?>