diff options
author | Nathan Gass | 2020-08-31 14:25:35 +0200 |
---|---|---|
committer | Nathan Gass | 2020-08-31 14:25:35 +0200 |
commit | 08890fa8a186b5aede42cf558e4bb1634b66ff0a (patch) | |
tree | b620f234e2c1d03402f30928b6f34bd05aff98a5 | |
parent | ee6ddf4597115d922e24376e675990fb56ec9b26 (diff) | |
download | itools-08890fa8a186b5aede42cf558e4bb1634b66ff0a.tar.gz itools-08890fa8a186b5aede42cf558e4bb1634b66ff0a.tar.bz2 itools-08890fa8a186b5aede42cf558e4bb1634b66ff0a.zip |
use insert values instead of mysql speficif insert set
-rw-r--r-- | it_dbi.class | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/it_dbi.class b/it_dbi.class index bf75a98..d24a0fe 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -238,10 +238,10 @@ function _connect($p = array()) /** - * INTERNAL: construct SQL SET clause of changed values from tags array. + * INTERNAL: construct SQL expressions of changed values from tags array. * $force = must write all fields, dont try to optimize */ -function _set($tags, $force = false) +function _expressions($tags, $force = false) { $result = array(); $dyndata = $this->_dyndata; @@ -273,15 +273,15 @@ function _set($tags, $force = false) $dyndata[$f] = $value; } else if (substr($field, 0, 1) === '-') # Unquoted value (always added) - $result[] = substr($field, 1) . "=$value"; + $result[substr($field, 1)] = $value; else if ($force || (isset($value) && isset($this->_data[$field]) ? strval($value) !== strval($this->_data[$field]) : $value !== $this->_data[$field] || !array_key_exists($field, $this->_data))) - $result[] = "`$field`=" . (isset($value) ? $this->escape_string($value) : 'NULL'); + $result[$field] = isset($value) ? $this->escape_string($value) : 'NULL'; } if ($alldyns) { if ($force == "insert") # INSERT/REPLACE - $result[] = "dyncols = JSON_OBJECT(" . join(", ", (array)$alldyns) . ")"; + $result['dyncols'] = "JSON_OBJECT(" . join(", ", (array)$alldyns) . ")"; else if ($newdyns || $deldyns) { $source = $this->_dyndata ? 'dyncols' : '"{}"'; @@ -289,13 +289,39 @@ function _set($tags, $force = false) $source = "JSON_SET($source, " . join(', ', $newdyns) . ')'; if ($deldyns) $source = "JSON_REMOVE($source, " . join(', ', $deldyns) . ')'; - $result[] = "dyncols = $source"; + $result['dyncols'] = $source; } } $this->_writes += $result ? 1 : 0; - return $result ? 'SET ' . implode(', ', $result) : ''; + return $result; +} + +/** + * INTERNAL: construct SQL SET clause of changed values from tags array. + * $force = must write all fields, dont try to optimize + */ +function _set($tags, $force = false) +{ + $expressions = $this->_expressions($tags, $force); + + return $expressions ? 'SET ' . implode(', ', it::map(fn ($k, $v) => "`$k`=$v", $expressions)) : ''; +} + +/** + * INTERNAL: construct SQL VALUES clause of changed values from tags array. + * $force = must write all fields, dont try to optimize + */ +function _values($tags, $force = false) +{ + $expressions = $this->_expressions($tags, $force); + + $result = ''; + if ($expressions) + $result = '(' . implode(', ', array_keys($expressions)) . ') VALUES (' . implode(', ', array_values($expressions)) . ')'; + + return $result; } /** @@ -756,9 +782,9 @@ function insert($tags = array(), $command = "INSERT") if ($this->_p['randomid'] && !isset($tags[$this->_p['keyfield']])) $tags[$this->_p['keyfield']] = bin2hex(random_bytes(16)); - $set = $this->_set($tags, "insert"); + $values = $this->_values($tags, "insert"); - if ($result = $this->query($query = "$command INTO {$this->_p['table']} " . $set)) + if ($result = $this->query($query = "$command INTO {$this->_p['table']} " . $values)) { $id = ($this->_p['autoincrement'] && !isset($tags[$this->_p['keyfield']])) ? $this->_insertid : $tags[$this->_p['keyfield']]; if ($this->_p['keyfield'] && !$this->read($id) && $this->_p['safety']) |