Allow renaming functions when backend supports it, and allow commenting on functions
authorchriskl <chriskl>
Sun, 9 May 2004 09:10:04 +0000 (09:10 +0000)
committerchriskl <chriskl>
Sun, 9 May 2004 09:10:04 +0000 (09:10 +0000)
BUGS
HISTORY
classes/database/BaseDB.php
classes/database/Postgres.php
classes/database/Postgres71.php
classes/database/Postgres72.php
classes/database/Postgres73.php
classes/database/Postgres74.php
functions.php

diff --git a/BUGS b/BUGS
index df3cbfa4c0a797e88551d3dbbe6ba296ab813bc0..61f60721f14e08480b6afd41705eadbac862747b 100644 (file)
--- a/BUGS
+++ b/BUGS
@@ -28,4 +28,5 @@ NEEDS TESTING
 * Comment stuff
 * Import
 * Script execution
-* Create view wizard looks buggy
\ No newline at end of file
+* Create view wizard looks buggy
+* alter function on < 7.2 needs testing
diff --git a/HISTORY b/HISTORY
index 3f3310e9800e682c404be80d9b16f704f66e1f0d..f3852da53f1c3f417ad7049b41db108e671fe1ed 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -27,6 +27,7 @@ Features
 * Automatically uncheck the NULL checkbox when data is typed in the value
   field while editing data.
 * Show query runtime when executing arbitrary SQL
+* Allow renaming functions when backend supports it
 
 Bugs
 * Fix pg_dump output for PostgreSQL 7.0.x and 7.1.x
index 5cb3b6f4197ad5e54cd75243a42e44b9439c35c8..25725584a5d8b02d01a6c9f08ac876aada11be1e 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: BaseDB.php,v 1.43 2004/04/12 06:30:55 chriskl Exp $
+ * $Id: BaseDB.php,v 1.44 2004/05/09 09:10:04 chriskl Exp $
  */
 
 include_once('./classes/database/ADODB_base.php');
@@ -273,6 +273,8 @@ class BaseDB extends ADODB_base {
        function hasFullExplain() { return false; }
        function hasStatsCollector() { return false; }
        function hasSchemaDump() { return false; }
+       function hasFunctionRename() { return false; }
+
 }
 
 ?>
index edee221e9cf07e206939888f03e9ffb606316354..ce41dcf113080b415a79ac85a924b2f6272fb7df 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.199 2004/05/09 06:56:48 chriskl Exp $
+ * $Id: Postgres.php,v 1.200 2004/05/09 09:10:04 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -1951,29 +1951,36 @@ class Postgres extends BaseDB {
 
        /**
         * Sets the comment for an object in the database
-        * @param $obj_type One of 'TABLE' | 'COLUMN' | 'VIEW' | 'SCHEMA' | 'SEQUENCE' | 'TYPE'
+        * @param $obj_type One of 'TABLE' | 'COLUMN' | 'VIEW' | 'SCHEMA' | 'SEQUENCE' | 'TYPE' | 'FUNCTION'
         * @param $obj_name The name of the object for which to attach a comment
         * @param $table Name of table that $obj_name belongs to.  Ignored unless $obj_type is 'TABLE' or 'COLUMN'.
         * @param $comment The comment to add
         * @return 0 success
         */
-
        function setComment($obj_type, $obj_name, $table, $comment) {
-               // Make sure we have a valid type
-               $types = array('TABLE','COLUMN','VIEW','SCHEMA','SEQUENCE','TYPE');
-               if (! in_array($obj_type,$types)) return -1;
-
+               $this->clean($comment);
+               
                $sql = "COMMENT ON {$obj_type} " ;
 
                switch ($obj_type) {
-               case 'TABLE':
-                       $sql .= "\"{$table}\" IS ";
-                       break;
-               case 'COLUMN':
-                       $sql .= "\"{$table}\".\"{$obj_name}\" IS ";
-                       break;
-               default:
-                       $sql .= "\"{$obj_name}\" IS ";
+                       case 'TABLE':
+                               $sql .= "\"{$table}\" IS ";
+                               break;
+                       case 'COLUMN':
+                               $sql .= "\"{$table}\".\"{$obj_name}\" IS ";
+                               break;
+                       case 'VIEW':
+                       case 'SCHEMA':
+                       case 'SEQUENCE':
+                       case 'TYPE':
+                               $sql .= "\"{$obj_name}\" IS ";
+                               break;
+                       case 'FUNCTION':                                
+                               $sql .= "{$obj_name} IS ";
+                               break;
+                       default:
+                               // Unknown object type
+                               return -1;
                }
 
                if ($comment != '')
@@ -3402,7 +3409,8 @@ class Postgres extends BaseDB {
                                        probin as binary,
                                        proretset,
                                        proiscachable,
-                                       oidvectortypes(pc.proargtypes) AS arguments
+                                       oidvectortypes(pc.proargtypes) AS arguments,
+                                       (SELECT description FROM pg_description pd WHERE pc.oid=pd.objoid) AS funccomment
                                FROM
                                        pg_proc pc, pg_language pl, pg_type pt
                                WHERE 
@@ -3437,32 +3445,45 @@ class Postgres extends BaseDB {
         * so we do it with a drop and a recreate.
         * @param $function_oid The OID of the function
         * @param $funcname The name of the function to create
+        * @param $newname The new name for the function
         * @param $args The array of argument types
         * @param $returns The return type
         * @param $definition The definition for the new function
         * @param $language The language the function is written for
         * @param $flags An array of optional flags
         * @param $setof True if returns a set, false otherwise
+        * @param $comment The comment on the function
         * @return 0 success
         * @return -1 transaction error
         * @return -2 drop function error
         * @return -3 create function error
+        * @return -4 comment error
         */
-       function setFunction($function_oid, $funcname, $args, $returns, $definition, $language, $flags, $setof) {
+       function setFunction($function_oid, $funcname, $newname, $args, $returns, $definition, $language, $flags, $setof, $comment) {
                $status = $this->beginTransaction();
                if ($status != 0) return -1;
-
+               
+               // Drop existing function
                $status = $this->dropFunction($function_oid, false);
                if ($status != 0) {
                        $this->rollbackTransaction();
                        return -2;
                }
                
-               $status = $this->createFunction($funcname, $args, $returns, $definition, $language, $flags, $setof, false);
+               // Create function with new name
+               $status = $this->createFunction($newname, $args, $returns, $definition, $language, $flags, $setof, false);
                if ($status != 0) {
                        $this->rollbackTransaction();
                        return -3;
                }
+               
+               // Comment on the function
+               $this->fieldClean($newname);
+               $status = $this->setComment('FUNCTION', "\"{$newname}\"({$args})", null, $comment);
+               if ($status != 0) {
+                 $this->rollbackTransaction();
+                 return -4;
+               }
 
                $status = $this->endTransaction();
                return ($status == 0) ? 0 : -1;
@@ -3722,6 +3743,7 @@ class Postgres extends BaseDB {
        function hasOpClasses() { return true; }
        function hasUserSessionDefaults() { return false; }
        function hasUserRename() { return false; }
+       function hasFunctionRename() { return true; }
 
 }
 
index affa433067b59f53be5ee028f69a5c3dbf043af1..5f2843d8723dd2a13b22d3a4ac68d923786ebf75 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.54 2004/05/08 11:34:14 chriskl Exp $
+ * $Id: Postgres71.php,v 1.55 2004/05/09 09:10:04 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -225,7 +225,8 @@ class Postgres71 extends Postgres {
                                        proretset,
                                        proisstrict,
                                        proiscachable,
-                                       oidvectortypes(pc.proargtypes) AS arguments
+                                       oidvectortypes(pc.proargtypes) AS arguments,
+                                       (SELECT description FROM pg_description pd WHERE pc.oid=pd.objoid) AS funccomment
                                FROM
                                        pg_proc pc, pg_language pl
                                WHERE 
index 34edf9a434cf9a375e0a56cd5113800b1ee26ec7..ee8ff0102cc46f4a0a453b00f7117c0790e4a884 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres72.php,v 1.62 2004/05/08 15:21:43 chriskl Exp $
+ * $Id: Postgres72.php,v 1.63 2004/05/09 09:10:04 chriskl Exp $
  */
 
 
@@ -282,16 +282,54 @@ class Postgres72 extends Postgres71 {
         * Updates (replaces) a function.
         * @param $function_oid The OID of the function
         * @param $funcname The name of the function to create
+        * @param $newname The new name for the function
         * @param $args The array of argument types
         * @param $returns The return type
         * @param $definition The definition for the new function
         * @param $language The language the function is written for
         * @param $flags An array of optional flags
         * @param $setof True if returns a set, false otherwise
+        * @param $comment The comment on the function   
         * @return 0 success
+        * @return -1 transaction error
+        * @return -2 commenting error
+        * @return -3 rename error
         */
-       function setFunction($function_oid, $funcname, $args, $returns, $definition, $language, $flags, $setof) {
-               return $this->createFunction($funcname, $args, $returns, $definition, $language, $flags, $setof, true);
+       function setFunction($function_oid, $funcname, $newname, $args, $returns, $definition, $language, $flags, $setof, $comment) {
+               // Begin a transaction
+               $status = $this->beginTransaction();
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return -1;
+               }
+
+               // Replace the existing function
+               $status = $this->createFunction($funcname, $args, $returns, $definition, $language, $flags, $setof, true);
+               if ($status != 0) {
+                       $this->rollbackTransaction();
+                       return -1;
+               }
+
+               // Comment on the function
+               $this->fieldClean($funcname);
+               $status = $this->setComment('FUNCTION', "\"{$funcname}\"({$args})", null, $comment);
+               if ($status != 0) {
+                 $this->rollbackTransaction();
+                 return -2;
+               }
+
+               // Rename the function, if necessary
+               if ($funcname != $newname && $this->hasFunctionRename()) {
+                       $this->fieldClean($newname);
+                       $sql = "ALTER FUNCTION \"{$funcname}\"({$args}) RENAME TO \"{$newname}\"";
+                       $status = $this->execute($sql);
+                       if ($status != 0) {
+                         $this->rollbackTransaction();
+                         return -3;
+                       }
+               }
+               
+               return $this->endTransaction();
        }
 
        // Type functions
index a2a232a7998c3f63d9ef3a9ce3e3d738b59593f0..24b00a9e049586b06501925d9e783ade8e7c3438 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.100 2004/05/09 04:31:42 chriskl Exp $
+ * $Id: Postgres73.php,v 1.101 2004/05/09 09:10:04 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -743,7 +743,8 @@ class Postgres73 extends Postgres72 {
                                        proisstrict,
                                        provolatile,
                                        prosecdef,
-                                       oidvectortypes(pc.proargtypes) AS arguments
+                                       oidvectortypes(pc.proargtypes) AS arguments,
+                                       pg_catalog.obj_description(pc.oid, 'pg_proc') AS funccomment
                                FROM
                                        pg_catalog.pg_proc pc, pg_catalog.pg_language pl
                                WHERE 
@@ -1641,6 +1642,7 @@ class Postgres73 extends Postgres72 {
        function hasUserSessionDefaults() { return true; }
        function hasVariables() { return true; }
        function hasFullExplain() { return true; }
+       function hasFunctionRename() { return false; }
 
 }
 
index d7462e745ed7fed45370d1285ac6beeb1c9956ee..9653120a6d00839dfc918e56d37f494d7d5081d2 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.27 2004/03/31 03:30:41 chriskl Exp $
+ * $Id: Postgres74.php,v 1.28 2004/05/09 09:10:04 chriskl Exp $
  */
 
 include_once('./classes/database/Postgres73.php');
@@ -411,6 +411,7 @@ class Postgres74 extends Postgres73 {
        function hasGrantOption() { return true; }
        function hasDomainConstraints() { return true; }
        function hasUserRename() { return true; }
+       function hasFunctionRename() { return true; }
        function hasSchemaDump() { return true; }
        
 }
index 28883c5b17fc375c559041f8d353fa4ede3529d8..72ab2bfc90c4fdf941993f54b939835270c39ac6 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * Manage functions in a database
         *
-        * $Id: functions.php,v 1.29 2004/05/08 14:45:09 chriskl Exp $
+        * $Id: functions.php,v 1.30 2004/05/09 09:10:04 chriskl Exp $
         */
 
        // Include application functions
         */
        function doSaveEdit() {
                global $data, $lang;
+               
+               // If the backend does not support renaming functions...
+               if (!$data->hasFunctionRename()) $_POST['formFunction'] = $_POST['original_function'];
 
-               $status = $data->setFunction($_POST['function_oid'], $_POST['original_function'], $_POST['original_arguments'], 
-                                                                                                               $_POST['original_returns'], $_POST['formDefinition'], 
-                                                                                                               $_POST['original_lang'], $_POST['formProperties'], isset($_POST['original_setof']), true);
+               $status = $data->setFunction($_POST['function_oid'], $_POST['original_function'], $_POST['formFunction'], 
+                                                                               $_POST['original_arguments'], 
+                                                                               $_POST['original_returns'], $_POST['formDefinition'], 
+                                                                               $_POST['original_lang'], $_POST['formProperties'], 
+                                                                               isset($_POST['original_setof']), $_POST['formComment']);
                if ($status == 0)
                        doProperties($lang['strfunctionupdated']);
                else
@@ -46,6 +51,8 @@
                        // Initialise variables
                        if (!isset($_POST['formDefinition'])) $_POST['formDefinition'] = $fndata->f[$data->fnFields['fndef']];
                        if (!isset($_POST['formProperties'])) $_POST['formProperties'] = $data->getFunctionProperties($fndata->f);
+                       if (!isset($_POST['formFunction'])) $_POST['formFunction'] = $fndata->f[$data->fnFields['fnname']];
+                       if (!isset($_POST['formComment'])) $_POST['formComment'] = $fndata->f['funccomment'];
 
                        $func_full = $fndata->f[$data->fnFields['fnname']] . "(". $fndata->f[$data->fnFields['fnarguments']] .")";
                        echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
                                
 
                        echo "<tr>\n";
-                       echo "<td class=\"data1\">", $misc->printVal($fndata->f[$data->fnFields['fnname']]), "\n";
+                       echo "<td class=\"data1\">";
                        echo "<input type=\"hidden\" name=\"original_function\" value=\"", htmlspecialchars($fndata->f[$data->fnFields['fnname']]),"\" />\n"; 
+                       // If we're 7.4 or above, we can rename functions
+                       if ($data->hasFunctionRename()) {
+                               echo "<input name=\"formFunction\" style=\"width: 100%\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_POST['formFunction']), "\" />";
+                       }
+                       else
+                               echo $misc->printVal($fndata->f[$data->fnFields['fnname']]);
                        echo "</td>\n";
 
                        echo "<td class=\"data1\">", $misc->printVal($fndata->f[$data->fnFields['fnarguments']]), "\n";
                        echo "<tr><th class=\"data required\" colspan=\"8\">{$lang['strdefinition']}</th></tr>\n";
                        echo "<tr><td class=\"data1\" colspan=\"8\"><textarea style=\"width:100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\" wrap=\"virtual\">", 
                                htmlspecialchars($_POST['formDefinition']), "</textarea></td></tr>\n";
+                       // Display function comment
+                       echo "<tr><th class=\"data\" colspan=\"4\">{$lang['strcomment']}</th></tr>\n";
+                       echo "<tr><td class=\"data1\" colspan=\"4\"><input name=\"formComment\" style=\"width: 100%\" value=\"", 
+                                       htmlspecialchars($_POST['formComment']), "\" /></td></tr>\n";
                        // Display function properies
                        if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
                                echo "<tr><th class=\"data\" colspan=\"4\">{$lang['strproperties']}</th></tr>\n";
                                }
                                echo "</td></tr>\n";
                        }               
+                       echo "</td></tr>\n";
                        echo "</table>\n";
                        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
                        echo "<input type=\"hidden\" name=\"function\" value=\"", htmlspecialchars($_REQUEST['function']), "\" />\n";
                        echo "<td class=\"data1\">", $misc->printVal($funcdata->f[$data->fnFields['fnlang']]), "</td></tr>\n";
                        echo "<tr><th class=\"data\" colspan=\"4\">{$lang['strdefinition']}</th></tr>\n";
                        echo "<tr><td class=\"data1\" colspan=\"4\">", $misc->printVal($funcdata->f[$data->fnFields['fndef']]), "</td></tr>\n";
+                       // Show comment
+                       echo "<tr><th class=\"data\" colspan=\"4\">{$lang['strcomment']}</th></tr>\n";
+                       echo "<tr><td class=\"data1\" colspan=\"4\">", $misc->printVal($funcdata->f['funccomment']), "</td></tr>\n";
+                       // Show flags
                        if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
                                // Fetch an array of the function properties
                                $funcprops = $data->getFunctionProperties($funcdata->f);