summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Schneider2020-06-24 14:45:27 +0200
committerChristian Schneider2020-06-24 14:45:27 +0200
commit53cc7ab05afb41a80b710fa1f4454a8e4cb372ab (patch)
treef2bbb606c02a3a6f248e40d58009cab5dd1c98a2
parent5c26a69b002b7f66b95ae4a0da0d7ed0b9490fb1 (diff)
downloaditools-53cc7ab05afb41a80b710fa1f4454a8e4cb372ab.tar.gz
itools-53cc7ab05afb41a80b710fa1f4454a8e4cb372ab.tar.bz2
itools-53cc7ab05afb41a80b710fa1f4454a8e4cb372ab.zip
Add static and pure replacements _read_postprocess/_write_preprocess for deprecated _read_post_process/_write_pre_process
-rw-r--r--it_dbi.class51
-rwxr-xr-xtest/it_dbi.t32
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');