diff options
author | Guillaume (ioguix) de Rorthais | 2008-10-28 19:32:41 +0000 |
---|---|---|
committer | Guillaume (ioguix) de Rorthais | 2008-10-28 19:32:41 +0000 |
commit | 681c244a09e1b4792de1b798e92a4702cb53a04a (patch) | |
tree | f30e5c93e8a5687bb601231406ca6823c329b2fd | |
parent | 6b03ca3443a89ebf42a815898c8feca61bf803bd (diff) |
Add alter schema Owner feature
-rwxr-xr-x | classes/database/Postgres.php | 31 | ||||
-rw-r--r-- | classes/database/Postgres74.php | 1 | ||||
-rw-r--r-- | classes/database/Postgres80.php | 20 | ||||
-rwxr-xr-x | schemas.php | 21 |
4 files changed, 61 insertions, 12 deletions
diff --git a/classes/database/Postgres.php b/classes/database/Postgres.php index 58e481df..be3bfd88 100755 --- a/classes/database/Postgres.php +++ b/classes/database/Postgres.php @@ -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; } diff --git a/classes/database/Postgres74.php b/classes/database/Postgres74.php index 3fe7739f..bb65c084 100644 --- a/classes/database/Postgres74.php +++ b/classes/database/Postgres74.php @@ -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; } diff --git a/classes/database/Postgres80.php b/classes/database/Postgres80.php index ebc0cac7..fef9c469 100644 --- a/classes/database/Postgres80.php +++ b/classes/database/Postgres80.php @@ -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); diff --git a/schemas.php b/schemas.php index 2cda5625..ad22fd5d 100755 --- a/schemas.php +++ b/schemas.php @@ -165,6 +165,7 @@ 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"; @@ -176,6 +177,20 @@ 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"; @@ -198,7 +213,7 @@ 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']); @@ -218,7 +233,7 @@ doDefault($lang['strspecifyschematodrop']); exit(); } - + if ($confirm) { $misc->printTrail('schema'); $misc->printTitle($lang['strdrop'],'pg.schema.drop'); @@ -320,7 +335,7 @@ 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"; |