allow altering columns in views. renaming and setting a default is possible. yes...
authorchriskl <chriskl>
Wed, 12 May 2004 15:29:59 +0000 (15:29 +0000)
committerchriskl <chriskl>
Wed, 12 May 2004 15:29:59 +0000 (15:29 +0000)
HISTORY
classes/database/Postgres.php
tblproperties.php
viewproperties.php

diff --git a/HISTORY b/HISTORY
index 8c1c64ea04245d243c89f8a1ac2a223dd676dd3f..4fdebbffba8976dd634e592bd74c92b2389e0871 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -30,6 +30,7 @@ Features
 * Allow renaming functions when backend supports it
 * Views are now more like tables.  They are listed in the browser,
   you can view the virtual columns of the view and see column defaults.
+  Columns in view can also be renamed and have defaults set.
 
 Bugs
 * Fix pg_dump output for PostgreSQL 7.0.x and 7.1.x
index ce41dcf113080b415a79ac85a924b2f6272fb7df..93f91471e15c7545242870082624517e43f45f50 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.200 2004/05/09 09:10:04 chriskl Exp $
+ * $Id: Postgres.php,v 1.201 2004/05/12 15:30:00 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -705,7 +705,8 @@ class Postgres extends BaseDB {
 
        /**
         * Given an array of attnums and a relation, returns an array mapping
-        * atttribute number to attribute name.
+        * atttribute number to attribute name.  Relation could be a table OR
+        * a view.
         * @param $table The table to get attributes for
         * @param $atts An array of attribute numbers
         * @return An array mapping attnum to attname
@@ -1087,11 +1088,12 @@ class Postgres extends BaseDB {
        }
        
        /**
-        * Alters a column in a table
+        * Alters a column in a table OR view
         * @param $table The table in which the column resides
         * @param $column The column to alter
         * @param $name The new name for the column
         * @param $notnull (boolean) True if not null, false otherwise
+        * @param $oldnotnull (boolean) True if column is already not null, false otherwise
         * @param $default The new default for the column
         * @param $olddefault The old default for the column
         * @param $comment Comment for the column
@@ -1101,17 +1103,19 @@ class Postgres extends BaseDB {
         * @return -3 rename column error
         * @return -4 comment error
         */
-       function alterColumn($table, $column, $name, $notnull, $default, $olddefault, $comment) {
+       function alterColumn($table, $column, $name, $notnull, $oldnotnull, $default, $olddefault, $comment) {
                $this->clean($comment);
                $this->beginTransaction();
 
                // @@ NEED TO HANDLE "NESTED" TRANSACTION HERE
-               $status = $this->setColumnNull($table, $column, !$notnull);
-               if ($status != 0) {
-                       $this->rollbackTransaction();
-                       return -1;
+               if ($notnull != $oldnotnull) {
+                       $status = $this->setColumnNull($table, $column, !$notnull);
+                       if ($status != 0) {
+                               $this->rollbackTransaction();
+                               return -1;
+                       }
                }
-
+               
                // Set default, if it has changed
                if ($default != $olddefault) {
                        if ($default == '')
@@ -1125,7 +1129,7 @@ class Postgres extends BaseDB {
                        }
                }
 
-               $status = $this->setComment('COLUMN', ($column != $name) ? $name: $column, $table, $comment);
+               $status = $this->setComment('COLUMN', $column, $table, $comment);
                if ($status != 0) {
                  $this->rollbackTransaction();
                  return -4;
index 2cd16429b14cda91f83dbed5e6837b220c83608a..728525584073b3804a679ec97ea5a0c67939942c 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * List tables in a database
         *
-        * $Id: tblproperties.php,v 1.43 2004/05/08 14:45:10 chriskl Exp $
+        * $Id: tblproperties.php,v 1.44 2004/05/12 15:30:00 chriskl Exp $
         */
 
        // Include application functions
 
                                // Output table header
                                echo "<table>\n<tr>";
-                               echo "<tr><th class=\"data required\">{$lang['strname']}</th><th class=\"data required\">{$lang['strtype']}</th><th class=\"data\">{$lang['strnotnull']}</th><th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>";
+                               echo "<tr><th class=\"data required\">{$lang['strname']}</th><th class=\"data required\">{$lang['strtype']}</th>";
+                               echo "<th class=\"data\">{$lang['strnotnull']}</th><th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>";
 
                                $column = &$data->getTableAttributes($_REQUEST['table'], $_REQUEST['column']);
                                $column->f['attnotnull'] = $data->phpBool($column->f['attnotnull']);
                                echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"column\" value=\"", htmlspecialchars($_REQUEST['column']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"olddefault\" value=\"", htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
+                               if ($column->f['attnotnull']) echo "<input type=\"hidden\" name=\"oldnotnull\" value=\"on\" />\n";
                                echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
                                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
                                echo "</form>\n";
                                }
                                
                                $status = $data->alterColumn($_REQUEST['table'], $_REQUEST['column'], $_REQUEST['field'], 
-                                                            isset($_REQUEST['notnull']), $_REQUEST['default'], $_REQUEST['olddefault'],$_REQUEST['comment']);
+                                                            isset($_REQUEST['notnull']), isset($_REQUEST['oldnotnull']), $_REQUEST['default'], 
+                                                            $_REQUEST['olddefault'],$_REQUEST['comment']);
                                if ($status == 0)
                                        doDefault($lang['strcolumnaltered']);
                                else {
index 40b7f3737d87af4fb04877b306a6dc84c9446e04..d62b97f5251b522adbc1d4ccb22f5642cb91eb12 100755 (executable)
@@ -3,7 +3,7 @@
        /**
         * List views in a database
         *
-        * $Id: viewproperties.php,v 1.1 2004/05/10 15:22:00 chriskl Exp $
+        * $Id: viewproperties.php,v 1.2 2004/05/12 15:30:00 chriskl Exp $
         */
 
        // Include application functions
                echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=edit&{$misc->href}&view=", 
                        urlencode($_REQUEST['view']), "\">{$lang['stralter']}</a></p>\n";
        }
+
+       /**
+        * Displays a screen where they can alter a column in a view
+        */
+       function doProperties($msg = '') {
+               global $data, $misc;
+               global $PHP_SELF, $lang;
+
+               if (!isset($_REQUEST['stage'])) $_REQUEST['stage'] = 1;
+
+               switch ($_REQUEST['stage']) {
+                       case 1:
+                               global $lang;
+
+                               echo "<h2>", $misc->printVal($_REQUEST['database']), ": {$lang['strviews']}: {$lang['straltercolumn']}: ",
+                                       $misc->printVal($_REQUEST['column']), "</h2>\n";
+                               $misc->printMsg($msg);
+
+                               echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
+
+                               // Output view header
+                               echo "<table>\n<tr>";
+                               echo "<tr><th class=\"data required\">{$lang['strname']}</th><th class=\"data required\">{$lang['strtype']}</th>";
+                               echo "<th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>";
+
+                               $column = &$data->getTableAttributes($_REQUEST['view'], $_REQUEST['column']);
+
+                               if (!isset($_REQUEST['default'])) {
+                                       $_REQUEST['field'] = $column->f['attname'];
+                                       $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->f['adsrc'];
+                                       $_REQUEST['comment'] = $column->f['comment'];
+                               }                               
+
+                               echo "<tr><td><input name=\"field\" size=\"32\" value=\"",
+                                       htmlspecialchars($_REQUEST['field']), "\" /></td>";
+                               echo "<td>", $misc->printVal($data->formatType($column->f['type'], $column->f['atttypmod'])), "</td>";
+                               echo "<td><input name=\"default\" size=\"20\" value=\"", 
+                                       htmlspecialchars($_REQUEST['default']), "\" /></td>";
+                               echo "<td><input name=\"comment\" size=\"60\" value=\"", 
+                                       htmlspecialchars($_REQUEST['comment']), "\" /></td>";
+                               
+                               echo "</table>\n";
+                               echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
+                               echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
+                               echo $misc->form;
+                               echo "<input type=\"hidden\" name=\"view\" value=\"", htmlspecialchars($_REQUEST['view']), "\" />\n";
+                               echo "<input type=\"hidden\" name=\"column\" value=\"", htmlspecialchars($_REQUEST['column']), "\" />\n";
+                               echo "<input type=\"hidden\" name=\"olddefault\" value=\"", htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
+                               echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
+                               echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
+                               echo "</form>\n";
+                                                               
+                               break;
+                       case 2:
+                               global $data, $lang;
+
+                               // Check inputs
+                               if (trim($_REQUEST['field']) == '') {
+                                       $_REQUEST['stage'] = 1;
+                                       doProperties($lang['strfieldneedsname']);
+                                       return;
+                               }
+                               
+                               $status = $data->alterColumn($_REQUEST['view'], $_REQUEST['column'], $_REQUEST['field'], 
+                                                            false, false, $_REQUEST['default'], $_REQUEST['olddefault'],$_REQUEST['comment']);
+                               if ($status == 0)
+                                       doDefault($lang['strcolumnaltered']);
+                               else {
+                                       $_REQUEST['stage'] = 1;
+                                       doProperties($lang['strcolumnalteredbad']);
+                                       return;
+                               }
+                               break;
+                       default:
+                               echo "<p>{$lang['strinvalidparam']}</p>\n";
+               }
+       }
        
        /**
         * Show view definition and virtual columns
                case 'definition':
                        doDefinition();
                        break;
+               case 'properties':
+                       if (isset($_POST['cancel'])) doDefault();
+                       else doProperties();
+                       break;
                case 'drop':
                        if (isset($_POST['drop'])) doDrop(false);
                        else doDefault();