Add very rudimentary support for row editing. Need to add type tracking. Fix a...
authorchriskl <chriskl>
Mon, 9 Sep 2002 10:16:29 +0000 (10:16 +0000)
committerchriskl <chriskl>
Mon, 9 Sep 2002 10:16:29 +0000 (10:16 +0000)
classes/database/BaseDB.php
classes/database/Postgres.php
classes/database/Postgres71.php
lang/template.php
public_html/tables.php

index c3a7e677bbf9aa5534c707d240d8f0f3fb5c0c88..3793728ca94281c6bce0a3b8ec4f209febe93eed 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: BaseDB.php,v 1.3 2002/07/26 09:03:06 chriskl Exp $\r
+ * $Id: BaseDB.php,v 1.4 2002/09/09 10:16:29 chriskl Exp $\r
  */\r
 \r
 include_once('../classes/database/ADODB_base.php');\r
@@ -37,6 +37,20 @@ class BaseDB extends ADODB_base {
        function deleteRow($table, $key) {\r
                if (!is_array($key)) return -1;\r
                else return $this->delete($table, $key);\r
+       }\r
+       \r
+       /**\r
+        * Updates a row in a table\r
+        * @param $table The table in which to update\r
+        * @param $values An array mapping new values for the row\r
+        * @param $key An array mapping column => value to update\r
+        * @return 0 success\r
+        */\r
+       function editRow($table, $values, $key) {\r
+               if (!is_array($values) || !is_array($key)) return -1;\r
+               // @@ WE NEED TO SUPPORT NULL VALUES HERE!\r
+               // @@ ALSO, WE CANNOT USE update AS WE NEED TO NO QUOTE SOME THINGS\r
+               else return $this->update($table, $values, $key);\r
        }       \r
        \r
        // Capabilities\r
index 12d57a7c9232485724ddd84c5f9d65c410032abf..18730b5b0968c1eaeaa3930cbda8bf8d56e4039f 100755 (executable)
@@ -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: Postgres.php,v 1.4 2002/09/09 05:08:55 chriskl Exp $\r
+ * $Id: Postgres.php,v 1.5 2002/09/09 10:16:31 chriskl Exp $\r
  */\r
 \r
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???\r
@@ -85,6 +85,33 @@ class Postgres extends BaseDB {
                        }                       \r
                }                       \r
        }\r
+       \r
+       /**\r
+        * Outputs the HTML code for a particular field\r
+        * @param $name The name to give the field\r
+        * @param $value The value of the field\r
+        * @param $type The database type of the field\r
+        */\r
+       function printField($name, $value, $type) {\r
+               switch ($type) {\r
+                       case 'bool':\r
+                       case 'boolean':\r
+                               echo "<select name=\"", htmlspecialchars($name), "\">\n";\r
+                               echo "<option value=\"Y\"", ($value) ? ' selected' : '', ">Yes</option>\n";\r
+                               echo "<option value=\"N\"", (!$value) ? ' selected' : '', ">No</option>\n";\r
+                               echo "</select>\n";\r
+                               break;\r
+                       case 'text':\r
+                       case 'bytea':\r
+                               echo "<textarea name=\"", htmlspecialchars($name), "\" rows=5 cols=35 wrap=virtual>\n";\r
+                               echo htmlspecialchars($value);\r
+                               echo "</textarea>\n";\r
+                               break;\r
+                       default:\r
+                               echo "<input name=\"", htmlspecialchars($name), "\" value=\"", htmlspecialchars($value), "\" size=35>\n";\r
+                               break;\r
+               }               \r
+       }       \r
 \r
        /**\r
         * Return all database available on the server\r
@@ -190,9 +217,26 @@ class Postgres extends BaseDB {
         * @return A recordset\r
         */\r
        function &browseTable($table, $offset, $limit) {\r
-               return $this->selectTable("SELECT * FROM \"{$table}\"", $offset, $limit);\r
+               return $this->selectTable("SELECT oid, * FROM \"{$table}\"", $offset, $limit);\r
        }\r
-       \r
+\r
+       /**\r
+        * Returns a recordset of all columns in a table\r
+        * @param $table The name of a table\r
+        * @param $key The associative array holding the key to retrieve\r
+        * @return A recordset\r
+        */\r
+       function &browseRow($table, $key) {             \r
+               $sql = "SELECT * FROM \"{$table}\" WHERE true";\r
+               foreach ($key as $k => $v) {\r
+                       $this->clean($k);\r
+                       $this->clean($v);\r
+                       $sql .= " AND \"{$k}\"='{$v}'";\r
+               }\r
+               \r
+               return $this->selectSet($sql);\r
+       }\r
+\r
        /**\r
         *\r
         */\r
index ee18b3d1be779e14f3cb27a8c70cbde085a6a9c3..41ab011e6b6a85911062c99e4a56cac11940edbf 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.11 2002/07/26 09:03:06 chriskl Exp $\r
+ * $Id: Postgres71.php,v 1.12 2002/09/09 10:16:31 chriskl Exp $\r
  */\r
 \r
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???\r
@@ -114,20 +114,6 @@ class Postgres71 extends Postgres {
                return $this->execute($sql);\r
        }\r
        \r
-       /**\r
-        *\r
-        */\r
-       function &browseTable($table, $offset, $limit) {\r
-               return $this->selectTable("SELECT * FROM \"{$table}\"", $offset, $limit);\r
-       }\r
-       \r
-       /**\r
-        *\r
-        */\r
-       function &selectTable($sql, $offset, $limit) {\r
-               return $this->selectSet($sql, $offset, $limit);\r
-       }\r
-\r
        /**\r
         * Adds a check constraint to a table\r
         * @param $table The table to which to add the check\r
index 6cabd10de291de4207e0ba403657cf5e20f18122..b201a383eb1114e79b67dd74cd34d89bd2a4775c 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.9 2002/08/30 15:10:09 xzilla Exp $\r
+        * $Id: template.php,v 1.10 2002/09/09 10:16:31 chriskl Exp $\r
         */\r
 \r
        $appLang = 'english';\r
 \r
        // Sequences\r
        $strNoSequences = 'No sequences found.';\r
+       \r
+       // Tables\r
+       $strField = 'Field';\r
+       $strType = 'Type';\r
+       $strValue = 'Value';\r
 \r
 ?>\r
index 5da6210b9cf19ba23229905ced5dbc0f21f45a6d..69ac5a6081e960f4cf87af9532b4a72c4915acbf 100644 (file)
@@ -3,7 +3,7 @@
        /**\r
         * List tables in a database\r
         *\r
-        * $Id: tables.php,v 1.4 2002/07/26 09:03:06 chriskl Exp $\r
+        * $Id: tables.php,v 1.5 2002/09/09 10:16:31 chriskl Exp $\r
         */\r
 \r
        // Include application functions\r
        $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';\r
        $PHP_SELF = $_SERVER['PHP_SELF'];\r
        \r
+       /**\r
+        * Show confirmation of edit and perform actual update\r
+        */\r
+       function doEditRow($confirm, $msg = '') {\r
+               global $localData, $database, $misc;\r
+               global $strField, $strType, $strValue;\r
+               global $PHP_SELF;\r
+\r
+               $key = $_REQUEST['key'];\r
+\r
+               if ($confirm) { \r
+                       echo "<h2>", htmlspecialchars($_REQUEST['database']), ": Tables: ", htmlspecialchars($_REQUEST['table']), ": Edit Row</h2>\n";\r
+                       $misc->printMsg($msg);\r
+\r
+                       $rs = &$localData->browseRow($_REQUEST['table'], $key);\r
+                       \r
+                       echo "<form action=\"$PHP_SELF\" method=\"post\">\n";\r
+                       if ($rs->recordCount() == 1) {\r
+                               echo "<table>\n<tr>";\r
+                               \r
+                               // Output table header\r
+                               echo "<tr><th class=data>{$strField}</th><th class=data>{$strType}</th><th class=data>{$strValue}</th></tr>";\r
+                               \r
+                               // @@ CHECK THAT KEY ACTUALLY IS IN THE RESULT SET...\r
+                               \r
+                               $i = 0;\r
+                               foreach ($rs->f as $k => $v) {\r
+                                       $id = (($i % 2) == 0 ? '1' : '2');\r
+                                       echo "<tr>\n";\r
+                                       echo "<td class=data{$id} nowrap>", htmlspecialchars($k), "</td>";\r
+                                       echo "<td class=data{$id} nowrap>Type</td>";\r
+                                       echo "<td class=data{$id} nowrap>", $localData->printField("values[{$k}]", $v, 'type goes here'), "</td>";\r
+                                       echo "</tr>\n";\r
+                                       $i++;\r
+                               }\r
+                               echo "</table></p>\n";\r
+                       }\r
+                       else echo "<p>No data.</p>\n";\r
+                       \r
+                       echo "<input type=hidden name=action value=editrow>\n";\r
+                       echo "<input type=hidden name=table value=\"", htmlspecialchars($_REQUEST['table']), "\">\n";\r
+                       echo "<input type=hidden name=database value=\"", htmlspecialchars($_REQUEST['database']), "\">\n";\r
+                       echo "<input type=hidden name=offset value=\"", htmlspecialchars($_REQUEST['offset']), "\">\n";\r
+                       echo "<input type=hidden name=limit value=\"", htmlspecialchars($_REQUEST['limit']), "\">\n";\r
+                       echo "<input type=hidden name=key value=\"", htmlspecialchars(serialize($key)), "\">\n";\r
+                       echo "<input type=submit name=choice value=\"Save\"> <input type=submit name=choice value=\"Cancel\">\n";\r
+                       echo "</form>\n";\r
+               }\r
+               else {\r
+                       $status = $localData->editRow($_POST['table'], $_POST['values'], unserialize($_POST['key']));\r
+                       if ($status == 0)\r
+                               doBrowse('Row updated.');\r
+                       else\r
+                               doBrowse('Row update failed.');\r
+               }\r
+               \r
+       }       \r
+       \r
        /**\r
         * Show confirmation of drop and perform actual drop\r
         */\r
                $misc->printMsg($msg);\r
 \r
                $rs = &$localData->browseTable($_REQUEST['table'], $_REQUEST['offset'], $_REQUEST['limit']);\r
-               \r
+\r
                // Fetch unique row identifier, if there is one\r
                $key = $localData->getRowIdentifier($_REQUEST['table']);\r
                \r
                                                $key_str .= urlencode("key[{$v}]") . '=' . urlencode($rs->f[$v]);\r
                                        }\r
                                        \r
-                                       echo "<td class=opbutton{$id}><a href=\"{$PHP_SELF}?action=editrow&database=", urlencode($_REQUEST['database']),\r
+                                       echo "<td class=opbutton{$id}><a href=\"{$PHP_SELF}?action=confeditrow&database=", urlencode($_REQUEST['database']),\r
                                                "&table=", urlencode($_REQUEST['table']), "&offset=", $_REQUEST['offset'], "&limit=", $_REQUEST['limit'], "&{$key_str}\">Edit</a></td>\n";\r
                                        echo "<td class=opbutton{$id}><a href=\"{$PHP_SELF}?action=confdelrow&database=", urlencode($_REQUEST['database']),\r
                                                "&table=", urlencode($_REQUEST['table']), "&offset=", $_REQUEST['offset'], "&limit=", $_REQUEST['limit'], "&{$key_str}\">Delete</a></td>\n";\r
        echo "<body>\n";\r
        \r
        switch ($action) {\r
+               case 'editrow':\r
+                       if ($_POST['choice'] == 'Save') doEditRow(false);\r
+                       else doBrowse();\r
+                       break;\r
+               case 'confeditrow':\r
+                       doEditRow(true);\r
+                       break;                  \r
                case 'delrow':\r
                        if ($_POST['choice'] == 'Yes') doDelRow(false);\r
                        else doBrowse();\r