summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Flatz2018-10-05 15:49:10 +0200
committerDavid Flatz2018-10-05 15:50:27 +0200
commitadadae917b981ec4cccc29951a0bf22edb8fe380 (patch)
treed67ff3bc3f1abad0455ace450686e4400985f9db
parent05d87bd756ab1cf392d50d3969c2c5586b643eb6 (diff)
downloaditools-adadae917b981ec4cccc29951a0bf22edb8fe380.tar.gz
itools-adadae917b981ec4cccc29951a0bf22edb8fe380.tar.bz2
itools-adadae917b981ec4cccc29951a0bf22edb8fe380.zip
fix addition of dynamic columns to record where dyncols is NULL
-rw-r--r--it_dbi.class4
-rwxr-xr-xtest/it_dbi.t5
2 files changed, 7 insertions, 2 deletions
diff --git a/it_dbi.class b/it_dbi.class
index cee004a..d9688cd 100644
--- a/it_dbi.class
+++ b/it_dbi.class
@@ -281,7 +281,7 @@ function _set($tags, $force = false)
if ($force == "insert") # INSERT/REPLACE
$result[] = "dyncols = JSON_OBJECT(" . join(", ", (array)$alldyns) . ")";
else if ($newdyns)
- $result[] = "dyncols = JSON_SET(dyncols, " . join(", ", $newdyns) .")";
+ $result[] = "dyncols = JSON_SET(" . ($this->_dyndata ? 'dyncols' : '"{}"') . ", " . join(", ", $newdyns) .")";
}
$this->_writes += $result ? 1 : 0;
@@ -682,7 +682,7 @@ function iterate()
foreach ((array)$this->_dyndata as $field => $dummy)
unset($this->$field);
- foreach (($t = $this->_data['dyncols']) ? ($this->_dyndata = (array)json_decode($t, true)) : [] as $field => $value)
+ foreach (($this->_dyndata = ($t = $this->_data['dyncols']) ? (array)json_decode($t, true) : []) as $field => $value)
$this->_data[$field] = $value;
unset($this->_data['dyncols']);
diff --git a/test/it_dbi.t b/test/it_dbi.t
index faac5a3..bf5d0f0 100755
--- a/test/it_dbi.t
+++ b/test/it_dbi.t
@@ -352,12 +352,17 @@ $r->update(['key4' => "val4'", 'key5' => "val5'"]); is($r->_writes, 1); is($r-
$r->delete(["WHERE 1"]);
$r->replace(['ID' => 1, 'key1' => "a"]);
$r->replace(['ID' => 2, 'key2' => "b"]);
+$r->replace(['ID' => 3]);
$r->clear(false);
$r->select("WHERE 1 ORDER BY ID");
$r->iterate();
is($r->key1, "a"); is($r->key2, null);
$r->iterate();
is($r->key1, null); is($r->key2, "b");
+$r->iterate();
+is($r->_dyndata, [], '_dyndata for record with empty dyncols should be empty');
+$r->update(['key3' => 'c']);
+is($r->key3, 'c', 'dynamic column for record with empty dyncols whould be correctly created');
# don't do dyncols stuff in where when we select form multiple tables (column names are unknown then)
is($r->_where(['FROM' => 'it_dbi_test AS a, it_dbi_test AS b', '-a.ID' => 'b.ID']), 'WHERE a.ID = b.ID', "don't do JSON_EXTRACT when we have multiple tables in FROM");