From: xzilla Date: Sun, 19 Nov 2006 21:33:13 +0000 (+0000) Subject: Add comments for databases and tablespaces using 8.2 global system catalog X-Git-Tag: REL_4-1-BETA-1~6 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=9f306701789e6650b34a60b04f7f8654aa3ef28c;p=phppgadmin.git Add comments for databases and tablespaces using 8.2 global system catalog --- diff --git a/all_db.php b/all_db.php index fa68b2bf..611da916 100644 --- a/all_db.php +++ b/all_db.php @@ -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 @@ -50,6 +50,14 @@ } echo "\n"; } + if ($data->hasSharedComments()){ + $rs = $data->getDatabaseComment($_REQUEST['alterdatabase']); + $comment = isset($rs->fields['description']) ? $rs->fields['description'] : ''; + echo "{$lang['strcomment']}\n"; + echo ""; + echo "\n"; + } echo "\n"; echo "\n"; echo $misc->form; @@ -60,9 +68,8 @@ echo "\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']); } @@ -122,6 +129,7 @@ $_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(); @@ -159,7 +167,14 @@ } echo "\t\t\t\n\t\t\n\t\n"; } - + + // Comments (if available) + if ($data->hasSharedComments()) { + echo "\t\n\t\t{$lang['strcomment']}\n"; + echo "\t\t\n\t\n"; + } + echo "\n"; echo "

\n"; echo $misc->form; @@ -177,10 +192,13 @@ // 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']); diff --git a/classes/database/Postgres.php b/classes/database/Postgres.php index f32f5ba4..d04cc340 100755 --- a/classes/database/Postgres.php +++ b/classes/database/Postgres.php @@ -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;} } ?> diff --git a/classes/database/Postgres80.php b/classes/database/Postgres80.php index 752842d5..04d9a15f 100644 --- a/classes/database/Postgres80.php +++ b/classes/database/Postgres80.php @@ -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(); } diff --git a/classes/database/Postgres82.php b/classes/database/Postgres82.php index bb62c0cd..54c2d553 100644 --- a/classes/database/Postgres82.php +++ b/classes/database/Postgres82.php @@ -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;} } ?> diff --git a/tablespaces.php b/tablespaces.php index a386d44d..34aa1620 100755 --- a/tablespaces.php +++ b/tablespaces.php @@ -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 "

\n"; echo $misc->form; @@ -49,7 +52,13 @@ ($uname == $_POST['owner']) ? ' selected="selected"' : '', ">", htmlspecialchars($uname), "\n"; $users->moveNext(); } - echo "\n"; + echo "\n"; + if ($data->hasSharedComments()){ + echo "{$lang['strcomment']}\n"; + echo ""; + echo "\n"; + } echo "\n"; echo "

\n"; echo "\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']) { @@ -126,6 +135,7 @@ 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(); @@ -150,6 +160,12 @@ echo "\t\t\n\t\n"; echo "\t\n\t\t{$lang['strlocation']}\n"; echo "\t\t\n\t\n"; + // Comments (if available) + if ($data->hasSharedComments()) { + echo "\t\n\t\t{$lang['strcomment']}\n"; + echo "\t\t\n\t\n"; + } echo "\n"; echo "

\n"; echo "\n"; @@ -169,8 +185,11 @@ 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 @@ -208,6 +227,15 @@ 'title' => $lang['stractions'] ) ); + + if ($data->hasSharedComments()) { + $columns['comment'] = array( + 'title' => $lang['strcomment'], + 'field' => 'spccomment', + ); + } + + $actions = array( 'alter' => array(