diff options
-rw-r--r-- | it_dbi.class | 20 | ||||
-rwxr-xr-x | tests/it_dbi.t | 35 |
2 files changed, 53 insertions, 2 deletions
diff --git a/it_dbi.class b/it_dbi.class index ab132ff..1c30d6f 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -719,6 +719,8 @@ function insert($tags = array(), $command = "INSERT") $id = ($this->_p['autoincrement'] && !isset($tags[$this->_p['keyfield']])) ? mysqli_insert_id($this->_link) : $tags[$this->_p['keyfield']]; if ($this->_p['keyfield'] && !$this->read($id) && $this->_p['safety']) $this->_fatal("insert(): can't read record back (key truncated?), id=\"$id\""); + + $this->_touchedids[$this->_key] = true; } else $this->clear(false); @@ -782,6 +784,8 @@ function update($tags = array(), $where = null) } } + $this->_touchedids[$this->_key] = true; + return $result; } @@ -811,6 +815,20 @@ function delete($query = null) /** + * Delete records matching query which were not touched since creation of this object or last call of this func + */ +function delete_untouched($query = null) +{ + if ($this->select($query)) + while ($this->iterate()) + if (($id = $this->_key) && !$this->_touchedids[$id]) + $result += $this->delete(); + + unset($this->_touchedids); + return (int)$result; +} + +/** * Escapes a string for use in a DB query * @param The string to be quoted * @return The quoted value @@ -953,5 +971,3 @@ static function get($id) } } /* End class it_dbi */ - -?> diff --git a/tests/it_dbi.t b/tests/it_dbi.t index cec097e..b29b164 100755 --- a/tests/it_dbi.t +++ b/tests/it_dbi.t @@ -327,3 +327,38 @@ $record->update(['-key3' => "2*2"]); is($record->key1, "val1"); is($ $record->update(['key1' => "val0"]); is($record->key1, "val0"); is($record->key2, "val2"); is($record->key3, 4); $record->replace(['ID' => 6, 'key4' => "val4"]); is($record->key4, "val4"); $record->select(['key2' => "val2"]); is($record->key2, "val2"); is($record->key4, null, "clear previous fields"); + +# +# Test tracked update +# +function allrecs() +{ + foreach (new it_dbi_test() as $r) + $result[] = ['ID' => $r->ID, 'foo' => $r->foo]; + return json_encode($result); +} + +$record = new it_dbi_test; +$record->delete(['WHERE 1' ]); +$record->upsert(['ID' => 1, 'foo' => "a"]); +$record->upsert(['ID' => 2, 'foo' => "b"]); +$record->upsert(['ID' => 2, 'foo' => "B"]); +is($record->delete_untouched(), 0, 'delete_untouched'); +is(allrecs(), '[{"ID":1,"foo":"a"},{"ID":2,"foo":"B"}]', 'data after delete_untouched'); + +$record->upsert(['ID' => 1, 'foo' => "A"]); +$record->upsert(['ID' => 3, 'foo' => "c"]); +is($record->delete_untouched([ 'ID >' => 2 ]), 0, 'delete_untouched with query'); +is(allrecs(), '[{"ID":1,"foo":"A"},{"ID":2,"foo":"B"},{"ID":3,"foo":"c"}]', 'data after delete_untouched with query'); + +$record->upsert(['ID' => 3, 'foo' => "C"]); +is($record->delete_untouched(), 2, 'delete_untouched with query'); +is(allrecs(), '[{"ID":3,"foo":"C"}]', 'data after delete_untouched with query'); + +$record->replace(['ID' => 4, 'foo' => "C"]); +is($record->delete_untouched(), 1, 'delete_untouched after replace'); +is(allrecs(), '[{"ID":4,"foo":"C"}]', 'data after delete_untouched after replace'); + +$record->upsert(['ID' => 4, 'foo' => "C"]); +is($record->delete_untouched(), 0, 'delete_untouched after upsert without changes'); +is(allrecs(), '[{"ID":4,"foo":"C"}]', 'data after delete_untouched after upsert without changes'); |