diff options
author | Christian Schneider | 2007-10-11 15:18:41 +0000 |
---|---|---|
committer | Christian Schneider | 2007-10-11 15:18:41 +0000 |
commit | 3f122785bcbad2d685a2e9042c3f9efaffe59f9a (patch) | |
tree | dc6bb2ca9e6cc7bdb003899aa4dd35a5fefc84c0 /it_dbi.class | |
parent | c18587994161c9125497400ec18a8cac9d9974e0 (diff) | |
download | itools-3f122785bcbad2d685a2e9042c3f9efaffe59f9a.tar.gz itools-3f122785bcbad2d685a2e9042c3f9efaffe59f9a.tar.bz2 itools-3f122785bcbad2d685a2e9042c3f9efaffe59f9a.zip |
Removed deprecated manual settings of fields for update() and added basic tests
Diffstat (limited to 'it_dbi.class')
-rw-r--r-- | it_dbi.class | 44 |
1 files changed, 16 insertions, 28 deletions
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; } |