Support for VIEW manipulation. Use this as a template for functions, operators,...
authorchriskl <chriskl>
Mon, 15 Apr 2002 11:57:28 +0000 (11:57 +0000)
committerchriskl <chriskl>
Mon, 15 Apr 2002 11:57:28 +0000 (11:57 +0000)
classes/Misc.php [new file with mode: 0644]
classes/database/Postgres71.php
conf/config.inc.php
lang/template.php
public_html/views.php [new file with mode: 0644]
themes/default/global.css

diff --git a/classes/Misc.php b/classes/Misc.php
new file mode 100644 (file)
index 0000000..c8c9057
--- /dev/null
@@ -0,0 +1,23 @@
+<?php\r
+       /**\r
+        * Class to hold various commonly used functions\r
+        *\r
+        * $Id: Misc.php,v 1.1 2002/04/15 11:57:28 chriskl Exp $\r
+        */\r
+        \r
+       class Misc {\r
+               \r
+               /* Empty constructor */\r
+               function Misc() {}\r
+               \r
+               /**\r
+                * Print out a message\r
+                * @param $msg The message to print\r
+                */\r
+               function printMsg($msg) {\r
+                       if ($msg != '') echo "<p class=message>", htmlspecialchars($msg), "</p>\n";\r
+               }\r
+               \r
+       \r
+       }\r
+?>
\ No newline at end of file
index 78c68bfc4d105f56043eaf1abab6757258d73126..222ae195383d5632fa35a1a66b4a77517cae0c0c 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres\r
  * Note: This class uses ADODB and returns RecordSets.\r
  *\r
- * $Id: Postgres71.php,v 1.5 2002/04/10 04:09:47 chriskl Exp $\r
+ * $Id: Postgres71.php,v 1.6 2002/04/15 11:57:28 chriskl Exp $\r
  */\r
 \r
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???\r
@@ -15,6 +15,7 @@ class Postgres71 extends BaseDB {
 \r
        var $dbFields = array('dbname' => 'datname', 'dbcomment' => 'description');\r
        var $tbFields = array('tbname' => 'tablename', 'tbowner' => 'tableowner');\r
+       var $vwFields = array('vwname' => 'viewname', 'vwowner' => 'viewowner', 'vwdef' => 'definition');\r
 \r
        // @@ Should we bother querying for this?\r
        var $_lastSystemOID = 18539;\r
@@ -435,6 +436,93 @@ class Postgres71 extends BaseDB {
        function doUpdate()\r
 */\r
 \r
+       // View functions\r
+       \r
+       /**\r
+        * Returns a list of all views in the database\r
+        * @return All views\r
+        */\r
+       function getViews() {\r
+               if (!$this->_showSystem)\r
+                       $where = "WHERE viewname NOT LIKE 'pg_%'";\r
+               else $where  = '';\r
+               \r
+               $sql = "SELECT viewname, viewowner FROM pg_views {$where} ORDER BY viewname";\r
+\r
+               return $this->selectSet($sql);\r
+       }\r
+       \r
+       /**\r
+        * Returns all details for a particular view\r
+        * @param $view The name of the view to retrieve\r
+        * @return View info\r
+        */\r
+       function getView($view) {\r
+               $this->clean($view);\r
+               \r
+               $sql = "SELECT viewname, viewowner, definition FROM pg_views WHERE viewname='$view'";\r
+\r
+               return $this->selectSet($sql);\r
+       }       \r
+\r
+       /**\r
+        * Creates a new view.\r
+        * @param $viewname The name of the view to create\r
+        * @param $definition The definition for the new view\r
+        * @return 0 success\r
+        */\r
+       function createView($viewname, $definition) {\r
+               $this->clean($viewname);\r
+               // Note: $definition not cleaned\r
+               \r
+               $sql = "CREATE VIEW \"{$viewname}\" AS {$definition}";\r
+               \r
+               return $this->execute($sql);\r
+       }\r
+       \r
+       /**\r
+        * Drops a view.\r
+        * @param $viewname The name of the view to drop\r
+        * @return 0 success\r
+        */\r
+       function dropView($viewname) {\r
+               $this->clean($viewname);\r
+               \r
+               $sql = "DROP VIEW \"{$viewname}\"";\r
+               \r
+               return $this->execute($sql);\r
+       }\r
+       \r
+       /**\r
+        * Updates a view.  Postgres doesn't have CREATE OR REPLACE view,\r
+        * so we do it with a drop and a recreate.\r
+        * @param $viewname The name fo the view to update\r
+        * @param $definition The new definition for the view\r
+        * @return 0 success\r
+        * @return -1 transaction error\r
+        * @return -2 drop view error\r
+        * @return -3 create view error\r
+        */\r
+       function setView($viewname, $definition) {\r
+               $status = $this->beginTransaction();\r
+               if ($status != 0) return -1;\r
+               \r
+               $status = $this->dropView($viewname);\r
+               if ($status != 0) {\r
+                       $this->rollbackTransaction();\r
+                       return -2;\r
+               }\r
+               \r
+               $status = $this->createView($viewname, $definition);\r
+               if ($status != 0) {\r
+                       $this->rollbackTransaction();\r
+                       return -3;\r
+               }\r
+               \r
+               $status = $this->endTransaction();\r
+               return ($status == 0) ? 0 : -1;\r
+       }       \r
+\r
        // Operator functions\r
        \r
        /**\r
@@ -442,7 +530,7 @@ class Postgres71 extends BaseDB {
         * @return All operators\r
         */\r
        function getOperators() {\r
-               if ($this->_showSystem)\r
+               if (!$this->_showSystem)\r
                        $where = "WHERE po.oid > '{$this->_lastSystemOID}'::oid";\r
                else $where  = '';\r
                \r
index 493d3ea69e0855df7c8211ddc8b282a1d0f7b044..704114f7dc2e60a5df7353977fca34817bb0f0d4 100644 (file)
@@ -3,7 +3,7 @@
        /**\r
         * Central WebDB configuration\r
         *\r
-        * $Id: config.inc.php,v 1.5 2002/04/10 04:09:47 chriskl Exp $\r
+        * $Id: config.inc.php,v 1.6 2002/04/15 11:57:28 chriskl Exp $\r
         */\r
 \r
        // Set error reporting level\r
                                                                                        $webdbPassword);\r
        }\r
 \r
+       // Create Misc class references\r
+       include_once('../classes/Misc.php');\r
+       $misc = new Misc();\r
+\r
        // Theme\r
        echo "<style type=\"text/css\">\n<!--\n";\r
        include("../themes/{$guiTheme}/global.css");\r
index d0e1a23b4bd18587ce78fc61402197620b8598e4..504e593ad32d9ce784404bde548f8c9bf2514816 100644 (file)
@@ -4,7 +4,7 @@
         * Language template file for WebDB.  Use this to base language\r
         * files.\r
         *\r
-        * $Id: template.php,v 1.4 2002/04/10 04:09:47 chriskl Exp $\r
+        * $Id: template.php,v 1.5 2002/04/15 11:57:29 chriskl Exp $\r
         */\r
 \r
        $appLang = 'english';\r
        $strNoFrames = 'You need a frames-enabled browser to use this application.';\r
        $strLogin = 'Login';\r
        $strNoTables = 'No tables found.';\r
+       $strNoViews = 'No views found.';\r
        $strOwner = 'Owner';\r
        $strActions = 'Actions';        \r
+       $strName = 'Name';\r
        $strTable = 'Table';\r
        $strTables = 'Tables';\r
+       $strView = 'View';\r
        $strViews = 'Views';\r
+       $strDefinition = 'Definition';\r
        $strTriggers = 'Triggers';\r
        $strRules = 'Rules';\r
        $strSequences = 'Sequences';\r
diff --git a/public_html/views.php b/public_html/views.php
new file mode 100644 (file)
index 0000000..c81c2bc
--- /dev/null
@@ -0,0 +1,215 @@
+<?php\r
+\r
+       /**\r
+        * Manage views in a database\r
+        *\r
+        * $Id: views.php,v 1.1 2002/04/15 11:57:29 chriskl Exp $\r
+        */\r
+\r
+       // Include application functions\r
+       include_once('../conf/config.inc.php');\r
+       \r
+       if (!isset($action)) $action = '';\r
+       if (!isset($msg)) $msg = '';\r
+       \r
+       /** \r
+        * Function to save after editing a view\r
+        */\r
+       function doSaveEdit() {\r
+               global $localData, $view, $formDefinition;\r
+               \r
+               $status = $localData->setView($view, $formDefinition);\r
+               if ($status == 0)\r
+                       doProperties('View updated.');\r
+               else\r
+                       doEdit('View update failed.');\r
+       }\r
+       \r
+       /**\r
+        * Function to allow editing of a view\r
+        */\r
+       function doEdit($msg = '') {\r
+               global $data, $localData, $misc, $database, $view;\r
+               global $PHP_SELF, $strName, $strDefinition;\r
+               \r
+               echo "<h2>", htmlspecialchars($database), ": ", htmlspecialchars($view), "</h2>\n";\r
+               $misc->printMsg($msg);\r
+               \r
+               $viewdata = &$localData->getView($view);\r
+               \r
+               if ($viewdata->recordCount() > 0) {\r
+                       echo "<form action=\"$PHP_SELF\" method=post>\n";\r
+                       echo "<table>\n";\r
+                       echo "<tr><th class=data>{$strName}</th></tr>\n";\r
+                       echo "<tr><td class=data1>", htmlspecialchars($viewdata->f[$data->vwFields['vwname']]), "</td></tr>\n";\r
+                       echo "<tr><th class=data>{$strDefinition}</th></tr>\n";\r
+                       echo "<tr><td class=data1><textarea style=\"width:100%;\" rows=20 cols=50 name=formDefinition wrap=virtual>", \r
+                               htmlspecialchars($viewdata->f[$data->vwFields['vwdef']]), "</textarea></td></tr>\n";\r
+                       echo "</table>\n";\r
+                       echo "<input type=hidden name=action value=save_edit>\n";\r
+                       echo "<input type=hidden name=view value=\"", htmlspecialchars($view), "\">\n";\r
+                       echo "<input type=hidden name=database value=\"", htmlspecialchars($database), "\">\n";\r
+                       echo "<input type=submit value=Save> <input type=reset>\n";\r
+                       echo "</form>\n";\r
+               }\r
+               else echo "<p>No data.</p>\n";\r
+               \r
+               echo "<p><a class=navlink href=\"$PHP_SELF?database=", urlencode($database), "\">Show All Views</a> |\n";\r
+               echo "<a class=navlink href=\"$PHP_SELF?action=properties&database=", urlencode($database), "&view=", \r
+                       urlencode($view), "\">Properties</a>\n";\r
+       }\r
+       \r
+       /**\r
+        * Show read only properties for a view\r
+        */\r
+       function doProperties($msg = '') {\r
+               global $data, $localData, $misc, $database, $view;\r
+               global $PHP_SELF, $strName, $strDefinition;\r
+       \r
+               echo "<h2>", htmlspecialchars($database), ": ", htmlspecialchars($view), "</h2>\n";\r
+               $misc->printMsg($msg);\r
+               \r
+               $viewdata = &$localData->getView($view);\r
+               \r
+               if ($viewdata->recordCount() > 0) {\r
+                       echo "<table>\n";\r
+                       echo "<tr><th class=data>{$strName}</th></tr>\n";\r
+                       echo "<tr><td class=data1>", htmlspecialchars($viewdata->f[$data->vwFields['vwname']]), "</td></tr>\n";\r
+                       echo "<tr><th class=data>{$strDefinition}</th></tr>\n";\r
+                       echo "<tr><td class=data1>", nl2br(htmlspecialchars($viewdata->f[$data->vwFields['vwdef']])), "</td></tr>\n";\r
+                       echo "</table>\n";\r
+               }\r
+               else echo "<p>No data.</p>\n";\r
+               \r
+               echo "<p><a class=navlink href=\"$PHP_SELF?database=", urlencode($database), "\">Show All Views</a> |\n";\r
+               echo "<a class=navlink href=\"$PHP_SELF?action=edit&database=", urlencode($database), "&view=", \r
+                       urlencode($view), "\">Edit</a>\n";\r
+       }\r
+       \r
+       /**\r
+        * Show confirmation of drop and perform actual drop\r
+        */\r
+       function doDrop($confirm) {\r
+               global $localData, $database, $view;\r
+               global $PHP_SELF;\r
+\r
+               echo "<h2>", htmlspecialchars($database), ": ", htmlspecialchars($view), "</h2>\n";\r
+               \r
+               if ($confirm) { \r
+                       echo "<p>Are you sure you want to drop the view \"", htmlspecialchars($view), "\"?</p>\n";\r
+                       \r
+                       echo "<form action=\"$PHP_SELF\" method=\"post\">\n";\r
+                       echo "<input type=hidden name=action value=drop>\n";\r
+                       echo "<input type=hidden name=view value=\"", htmlspecialchars($view), "\">\n";\r
+                       echo "<input type=hidden name=database value=\"", htmlspecialchars($database), "\">\n";\r
+                       echo "<input type=submit name=choice value=\"Yes\"> <input type=submit name=choice value=\"No\">\n";\r
+                       echo "</form>\n";\r
+               }\r
+               else {\r
+                       $status = $localData->dropView($view);\r
+                       if ($status == 0)\r
+                               doDefault('View dropped.');\r
+                       else\r
+                               doDefault('View drop failed.');\r
+               }\r
+               \r
+       }       \r
+\r
+       /**\r
+        * Show default list of views in the database\r
+        */\r
+       function doDefault($msg = '') {\r
+               global $data, $localData, $misc, $database, $view;\r
+               global $PHP_SELF, $strView, $strOwner, $strActions, $strNoViews;\r
+               \r
+               echo "<h2>", htmlspecialchars($database), "</h2>\n";\r
+               $misc->printMsg($msg);\r
+               \r
+               $views = &$localData->getViews();\r
+               \r
+               if ($views->recordCount() > 0) {\r
+                       echo "<table>\n";\r
+                       echo "<tr><th class=data>{$strView}</th><th class=data>{$strOwner}</th><th colspan=4 class=data>{$strActions}</th>\n";\r
+                       $i = 0;\r
+                       while (!$views->EOF) {\r
+                               $id = (($i % 2) == 0 ? '1' : '2');\r
+                               echo "<tr><td class=data{$id}>", htmlspecialchars($views->f[$data->vwFields['vwname']]), "</td>\n";\r
+                               echo "<td class=data{$id}>", htmlspecialchars($views->f[$data->vwFields['vwowner']]), "</td>\n";\r
+                               echo "<td class=opbutton{$id}><a href=\"$PHP_SELF?action=browse&offset=0&limit=30&database=", \r
+                                       htmlspecialchars($database), "&table=", urlencode($views->f[$data->vwFields['vwname']]), "\">Browse</a></td>\n";\r
+                               echo "<td class=opbutton{$id}>Select</td>\n";\r
+                               echo "<td class=opbutton{$id}><a href=\"$PHP_SELF?action=properties&database=", \r
+                                       htmlspecialchars($database), "&view=", urlencode($views->f[$data->vwFields['vwname']]), "\">Properties</a></td>\n";\r
+                               echo "<td class=opbutton{$id}><a href=\"$PHP_SELF?action=confirm_drop&database=", \r
+                                       htmlspecialchars($database), "&view=", urlencode($views->f[$data->vwFields['vwname']]), "\">Drop</a></td>\n";\r
+                               echo "</tr>\n";\r
+                               $views->moveNext();\r
+                               $i++;\r
+                       }\r
+               }\r
+               else {\r
+                       echo "<p>{$strNoViews}</p>\n";\r
+               }\r
+       }\r
+\r
+       echo "<html>\n";\r
+       echo "<body>\n";\r
+       \r
+       switch ($action) {              \r
+               case 'drop':\r
+                       if ($choice == 'Yes') doDrop(false);\r
+                       else doDefault();\r
+                       break;\r
+               case 'confirm_drop':\r
+                       doDrop(true);\r
+                       break;                  \r
+               case 'save_edit':\r
+                       doSaveEdit();\r
+                       break;\r
+               case 'edit':\r
+                       doEdit();\r
+                       break;\r
+               case 'properties':\r
+                       doProperties();\r
+                       break;\r
+               case 'browse':          \r
+               /*\r
+                       echo "<h2>", htmlspecialchars($database), ": ", htmlspecialchars($table), "</h2>\n";\r
+                       $rs = &$localData->browseTable($table, $offset, $limit);\r
+                       \r
+                       if ($rs->recordCount() > 0) {\r
+                               echo "<table>\n<tr>";\r
+                               reset($rs->f);\r
+                               while(list($k, ) = each($rs->f)) {\r
+                                       echo "<th class=data>", htmlspecialchars($k), "</td>";\r
+                               }                       \r
+                               echo "<th colspan=2 class=data>{$strActions}</th>\n";\r
+                               \r
+                               $i = 0;\r
+                               reset($rs->f);\r
+                               while (!$rs->EOF) {\r
+                                       $id = (($i % 2) == 0 ? '1' : '2');\r
+                                       echo "<tr>\n";\r
+                                       while(list(, $v) = each($rs->f)) {\r
+                                               echo "<td class=data{$id} nowrap>", nl2br(htmlspecialchars($v)), "</td>";\r
+                                       }                                                       \r
+                                       echo "<td class=opbutton{$id}>Edit</td>\n";\r
+                                       echo "<td class=opbutton{$id}>Delete</td>\n";\r
+                                       echo "</tr>\n";\r
+                                       $rs->moveNext();\r
+                                       $i++;\r
+                               }\r
+                       }\r
+                       else echo "<p>No data.</p>\n";\r
+                               \r
+                       break;\r
+               */              \r
+               default:\r
+                       doDefault();\r
+                       break;\r
+       }       \r
+\r
+       echo "</body>\n";\r
+       echo "</html>\n";\r
+       \r
+?>
\ No newline at end of file
index a7493c45e168b318984e258da6278426342ec8ff..f9c094486ad9a96cd190a16d42416382b9bc0e61 100644 (file)
@@ -99,7 +99,7 @@ td.opbutton2 {
        font-size: 9pt;
 }
 
-a.toplink {
+a.toplink, a.navlink {
        font-weight: bold;
 }
 
@@ -116,6 +116,11 @@ p
        margin: 0px;
 }
 
+p.message
+{
+       color: blue;
+}
+
 a:link
 {
    color: #336699;