Add comments for databases and tablespaces using 8.2 global system catalog
authorxzilla <xzilla>
Sun, 19 Nov 2006 21:33:13 +0000 (21:33 +0000)
committerxzilla <xzilla>
Sun, 19 Nov 2006 21:33:13 +0000 (21:33 +0000)
all_db.php
classes/database/Postgres.php
classes/database/Postgres80.php
classes/database/Postgres82.php
tablespaces.php

index fa68b2bf7209091d14366f41d7d681b448bf6389..611da9164cbb2bdbdd853c51f3c3d95b1a23639f 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * Manage databases within a server
         *
-        * $Id: all_db.php,v 1.44 2006/06/17 12:57:36 xzilla Exp $
+        * $Id: all_db.php,v 1.45 2006/11/19 21:33:13 xzilla Exp $
         */
 
        // Include application functions
                                }
                                echo "</select></td></tr>\n";
                        }
+                       if ($data->hasSharedComments()){
+                               $rs = $data->getDatabaseComment($_REQUEST['alterdatabase']);
+                               $comment = isset($rs->fields['description']) ? $rs->fields['description'] : '';
+                               echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
+                               echo "<td class=\"data1\">";
+                               echo "<textarea rows=\"3\" cols=\"32\" name=\"dbcomment\" wrap=\"virtual\">",
+                                       htmlspecialchars($comment), "</textarea></td></tr>\n";
+                       }
                        echo "</table>\n";
                        echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
                        echo $misc->form;
@@ -60,9 +68,8 @@
                        echo "</form>\n";
                }
                else {
-                       //all versions that support the alter database functionality (starting 7.4) support renaming a db
                        $newOwner = isset($_POST['owner']) ? $_POST['owner'] : '';
-                       if ($data->AlterDatabase($_POST['oldname'], $_POST['newname'], $newOwner) == 0) {
+                       if ($data->AlterDatabase($_POST['oldname'], $_POST['newname'], $newOwner, $_POST['dbcomment']) == 0) {
                                $_reload_browser = true;
                                doDefault($lang['strdatabasealtered']);
                        }
                                $_POST['formEncoding'] = '';
                }
                if (!isset($_POST['formSpc'])) $_POST['formSpc'] = '';
+               if (!isset($_POST['formComment'])) $_POST['formComment'] = '';
                
                // Fetch all tablespaces from the database
                if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces();
                        }
                        echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
                }
-               
+
+               // Comments (if available)
+               if ($data->hasSharedComments()) {
+                       echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
+                       echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\" wrap=\"virtual\">", 
+                               htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
+               }
+
                echo "</table>\n";
                echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
                echo $misc->form;
                // Default tablespace to null if it isn't set
                if (!isset($_POST['formSpc'])) $_POST['formSpc'] = null;
 
+               // Default comment to blank if it isn't set
+               if (!isset($_POST['formComment'])) $_POST['formComment'] = null;
+
                // Check that they've given a name and a definition
                if ($_POST['formName'] == '') doCreate($lang['strdatabaseneedsname']);
                else {
-                       $status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc']);
+                       $status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc'], $_POST['formComment']);
                        if ($status == 0) {
                                $_reload_browser = true;
                                doDefault($lang['strdatabasecreated']);
index f32f5ba4b7a3c5aee9195ba4f263822905417968..d04cc340376deffd825ab61bcfc34f7e6a16da06 100755 (executable)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres.php,v 1.291 2006/11/01 00:51:19 xzilla Exp $
+ * $Id: Postgres.php,v 1.292 2006/11/19 21:33:13 xzilla Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -512,11 +512,14 @@ class Postgres extends ADODB_base {
         * @param $encoding Encoding of the database
         * @param $tablespace (optional) The tablespace name
         * @return 0 success
+        * @return -1 tablespace error
+        * @return -2 comment error
         */
-       function createDatabase($database, $encoding, $tablespace = '') {
+       function createDatabase($database, $encoding, $tablespace = '', $comment = '') {
                $this->fieldClean($database);
                $this->clean($encoding);
                $this->fieldClean($tablespace);
+               $this->fieldClean($comment);
 
                if ($encoding == '') {
                        $sql = "CREATE DATABASE \"{$database}\"";
@@ -525,8 +528,17 @@ class Postgres extends ADODB_base {
                }
                
                if ($tablespace != '' && $this->hasTablespaces()) $sql .= " TABLESPACE \"{$tablespace}\"";
-               
-               return $this->execute($sql);
+                       
+               $status = $this->execute($sql);
+               if ($status != 0) return -1;
+       
+               if ($comment != '' && $this->hasSharedComments()) { 
+                       $status = $this->setComment('DATABASE',$database,'',$comment);
+                       if ($status != 0) return -2;
+               }
+
+               return 0;
+
        }
 
        /**
@@ -3867,10 +3879,13 @@ class Postgres extends ADODB_base {
                        case 'COLUMN':
                                $sql .= "\"{$table}\".\"{$obj_name}\" IS ";
                                break;
-                       case 'VIEW':
+                       case 'DATABASE';
+                       case 'ROLE';
                        case 'SCHEMA':
                        case 'SEQUENCE':
+                       case 'TABLESPACE';
                        case 'TYPE':
+                       case 'VIEW':
                                $sql .= "\"{$obj_name}\" IS ";
                                break;
                        case 'FUNCTION':                                
@@ -4638,6 +4653,7 @@ class Postgres extends ADODB_base {
        function hasPreparedXacts() { return false; }
        function hasDisableTriggers() { return false; }
        function hasAlterAggregate() { return false; }
+       function hasSharedComments() {return false;}
 }
 
 ?>
index 752842d525fff50bcab909a537c68a9b34cb692b..04d9a15fe2e979a2fb15c9336225a58fd4dd8a16 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * PostgreSQL 8.0 support
  *
- * $Id: Postgres80.php,v 1.19 2006/05/19 07:17:30 chriskl Exp $
+ * $Id: Postgres80.php,v 1.20 2006/11/19 21:33:13 xzilla Exp $
  */
 
 include_once('./classes/database/Postgres74.php');
@@ -386,9 +386,10 @@ class Postgres80 extends Postgres74 {
         * @param $spcloc The directory in which to create the tablespace
         * @return 0 success
         */
-       function createTablespace($spcname, $spcowner, $spcloc) {
+       function createTablespace($spcname, $spcowner, $spcloc, $comment='') {
                $this->fieldClean($spcname);
                $this->clean($spcloc);
+               $this->clean($comment);
                
                $sql = "CREATE TABLESPACE \"{$spcname}\"";
                
@@ -399,7 +400,16 @@ class Postgres80 extends Postgres74 {
                
                $sql .= " LOCATION '{$spcloc}'";
 
-               return $this->execute($sql);
+               $status = $this->execute($sql);
+               if ($status != 0) return -1;
+
+               if ($comment != '' && $this->hasSharedComments()) { 
+                       $status = $this->setComment('TABLESPACE',$spcname,'',$comment);
+                       if ($status != 0) return -2;
+               }
+
+               return 0;
+       
        }
 
        /**
@@ -424,8 +434,9 @@ class Postgres80 extends Postgres74 {
         * @return -1 transaction error
         * @return -2 owner error
         * @return -3 rename error
+        * @return -4 comment error
         */
-       function alterTablespace($spcname, $name, $owner) {
+       function alterTablespace($spcname, $name, $owner, $comment='') {
                $this->fieldClean($spcname);
                $this->fieldClean($name);
                $this->fieldClean($owner);
@@ -451,6 +462,12 @@ class Postgres80 extends Postgres74 {
                                return -3;
                        }
                }
+
+               // Set comment if it has changed
+               if (trim($comment) != '' && $this->hasSharedComments()) { 
+                       $status = $this->setComment('TABLESPACE',$spcname,'',$comment);
+                       if ($status != 0) return -4;
+               }
                                
                return $this->endTransaction();
        }
index bb62c0cde1aeffc5a6016d6cd7bde8fe62fc534c..54c2d55339cd674263055ea313289761c56be5d8 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * PostgreSQL 8.2 support
  *
- * $Id: Postgres82.php,v 1.2 2006/11/01 00:50:17 xzilla Exp $
+ * $Id: Postgres82.php,v 1.3 2006/11/19 21:33:13 xzilla Exp $
  */
 
 include_once('./classes/database/Postgres81.php');
@@ -60,6 +60,125 @@ class Postgres82 extends Postgres81 {
                return $this->help_page;
        }
 
+       // Database functions
+       /**
+        * Return all database available on the server
+        * @return A list of databases, sorted alphabetically
+        */
+       function getDatabases($currentdatabase = NULL) {
+               global $conf, $misc;
+               
+               $server_info = $misc->getServerInfo();
+               
+               if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser($server_info['username'])) {
+                       $username = $server_info['username'];
+                       $this->clean($username);
+                       $clause = " AND pu.usename='{$username}'";
+               }
+               else $clause = '';
+
+               if ($currentdatabase != NULL)
+                       $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, pr.rolname AS datowner, pg_encoding_to_char(encoding) AS datencoding,
+                               (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pdb.oid=pd.objoid) AS datcomment,
+                               (SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=pdb.dattablespace) AS tablespace,
+                                                          pg_catalog.pg_database_size(pdb.oid) as dbsize 
+                        FROM pg_catalog.pg_database pdb LEFT JOIN pg_catalog.pg_roles pr ON (pdb.datdba = pr.oid)  
+                                               WHERE true 
+                       {$where}
+                       {$clause}
+                       {$orderby}";
+
+               return $this->selectSet($sql);
+       }
+
+       /**
+        * Alters a database
+        * the multiple return vals are for postgres 8+ which support more functionality in alter database
+        * @param $dbName The name of the database
+        * @param $newName new name for the database
+        * @param $newOwner The new owner for the database
+        * @return 0 success
+        * @return -1 transaction error
+        * @return -2 owner error
+        * @return -3 rename error
+        * @return -4 comment error
+        */
+       function alterDatabase($dbName, $newName, $newOwner = '',$comment = '')
+       {
+               //ignore $newowner, not supported pre 8.0
+               //ignore $comment, not supported pre 8.2
+               $this->clean($dbName);
+               $this->clean($newName);
+               
+               $status = $this->alterDatabaseRename($dbName, $newName);
+               if ($status != 0) return -3;
+
+               if (trim($comment) != '' ) {    
+                       $status = $this->setComment('DATABASE',$dbName,'', $comment);
+                       if ($status != 0) return -4;
+               }
+               else return 0;
+       }
+
+       /**
+        * Return the database comment of a db from the shared description table
+        * @param string $database the name of the database to get the comment for
+        * @return recordset of the db comment info
+        */
+       function getDatabaseComment($database) {
+               $this->clean($database);
+               $sql = "SELECT description FROM pg_catalog.pg_database JOIN pg_catalog.pg_shdescription ON (oid=objoid) WHERE pg_database.datname = '{$database}' ";
+               return $this->selectSet($sql);
+       }
+
+       // Tablespace functions
+       
+       /**
+        * Retrieves information for all tablespaces
+        * @param $all Include all tablespaces (necessary when moving objects back to the default space)
+        * @return A recordset
+        */
+       function getTablespaces($all = false) {
+               global $conf;
+               
+               $sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, spclocation,
+                    (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pg_tablespace.oid=pd.objoid) AS spccomment
+                                       FROM pg_catalog.pg_tablespace";
+                                       
+               if (!$conf['show_system'] && !$all) {
+                       $sql .= " WHERE spcname NOT LIKE 'pg\\\\_%'";
+               }
+               
+               $sql .= " ORDER BY spcname";
+                                       
+               return $this->selectSet($sql);
+       }
+
+       /**
+        * Retrieves a tablespace's information
+        * @return A recordset
+        */
+       function getTablespace($spcname) {
+               $this->clean($spcname);
+               
+               $sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, spclocation,
+                    (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pg_tablespace.oid=pd.objoid) AS spccomment
+                                       FROM pg_catalog.pg_tablespace WHERE spcname='{$spcname}'";
+                                       
+               return $this->selectSet($sql);
+       }
+       
+       // Capabilities
+       function hasSharedComments() {return true;}
 }
 
 ?>
index a386d44de3d0e886fe72f2662c0cd233bf63080b..34aa162030e22efee5f8cccd2b4703d316fbb266 100755 (executable)
@@ -3,7 +3,7 @@
        /**
         * Manage tablespaces in a database cluster
         *
-        * $Id: tablespaces.php,v 1.9 2005/10/18 03:45:16 chriskl Exp $
+        * $Id: tablespaces.php,v 1.10 2006/11/19 21:33:13 xzilla Exp $
         */
 
        // Include application functions
@@ -33,6 +33,9 @@
                        
                        if (!isset($_POST['name'])) $_POST['name'] = $tablespace->f['spcname'];
                        if (!isset($_POST['owner'])) $_POST['owner'] = $tablespace->f['spcowner'];
+                       if (!isset($_POST['comment'])) {
+                               $_POST['comment'] = ($data->hasSharedComments()) ? $tablespace->f['spccomment'] : '';
+                       }
                        
                        echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
                        echo $misc->form;
                                        ($uname == $_POST['owner']) ? ' selected="selected"' : '', ">", htmlspecialchars($uname), "</option>\n";
                                $users->moveNext();
                        }
-                       echo "</select></td></tr>\n";                           
+                       echo "</select></td></tr>\n"; 
+                       if ($data->hasSharedComments()){
+                               echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
+                               echo "<td class=\"data1\">";
+                               echo "<textarea rows=\"3\" cols=\"32\" name=\"comment\" wrap=\"virtual\">",
+                                       htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
+                       }
                        echo "</table>\n";
                        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
                        echo "<input type=\"hidden\" name=\"tablespace\" value=\"", htmlspecialchars($_REQUEST['tablespace']), "\" />\n";
@@ -70,7 +79,7 @@
                if (trim($_POST['name']) == '')
                        doAlter($lang['strtablespaceneedsname']);
                else {
-                       $status = $data->alterTablespace($_POST['tablespace'], $_POST['name'], $_POST['owner']);
+                       $status = $data->alterTablespace($_POST['tablespace'], $_POST['name'], $_POST['owner'], $_POST['comment']);
                        if ($status == 0) {
                                // If tablespace has been renamed, need to change to the new name
                                if ($_POST['tablespace'] != $_POST['name']) {
                if (!isset($_POST['formSpcname'])) $_POST['formSpcname'] = '';
                if (!isset($_POST['formOwner'])) $_POST['formOwner'] = $server_info['username'];
                if (!isset($_POST['formLoc'])) $_POST['formLoc'] = '';
+               if (!isset($_POST['formComment'])) $_POST['formComment'] = '';
 
                // Fetch all users
                $users = $data->getUsers();
                echo "\t\t</select></td>\n\t</tr>\n";                           
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strlocation']}</th>\n";
                echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formLoc\" value=\"", htmlspecialchars($_POST['formLoc']), "\" /></td>\n\t</tr>\n";
+               // Comments (if available)
+               if ($data->hasSharedComments()) {
+                       echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
+                       echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\" wrap=\"virtual\">", 
+                               htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
+               }
                echo "</table>\n";
                echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
                echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
                        doCreate($lang['strtablespaceneedsname']);
                elseif (trim($_POST['formLoc']) == '')
                        doCreate($lang['strtablespaceneedsloc']);
-               else {          
-                       $status = $data->createTablespace($_POST['formSpcname'], $_POST['formOwner'], $_POST['formLoc']);
+               else {
+                       // Default comment to blank if it isn't set
+                       if (!isset($_POST['formComment'])) $_POST['formComment'] = null;
+               
+                       $status = $data->createTablespace($_POST['formSpcname'], $_POST['formOwner'], $_POST['formLoc'], $_POST['formComment']);
                        if ($status == 0)
                                doDefault($lang['strtablespacecreated']);
                        else
                                'title' => $lang['stractions']
                        )
                );
+
+               if ($data->hasSharedComments()) {
+                       $columns['comment'] = array(
+                               'title' => $lang['strcomment'],
+                               'field' => 'spccomment',
+                       );
+               }
+
+
                
                $actions = array(
                        'alter' => array(