add listing, dropping and creation of schemas
authorchriskl <chriskl>
Sat, 4 Jan 2003 07:56:23 +0000 (07:56 +0000)
committerchriskl <chriskl>
Sat, 4 Jan 2003 07:56:23 +0000 (07:56 +0000)
classes/database/Postgres73.php
lang/english.php
public_html/database.php

index 3f525cbdeb698d3bc7f28e572fb4a710f8cea5d4..19523ecfd97bee3a8381b0ca751ff7606e8cad8d 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.10 2002/12/27 16:28:01 chriskl Exp $
+ * $Id: Postgres73.php,v 1.11 2003/01/04 07:56:23 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -74,9 +74,13 @@ class Postgres73 extends Postgres72 {
        function &getSchemas() {
                if (!$this->_showSystem) $and = "AND nspname NOT LIKE 'pg_%'";
                else $and = '';
-               $sql = "SELECT nspname, nspowner FROM pg_namespace WHERE nspname = 'public'
+               $sql = "SELECT pn.nspname, pu.usename AS nspowner FROM pg_namespace pn, pg_user pu
+                       WHERE pn.nspowner = pu.usesysid
+                       AND nspname = 'public'
                        UNION ALL
-                       SELECT nspname, nspowner FROM pg_namespace WHERE nspname != 'public' {$and}ORDER BY nspname";
+                       SELECT pn.nspname, pu.usename AS nspowner FROM pg_namespace pn, pg_user pu
+                       WHERE pn.nspowner = pu.usesysid
+                       AND nspname != 'public' {$and}ORDER BY nspname";
 
                return $this->selectSet($sql);
        }
@@ -100,8 +104,8 @@ class Postgres73 extends Postgres72 {
         * @return 0 success
         */
        function createSchema($schemaname, $authorization = '') {
-               $this->clean($schemaname);
-               $this->clean($authorization);
+               $this->fieldClean($schemaname);
+               $this->fieldClean($authorization);
 
                $sql = "CREATE SCHEMA \"{$schemaname}\"";
                if ($authorization != '') $sql .= " AUTHORIZATION \"{$authorization}\"";
@@ -115,7 +119,7 @@ class Postgres73 extends Postgres72 {
         * @return 0 success
         */
        function dropSchema($schemaname) {
-               $this->clean($schemaname);
+               $this->fieldClean($schemaname);
                
                $sql = "DROP SCHEMA \"{$schemaname}\"";
                
index b320bf722c3851788192103f00e8bd0601854efc..545b4d54d7439f2d5247628e990948e464ca15c8 100755 (executable)
@@ -4,7 +4,7 @@
         * Language template file for WebDB.  Use this to base language
         * files.
         *
-        * $Id: english.php,v 1.28 2003/01/04 07:08:03 chriskl Exp $
+        * $Id: english.php,v 1.29 2003/01/04 07:56:23 chriskl Exp $
         */
 
        $appLang = 'English';
        $strTypeNeedsLen = 'You must give a length for your type.';
 
        // Schemas
+       $strSchema = 'Schema';
+       $strSchemas = 'Schemas';
        $strCreateSchema = 'Create Schema';
+       $strNoSchemas = 'No schemas found.';
+       $strConfDropSchema = 'Are you sure you want to drop the schema "%s"?';
+       $strSchemaDropped = 'Schema dropped.';
+       $strSchemaDroppedBad = 'Schema drop failed.';
+       $strSchemaCreated = 'Schema created';
+       $strSchemaCreatedBad = 'Schema creation failed.';
+       $strShowAllSchemas = 'Show All Schemas';
+       $strSchemaNeedsName = 'You must give a name for your schema.';
+
 ?>
index 5b190772cfbe46b2aeb0951894acc546e35a6e21..dbf58d995e73963c52cebb6e03442fcd907aa9cd 100755 (executable)
 <?php
 
        /**
-        * List database controls
+        * Manage schemas within a database
         *
-        * $Id: database.php,v 1.6 2003/01/04 07:08:03 chriskl Exp $
+        * $Id: database.php,v 1.7 2003/01/04 07:56:23 chriskl Exp $
         */
 
        // Include application functions
        include_once('../conf/config.inc.php');
 
-       $misc->printHeader();
-?>
+       $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
+       if (!isset($msg)) $msg = '';
+       $PHP_SELF = $_SERVER['PHP_SELF'];
+
+       /**
+        * Show confirmation of drop and perform actual drop
+        */
+       function doDrop($confirm) {
+               global $PHP_SELF, $data, $localData;
+               global $strDrop, $strConfDropSchema, $strSchemaDropped, $strSchemaDroppedBad;
 
-<h2><?php echo $appName ?> :: <?php echo $_GET['database'] ?></h2>
+               if ($confirm) {
+                       echo "<h2>", htmlspecialchars($_REQUEST['database']), ": ",
+                               htmlspecialchars($_REQUEST['schema']), ": {$strDrop}</h2>\n";
+
+                       echo "<p>", sprintf($strConfDropSchema, htmlspecialchars($_REQUEST['schema'])), "</p>\n";
+
+                       echo "<form action=\"{$PHP_SELF}\" method=\"post\">\n";
+                       echo "<input type=\"hidden\" name=\"action\" value=\"drop\">\n";
+                       echo "<input type=\"hidden\" name=\"database\" value=\"", htmlspecialchars($_REQUEST['database']), "\">\n";
+                       echo "<input type=\"hidden\" name=\"schema\" value=\"", htmlspecialchars($_REQUEST['schema']), "\">\n";
+                       echo "<input type=\"submit\" name=\"choice\" value=\"Yes\"> <input type=\"submit\" name=\"choice\" value=\"No\">\n";
+                       echo "</form>\n";
+               }
+               else {
+                       $status = $localData->dropSchema($_POST['schema']);
+                       if ($status == 0)
+                               doDefault($strSchemaDropped);
+                       else
+                               doDefault($strSchemaDroppedBad);
+               }
+               
+       }
+       
+       /**
+        * Displays a screen where they can enter a new schema
+        */
+       function doCreate($msg = '') {
+               global $data, $misc;
+               global $PHP_SELF, $strName, $strOwner, $strCreateSchema, $strShowAllSchemas;
+
+               if (!isset($_POST['formName'])) $_POST['formName'] = '';
+               if (!isset($_POST['formAuth'])) $_POST['formAuth'] = $_SESSION['webdbUsername'];
+
+               // Fetch all users from the database
+               $users = &$data->getUsers();
+
+               echo "<h2>", htmlspecialchars($_REQUEST['database']), ": {$strCreateSchema}</h2>\n";
+               $misc->printMsg($msg);
+               
+               echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
+               echo "<table width=\"100%\">\n";
+               echo "<tr><th class=\"data\">{$strName}</th><th class=\"data\">{$strOwner}</th></tr>\n";
+               echo "<tr><td class=\"data1\"><input name=\"formName\" size=\"{$data->_maxNameLen}\" maxlength=\"{$data->_maxNameLen}\" value=\"",
+                       htmlspecialchars($_POST['formName']), "\"></td>\n";
+               echo "<td class=\"data1\"><select name=\"formAuth\">";
+               while (!$users->EOF) {
+                       $uname = htmlspecialchars($users->f[$data->uFields['uname']]);
+                       echo "<option value=\"{$uname}\"",
+                               ($uname == $_POST['formAuth']) ? ' selected' : '', ">{$uname}</option>\n";
+                       $users->moveNext();
+               }
+               echo "</select></td></tr>\n";
+               echo "</table>\n";
+               echo "<p>\n";
+               echo "<input type=\"hidden\" name=\"action\" value=\"save_create\">\n";
+               echo "<input type=\"hidden\" name=\"database\" value=\"", htmlspecialchars($_REQUEST['database']), "\">\n";
+               echo "<input type=\"submit\" value=\"Save\"> <input type=\"reset\">\n";
+               echo "</p>\n";
+               echo "</form>\n";
+               
+               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?database=", 
+                       urlencode($_REQUEST['database']), "\">{$strShowAllSchemas}</a></p>\n";
+       }
+       
+       /**
+        * Actually creates the new schema in the database
+        */
+       function doSaveCreate() {
+               global $localData, $strSchemaNeedsName, $strSchemaCreated, $strSchemaCreatedBad;
+
+               // Check that they've given a name
+               if ($_POST['formName'] == '') doCreate($strSchemaNeedsName);
+               else {
+                       $status = $localData->createSchema($_POST['formName'], $_POST['formAuth']);
+                       if ($status == 0)
+                               doDefault($strSchemaCreated);
+                       else
+                               doCreate($strSchemaCreatedBad);
+               }
+       }       
+
+       /**
+        * Show default list of schemas in the server
+        */
+       function doDefault($msg = '') {
+               global $data, $localData, $misc;
+               global $PHP_SELF, $strName, $strOwner, $strSchemas, $strDrop, $strActions, $strCreateSchema, $strNoSchemas;
+               
+               echo "<h2>", htmlspecialchars($_REQUEST['database']), ": {$strSchemas}</h2>\n";
+               $misc->printMsg($msg);
+               
+               // Check that the DB actually supports schemas
+               if ($data->hasSchemas()) {
+                       $schemas = &$localData->getSchemas();
+
+                       if ($schemas->recordCount() > 0) {
+                               echo "<table>\n";
+                               echo "<tr><th class=data>{$strName}</th><th class=data>{$strOwner}</th><th class=data>{$strActions}</th>\n";
+                               $i = 0;
+                               while (!$schemas->EOF) {
+                                       $id = (($i % 2) == 0 ? '1' : '2');
+                                       echo "<tr><td class=data{$id}>", htmlspecialchars($schemas->f[$data->nspFields['nspname']]), "</td>\n";
+                                       echo "<td class=data{$id}>", htmlspecialchars($schemas->f[$data->nspFields['nspowner']]), "</td>\n";
+                                       echo "<td class=opbutton{$id}><a href=\"$PHP_SELF?action=confirm_drop&database=",
+                                               htmlspecialchars($_REQUEST['database']), "&schema=",
+                                               htmlspecialchars($schemas->f[$data->nspFields['nspname']]), "\">{$strDrop}</a></td>\n";
+                                       echo "</tr>\n";
+                                       $schemas->moveNext();
+                                       $i++;
+                               }
+                               echo "</table>\n";
+                       }
+                       else {
+                               echo "<p>{$strNoSchemas}</p>\n";
+                       }
+
+                       echo "<p><a class=navlink href=\"$PHP_SELF?database=", urlencode($_REQUEST['database']),
+                               "&action=create\">{$strCreateSchema}</a></p>\n";
+               } else {
+                       // If the database does not support schemas...
+                       echo "<p>{$strNoSchemas}</p>\n";
+               }
+       }
+
+       $misc->printHeader($strSchemas);
+
+       switch ($action) {
+               case 'save_create':
+                       doSaveCreate();
+                       break;
+               case 'create':
+                       doCreate();
+                       break;
+               case 'drop':
+                       if ($_POST['choice'] == 'Yes') doDrop(false);
+                       else doDefault();
+                       break;
+               case 'confirm_drop':
+                       doDrop(true);
+                       break;
+               default:
+                       doDefault();
+                       break;
+       }
 
-<?php
        $misc->printFooter();
+
 ?>