From 26917884ccacbe0478629a96120dcbba9cbc1775 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Mon, 10 Sep 2018 14:26:14 +0300 Subject: [PATCH] isSphinx, streamResult --- DatabaseMysql.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/DatabaseMysql.php b/DatabaseMysql.php index b9d44dd..47d94ad 100644 --- a/DatabaseMysql.php +++ b/DatabaseMysql.php @@ -4,8 +4,8 @@ * Very stable interface for MySQL - object-oriented at last :) * Select builder is inspired by MediaWiki's one. * Also usable for querying SphinxQL. - * Version: 2016-09-02 - * (c) Vitaliy Filippov, 2012-2016 + * Version: 2018-09-10 + * (c) Vitaliy Filippov, 2012-2018 */ if (!defined('MS_HASH')) @@ -39,7 +39,7 @@ if (!interface_exists('Database')) class DatabaseMysql implements Database { - var $host, $port, $socket, $username, $password, $dbname; + var $host, $port, $socket, $username, $password, $dbname, $isSphinx; var $tableNames = array(); var $init = array(); @@ -94,7 +94,8 @@ class DatabaseMysql implements Database { $this->$k = $options[$k]; } - if ($this->username || $this->dbname) // skip for Sphinx + $this->isSphinx = !$this->username && !$this->password && !$this->dbname; + if (!$this->isSphinx) { // READ COMMITTED is more consistent for average usage $this->init[] = 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED'; @@ -170,7 +171,7 @@ class DatabaseMysql implements Database return "'" . $this->link->real_escape_string($value) . "'"; } - function query($sql, $fetchMode = MYSQLI_STORE_RESULT) + function query($sql, $streamResult = false) { if (!$this->link) $this->connect(); @@ -181,7 +182,7 @@ class DatabaseMysql implements Database { $begin = explode(' ', microtime(), 2); } - $r = $this->link->query($sql, $fetchMode); + $r = $this->link->query($sql, $streamResult ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT); if (!$r) $r = $this->check_reconnect('query', [ $sql, $fetchMode ]); if ($this->queryLogFile) @@ -380,7 +381,6 @@ class DatabaseMysql implements Database { if (!$v) { - // FIXME: It seems we should return empty result in that case throw new DatabaseMysqlException("Error: empty array for '$k IN (...)', don't know what to do"); } else @@ -425,7 +425,7 @@ class DatabaseMysql implements Database else $wh[] = "$k IS NULL"; } - if (!$this->username && !$this->password && !$this->dbname) + if ($this->isSphinx) { // Sphinx supports neither brackets nor OR operator as of 2.0.6-release O_o $where = join(' AND ', $wh); @@ -502,13 +502,13 @@ class DatabaseMysql implements Database { $sql .= " ORDER BY ".$this->order_option($options['ORDER BY']); } - if (!empty($options['WITHIN GROUP ORDER BY']) && $options['WITHIN GROUP GROUP BY'] !== '0') + if ($this->isSphinx && !empty($options['WITHIN GROUP ORDER BY']) && $options['WITHIN GROUP GROUP BY'] !== '0') { // Sphinx Search extension $sql .= " WITHIN GROUP ORDER BY ".$this->order_option($options['WITHIN GROUP ORDER BY']); } $sql .= $this->limit_option($options); - if (!empty($options['FIELD_WEIGHTS']) || !empty($options['RANKER'])) + if ($this->isSphinx && (!empty($options['FIELD_WEIGHTS']) || !empty($options['RANKER']))) { // Sphinx Search extension $opt = array(); @@ -754,9 +754,8 @@ class DatabaseMysql implements Database $rs[] = $this->quote($r[$k]); $r = "(".implode(",", $rs).")"; } - $sphinx = !$this->username && !$this->password && !$this->dbname; foreach ($key as &$k) - if (strpos($k, '`') === false && (!$sphinx || $k !== 'id')) + if (strpos($k, '`') === false && (!$this->isSphinx || $k !== 'id')) $k = $this->quoteId($k); $sql = ($action == "REPLACE" ? "REPLACE" : "INSERT" . ($action == "IGNORE" ? " IGNORE" : "")). " INTO $table (".implode(",",$key).") VALUES ".implode(",",$rows);