diff options
Diffstat (limited to 'it_dbi.class')
-rw-r--r-- | it_dbi.class | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/it_dbi.class b/it_dbi.class index 2d4be89..4064465 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -486,12 +486,36 @@ function _read_post_process() * @param $tags Reference to update/create tags, can be modified as needed * @param $command SQL command ('INSERT', 'REPLACE' or 'UPDATE') */ -function _write_pre_process(&$tags, $command) # NOPHPLINT +function _write_pre_process(&$tags, $command) # NOPHPLINT FIXME 2020-12 CS Remove legacy hooks now replace by pure and static versions { } /** + * Hook to postprocess data after reading a record. + * This is a stub-function that can be overloaded. + * @param $data Data of record read from db + * @return Record data including postprocess modifications + */ +static function _read_postprocess($data) +{ + return $data; +} + + +/** + * Hook to preprocess data before writing the record. + * This is a stub-function that can be overloaded. + * @param $data Update/create data tags + * @return Record data to be written including preprocess modifications/additions/deletions + */ +static function _write_preprocess($data) +{ + return $data; +} + + +/** * Return an array of all tables of this database */ function tables($p = array()) @@ -506,13 +530,11 @@ function tables($p = array()) /** * Clear record */ -function clear($pp = true) +function clear() { foreach ((array)$this->_fields + (array)$this->_localizedfields + (array)$this->_data as $field => $dummy) unset($this->$field); unset($this->_key, $this->_data); - - $pp && $this->_read_post_process(); } @@ -664,7 +686,7 @@ function select(/* $query = array|string, ... */) $nofetch = $this->_nofetch = $query['NOFETCH']; unset($query['NOFETCH']); - $this->clear(false); + $this->clear(); if ($this->_result = $this->query($sql = "SELECT $what " . $this->_from($query) . " " . $this->_where($query))) { $result = $this->_p['unbuffered'] ? true : mysqli_num_rows($this->_result); @@ -735,12 +757,25 @@ function iterate() } else { - $this->clear(false); + $this->clear(); if ($this->_p['unbuffered']) $this->_result->close(); } $this->_read_post_process(); + + if (is_array($this->_data)) + { + $originalfields = array_keys($this->_data); + $this->_data = static::_read_postprocess($this->_data, $this); + # Remove fields which have been unset in _read_postprocess + foreach (array_diff($originalfields, array_keys($this->_data)) as $field) + unset($this->$field); + # Update fields as they may have been changed in _read_postprocess + foreach ($this->_data as $field => $value) + $this->$field = $value; + } + } else $this->_nofetch = false; @@ -764,6 +799,7 @@ function insert($tags = array(), $command = "INSERT") /* Pre-processing, $tags is passed by reference and may be modified here */ $this->_write_pre_process($tags, $command); + $tags = static::_write_preprocess($tags); if ($this->_p['randomid'] && !isset($tags[$this->_p['keyfield']])) $tags[$this->_p['keyfield']] = bin2hex(random_bytes(16)); @@ -779,7 +815,7 @@ function insert($tags = array(), $command = "INSERT") $this->_touchedids[$this->_key] = true; } else - $this->clear(false); + $this->clear(); return $result; } @@ -822,6 +858,7 @@ function update($tags = array(), $where = null) /* Pre-processing, $tags is passed by reference and may be modified here */ $this->_write_pre_process($tags, 'UPDATE'); + $tags = static::_write_preprocess($tags); if ($set = $this->_set($tags, isset($where))) { |