From 3f122785bcbad2d685a2e9042c3f9efaffe59f9a Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Thu, 11 Oct 2007 15:18:41 +0000 Subject: Removed deprecated manual settings of fields for update() and added basic tests --- it_dbi.class | 44 ++++++++++----------------- tests/it_dbi.t | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 28 deletions(-) create mode 100755 tests/it_dbi.t diff --git a/it_dbi.class b/it_dbi.class index 9c6e691..0926a1c 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -190,28 +190,16 @@ function _connect($p = array()) /** * INTERNAL: construct SQL SET clause of changed values from member vars and tags array. - * Merge current values into $tags. Modifies caller's array (callers rely on it)! */ -function _set(&$tags) +function _set($tags, $allfields = false) { - # DEPRECATED BEHAVIOUR: Add member vars to tags, considering unquoted fields - foreach (get_object_vars($this) as $field => $value) - # Don't use isset($tags[$field]) (would not handle null values correctly) - if (isset($this->_fields[$field]) && !array_key_exists('-'.$field, $tags) && !array_key_exists($field, $tags)) - { - $tags[$field] = $value; - if ($this->_data && ($value != $this->_data[$field])) - it::error("it_dbi::_set() would take value '$value' from this->$field:" . D($this, $tags)); - } - - # Create SQL $r = array(); - foreach((array)$tags as $key => $value) + foreach((array)$tags as $field => $value) { - if (substr($key, 0, 1) == '-') # Unquoted value (always added) - $r[] = substr($key, 1)."=$value"; - elseif (!isset($this->_data) || ($value !== $this->_data[$key])) # Add to SQL if value has changed - $r[] = "$key=".(isset($value) ? "'".mysql_real_escape_string($value, $this->_link)."'" : 'NULL'); + if (substr($field, 0, 1) == '-') # Unquoted value (always added) + $r[] = substr($field, 1)."=$value"; + else if ($allfields || ($value !== $this->_data[$field])) + $r[] = "$field=".(isset($value) ? "'".mysql_real_escape_string($value, $this->_link)."'" : 'NULL'); } return $r ? 'SET '.implode(', ', $r) : ''; @@ -362,8 +350,8 @@ function _write_pre_process(&$tags) */ function clear($pp = true) { - foreach ((array)$this->_fields as $key => $dummy) - unset($this->$key); + foreach ((array)$this->_fields as $field => $dummy) + unset($this->$field); unset($this->_data); unset($this->_key); $pp && $this->_read_post_process(); @@ -505,8 +493,8 @@ function iterate() if (!empty($this->_p['keyfield'])) $this->_key = $this->_data[$this->_p['keyfield']]; - foreach ($this->_data as $key => $value) - $this->$key = $value; + foreach ($this->_data as $field => $value) + $this->$field = $value; } else $this->clear(false); @@ -533,14 +521,10 @@ function insert($tags = array(), $command = "INSERT") /* Pre-processing, $tags is passed by reference and may be modified here */ $this->_write_pre_process($tags); - unset($this->_data); # All new value set - $set = $this->_set($tags); # Update $tags (!) and generate SQL - if ($this->_p['randomid'] && !isset($tags[$this->_p['keyfield']])) - { $tags[$this->_p['keyfield']] = md5(uniqid(mt_rand())); - $set = $this->_set($tags); # Generate new SQL containing ID - } + + $set = $this->_set($tags, true); if ($result = $this->query("$command INTO {$this->_p['table']} " . $set)) { @@ -548,6 +532,8 @@ function insert($tags = array(), $command = "INSERT") if (!$this->read($id) && $this->_p['safety']) $this->_fatal("insert(): can't read record back (key truncated?), id=\"$id\""); } + else + $this->clear(false); return $result; } @@ -582,6 +568,7 @@ function update($tags = array(), $query = null) $query = array($this->_p['keyfield'] => $this->_data[$this->_p['keyfield']]); if ($set = $this->_set($tags)) + { if ($result = $this->query("UPDATE {$this->_p['table']} $set " . $this->_where($query, $this->_link))) { if (array_key_exists($this->_p['keyfield'], $tags)) # Did we just update the key? @@ -590,6 +577,7 @@ function update($tags = array(), $query = null) if ($this->read($this->_key)) $this->_nofetch = false; # So we can do while(iterate()) update(); } + } return $result; } diff --git a/tests/it_dbi.t b/tests/it_dbi.t new file mode 100755 index 0000000..8ef8cab --- /dev/null +++ b/tests/it_dbi.t @@ -0,0 +1,94 @@ +#!/www/server/bin/php -qC + "lib_search_ch"); +$dbi = new it_dbi($db); +$dbi->query('create temporary table it_dbi_test ( + ID int not null auto_increment, + x int, + foo varchar(42), + primary key(ID) +);'); + +$record = new it_dbi($db + array('table' => "it_dbi_test")); + +$record->insert(array('x' => 42, 'foo' => null)); +$record->insert(array('foo' => "bar")); +$record->insert(array('x' => 64738, 'foo' => "q'uux")); + +is( + $record->ID, + 3, + "auto_increment" +); + +$record->read(1); +is( + array($record->_key, $record->x, $record->foo), + array(1, 42, null), + "read" +); + +$record->select(array('x' => 64738)); +is( + array($record->_key, $record->x, $record->foo), + array(3, 64738, "q'uux"), + "select" +); + +$record->update(array('x' => 17)); +is( + array($record->_key, $record->x, $record->foo), + array(3, 17, "q'uux"), + "update" +); + +$record->update(array('-x' => 'RAND() * 10')); +isnt( + array($record->_key, $record->x, $record->foo), + array(3, 17, "q'uux"), + "update" +); + +$rand = $record->x; +is ( + $record->_set(array('x' => $rand, 'foo' => "bar")), + "SET foo='bar'", + 'update: _set optimization' +); + +$record->update(array('foo' => "bar")); +$record->select(array('foo' => "bar")); +$record->iterate(); +is( + array($record->_key, $record->x, $record->foo), + array(2, null, "bar"), + "iterate record 2" +); +$record->update(array('foo' => "qux")); +$record->iterate(); +is( + array($record->_key, $record->x, $record->foo), + array(3, $rand, "bar"), + "iterate record 3" +); +$record->update(array('foo' => "quux")); + +$record->read(2); +is( + array($record->_key, $record->x, $record->foo), + array(2, null, "qux"), + "iterate update record 2" +); + +$record->read(3); +is( + array($record->_key, $record->x, $record->foo), + array(3, $rand, "quux"), + "iterate update record 3" +); -- cgit v1.2.3