Refacto alterView + add support for schema alteration
authorioguix <ioguix>
Tue, 11 Dec 2007 14:17:17 +0000 (14:17 +0000)
committerioguix <ioguix>
Tue, 11 Dec 2007 14:17:17 +0000 (14:17 +0000)
classes/Misc.php
classes/database/Postgres.php
classes/database/Postgres71.php
classes/database/Postgres72.php
classes/database/Postgres73.php
classes/database/Postgres74.php
classes/database/Postgres81.php
classes/database/Postgres83.php
viewproperties.php

index 3b267cb8f374d3c5e199d1322755e4da6b348639..6bd9462c6694a6b8cd8e533fd60e9158e3b1112a 100644 (file)
@@ -2,7 +2,7 @@
        /**
         * Class to hold various commonly used functions
         *
-        * $Id: Misc.php,v 1.165 2007/11/29 23:23:56 ioguix Exp $
+        * $Id: Misc.php,v 1.166 2007/12/11 14:17:17 ioguix Exp $
         */
 
        class Misc {
                        );
                }
        
-       /*
-        * Output dropdown list to select server and 
-        * databases form the popups windows.
-        * @param $onchange Javascript action to take when selections change.
-        */     
-       function printConnection($onchange) {
-               global $data, $lang, $misc;
-
-               echo "<table style=\"width: 100%\"><tr><td>\n";
-               echo "<label>";
-               $misc->printHelp($lang['strserver'], 'pg.server');
-               echo "</label>";
-               echo ": <select name=\"server\" {$onchange}>\n";
-               
-               $servers = $misc->getServers();
-               foreach($servers as $info) {
-                       if (empty($info['username'])) continue; // not logged on this server
-                       echo "<option value=\"", htmlspecialchars($info['id']), "\"",
-                               ((isset($_REQUEST['server']) && $info['id'] == $_REQUEST['server'])) ? ' selected="selected"' : '', ">",
-                               htmlspecialchars("{$info['desc']} ({$info['id']})"), "</option>\n";
-               }
-               echo "</select>\n</td><td style=\"text-align: right\">\n";
-               
-               // Get the list of all databases
-               $databases = $data->getDatabases();
-
-               if ($databases->recordCount() > 0) {
-
+               /*
+                * Output dropdown list to select server and 
+                * databases form the popups windows.
+                * @param $onchange Javascript action to take when selections change.
+                */     
+               function printConnection($onchange) {
+                       global $data, $lang, $misc;
+
+                       echo "<table style=\"width: 100%\"><tr><td>\n";
                        echo "<label>";
-                       $misc->printHelp($lang['strdatabase'], 'pg.database');
-                       echo ": <select name=\"database\" {$onchange}>\n";
+                       $misc->printHelp($lang['strserver'], 'pg.server');
+                       echo "</label>";
+                       echo ": <select name=\"server\" {$onchange}>\n";
                        
-                       //if no database was selected, user should select one
-                       if (!isset($_REQUEST['database']))
-                               echo "<option value=\"\">--</option>\n";
+                       $servers = $misc->getServers();
+                       foreach($servers as $info) {
+                               if (empty($info['username'])) continue; // not logged on this server
+                               echo "<option value=\"", htmlspecialchars($info['id']), "\"",
+                                       ((isset($_REQUEST['server']) && $info['id'] == $_REQUEST['server'])) ? ' selected="selected"' : '', ">",
+                                       htmlspecialchars("{$info['desc']} ({$info['id']})"), "</option>\n";
+                       }
+                       echo "</select>\n</td><td style=\"text-align: right\">\n";
                        
-                       while (!$databases->EOF) {
-                               $dbname = $databases->fields['datname'];
-                               echo "<option value=\"", htmlspecialchars($dbname), "\"",
-                                       ((isset($_REQUEST['database']) && $dbname == $_REQUEST['database'])) ? ' selected="selected"' : '', ">",
-                                       htmlspecialchars($dbname), "</option>\n";
-                               $databases->moveNext();
+                       // Get the list of all databases
+                       $databases = $data->getDatabases();
+
+                       if ($databases->recordCount() > 0) {
+
+                               echo "<label>";
+                               $misc->printHelp($lang['strdatabase'], 'pg.database');
+                               echo ": <select name=\"database\" {$onchange}>\n";
+                               
+                               //if no database was selected, user should select one
+                               if (!isset($_REQUEST['database']))
+                                       echo "<option value=\"\">--</option>\n";
+                               
+                               while (!$databases->EOF) {
+                                       $dbname = $databases->fields['datname'];
+                                       echo "<option value=\"", htmlspecialchars($dbname), "\"",
+                                               ((isset($_REQUEST['database']) && $dbname == $_REQUEST['database'])) ? ' selected="selected"' : '', ">",
+                                               htmlspecialchars($dbname), "</option>\n";
+                                       $databases->moveNext();
+                               }
+                               echo "</select></label>\n";
                        }
-                       echo "</select></label>\n";
-               }
-               else {
-                       $server_info = $misc->getServerInfo();
-                       echo "<input type=\"hidden\" name=\"database\" value=\"", 
-                               htmlspecialchars($server_info['defaultdb']), "\" />\n";
+                       else {
+                               $server_info = $misc->getServerInfo();
+                               echo "<input type=\"hidden\" name=\"database\" value=\"", 
+                                       htmlspecialchars($server_info['defaultdb']), "\" />\n";
+                       }
+                       
+                       echo "</td></tr></table>\n";
                }
-               
-               echo "</td></tr></table>\n";
-       }
        }
 ?>
index 562ea679fa9211d08f65502eec9302f3df9e1c5a..f5fb4c07e45735ad8e4e507240e37efcd9a16d97 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.316 2007/12/05 12:05:00 ioguix Exp $
+ * $Id: Postgres.php,v 1.317 2007/12/11 14:17:17 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -646,7 +646,7 @@ class Postgres extends ADODB_base {
                $sql = "SELECT NULL AS nspname, c.relname,
                                        (SELECT usename FROM pg_user u WHERE u.usesysid=c.relowner) AS relowner,
                                        (SELECT description FROM pg_description pd WHERE c.oid=pd.objoid) AS relcomment,
-                                       reltuples::bigint AS reltuples 
+                                       reltuples::bigint AS reltuples
                                FROM pg_class c
                                WHERE c.relkind='r'
                                        AND NOT EXISTS (SELECT 1 FROM pg_rewrite r WHERE r.ev_class = c.oid AND r.ev_type = '1')
@@ -869,9 +869,9 @@ class Postgres extends ADODB_base {
                $this->fieldClean($name);
                $this->clean($comment);
                /* $schema, $owner, $tablespace not supported in pg70 */
-               
+
                $table = $tblrs->fields['relname'];
-               
+
                // Comment
                $status = $this->setComment('TABLE', '', $table, $comment);
                if ($status != 0) {
@@ -906,7 +906,7 @@ class Postgres extends ADODB_base {
         * @return $this->_alterTable error code
         */
        function alterTable($table, $name, $owner, $schema, $comment, $tablespace) {
-               
+
                $this->fieldClean($table);
                $data = $this->getTable($table);
                if ($data->recordCount() != 1)
@@ -927,7 +927,7 @@ class Postgres extends ADODB_base {
 
                return $this->endTransaction();
        }
-       
+
        /**
         * Removes a table from the database
         * @param $table The table to drop
@@ -2539,7 +2539,7 @@ class Postgres extends ADODB_base {
        function getView($view) {
                $this->clean($view);
 
-               $sql = "SELECT viewname AS relname, viewowner AS relowner, definition AS vwdefinition,
+               $sql = "SELECT viewname AS relname, NULL AS nspname, viewowner AS relowner, definition AS vwdefinition,
                          (SELECT description FROM pg_description pd, pg_class pc
                            WHERE pc.oid=pd.objoid AND pc.relname=v.viewname) AS relcomment
                        FROM pg_views v
@@ -2630,69 +2630,85 @@ class Postgres extends ADODB_base {
                $status = $this->endTransaction();
                return ($status == 0) ? 0 : -1;
        }
+       /**
+        * Rename a view
+        * @param $view The current view's name
+        * @param $name The new view's name
+        * @return -1 Failed
+        * @return 0 success
+        */
+       function renameView($view, $name) {
+               $this->fieldClean($name);
+               $this->fieldClean($view);
+               $sql = "ALTER TABLE \"{$view}\" RENAME TO \"{$name}\"";
+               if ($this->execute($sql) != 0)
+                       return -1;
+               return 0;
+       }
 
         /**
-         * Alters a view
-         * @param $view The name of the view
+         * Protected method which alter a view
+         * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION
+         * @param $vwrs The view recordSet returned by getView()
          * @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
+         * @return -5 owner error
+         * @return -6 schema error
          */
-    function alterView($view, $name, $owner, $comment) {
-               $this->fieldClean($view);
+    function _alterView($vwrs, $name, $owner, $schema, $comment) {
+
                $this->fieldClean($name);
-               $this->fieldClean($owner);
                $this->clean($comment);
 
-               $status = $this->beginTransaction();
-               if ($status != 0) {
-                       $this->rollbackTransaction();
-                       return -1;
-               }
+               $view = $vwrs->fields['relname'];
 
                // Comment
-               $status = $this->setComment('VIEW', $view, '', $comment);
-               if ($status != 0) {
-                       $this->rollbackTransaction();
+               if ($this->setComment('VIEW', $view, '', $comment) != 0)
                        return -4;
+
+               // Rename (only if name has changed)
+               if ($name != $view) {
+                       if ($this->renameView($view, $name) != 0)
+                       return -3;
                }
 
-               // Owner
-               if ($this->hasAlterTableOwner() && $owner != '') {
-                       // Fetch existing owner
-                       $data = $this->getView($view);
-                       if ($data->recordCount() != 1) {
-                               $this->rollbackTransaction();
-                               return -5;
-                       }
+               return 0;
+       }
 
-                       // 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;
-                               }
-                       }
+       /**
+        * Alter table properties
+        * @param $table The name of the view
+        * @param $name The new name for the view
+        * @param $owner The new owner for the view
+        * @param $schema The new schema for the view
+        * @param $comment The comment on the view
+        * @return 0 success
+        * @return -1 transaction error
+        * @return -2 get existing view error
+        * @return $this->_alterView error code
+        */
+       function alterView($view, $name, $owner, $schema, $comment) {
+
+               $this->fieldClean($view);
+               $data = $this->getView($view);
+               if ($data->recordCount() != 1)
+                       return -2;
+
+               $status = $this->beginTransaction();
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return -1;
                }
 
-               // 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;
-                       }
+               $status = $this->_alterView($data, $name, $owner, $schema, $comment);
+
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return $status;
                }
 
                return $this->endTransaction();
index 4fc3db7655540847f6528d65ac4249a6a6403836..247409b336817d218bfffae470df753fe87a2cd9 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres71.php,v 1.79 2007/11/21 15:45:31 ioguix Exp $
+ * $Id: Postgres71.php,v 1.80 2007/12/11 14:17:17 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -228,6 +228,45 @@ class Postgres71 extends Postgres {
                return $typname;
        }
 
+       // View functions
+       
+       /**
+         * Protected method which alter a view
+         * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION
+         * @param $vwrs The view recordSet returned by getView()
+         * @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 -3 rename error
+         * @return -4 comment error
+         * @return -5 owner error
+         * @return -6 schema error
+         */
+    function _alterView($vwrs, $name, $owner, $schema, $comment) {
+
+               $status = parent::_alterView($vwrs, $name, $owner, $schema, $comment);
+               if ($status != 0)
+                       return $status;
+
+               $this->fieldClean($name);
+               // if name is not empty, view has been renamed in parent
+               $view = (!empty($name)) ? $name : $tblrs->fields['relname'];
+
+               $this->fieldClean($owner);
+               // Owner
+               if ((!empty($owner)) && ($vwrs->fields['relowner'] != $owner)) {
+                       // If owner has been changed, then do the alteration.  We are
+                       // careful to avoid this generally as changing owner is a
+                       // superuser only function.
+                       $sql = "ALTER TABLE \"{$view}\" OWNER TO \"{$owner}\"";
+                       $status = $this->execute($sql);
+                       if ($status != 0) return -5;
+               }
+
+               return 0;
+       }
+       
        // Sequence functions
 
        /**
index eb33a06d555ebf50ac9fa237494f8876c3476325..16d64b8c19620401da7c21f544fef0fbd514253d 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres72.php,v 1.92 2007/11/15 06:06:45 xzilla Exp $
+ * $Id: Postgres72.php,v 1.93 2007/12/11 14:17:17 ioguix Exp $
  */
 
 
@@ -41,7 +41,7 @@ class Postgres72 extends Postgres71 {
        }
 
        // Help functions
-       
+
        function getHelpPages() {
                include_once('./help/PostgresDoc72.php');
                return $this->help_page;
@@ -57,7 +57,7 @@ class Postgres72 extends Postgres71 {
        function _encryptPassword($username, $password) {
                return 'md5' . md5($password . $username);
        }
-       
+
        /**
         * Changes a user's password
         * @param $username The username
@@ -68,9 +68,9 @@ class Postgres72 extends Postgres71 {
                $enc = $this->_encryptPassword($username, $password);
                $this->fieldClean($username);
                $this->clean($enc);
-               
+
                $sql = "ALTER USER \"{$username}\" WITH ENCRYPTED PASSWORD '{$enc}'";
-               
+
                return $this->execute($sql);
        }
 
@@ -89,7 +89,7 @@ class Postgres72 extends Postgres71 {
                $this->fieldClean($username);
                $this->clean($enc);
                $this->clean($expiry);
-               $this->fieldArrayClean($groups);                
+               $this->fieldArrayClean($groups);
 
                $sql = "CREATE USER \"{$username}\"";
                if ($password != '') $sql .= " WITH ENCRYPTED PASSWORD '{$enc}'";
@@ -98,7 +98,7 @@ class Postgres72 extends Postgres71 {
                if (is_array($groups) && sizeof($groups) > 0) $sql .= " IN GROUP \"" . join('", "', $groups) . "\"";
                if ($expiry != '') $sql .= " VALID UNTIL '{$expiry}'";
                else $sql .= " VALID UNTIL 'infinity'";
-               
+
                return $this->execute($sql);
        }
 
@@ -116,16 +116,16 @@ class Postgres72 extends Postgres71 {
                $this->fieldClean($username);
                $this->clean($enc);
                $this->clean($expiry);
-               
+
                $sql = "ALTER USER \"{$username}\"";
                if ($password != '') $sql .= " WITH ENCRYPTED PASSWORD '{$enc}'";
                $sql .= ($createdb) ? ' CREATEDB' : ' NOCREATEDB';
                $sql .= ($createuser) ? ' CREATEUSER' : ' NOCREATEUSER';
                if ($expiry != '') $sql .= " VALID UNTIL '{$expiry}'";
                else $sql .= " VALID UNTIL 'infinity'";
-               
+
                return $this->execute($sql);
-       }       
+       }
 
        /**
         * Returns all available process information.
@@ -139,10 +139,10 @@ class Postgres72 extends Postgres71 {
                        $this->clean($database);
                        $sql = "SELECT * FROM pg_stat_activity WHERE datname='{$database}' ORDER BY usename, procpid";
                }
-               
+
                return $this->selectSet($sql);
        }
-       
+
        // Table functions
 
        /**
@@ -181,29 +181,29 @@ class Postgres72 extends Postgres71 {
         */
        function getTable($table) {
                $this->clean($table);
-                               
-               $sql = "SELECT pc.relname, 
-                       pg_get_userbyid(pc.relowner) AS relowner, 
-                       (SELECT description FROM pg_description pd 
-                        WHERE pc.oid=pd.objoid AND objsubid = 0) AS relcomment 
+
+               $sql = "SELECT pc.relname,
+                       pg_get_userbyid(pc.relowner) AS relowner,
+                       (SELECT description FROM pg_description pd
+                        WHERE pc.oid=pd.objoid AND objsubid = 0) AS relcomment
                        FROM pg_class pc
                        WHERE pc.relname='{$table}'";
-                                                       
+
                return $this->selectSet($sql);
        }
 
        /**
         * Return all tables in current database
         * @param $all True to fetch all tables, false for just in current schema
-        * @return All tables, sorted alphabetically 
+        * @return All tables, sorted alphabetically
         */
        function getTables($all = false) {
                global $conf;
                if (!$conf['show_system'] || $all) $where = "AND c.relname NOT LIKE 'pg\\\\_%' ";
                else $where = '';
-               
-               $sql = "SELECT NULL AS nspname, c.relname, 
-                                       (SELECT usename FROM pg_user u WHERE u.usesysid=c.relowner) AS relowner, 
+
+               $sql = "SELECT NULL AS nspname, c.relname,
+                                       (SELECT usename FROM pg_user u WHERE u.usesysid=c.relowner) AS relowner,
                                        (SELECT description FROM pg_description pd WHERE c.oid=pd.objoid AND objsubid = 0) AS relcomment,
                                        reltuples::numeric
                         FROM pg_class c WHERE c.relkind='r' {$where}ORDER BY relname";
@@ -220,21 +220,21 @@ class Postgres72 extends Postgres71 {
                $this->clean($table);
                $this->clean($field);
 
-               if ($field == '') {             
+               if ($field == '') {
                        $sql = "
                                SELECT
                                        a.attname,
                                        format_type(a.atttypid, a.atttypmod) as type, a.atttypmod,
                                        a.attnotnull, a.atthasdef, adef.adsrc,
-                                       -1 AS attstattarget, a.attstorage, t.typstorage, false AS attisserial, 
+                                       -1 AS attstattarget, a.attstorage, t.typstorage, false AS attisserial,
                                         description as comment
-                               FROM 
+                               FROM
                                        pg_attribute a LEFT JOIN pg_attrdef adef
                                        ON a.attrelid=adef.adrelid AND a.attnum=adef.adnum
                                        LEFT JOIN pg_type t ON a.atttypid=t.oid
                                         LEFT JOIN pg_description d ON (a.attrelid = d.objoid AND a.attnum = d.objsubid)
-                               WHERE 
-                                       a.attrelid = (SELECT oid FROM pg_class WHERE relname='{$table}') 
+                               WHERE
+                                       a.attrelid = (SELECT oid FROM pg_class WHERE relname='{$table}')
                                        AND a.attnum > 0
                                ORDER BY a.attnum";
                }
@@ -242,27 +242,27 @@ class Postgres72 extends Postgres71 {
                        $sql = "
                                SELECT
                                        a.attname,
-                                       format_type(a.atttypid, a.atttypmod) as type, 
-                                       format_type(a.atttypid, NULL) as base_type, 
+                                       format_type(a.atttypid, a.atttypmod) as type,
+                                       format_type(a.atttypid, NULL) as base_type,
                                        a.atttypmod,
                                        a.attnotnull, a.atthasdef, adef.adsrc,
-                                       -1 AS attstattarget, a.attstorage, t.typstorage, 
+                                       -1 AS attstattarget, a.attstorage, t.typstorage,
                                         description as comment
-                               FROM 
+                               FROM
                                        pg_attribute a LEFT JOIN pg_attrdef adef
                                        ON a.attrelid=adef.adrelid AND a.attnum=adef.adnum
                                        LEFT JOIN pg_type t ON a.atttypid=t.oid
                                         LEFT JOIN pg_description d ON (a.attrelid = d.objoid AND a.attnum = d.objsubid)
-                               WHERE 
-                                       a.attrelid = (SELECT oid FROM pg_class WHERE relname='{$table}') 
+                               WHERE
+                                       a.attrelid = (SELECT oid FROM pg_class WHERE relname='{$table}')
                                        AND a.attname = '{$field}'";
                }
-                                       
+
                return $this->selectSet($sql);
        }
 
        // View functions
-       
+
        /**
         * Returns a list of all views in the database
         * @return All views
@@ -276,7 +276,7 @@ class Postgres72 extends Postgres71 {
                        $where = '';
 
                $sql = "SELECT viewname AS relname, viewowner AS relowner, definition AS vwdefinition,
-                             (SELECT description FROM pg_description pd, pg_class pc 
+                             (SELECT description FROM pg_description pd, pg_class pc
                               WHERE pc.oid=pd.objoid AND pc.relname=v.viewname AND pd.objsubid = 0) AS relcomment
                        FROM pg_views v
                        {$where}
@@ -284,7 +284,7 @@ class Postgres72 extends Postgres71 {
 
                return $this->selectSet($sql);
        }
-       
+
        /**
         * Returns all details for a particular view
         * @param $view The name of the view to retrieve
@@ -292,16 +292,16 @@ class Postgres72 extends Postgres71 {
         */
        function getView($view) {
                $this->clean($view);
-               
-               $sql = "SELECT viewname AS relname, viewowner AS relowner, definition AS vwdefinition,
-                         (SELECT description FROM pg_description pd, pg_class pc 
+
+               $sql = "SELECT viewname AS relname, NULL AS nspname, viewowner AS relowner, definition AS vwdefinition,
+                         (SELECT description FROM pg_description pd, pg_class pc
                            WHERE pc.oid=pd.objoid AND pc.relname=v.viewname AND pd.objsubid = 0) AS relcomment
                        FROM pg_views v
                        WHERE viewname='{$view}'";
-                       
+
                return $this->selectSet($sql);
        }
-       
+
        // Constraint functions
 
        /**
@@ -319,9 +319,9 @@ class Postgres72 extends Postgres71 {
 
                switch ($type) {
                        case 'c':
-                               // CHECK constraint             
+                               // CHECK constraint
                                $sql = "ALTER TABLE \"{$relation}\" DROP CONSTRAINT \"{$constraint}\" RESTRICT";
-               
+
                                return $this->execute($sql);
                                break;
                        case 'p':
@@ -332,7 +332,7 @@ class Postgres72 extends Postgres71 {
                        case 'f':
                                // FOREIGN KEY constraint
                                return -99;
-               }                               
+               }
        }
 
        /**
@@ -383,7 +383,7 @@ class Postgres72 extends Postgres71 {
 
                if ($tablespace != '' && $this->hasTablespaces())
                        $sql .= " USING INDEX TABLESPACE \"{$tablespace}\"";
-               
+
                return $this->execute($sql);
        }
 
@@ -422,7 +422,7 @@ class Postgres72 extends Postgres71 {
 
                return $this->selectSet($sql);
        }
-               
+
        /**
         * Updates (replaces) a function.
         * @param $function_oid The OID of the function
@@ -434,7 +434,7 @@ class Postgres72 extends Postgres71 {
         * @param $language The language the function is written for
         * @param $flags An array of optional flags
         * @param $setof True if returns a set, false otherwise
-        * @param $comment The comment on the function   
+        * @param $comment The comment on the function
         * @return 0 success
         * @return -1 transaction error
         * @return -2 drop function error
@@ -478,7 +478,7 @@ class Postgres72 extends Postgres71 {
                        $this->rollbackTransaction();
                        return -4;
                }
-               
+
                return $this->endTransaction();
        }
 
@@ -493,7 +493,7 @@ class Postgres72 extends Postgres71 {
         */
        function getTypes($all = false, $tabletypes = false, $domains = false) {
                global $conf;
-               
+
                if ($all || $conf['show_system']) {
                        $where = '';
                } else {
@@ -501,12 +501,12 @@ class Postgres72 extends Postgres71 {
                }
                // Never show system table types
                $where2 = "AND c.oid > '{$this->_lastSystemOID}'::oid";
-               
+
                // Create type filter
                $tqry = "'c'";
                if ($tabletypes)
                        $tqry .= ", 'r', 'v'";
-               
+
                $sql = "SELECT
                                pt.typname AS basename,
                                format_type(pt.oid, NULL) AS typname,
@@ -534,7 +534,7 @@ class Postgres72 extends Postgres71 {
         */
        function getOpClasses() {
                global $conf;
-               
+
                if ($conf['show_system'])
                        $where = '';
                else
@@ -542,8 +542,8 @@ class Postgres72 extends Postgres71 {
 
                $sql = "
                        SELECT DISTINCT
-                               pa.amname, 
-                               po.opcname, 
+                               pa.amname,
+                               po.opcname,
                                format_type(po.opcintype, NULL) AS opcintype,
                                TRUE AS opcdefault,
                                NULL::text AS opccomment
@@ -557,7 +557,7 @@ class Postgres72 extends Postgres71 {
 
                return $this->selectSet($sql);
        }
-       
+
        // Administration functions
 
        /**
index d8d962f1b8d939ab816d96f8692d84b1faba2848..31bc7661667c2f3d67da43cfaf7048f412ecee2e 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres73.php,v 1.179 2007/11/30 06:04:43 xzilla Exp $
+ * $Id: Postgres73.php,v 1.180 2007/12/11 14:17:17 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -634,7 +634,7 @@ class Postgres73 extends Postgres72 {
        function getView($view) {
                $this->clean($view);
 
-               $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
+               $sql = "SELECT c.relname, n.nspname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
                           pg_catalog.pg_get_viewdef(c.oid) AS vwdefinition, pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment
                         FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)
                         WHERE (c.relname = '$view')
index 67ecdcc1dc0914b48b46dd73aa1cd2f4ea6c5b0d..af0adcd82016b336cb4442fdd564d34a220354d0 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres74.php,v 1.68 2007/11/21 15:45:31 ioguix Exp $
+ * $Id: Postgres74.php,v 1.69 2007/12/11 14:17:17 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres73.php');
@@ -321,7 +321,7 @@ class Postgres74 extends Postgres73 {
        function getView($view) {
                $this->clean($view);
 
-               $sql = "SELECT c.relname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
+               $sql = "SELECT c.relname, n.nspname, pg_catalog.pg_get_userbyid(c.relowner) AS relowner,
                           pg_catalog.pg_get_viewdef(c.oid, true) AS vwdefinition, pg_catalog.obj_description(c.oid, 'pg_class') AS relcomment
                         FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)
                         WHERE (c.relname = '$view')
index e5b7ae12e9d8b2ff3facf4437a6f7e28911c61f5..fa133a79d8206d304f699109ddaa8c7862cf0af0 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * PostgreSQL 8.1 support
  *
- * $Id: Postgres81.php,v 1.17 2007/11/21 15:45:31 ioguix Exp $
+ * $Id: Postgres81.php,v 1.18 2007/12/11 14:17:17 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres80.php');
@@ -540,6 +540,46 @@ class Postgres81 extends Postgres80 {
                return $this->execute($sql);
        }
 
+       // View functions
+       
+       /**
+         * Protected method which alter a view
+         * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION
+         * @param $vwrs The view recordSet returned by getView()
+         * @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 -3 rename error
+         * @return -4 comment error
+         * @return -5 owner error
+         * @return -6 schema error
+         */
+    function _alterView($vwrs, $name, $owner, $schema, $comment) {
+
+               $status = parent::_alterView($vwrs, $name, $owner, $schema, $comment);
+               if ($status != 0)
+                       return $status;
+
+               $this->fieldClean($name);
+               // if name is not empty, view has been renamed in parent
+               $view = (!empty($name)) ? $name : $tblrs->fields['relname'];
+
+               // Schema
+               $this->fieldClean($schema);
+               if (!empty($schema) && ($vwrs->fields['nspname'] != $schema)) {
+
+                       // If tablespace has been changed, then do the alteration.  We
+                       // don't want to do this unnecessarily.
+                       $sql = "ALTER TABLE \"{$view}\" SET SCHEMA \"{$schema}\"";
+
+                       $status = $this->execute($sql);
+                       if ($status != 0) return -6;
+               }
+
+               return 0;
+       }
+       
        // Sequence methods
        
        /**
index 3fb93e5cf2e91eba32a893045e61c3b74f0055d8..2352cda7868d18decc0c3a622ad30e9453e1f3a2 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * PostgreSQL 8.3 support
  *
- * $Id: Postgres83.php,v 1.14 2007/11/30 07:56:06 xzilla Exp $
+ * $Id: Postgres83.php,v 1.15 2007/12/11 14:17:17 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres82.php');
@@ -65,73 +65,21 @@ class Postgres83 extends Postgres82 {
 
     // 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();
-       }
+       /**
+        * Rename a view
+        * @param $view The current view's name
+        * @param $name The new view's name
+        * @return -1 Failed
+        * @return 0 success
+        */
+       function renameView($view, $name) {
+               $this->fieldClean($name);
+               $this->fieldClean($view);
+               $sql = "ALTER VIEW \"{$view}\" RENAME TO \"{$name}\"";
+               if ($this->execute($sql) != 0)
+                       return -1;
+               return 0;
+       }
 
        // Indexe functions
 
@@ -811,7 +759,7 @@ class Postgres83 extends Postgres82 {
                $this->clean($table);
 
                $sql = "SELECT
-                               t.tgname, pg_catalog.pg_get_triggerdef(t.oid) AS tgdef, 
+                               t.tgname, pg_catalog.pg_get_triggerdef(t.oid) AS tgdef,
                                CASE WHEN t.tgenabled = 'D' THEN FALSE ELSE TRUE END AS tgenabled, p.oid AS prooid,
                                p.proname || ' (' || pg_catalog.oidvectortypes(p.proargtypes) || ')' AS proproto,
                                ns.nspname AS pronamespace
index 5f6863a381bacb1b15506bf6564629c37cee8084..d0c8f14875b7c8ae7bb68a4aaa75f1827329fd38 100755 (executable)
@@ -3,7 +3,7 @@
        /**
         * List views in a database
         *
-        * $Id: viewproperties.php,v 1.33 2007/09/13 14:53:41 ioguix Exp $
+        * $Id: viewproperties.php,v 1.34 2007/12/11 14:17:17 ioguix Exp $
         */
 
        // Include application functions
                        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['newschema'])) $_POST['newschema'] = $view->fields['nspname'];
                                if (!isset($_POST['comment'])) $_POST['comment'] = $view->fields['relcomment'];
 
                                echo "<form action=\"viewproperties.php\" method=\"post\">\n";
                                        echo "</select></td></tr>\n";
                                }
 
+                               if ($data->hasAlterTableSchema()) {
+                                       $schemas = $data->getSchemas();
+                                       echo "<tr><th class=\"data left required\">{$lang['strschema']}</th>\n";
+                                       echo "<td class=\"data1\"><select name=\"newschema\">";
+                                       while (!$schemas->EOF) {
+                                               $schema = $schemas->fields['nspname'];
+                                               echo "<option value=\"", htmlspecialchars($schema), "\"",
+                                                       ($schema == $_POST['newschema']) ? ' selected="selected"' : '', ">", htmlspecialchars($schema), "</option>\n";
+                                               $schemas->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\">",
                        else echo "<p>{$lang['strnodata']}</p>\n";
                }
                else{
-                       global $data, $lang, $_reload_browser;
+                       global $data, $lang, $_reload_browser, $misc;
 
                        // 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 (!isset($_POST['newschema'])) $_POST['newschema'] = null;
+
+                       $status = $data->alterView($_POST['view'], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']);
                        if ($status == 0) {
                                // If view has been renamed, need to change to the new name and
                                // reload the browser frame.
                        // Force a browser reload
                                        $_reload_browser = true;
                                }
+                               // If schema has changed, need to change to the new schema and reload the browser
+                               if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
+                                       // Jump them to the new sequence schema
+                                       $misc->setCurrentSchema($_POST['newschema']);
+                                       $_reload_browser = true;
+                               }
                                doDefault($lang['strviewaltered']);
                        }
                        else doAlter(true, $lang['strviewalteredbad']);