From 4bf376c1a603caa7bd093a1c36817f8ea21180b1 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Tue, 26 Sep 2017 16:38:14 +0200 Subject: Added update/insert/upsert tracking and delete_untouched() to delete rows not touch by one of those functions --- it_dbi.class | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'it_dbi.class') 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; } @@ -810,6 +814,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 @@ -953,5 +971,3 @@ static function get($id) } } /* End class it_dbi */ - -?> -- cgit v1.2.3