From b60fd48869194a61d26c30f99293b5080330d98e Mon Sep 17 00:00:00 2001 From: chriskl Date: Sat, 31 May 2003 06:56:01 +0000 Subject: [PATCH] much more sophisticated edit row --- classes/database/ADODB_base.php | 8 +++--- classes/database/BaseDB.php | 50 ++++++++++++++++++++++----------- tables.php | 21 ++++++++++++-- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/classes/database/ADODB_base.php b/classes/database/ADODB_base.php index 1570cc52..d1c6320d 100644 --- a/classes/database/ADODB_base.php +++ b/classes/database/ADODB_base.php @@ -3,7 +3,7 @@ /* * 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'); @@ -215,7 +215,7 @@ class ADODB_base { // 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}'"; @@ -223,14 +223,14 @@ class ADODB_base { 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}'"; diff --git a/classes/database/BaseDB.php b/classes/database/BaseDB.php index 7994ca4c..d9794aef 100644 --- a/classes/database/BaseDB.php +++ b/classes/database/BaseDB.php @@ -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.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'); @@ -31,22 +31,44 @@ class BaseDB extends ADODB_base { /** * 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); } } @@ -71,16 +93,12 @@ class BaseDB extends ADODB_base { $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}\""; diff --git a/tables.php b/tables.php index e958ec66..8b85b2b6 100644 --- a/tables.php +++ b/tables.php @@ -3,7 +3,7 @@ /** * 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 @@ -415,6 +415,7 @@ // Output table header echo "{$lang['strfield']}{$lang['strtype']}"; + echo "{$lang['strformat']}\n"; echo "{$lang['strnull']}{$lang['strvalue']}"; // @@ CHECK THAT KEY ACTUALLY IS IN THE RESULT SET... @@ -423,9 +424,22 @@ 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 "\n"; echo "", htmlspecialchars($attrs->f['attname']), ""; - echo "", htmlspecialchars($attrs->f['type']), ""; + echo "\n"; + echo htmlspecialchars($attrs->f['type']); + echo "f['attname']), "]\" value=\"", + htmlspecialchars($attrs->f['type']), "\" />"; + echo "\n"; + echo "\n\n"; echo ""; // Output null box if the column allows nulls (doesn't look at CHECKs or ASSERTIONS) if (!$attrs->f['attnotnull']) { @@ -465,7 +479,8 @@ 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 -- 2.39.5