summaryrefslogtreecommitdiff
path: root/it_dbi.class
diff options
context:
space:
mode:
authorNathan Gass2020-09-01 15:41:49 +0200
committerNathan Gass2020-09-02 15:13:55 +0200
commit696bf01dbfb4097580c9b41e7249424d687e44c9 (patch)
treee2d667b4240a88993bcf75e07465ff9c7d9bc095 /it_dbi.class
parentd11c2f66bf09b05a1f3764f7397ffd01086edd15 (diff)
downloaditools-696bf01dbfb4097580c9b41e7249424d687e44c9.tar.gz
itools-696bf01dbfb4097580c9b41e7249424d687e44c9.tar.bz2
itools-696bf01dbfb4097580c9b41e7249424d687e44c9.zip
extract mysql specific json code into overridable functions
Diffstat (limited to 'it_dbi.class')
-rw-r--r--it_dbi.class42
1 files changed, 31 insertions, 11 deletions
diff --git a/it_dbi.class b/it_dbi.class
index 0dfde04..217b572 100644
--- a/it_dbi.class
+++ b/it_dbi.class
@@ -258,18 +258,18 @@ function _expressions($tags, $force = false)
if (!$this->_fields[$f] && $this->_fields['dyncols'])
{
if (substr($field, 0, 1) === "-")
- $newdyns[] = "'\$.$f', " . $value;
+ $newdyns[$f] = $value;
else if ($force || isset($value) && isset($dyndata[$f]) ? strval($value) !== strval($dyndata[$f]) : $value !== $dyndata[$f] || !array_key_exists($f, $dyndata))
{
if (is_null($value))
- $deldyns[] = "'\$.$f'";
- if (is_int($value))
- $newdyns[] = "'\$.$f', $value";
+ $deldyns[] = $f;
+ else if (is_int($value))
+ $newdyns[$f] = $value;
else
- $newdyns[] = "'\$.$f', " . $this->escape_string($value);
+ $newdyns[$f] = $this->escape_string($value);
}
- $alldyns[] = $this->escape_string($f) . ", " . (substr($field, 0, 1) === "-" || is_int($value) ? $value : $this->escape_string($value));
+ $alldyns[$f] = (substr($field, 0, 1) === "-" || is_int($value) ? $value : $this->escape_string($value));
$dyndata[$f] = $value;
}
else if (substr($field, 0, 1) === '-') # Unquoted value (always added)
@@ -281,14 +281,14 @@ function _expressions($tags, $force = false)
if ($alldyns)
{
if ($force == "insert") # INSERT/REPLACE
- $result['dyncols'] = "JSON_OBJECT(" . join(", ", (array)$alldyns) . ")";
+ $result['dyncols'] = $this->_json_object($alldyns);
else if ($newdyns || $deldyns)
{
- $source = $this->_dyndata ? 'dyncols' : '"{}"';
+ $source = $this->_dyndata ? 'dyncols' : $this->escape_string('{}');
if ($newdyns)
- $source = "JSON_SET($source, " . join(', ', $newdyns) . ')';
+ $source = $this->_json_set($source, $newdyns);
if ($deldyns)
- $source = "JSON_REMOVE($source, " . join(', ', $deldyns) . ')';
+ $source = $this->_json_remove($source, $deldyns);
$result['dyncols'] = $source;
}
}
@@ -412,7 +412,7 @@ function _where($params)
}
if ($dyncols_enabled && $this->_fields['dyncols'] && !$this->_fields[$field] && strpos($field, '(') === false)
- $field = "JSON_EXTRACT(dyncols, " . $this->escape_string('$.' . $field) . ")";
+ $field = $this->_json_extract('dyncols', $field);
switch ($op)
{
@@ -1119,6 +1119,26 @@ function _query($query, $p)
return $result;
}
+function _json_extract($col, $field)
+{
+ return "JSON_EXTRACT($col, " . $this->escape_string('$.' . $field) . ")";
+}
+
+function _json_object($tags)
+{
+ return "JSON_OBJECT(" . implode(', ', it::map(fn ($f, $v) => "'$f', $v", (array)$tags)) . ")";
+}
+
+function _json_set($source, $tags)
+{
+ return "JSON_SET($source, " . implode(", ", it::map(fn ($f, $v) => "'\$.$f', $v", (array)$tags)) . ')';
+}
+
+function _json_remove($source, $fields)
+{
+ return "JSON_REMOVE($source, " . implode(", ", it::map(fn ($dummy, $f) => "'\$.$f'", (array)$fields)) . ')';
+}
+
function _fetch_assoc($res)
{
return mysqli_fetch_assoc($res);