/*
  * Parent class of all ADODB objects.
  *
- * $Id: ADODB_base.php,v 1.12 2003/05/01 03:27:54 chriskl Exp $
+ * $Id: ADODB_base.php,v 1.13 2003/05/31 06:56:02 chriskl Exp $
  */
 
 include_once('libraries/errorhandler.inc.php');
                // Populate the syntax arrays
                reset($vars);
                while(list($key, $value) = each($vars)) {
-                       $this->clean($key);
+                       $this->fieldClean($key);
                        $this->clean($value);
                        if ($setClause) $setClause .= ", \"{$key}\"='{$value}'";
                        else $setClause = "UPDATE \"{$table}\" SET \"{$key}\"='{$value}'";
 
                reset($nulls);
                while(list(, $value) = each($nulls)) {
-                       $this->clean($value);
+                       $this->fieldClean($value);
                        if ($setClause) $setClause .= ", \"{$value}\"=NULL";
                        else $setClause = "UPDATE \"{$table}\" SET \"{$value}\"=NULL";
                }
 
                reset($where);
                while(list($key, $value) = each($where)) {
-                       $this->clean($key);
+                       $this->fieldClean($key);
                        $this->clean($value);
                        if ($whereClause) $whereClause .= " AND \"{$key}\"='{$value}'";
                        else $whereClause = " WHERE \"{$key}\"='{$value}'";
 
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: BaseDB.php,v 1.18 2003/05/25 09:41:57 chriskl Exp $
+ * $Id: BaseDB.php,v 1.19 2003/05/31 06:56:02 chriskl Exp $
  */
 
 include_once('classes/database/ADODB_base.php');
        /**
         * Updates a row in a table
         * @param $table The table in which to update
-        * @param $values An array mapping new values for the row
+        * @param $vars An array mapping new values for the row
         * @param $nulls An array mapping column => something if it is to be null
-        * @param $key An array mapping column => value to update
+        * @param $format An array of the data type (VALUE or EXPRESSION)
+        * @param $types An array of field types
+        * @param $keyarr An array mapping column => value to update
         * @return 0 success
         * @return -1 invalid parameters
         */
-       function editRow($table, $values, $nulls, $key) {
-               if (!is_array($values) || !is_array($nulls) || !is_array($key)) return -1;
-               // @@ WE CANNOT USE update AS WE NEED TO NOT QUOTE SOME THINGS
-               // @@ WHAT ABOUT BOOLEANS??
+       function editRow($table, $vars, $nulls, $format, $types, $keyarr) {
+               if (!is_array($vars) || !is_array($nulls) || !is_array($format)
+                       || !is_array($types)) return -1;
                else {
-                       $temp = array();
-                       foreach($values as $k => $v) {
-                               if (!isset($nulls[$k])) $temp[$k] = $v;
-                       }
-                       return $this->update($table, $temp, $key, array_keys($nulls));
+                       $this->fieldClean($table);
+
+                       // Build clause
+                       if (sizeof($vars) > 0) {
+                               foreach($vars as $key => $value) {
+                                       $this->fieldClean($key);
+       
+                                       // Handle NULL values
+                                       if (isset($nulls[$key])) $tmp = 'NULL';
+                                       else $tmp = $this->formatValue($types[$key], $format[$key], $value);
+                                       
+                                       if (isset($sql)) $sql .= ", \"{$key}\"={$tmp}";
+                                       else $sql = "UPDATE \"{$table}\" SET \"{$key}\"={$tmp}";
+                               }
+                               $first = true;
+                               foreach ($keyarr as $k => $v) {
+                                       $this->fieldClean($k);
+                                       $this->clean($v);
+                                       if ($first) {
+                                               $sql .= " WHERE \"{$k}\"='{$v}'";
+                                               $first = false;
+                                       }
+                                       else $sql .= " AND \"{$k}\"='{$v}'";
+                               }                               
+                       }                       
+                       return $this->execute($sql);
                }
        }
 
                                $fields = '';
                                $values = '';
                                foreach($vars as $key => $value) {
-                                       $doEscape = $format[$key] == 'VALUE';
                                        $this->fieldClean($key);
-                                       if ($doEscape) $this->clean($value);
        
                                        // Handle NULL values
                                        if (isset($nulls[$key])) $tmp = 'NULL';
                                        else $tmp = $this->formatValue($types[$key], $format[$key], $value);
                                        
-                                       // If format Value retuns a null value, then don't bother
-                                       // inserting a value for that column.
                                        if ($fields) $fields .= ", \"{$key}\"";
                                        else $fields = "INSERT INTO \"{$table}\" (\"{$key}\"";
 
 
        /**
         * List tables in a database
         *
-        * $Id: tables.php,v 1.24 2003/05/19 06:08:07 chriskl Exp $
+        * $Id: tables.php,v 1.25 2003/05/31 06:56:01 chriskl Exp $
         */
 
        // Include application functions
 
                                // Output table header
                                echo "<tr><th class=\"data\">{$lang['strfield']}</th><th class=\"data\">{$lang['strtype']}</th>";
+                               echo "<th class=\"data\">{$lang['strformat']}</th>\n";
                                echo "<th class=\"data\">{$lang['strnull']}</th><th class=\"data\">{$lang['strvalue']}</th></tr>";
 
                                // @@ CHECK THAT KEY ACTUALLY IS IN THE RESULT SET...
                                while (!$attrs->EOF) {
                                        $attrs->f['attnotnull'] = $localData->phpBool($attrs->f['attnotnull']);
                                        $id = (($i % 2) == 0 ? '1' : '2');
+                                       
+                                       // Initialise variables
+                                       if (!isset($_REQUEST['format'][$attrs->f['attname']]))
+                                               $_REQUEST['format'][$attrs->f['attname']] = 'VALUE';
+                                       
                                        echo "<tr>\n";
                                        echo "<td class=\"data{$id}\" nowrap=\"nowrap\">", htmlspecialchars($attrs->f['attname']), "</td>";
-                                       echo "<td class=\"data{$id}\" nowrap=\"nowrap\">", htmlspecialchars($attrs->f['type']), "</td>";
+                                       echo "<td class=\"data{$id}\" nowrap=\"nowrap\">\n";
+                                       echo htmlspecialchars($attrs->f['type']);
+                                       echo "<input type=\"hidden\" name=\"types[", htmlspecialchars($attrs->f['attname']), "]\" value=\"", 
+                                               htmlspecialchars($attrs->f['type']), "\" /></td>";
+                                       echo "<td class=\"data{$id}\" nowrap=\"nowrap\">\n";
+                                       echo "<select name=\"format[", htmlspecialchars($attrs->f['attname']), "]\">\n";
+                                       echo "<option value=\"VALUE\"", ($_REQUEST['format'][$attrs->f['attname']] == 'VALUE') ? ' selected' : '', ">{$lang['strvalue']}</option>\n";
+                                       echo "<option value=\"EXPRESSION\"", ($_REQUEST['format'][$attrs->f['attname']] == 'EXPRESSION') ? ' selected' : '', ">{$lang['strexpression']}</option>\n";
+                                       echo "</select>\n</td>\n";
                                        echo "<td class=\"data{$id}\" nowrap=\"nowrap\">";
                                        // Output null box if the column allows nulls (doesn't look at CHECKs or ASSERTIONS)
                                        if (!$attrs->f['attnotnull']) {
                        if (!isset($_POST['values'])) $_POST['values'] = array();
                        if (!isset($_POST['nulls'])) $_POST['nulls'] = array();
                        
-                       $status = $localData->editRow($_POST['table'], $_POST['values'], $_POST['nulls'], unserialize($_POST['key']));
+                       $status = $localData->editRow($_POST['table'], $_POST['values'], $_POST['nulls'], 
+                                                                                               $_POST['format'], $_POST['types'], unserialize($_POST['key']));
                        if ($status == 0)
                                doBrowse($lang['strrowupdated']);
                        else