Add support for altering nmae, owner and comment on View
authorioguix <ioguix>
Thu, 13 Sep 2007 14:53:41 +0000 (14:53 +0000)
committerioguix <ioguix>
Thu, 13 Sep 2007 14:53:41 +0000 (14:53 +0000)
HISTORY
TODO
classes/database/Postgres.php
classes/database/Postgres83.php
help/PostgresDoc72.php
lang/english.php
lang/french.php
lang/recoded/english.php
lang/recoded/french.php
viewproperties.php

diff --git a/HISTORY b/HISTORY
index 7056b9e86948048d99f38b11096b274f98eec9db..062422c4c62d478a6bdfe0e38e165ce479eb4fea 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -15,6 +15,7 @@ Features
 * Auto-expand a node in the tree browser if there are no other nodes (Tomasz Pala)
 * Add column about fields constraints type + links in table properties page (ioguix)
 * Support for built-in Full Text Search (Ivan Zolotukhin)  
+* Add alter name, owner & comment on views (ioguix)
 
 Bugs
 * Fix inability to assign a field type/domain of a different schema
diff --git a/TODO b/TODO
index 53ce0f8d9422c67c1e10c1a0afba127b469ce652..e0854ad62946083c4ce568e3102169735c79cdcc 100644 (file)
--- a/TODO
+++ b/TODO
@@ -89,7 +89,7 @@ Views
 
 * Support temporary views per 8.1?
 * Allow INSERT and import on views with the appropriate rules.
-* Allow altering of comments
+* - Allow altering of comments (ioguix, with rename & owner)
 
 Sequences
 ---------
index 96a94391b19457ba1e80f7162d27f8904daf445f..e02244d3c741112a192c004cad16df2d265df8f7 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.302 2007/09/13 05:16:41 xzilla Exp $
+ * $Id: Postgres.php,v 1.303 2007/09/13 14:53:41 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -2531,7 +2531,74 @@ class Postgres extends ADODB_base {
                
                $status = $this->endTransaction();
                return ($status == 0) ? 0 : -1;
-       }       
+       }
+
+        /**
+         * Alters a view
+         * @param $view The name of the view
+         * @param $name The new name for the view
+         * @param $owner The new owner for the view
+         * @param $comment The comment on the view
+         * @return 0 success
+         * @return -1 transaction error
+         * @return -2 owner error
+         * @return -3 rename error
+         * @return -4 comment error
+         * @return -5 get existing view error
+         */
+    function alterView($view, $name, $owner, $comment) {
+               $this->fieldClean($view);
+               $this->fieldClean($name);
+               $this->fieldClean($owner);
+               $this->clean($comment);
+
+               $status = $this->beginTransaction();
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return -1;
+               }
+
+               // Comment
+               $status = $this->setComment('VIEW', $view, '', $comment);
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return -4;
+               }
+
+               // Owner
+               if ($this->hasAlterTableOwner() && $owner != '') {
+                       // Fetch existing owner
+                       $data = $this->getView($view);
+                       if ($data->recordCount() != 1) {
+                               $this->rollbackTransaction();
+                               return -5;
+                       }
+
+                       // If owner has been changed, then do the alteration.  We are
+                       // careful to avoid this generally as changing owner is a
+                       // superuser only function.
+                       if ($data->fields['relowner'] != $owner) {
+                               $sql = "ALTER TABLE \"{$view}\" OWNER TO \"{$owner}\"";
+                               $status = $this->execute($sql);
+                               if ($status != 0) {
+                                       $this->rollbackTransaction();
+                                       return -2;
+                               }
+                       }
+               }
+
+               // Rename (only if name has changed)
+               if ($name != $view) {
+                       $sql = "ALTER TABLE \"{$view}\" RENAME TO \"{$name}\"";
+                       $status = $this->execute($sql);
+                       if ($status != 0) {
+                               $this->rollbackTransaction();
+                               return -3;      
+                       }                       
+               }
+
+               return $this->endTransaction();
+       }
 
        // Operator functions
 
index d04d5232ca0f28b819710098d30ea6831d19f4f9..7a0f1b709e5cc7ab6d721823cb3636d28dc5b8a4 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * PostgreSQL 8.3 support
  *
- * $Id: Postgres83.php,v 1.3 2007/09/13 05:16:42 xzilla Exp $
+ * $Id: Postgres83.php,v 1.4 2007/09/13 14:53:41 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres82.php');
@@ -30,6 +30,77 @@ class Postgres83 extends Postgres82 {
                return $this->help_page;
        }
 
+    // Views functions
+    
+       /**
+        * Alters a view
+        * @param $view The name of the view
+        * @param $name The new name for the view
+        * @param $owner The new owner for the view
+        * @param $comment The comment on the view
+        * @return 0 success
+        * @return -1 transaction error
+        * @return -2 owner error
+        * @return -3 rename error
+        * @return -4 comment error
+        * @return -5 get existing view error
+        */
+       function alterView($view, $name, $owner, $comment) {
+        $this->fieldClean($view);
+               $this->fieldClean($name);
+               $this->fieldClean($owner);
+               $this->clean($comment);
+               $status = $this->beginTransaction();
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return -1;
+               }
+               // Comment
+               $status = $this->setComment('VIEW', $view, '', $comment);
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return -4;
+               }
+               // Owner
+               if ($owner != '') {
+                       // Fetch existing owner
+                       $data = $this->getView($view);
+                       if ($data->recordCount() != 1) {
+                               $this->rollbackTransaction();
+                               return -5;
+                       }
+                       // If owner has been changed, then do the alteration.  We are
+                       // careful to avoid this generally as changing owner is a
+                       // superuser only function.
+                       if ($data->fields['relowner'] != $owner) {
+                               $sql = "ALTER TABLE \"{$view}\" OWNER TO \"{$owner}\"";
+                               $status = $this->execute($sql);
+                               if ($status != 0) {
+                                       $this->rollbackTransaction();
+                                       return -2;
+                               }
+                       }
+               }
+               // Rename (only if name has changed)
+               if ($name != $view) {
+                       $sql = "ALTER VIEW \"{$view}\" RENAME TO \"{$name}\"";
+                       $status = $this->execute($sql);
+                       if ($status != 0) {
+                               $this->rollbackTransaction();
+                               return -3;      
+                       }                       
+               }
+               return $this->endTransaction();
+       }
+
+    // FTS functions
        /**
         * Creates a new FTS configuration.
         * @param string $cfgname The name of the FTS configuration to create
index cff96db5bdd90b42a09e372e093277301a0dccc5..10b4df0b1eea631b97ac6eb8a1dc1a1e83973378 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * Help links for PostgreSQL 7.2 documentation
  *
- * $Id: PostgresDoc72.php,v 1.4 2005/02/16 10:27:44 jollytoad Exp $
+ * $Id: PostgresDoc72.php,v 1.5 2007/09/13 14:53:41 ioguix Exp $
  */
 
 include('./help/PostgresDoc71.php');
@@ -16,4 +16,5 @@ $this->help_page['pg.admin.analyze'] = 'sql-analyze.html';
 
 $this->help_page['pg.aggregate'][1] = 'tutorial-agg.html';
 
+$this->help_page['pg.view.alter'] = array('sql-createview.html','sql-altertable.html');
 ?>
index 23f31afbd4f6e6a02a9daa2a575e73de1e9f1c52..a9e8920ba5637a2a654c779608d76e4cf6a83131 100755 (executable)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.216 2007/09/13 05:16:42 xzilla Exp $
+        * $Id: english.php,v 1.217 2007/09/13 14:53:41 ioguix Exp $
         */
 
        // Language and character set
        $lang['strrenamedupfields'] = 'Rename duplicate fields';
        $lang['strdropdupfields'] = 'Drop duplicate fields';
        $lang['strerrordupfields'] = 'Error on duplicate fields';
+       $lang['strviewaltered'] = 'View altered.';
+       $lang['strviewalteredbad'] = 'View alteration failed.';
 
        // Sequences
        $lang['strsequence'] = 'Sequence';
index 2b1ba56ab48c585ea31ce62876001863a06edd8f..56984a63e4e76294ed0e1f54abab61206f13a793 100644 (file)
@@ -4,7 +4,7 @@
      * French Language file for phpPgAdmin. 
      * @maintainer Pascal PEYRE [pascal.peyre@cir.fr]
      *
-     * $Id: french.php,v 1.29 2007/09/10 10:47:58 ioguix Exp $
+     * $Id: french.php,v 1.30 2007/09/13 14:53:41 ioguix Exp $
      */
 
     // Language and character set
        $lang['strrenamedupfields']  =  'Renommer les champs dupliqués';
        $lang['strdropdupfields']  =  'Ignorer les champs dupliqués';
        $lang['strerrordupfields']  =  'Erreur en cas de champs dupliqués';
+       $lang['strviewaltered']  =  'Vue modifiée.';
+       $lang['strviewalteredbad']  =  'Échec lors de la modification de la vue.';
 
     // Sequences
     $lang['strsequence'] = 'Séquence';
index 27470a2f1d0d026cb306e0e21ed09bbc008c6739..6597553732ce36309a09aa513a8fba4ce5cd0d67 100644 (file)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.168 2007/09/13 05:16:42 xzilla Exp $
+        * $Id: english.php,v 1.169 2007/09/13 14:53:41 ioguix Exp $
         */
 
        // Language and character set
        $lang['strrenamedupfields'] = 'Rename duplicate fields';
        $lang['strdropdupfields'] = 'Drop duplicate fields';
        $lang['strerrordupfields'] = 'Error on duplicate fields';
+       $lang['strviewaltered'] = 'View altered.';
+       $lang['strviewalteredbad'] = 'View alteration failed.';
 
        // Sequences
        $lang['strsequence'] = 'Sequence';
index 179ce14b26da34af9c34e90815e7ef86df2ce832..66cf035a7e80d7b87c302a3b3c5ac8cdaba95e6e 100644 (file)
@@ -4,7 +4,7 @@
      * French Language file for phpPgAdmin. 
      * @maintainer Pascal PEYRE [pascal.peyre@cir.fr]
      *
-     * $Id: french.php,v 1.28 2007/09/10 10:47:58 ioguix Exp $
+     * $Id: french.php,v 1.29 2007/09/13 14:53:41 ioguix Exp $
      */
 
     // Language and character set
        $lang['strrenamedupfields']  =  'Renommer les champs dupliqu&#233;s';
        $lang['strdropdupfields']  =  'Ignorer les champs dupliqu&#233;s';
        $lang['strerrordupfields']  =  'Erreur en cas de champs dupliqu&#233;s';
+       $lang['strviewaltered']  =  'Vue modifi&#233;e.';
+       $lang['strviewalteredbad']  =  '&#201;chec lors de la modification de la vue.';
 
     // Sequences
     $lang['strsequence'] = 'S&#233;quence';
index 9b660bafcdd8941bbe93fa4243f9431e4400c5e1..5f6863a381bacb1b15506bf6564629c37cee8084 100755 (executable)
@@ -3,7 +3,7 @@
        /**
         * List views in a database
         *
-        * $Id: viewproperties.php,v 1.32 2007/09/13 13:41:01 ioguix Exp $
+        * $Id: viewproperties.php,v 1.33 2007/09/13 14:53:41 ioguix Exp $
         */
 
        // Include application functions
                }
        }
 
+       function doAlter($confirm = false, $msg = '') {
+               if ($confirm) {
+                       global $data, $misc, $lang;
+
+                       $misc->printTrail('view');
+                       $misc->printTitle($lang['stralter'], 'pg.view.alter');
+                       $misc->printMsg($msg);
+
+                       // Fetch view info
+                       $view = $data->getView($_REQUEST['view']);
+
+                       if ($view->recordCount() > 0) {
+                               if (!isset($_POST['name'])) $_POST['name'] = $view->fields['relname'];
+                   if (!isset($_POST['owner'])) $_POST['owner'] = $view->fields['relowner'];
+                               if (!isset($_POST['comment'])) $_POST['comment'] = $view->fields['relcomment'];
+
+                               echo "<form action=\"viewproperties.php\" method=\"post\">\n";
+                               echo "<table>\n";
+                   echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
+                               echo "<td class=\"data1\">";
+                               echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
+                                       htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
+
+                               $server_info = $misc->getServerInfo();
+                               if ($data->hasAlterTableOwner() && $data->isSuperUser($server_info['username'])) {
+
+                                       // Fetch all users
+                                       $users = $data->getUsers();
+
+                                       echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
+                                       echo "<td class=\"data1\"><select name=\"owner\">";
+                                       while (!$users->EOF) {
+                                               $uname = $users->fields['usename'];
+                                               echo "<option value=\"", htmlspecialchars($uname), "\"",
+                                               ($uname == $_POST['owner']) ? ' selected="selected"' : '', ">", htmlspecialchars($uname), "</option>\n";
+                                               $users->moveNext();
+                                       }
+                                       echo "</select></td></tr>\n";
+                               }
+
+                               echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
+                               echo "<td class=\"data1\">";
+                               echo "<textarea rows=\"3\" cols=\"32\" name=\"comment\">",
+                       htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
+                               echo "</table>\n";
+                               echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
+                               echo "<input type=\"hidden\" name=\"view\" value=\"", htmlspecialchars($_REQUEST['view']), "\" />\n";
+                               echo $misc->form;
+                               echo "<p><input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
+                   echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
+                               echo "</form>\n";
+                       }
+                       else echo "<p>{$lang['strnodata']}</p>\n";
+               }
+               else{
+                       global $data, $lang, $_reload_browser;
+
+                       // For databases that don't allow owner change
+               if (!isset($_POST['owner'])) $_POST['owner'] = '';
+                       $status = $data->alterView($_POST['view'], $_POST['name'], $_POST['owner'], $_POST['comment']);
+                       if ($status == 0) {
+                               // If view has been renamed, need to change to the new name and
+                               // reload the browser frame.
+                               if ($_POST['view'] != $_POST['name']) {
+                                       // Jump them to the new view name
+                                       $_REQUEST['view'] = $_POST['name'];
+                       // Force a browser reload
+                                       $_reload_browser = true;
+                               }
+                               doDefault($lang['strviewaltered']);
+                       }
+                       else doAlter(true, $lang['strviewalteredbad']);
+               }
+       }
+
        function doTree () {
                global $misc, $data;
 
                        urlencode($lang['strback']), "\">{$lang['strbrowse']}</a></li>\n";
                echo "\t<li><a href=\"views.php?action=confselectrows&amp;{$misc->href}&amp;view=", urlencode($_REQUEST['view']),"\">{$lang['strselect']}</a></li>\n";
                echo "\t<li><a href=\"views.php?action=confirm_drop&amp;{$misc->href}&amp;view=", urlencode($_REQUEST['view']),"\">{$lang['strdrop']}</a></li>\n";
+               echo "\t<li><a href=\"viewproperties.php?action=confirm_alter&amp;{$misc->href}&amp;view=", urlencode($_REQUEST['view']),"\">{$lang['stralter']}</a></li>\n";
                echo "</ul>\n";
        }
 
                        if (isset($_POST['cancel'])) doDefault();
                        else doProperties();
                        break;
+               case 'alter':
+                       if (isset($_POST['alter'])) doAlter(false);
+                       else doDefault();
+                       break;
+               case 'confirm_alter':
+                       doAlter(true);
+                       break;
                case 'drop':
                        if (isset($_POST['drop'])) doDrop(false);
                        else doDefault();