diff options
| -rw-r--r-- | it_dbi.class | 51 | ||||
| -rwxr-xr-x | test/it_dbi.t | 32 | 
2 files changed, 76 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)))  	{ diff --git a/test/it_dbi.t b/test/it_dbi.t index 750841a..b09cdbb 100755 --- a/test/it_dbi.t +++ b/test/it_dbi.t @@ -422,3 +422,35 @@ is(allrecs(), '[{"ID":4,"foo":"C"}]', 'data after delete_untouched after replace  $record->upsert(['ID' => 4, 'foo' => "C"]);  is($record->delete_untouched(), [], 'delete_untouched after upsert without changes');  is(allrecs(), '[{"ID":4,"foo":"C"}]', 'data after delete_untouched after upsert without changes'); + +# +# Test _read_postprocess and _write_preprocess +# +class my_dbi_test extends it_dbi_test +{ + +static function _read_postprocess($data) +{ +	$data['x']--; +	return $data; +} + +static function _write_preprocess($data) +{ +	$data['x']++; +	return $data; +} + +} + +$record = new my_dbi_test; +$record->delete(['WHERE 1' ]); +$record->upsert(['ID' => 1, 'x' => 42, 'foo' => "a"]); +$record->read(1); +is($record->x, 42, 'value of x after reading with _read_postprocess'); +is($record->_data,  ["ID" => 1, "x" => 42, 'foo' => "a"], 'data after reading with _read_postprocess'); + +$record2 = new it_dbi_test; +$record2->read(1); +is($record2->x, 43, 'raw value of x after writing with _write_preprocess'); +is($record2->_data, ["ID" => 1, "x" => 43, 'foo' => "a"], 'raw data after writing with _write_preprocess');  |