* 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
\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
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
* @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
--- /dev/null
+<?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