support altering sequence schema need some test for pg < 8.2
authorioguix <ioguix>
Wed, 21 Nov 2007 12:59:42 +0000 (12:59 +0000)
committerioguix <ioguix>
Wed, 21 Nov 2007 12:59:42 +0000 (12:59 +0000)
TODO
classes/Misc.php
classes/database/Postgres.php
classes/database/Postgres71.php
classes/database/Postgres73.php
classes/database/Postgres74.php
classes/database/Postgres81.php
sequences.php

diff --git a/TODO b/TODO
index 500bb4549afc4a1977f43625e3bd4fab62239398..bebca71c9580298b5626e8ab6d2720f9f9a6d684 100644 (file)
--- a/TODO
+++ b/TODO
@@ -43,7 +43,7 @@ Roles
 -----
 
 * -Support for 8.1 roles (removing users and groups) (chriskl,javier)
-* Support for ALTER ROLE
+* -Support for ALTER ROLE (done by ?)
 
 Permissions
 -----------
@@ -101,8 +101,8 @@ Sequences
 
 * -Alter sequence (Guillaume LELARGE)
 * -setval & nextval (Guillaume LELARGE)
-* change schema
-* rename (done using alter table)
+* -change schema (ioguix)
+* -rename (done using alter table) (ioguix)
 
 Functions
 ---------
@@ -110,9 +110,9 @@ Functions
 * -Support 8.1 IN, OUT and INOUT parameters.(Jawed)
 * Remove options for OUT/INOUT params in older servers
 * Clean up javascript html spec warnings
-* Display owner
-* Alter owner
-* Alter schema 
+* -Display owner (xzilla)
+* -Alter owner (xzilla)
+* -Alter schema (xzilla)
 * GUC settings [8.3]
 
 Indexes 
index 26897ac8a80d6387ddb1d566813efdfb348b9e2a..7f07e8da8036c30001a0c851882c301efc86240d 100644 (file)
@@ -2,7 +2,7 @@
        /**
         * Class to hold various commonly used functions
         *
-        * $Id: Misc.php,v 1.162 2007/10/17 18:24:32 ioguix Exp $
+        * $Id: Misc.php,v 1.163 2007/11/21 12:59:42 ioguix Exp $
         */
 
        class Misc {
                                        $_SESSION['webdbLogin'][$server_id][$key] = $value;
                        }
                }
+               
+               /**
+                * Set the current schema
+                * @param $schema The schema name
+                * @return 0 on success
+                * @return $data->seSchema() on error
+                */
+               function setCurrentSchema($schema) {
+                       global $data;
+                       
+                       $status = $data->setSchema($schema);
+                       if($status != 0)
+                               return $status;
+
+                       $_REQUEST['schema'] = $schema;
+                       $this->setHREF();
+                       return 0;
+               }
        }
 ?>
index 0c718adaf3deef21b2808275f11cb53a49adeed6..3cc13c98b75217318112961130bbc1f86424dff8 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.310 2007/11/15 23:09:21 xzilla Exp $
+ * $Id: Postgres.php,v 1.311 2007/11/21 12:59:42 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -2003,6 +2003,7 @@ class Postgres extends ADODB_base {
         * @param $name The new name for the sequence
         * @param $comment The comment on the sequence
         * @param $owner The new owner for the sequence
+        * @param $schema The new schema for the sequence
         * @param $increment The increment
         * @param $minvalue The min value
         * @param $maxvalue The max value
@@ -2012,15 +2013,17 @@ class Postgres extends ADODB_base {
         * @return 0 success
         * @return -3 rename error
         * @return -4 comment error
+        * @return -5 owner error
+        * @return -7 schema error
         */
        /*protected*/
-       function _alterSequence($seqrs, $name, $comment, $owner, $increment,
+       function _alterSequence($seqrs, $name, $comment, $owner, $schema, $increment,
                                $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue) {
 
                $sequence = $seqrs->fields['seqname'];
                $this->fieldClean($name);
                $this->clean($comment);
-               /* $owner, $increment, $minvalue, $maxvalue,
+               /* $owner, $schema, $increment, $minvalue, $maxvalue,
                 * $startvalue, $cachevalue, $cycledvalue not supported in pg70 */
 
                // Comment
@@ -2044,6 +2047,7 @@ class Postgres extends ADODB_base {
         * @param $name The new name for the sequence
         * @param $comment The comment on the sequence
         * @param $owner The new owner for the sequence
+        * @param $schema The new schema for the sequence
         * @param $increment The increment
         * @param $minvalue The min value
         * @param $maxvalue The max value
@@ -2055,7 +2059,7 @@ class Postgres extends ADODB_base {
         * @return -2 get existing sequence error
         * @return $this->_alterSequence error code
         */
-    function alterSequence($sequence, $name, $comment, $owner=null, $increment=null,
+    function alterSequence($sequence, $name, $comment, $owner=null, $schema=null, $increment=null,
                                $minvalue=null, $maxvalue=null, $startvalue=null, $cachevalue=null, $cycledvalue=null) {
 
                $this->fieldClean($sequence);
@@ -2069,7 +2073,7 @@ class Postgres extends ADODB_base {
                        return -1;
                }
 
-               $status = $this->_alterSequence($data, $name, $comment, $owner, $increment,
+               $status = $this->_alterSequence($data, $name, $comment, $owner, $schema, $increment,
                                $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue);
 
                if ($status != 0) {
@@ -4806,6 +4810,7 @@ class Postgres extends ADODB_base {
        function hasAlterTableOwner() { return false; }
        function hasAlterSequenceOwner() { return false; }
        function hasAlterSequenceProps() { return false; }
+       function hasSequenceAlterSchema() { return false; }
        function hasPartialIndexes() { return false; }
        function hasCasts() { return false; }
        function hasFullSubqueries() { return false; }
@@ -4846,8 +4851,8 @@ class Postgres extends ADODB_base {
        function hasVirtualTransactionId() {return false;}
        function hasFunctionCosting() {return false;}
        function hasFunctionGUC() {return false;}
-        function hasFunctionAlterSchema() { return false; }
-        function hasFunctionAlterOwner() { return false; }
+       function hasFunctionAlterSchema() { return false; }
+       function hasFunctionAlterOwner() { return false; }
 }
 
 ?>
index 25980c653895368d71b84408c3f4b548c8032398..24fba0480440cd86d1b13e1eef6a769dcde25b45 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.77 2007/10/17 15:55:33 ioguix Exp $
+ * $Id: Postgres71.php,v 1.78 2007/11/21 12:59:42 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -194,6 +194,7 @@ class Postgres71 extends Postgres {
         * @param $name The new name for the sequence
         * @param $comment The comment on the sequence
         * @param $owner The new owner for the sequence
+        * @param $schema The new schema for the sequence
         * @param $increment The increment
         * @param $minvalue The min value
         * @param $maxvalue The max value
@@ -206,15 +207,15 @@ class Postgres71 extends Postgres {
         * @return -5 owner error
         */
        /*protected*/
-       function _alterSequence($seqrs, $name, $comment, $owner, $increment,
+       function _alterSequence($seqrs, $name, $comment, $owner, $schema, $increment,
                                $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue) {
 
-               $status = parent::_alterSequence($seqrs, $name, $comment, $owner, $increment,
+               $status = parent::_alterSequence($seqrs, $name, $comment, $owner, $schema, $increment,
                                $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue);
                if ($status != 0)
                        return $status;
 
-               /* $increment, $minvalue, $maxvalue, $startvalue, $cachevalue,
+               /* $schema, $increment, $minvalue, $maxvalue, $startvalue, $cachevalue,
                 * $cycledvalue not supported in pg71 */
                // if name != seqname, sequence has been renamed in parent
                $sequence = ($seqrs->fields['seqname'] = $name) ? $seqrs->fields['seqname'] : $name;
index ed79e2d8f1e51e1a8524efd673c3d4ee370189bc..f60af2fe8f6391a7dc6131751c002d662328c59a 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.176 2007/11/16 18:34:24 ioguix Exp $
+ * $Id: Postgres73.php,v 1.177 2007/11/21 12:59:42 ioguix Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -746,10 +746,11 @@ class Postgres73 extends Postgres72 {
 
                $sql = "SELECT c.relname AS seqname, s.*,
                                        pg_catalog.obj_description(s.tableoid, 'pg_class') AS seqcomment,
-                                       u.usename AS seqowner
+                                       u.usename AS seqowner, n.nspname
                                FROM \"{$sequence}\" AS s, pg_catalog.pg_class c, pg_catalog.pg_user u, pg_catalog.pg_namespace n
                                WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
-                                       AND c.relname = '{$sequence}' AND c.relkind = 'S' AND n.nspname='{$this->_schema}'";
+                                       AND c.relname = '{$sequence}' AND c.relkind = 'S' AND n.nspname='{$this->_schema}'
+                                       AND n.oid = c.relnamespace";
 
                return $this->selectSet( $sql );
        }
index 14c609d997e70f5037b0a11fb675c90b53f95a4f..a1699bc4dbef3aeecb9d61e3645a4d67b43dd21c 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.66 2007/11/15 23:09:21 xzilla Exp $
+ * $Id: Postgres74.php,v 1.67 2007/11/21 12:59:42 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres73.php');
@@ -605,34 +605,32 @@ class Postgres74 extends Postgres73 {
                         $funcname = $newname; 
                }
 
-                // Alter the owner, if necessary
-                if ($this->hasFunctionAlterOwner()) {
-               $this->fieldClean($newown);
+               // Alter the owner, if necessary
+               if ($this->hasFunctionAlterOwner()) {
+                       $this->fieldClean($newown);
                    if ($funcown != $newown) {
-                       $sql = "ALTER FUNCTION \"{$funcname}\"({$args}) OWNER TO \"{$newown}\"";
-                       $status = $this->execute($sql);
-                       if ($status != 0) {
-                               $this->rollbackTransaction();
-                               return -6;
-                       }
+                               $sql = "ALTER FUNCTION \"{$funcname}\"({$args}) OWNER TO \"{$newown}\"";
+                               $status = $this->execute($sql);
+                               if ($status != 0) {
+                                       $this->rollbackTransaction();
+                                       return -6;
+                               }
                    }   
 
-                }
+               }
 
-                // Alter the schema, if necessary
-                if ($this->hasFunctionAlterSchema()) {
+               // Alter the schema, if necessary
+               if ($this->hasFunctionAlterSchema()) {
                    $this->fieldClean($newschema);
                    if ($funcschema != $newschema) {
-                       $sql = "ALTER FUNCTION \"{$funcname}\"({$args}) SET SCHEMA \"{$newschema}\"";
-                       $status = $this->execute($sql);
-                       if ($status != 0) {
-                               $this->rollbackTransaction();
-                               return -7;
-                       }
+                               $sql = "ALTER FUNCTION \"{$funcname}\"({$args}) SET SCHEMA \"{$newschema}\"";
+                               $status = $this->execute($sql);
+                               if ($status != 0) {
+                                       $this->rollbackTransaction();
+                                       return -7;
+                               }
                    }
-
-                }
-
+               }
 
                return $this->endTransaction();
        }
@@ -646,6 +644,7 @@ class Postgres74 extends Postgres73 {
         * @param $name The new name for the sequence
         * @param $comment The comment on the sequence
         * @param $owner The new owner for the sequence
+        * @param $schema The new schema for the sequence
         * @param $increment The increment
         * @param $minvalue The min value
         * @param $maxvalue The max value
@@ -659,14 +658,16 @@ class Postgres74 extends Postgres73 {
         * @return -6 get sequence error
         */
        /*protected*/
-       function _alterSequence($seqrs, $name, $comment, $owner, $increment,
+       function _alterSequence($seqrs, $name, $comment, $owner, $schema, $increment,
                                $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue) {
 
-               $status = parent::_alterSequence($seqrs, $name, $comment, $owner, $increment,
+               $status = parent::_alterSequence($seqrs, $name, $comment, $owner, $schema, $increment,
                                $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue);
                if ($status != 0)
                        return $status;
-
+               
+               /* $schema not supported in pg74 */
+               
                // if name != seqname, sequence has been renamed in parent
                $sequence = ($seqrs->fields['seqname'] = $name) ? $seqrs->fields['seqname'] : $name;
                $this->clean($increment);
index 6b78987f995fa9a5b30f6d2334d8400d64d997b6..52934952b94ab1222071ce3df3fe6e8db1911fc7 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * PostgreSQL 8.1 support
  *
- * $Id: Postgres81.php,v 1.15 2007/11/15 23:09:21 xzilla Exp $
+ * $Id: Postgres81.php,v 1.16 2007/11/21 12:59:42 ioguix Exp $
  */
 
 include_once('./classes/database/Postgres80.php');
@@ -453,6 +453,8 @@ class Postgres81 extends Postgres80 {
                return $this->selectSet($sql);
        }
 
+       // Database methods
+       
        /**
         * Returns all available process information.
         * @return A recordset
@@ -464,6 +466,8 @@ class Postgres81 extends Postgres80 {
                return $this->selectSet($sql);
        }
        
+       // Table methods
+       
        /**
         * Enables a trigger
         * @param $tgname The name of the trigger to enable
@@ -494,6 +498,51 @@ class Postgres81 extends Postgres80 {
                return $this->execute($sql);
        }
 
+       // Sequence methods
+       
+       /**
+        * Protected method which alter a sequence
+        * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION
+        * @param $seqrs The sequence recordSet returned by getSequence()
+        * @param $name The new name for the sequence
+        * @param $comment The comment on the sequence
+        * @param $owner The new owner for the sequence
+        * @param $schema The new schema for the sequence
+        * @param $increment The increment
+        * @param $minvalue The min value
+        * @param $maxvalue The max value
+        * @param $startvalue The starting value
+        * @param $cachevalue The cache value
+        * @param $cycledvalue True if cycled, false otherwise
+        * @return 0 success
+        * @return -3 rename error
+        * @return -4 comment error
+        * @return -5 owner error
+        * @return -6 get sequence error
+        * @return -7 schema error
+        */
+       /*protected*/
+       function _alterSequence($seqrs, $name, $comment, $owner, $schema, $increment,
+                               $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue) {
+
+               $status = parent::_alterSequence($seqrs, $name, $comment, $owner, $schema, $increment,
+                               $minvalue, $maxvalue, $startvalue, $cachevalue, $cycledvalue);
+               if ($status != 0)
+                       return $status;
+               
+               // if name != seqname, sequence has been renamed in parent
+               $sequence = ($seqrs->fields['seqname'] = $name) ? $seqrs->fields['seqname'] : $name;
+
+               $this->clean($schema);
+               if ($seqrs->fields['nspname'] != $schema) {
+                       $sql = "ALTER SEQUENCE \"{$sequence}\" SET SCHEMA $schema";
+                       $status = $this->execute($sql);
+                       if ($status != 0)
+                               return -7;
+               }
+
+               return 0;
+       }
        
        // Capabilities
        function hasServerAdminFuncs() { return true; }
@@ -501,7 +550,8 @@ class Postgres81 extends Postgres80 {
        function hasAutovacuum() { return true; }
        function hasPreparedXacts() { return true; }
        function hasDisableTriggers() { return true; }
-        function hasFunctionAlterSchema() { return true; }
+       function hasFunctionAlterSchema() { return true; }
+       function hasSequenceAlterSchema() { return true; }
 }
 
 ?>
index 65b06f5d4a11b316342525f0835b062aa3e953e9..3e83442c2ed0a320e5820dddf74c693a1a154e34 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * Manage sequences in a database
         *
-        * $Id: sequences.php,v 1.46 2007/10/17 15:55:33 ioguix Exp $
+        * $Id: sequences.php,v 1.47 2007/11/21 12:59:42 ioguix Exp $
         */
 
        // Include application functions
         * Function to save after altering a sequence
         */
        function doSaveAlter() {
-               global $data, $lang, $_reload_browser;
+               global $data, $lang, $_reload_browser, $misc;
 
 
                if (!isset($_POST['owner'])) $_POST['owner'] = null;
+               if (!isset($_POST['newschema'])) $_POST['newschema'] = null;
                if (!isset($_POST['formIncrement'])) $_POST['formIncrement'] = null;
                if (!isset($_POST['formMinValue'])) $_POST['formMinValue'] = null;
                if (!isset($_POST['formMaxValue'])) $_POST['formMaxValue'] = null;
                if (!isset($_POST['formCycledValue'])) $_POST['formCycledValue'] = null;
 
                $status = $data->alterSequence($_POST['sequence'], $_POST['name'], $_POST['comment'], $_POST['owner'],
-                       $_POST['formIncrement'], $_POST['formMinValue'], $_POST['formMaxValue'], $_POST['formStartValue'],
-                       $_POST['formCacheValue'], isset($_POST['formCycledValue']));
+                       $_POST['newschema'], $_POST['formIncrement'], $_POST['formMinValue'], $_POST['formMaxValue'],
+                       $_POST['formStartValue'], $_POST['formCacheValue'], isset($_POST['formCycledValue']));
 
                if ($status == 0) {
                        if ($_POST['sequence'] != $_POST['name']) {
                                // Force a browser reload
                                $_reload_browser = true;
                        }
+                       if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
+                               // Jump them to the new sequence schema
+                               $misc->setCurrentSchema($_POST['newschema']);
+                               $_reload_browser = true;
+                        }
                        doProperties($lang['strsequencealtered']);
                }
                else
                        if (!isset($_POST['name'])) $_POST['name'] = $_REQUEST['sequence'];
                        if (!isset($_POST['comment'])) $_POST['comment'] = $sequence->fields['seqcomment'];
                        if (!isset($_POST['owner'])) $_POST['owner'] = $sequence->fields['seqowner'];
+                       if (!isset($_POST['newschema'])) $_POST['newschema'] = $sequence->fields['nspname'];
 
                        // Handle Checkbox Value
                        $sequence->fields['is_cycled'] = $data->phpBool($sequence->fields['is_cycled']);
                                }
                                echo "</select></td></tr>\n";
                        }
+                       
+                       if ($data->hasSequenceAlterSchema()) {
+                               $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\">";