From 53cc7ab05afb41a80b710fa1f4454a8e4cb372ab Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Wed, 24 Jun 2020 14:45:27 +0200 Subject: Add static and pure replacements _read_postprocess/_write_preprocess for deprecated _read_post_process/_write_pre_process --- it_dbi.class | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) (limited to 'it_dbi.class') diff --git a/it_dbi.class b/it_dbi.class index 2d4be89..4064465 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -486,11 +486,35 @@ 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 */ @@ -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))) { -- cgit v1.2.3