Add alter schema Owner feature
authorGuillaume (ioguix) de Rorthais <ioguix@free.fr>
Tue, 28 Oct 2008 19:32:41 +0000 (15:32 -0400)
committerGuillaume (ioguix) de Rorthais <ioguix@free.fr>
Tue, 28 Oct 2008 19:32:41 +0000 (15:32 -0400)
classes/database/Postgres.php
classes/database/Postgres74.php
classes/database/Postgres80.php
schemas.php

index 58e481df5900fc6e857f291c722133db1a791be9..be3bfd885ed5e81d55cb1d3fa3431958dfc5674e 100755 (executable)
@@ -872,7 +872,7 @@ class Postgres extends ADODB_base {
                                LEFT JOIN pg_catalog.pg_user pu ON (pn.nspowner = pu.usesysid)
                        {$where}
                        ORDER BY nspname";
-               
+
                return $this->selectSet($sql);
        }
 
@@ -884,9 +884,10 @@ class Postgres extends ADODB_base {
        function getSchemaByName($schema) {
                $this->clean($schema);
                $sql = "
-                       SELECT nspname, nspowner, nspacl,
+                       SELECT nspname, nspowner, r.rolname AS ownername, nspacl,
                                pg_catalog.obj_description(pn.oid, 'pg_namespace') as nspcomment
             FROM pg_catalog.pg_namespace pn
+               LEFT JOIN pg_authid as r ON pn.nspowner = r.oid
                        WHERE nspname='{$schema}'";
                return $this->selectSet($sql);
        }
@@ -982,11 +983,13 @@ class Postgres extends ADODB_base {
         * Updates a schema.
         * @param $schemaname The name of the schema to drop
         * @param $comment The new comment for this schema
+        * @param $owner The new owner for this schema
         * @return 0 success
         */
-       function updateSchema($schemaname, $comment, $name) {
+       function updateSchema($schemaname, $comment, $name, $owner) {
                $this->fieldClean($schemaname);
                $this->fieldClean($name);
+               $this->fieldClean($owner);
                $this->clean($comment);
 
                $status = $this->beginTransaction();
@@ -1011,6 +1014,17 @@ class Postgres extends ADODB_base {
                        }
                }
 
+               $schema_rs = $this->getSchemaByName($schemaname);
+               /* Only if the owner change */
+               if ($schema_rs->fields['ownername'] != $owner) {
+                       $sql = "ALTER SCHEMA \"{$schemaname}\" OWNER TO \"{$owner}\"";
+                       $status = $this->execute($sql);
+                       if ($status != 0) {
+                               $this->rollbackTransaction();
+                               return -1;
+                       }
+               }
+
                return $this->endTransaction();
        }
 
@@ -1791,8 +1805,8 @@ class Postgres extends ADODB_base {
                        // superuser only function.
                        $sql = "ALTER TABLE \"{$this->_schema}\".\"{$tblrs->fields['relname']}\" OWNER TO \"{$owner}\"";
 
-               return $this->execute($sql);
-       }
+                       return $this->execute($sql);
+               }
                return 0;
        }
 
@@ -1971,7 +1985,7 @@ class Postgres extends ADODB_base {
                        $this->fieldClean($table);
 
                $sql = "DELETE FROM \"{$this->_schema}\".\"{$table}\"";
-               
+
                return $this->execute($sql);
        }
 
@@ -2009,7 +2023,7 @@ class Postgres extends ADODB_base {
                $this->clean($comment);
 
                $schema = $this->schema();
-                       
+
                if ($length == '')
                        $sql = "ALTER TABLE {$schema}\"{$table}\" ADD COLUMN \"{$column}\" {$type}";
                else {
@@ -2218,7 +2232,7 @@ class Postgres extends ADODB_base {
        function setColumnNull($table, $column, $state) {
                $this->fieldClean($table);
                $this->fieldClean($column);
-               
+
                $sql = "ALTER TABLE \"{$this->_schema}\".\"{$table}\" ALTER COLUMN \"{$column}\" " . (($state) ? 'DROP' : 'SET') . " NOT NULL";
 
                return $this->execute($sql);
@@ -7384,6 +7398,7 @@ class Postgres extends ADODB_base {
        function hasAlterColumnType() { return true; }
        function hasAlterDatabaseOwner() { return true; }
        function hasAlterDatabaseRename() { return true; }
+       function hasAlterSchemaOwner() { return true; }
        function hasAlterSequenceOwner() { return true; }
        function hasAlterSequenceProps() { return true; }
        function hasAlterTableOwner() { return true; }
index 3fe7739f35d46ef0118191d3ecdf57bf99f3c47a..bb65c084b97fbc52a0064cb9da36bddf4fb07ba7 100644 (file)
@@ -334,6 +334,7 @@ class Postgres74 extends Postgres80 {
 
        function hasAlterColumnType() { return false; }
        function hasAlterDatabaseOwner() { return false; }
+       function hasAlterSchemaOwner() { return false; }
        function hasFunctionAlterOwner() { return false; }
        function hasNamedParams() { return false; }
        function hasSignals() { return false; }
index ebc0cac7ffff88956dd1d12cb69b1c0489fc7fee..fef9c469364ba663f256f9a2089ff29a0dc5840c 100644 (file)
@@ -101,6 +101,24 @@ class Postgres80 extends Postgres81 {
                return $this->selectSet($sql);
        }
 
+       // Schema functions
+
+       /**
+        * Return all information relating to a schema
+        * @param $schema The name of the schema
+        * @return Schema information
+        */
+       function getSchemaByName($schema) {
+               $this->clean($schema);
+               $sql = "
+                       SELECT nspname, nspowner, u.usename AS ownername, nspacl,
+                               pg_catalog.obj_description(pn.oid, 'pg_namespace') as nspcomment
+            FROM pg_catalog.pg_namespace pn
+               LEFT JOIN pg_shadow as u ON pn.nspowner = u.usesysid
+                       WHERE nspname='{$schema}'";
+               return $this->selectSet($sql);
+       }
+
        // Table functions
 
        /**
@@ -122,7 +140,7 @@ class Postgres80 extends Postgres81 {
        function _alterTable($tblrs, $name, $owner, $schema, $comment, $tablespace) {
 
                /* $schema not supported in pg80- */
-               
+
                // Comment
                $this->clean($comment);
                $status = $this->setComment('TABLE', '', $tblrs->fields['relname'], $comment);
index 2cda5625661943415098ddef4e2948e7416a8f38..ad22fd5dfac1f1e0763adf91594c9d23a7db4c55 100755 (executable)
                        if (!isset($_POST['comment'])) $_POST['comment'] = $schema->fields['nspcomment'];
                        if (!isset($_POST['schema'])) $_POST['schema'] = $_REQUEST['schema'];
                        if (!isset($_POST['name'])) $_POST['name'] = $_REQUEST['schema'];
+                       if (!isset($_POST['owner'])) $_POST['owner'] = $schema->fields['ownername'];
 
                        echo "<form action=\"schemas.php\" method=\"post\">\n";
                        echo "<table>\n";
                                htmlspecialchars($_POST['name']), "\" />\n";
                        echo "\t\t</td>\n";
                        echo "\t</tr>\n";
+
+                       if ($data->hasAlterSchemaOwner()) {
+                               $users = $data->getUsers();
+                               echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
+                                       echo "<td class=\"data2\"><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 "\t<tr>\n";
                        echo "\t\t<th class=\"data\">{$lang['strcomment']}</th>\n";
                        echo "\t\t<td class=\"data1\"><textarea cols=\"32\" rows=\"3\"name=\"comment\">", htmlspecialchars($_POST['comment']), "</textarea></td>\n";
        function doSaveAlter($msg = '') {
                global $data, $misc, $lang, $_reload_browser;
 
-               $status = $data->updateSchema($_POST['schema'], $_POST['comment'], $_POST['name']);
+               $status = $data->updateSchema($_POST['schema'], $_POST['comment'], $_POST['name'], $_POST['owner']);
                if ($status == 0) {
                        $_reload_browser = true;
                        doDefault($lang['strschemaaltered']);
                        doDefault($lang['strspecifyschematodrop']);
                        exit();
                }
-               
+
                if ($confirm) {
                        $misc->printTrail('schema');
                        $misc->printTitle($lang['strdrop'],'pg.schema.drop');
                echo "<tr><td><label for=\"sd_clean\">{$lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"sd_clean\" name=\"sd_clean\" /></td>\n</tr>\n";
                echo "<tr><td><label for=\"sd_oids\">{$lang['stroids']}</label></td><td><input type=\"checkbox\" id=\"sd_oids\" name=\"sd_oids\" /></td>\n</tr>\n";
                echo "</table>\n";
-               
+
                echo "<h3>{$lang['stroptions']}</h3>\n";
                echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$lang['strshow']}</label>\n";
                echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$lang['strdownload']}</label>\n";