diff options
author | Nathan Gass | 2017-04-25 14:16:24 +0200 |
---|---|---|
committer | Nathan Gass | 2017-04-25 14:16:24 +0200 |
commit | c81def8b826f25adbd8ac53a89898f56f5552267 (patch) | |
tree | a9a8c0b123abec7721eaf33203b5b4cd0c13f9df | |
parent | f84360a9e435559cc7630fef6c6083f5b170a5f6 (diff) | |
download | itools-c81def8b826f25adbd8ac53a89898f56f5552267.tar.gz itools-c81def8b826f25adbd8ac53a89898f56f5552267.tar.bz2 itools-c81def8b826f25adbd8ac53a89898f56f5552267.zip |
extract function _from from select and use it in for update and delete statements, this enables JOIN and FROM keys for update and delete
-rw-r--r-- | it_dbi.class | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/it_dbi.class b/it_dbi.class index 9f6aa65..5159431 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -267,6 +267,19 @@ function _set($tags, $allfields = false) return $r ? 'SET '.implode(', ', $r) : ''; } +/** + * INTERNAL: construct SQL FROM clause of fields JOIN and FROM in query params + */ +function _from($params = null, $omit_from = false) { + $res = $this->_p['table']; + if (isset($params['JOIN']) || isset($params['FROM'])) # WARNING: this field gets abused for "tablename USE INDEX (fast2) + { + $res = trim($params['FROM'] . " " . $params['JOIN']); + unset($params['JOIN'], $params['FROM']); + } + return ($omit_from ? "" : "FROM ") . $res; +} + /** * Create an SQL query (the stuff after 'WHERE') according to an array of selection criteria. @@ -288,6 +301,7 @@ function _set($tags, $allfields = false) */ function _where($params = null, $dummy_link = null, $omit_where = false) { + unset($params['JOIN'], $params['FROM']); if (is_array($params) && (count($params) > 0)) { $query = ''; @@ -578,13 +592,6 @@ function select(/* $query = array|string, ... */) if ($what == "*" && $this->_fields['dyncols']) $what .= ", COLUMN_JSON(dyncols) AS _dyncols"; - $join = $this->_p['table']; - if (isset($query['JOIN']) || isset($query['FROM'])) # WARNING: this field gets abused for "tablename USE INDEX (fast2) - { - $join = trim($query['FROM'] . " " . $query['JOIN']); - unset($query['JOIN'], $query['FROM']); - } - unset($this->_found_rows); if (isset($query['CALC_FOUND_ROWS']) && $query['CALC_FOUND_ROWS']) { @@ -600,7 +607,7 @@ function select(/* $query = array|string, ... */) unset($query['NOFETCH']); $this->clear(false); - if ($this->_result = $this->query($sql = "SELECT $what FROM $join " . $this->_where($query, $this->_link))) + if ($this->_result = $this->query($sql = "SELECT $what " . $this->_from($query) . " " . $this->_where($query, $this->_link))) { $result = $this->_p['unbuffered'] ? true : mysqli_num_rows($this->_result); @@ -754,7 +761,7 @@ function update($tags = array(), $where = null) if (!isset($where)) $where = array($this->_p['keyfield'] => $this->_data[$this->_p['keyfield']]); - if ($result = $this->query("UPDATE {$this->_p['table']} $set " . $this->_where($where, $this->_link))) + if ($result = $this->query("UPDATE " . $this->_from($where, true) . " $set " . $this->_where($where, $this->_link))) { $result = mysqli_affected_rows($this->_link); @@ -787,7 +794,7 @@ function delete($query = null) $this->clear(); } - if ($query && $this->query("DELETE FROM {$this->_p['table']} " . $this->_where($query, $this->_link))) + if ($query && $this->query("DELETE " . $this->_from($query) . " " . $this->_where($query, $this->_link))) $result = mysqli_affected_rows($this->_link); return $result; |