isSphinx, streamResult

master
Vitaliy Filippov 2018-09-10 14:26:14 +03:00
parent 796abb1837
commit 26917884cc
1 changed files with 11 additions and 12 deletions

View File

@ -4,8 +4,8 @@
* Very stable interface for MySQL - object-oriented at last :) * Very stable interface for MySQL - object-oriented at last :)
* Select builder is inspired by MediaWiki's one. * Select builder is inspired by MediaWiki's one.
* Also usable for querying SphinxQL. * Also usable for querying SphinxQL.
* Version: 2016-09-02 * Version: 2018-09-10
* (c) Vitaliy Filippov, 2012-2016 * (c) Vitaliy Filippov, 2012-2018
*/ */
if (!defined('MS_HASH')) if (!defined('MS_HASH'))
@ -39,7 +39,7 @@ if (!interface_exists('Database'))
class DatabaseMysql implements Database class DatabaseMysql implements Database
{ {
var $host, $port, $socket, $username, $password, $dbname; var $host, $port, $socket, $username, $password, $dbname, $isSphinx;
var $tableNames = array(); var $tableNames = array();
var $init = array(); var $init = array();
@ -94,7 +94,8 @@ class DatabaseMysql implements Database
{ {
$this->$k = $options[$k]; $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 // READ COMMITTED is more consistent for average usage
$this->init[] = 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED'; $this->init[] = 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED';
@ -170,7 +171,7 @@ class DatabaseMysql implements Database
return "'" . $this->link->real_escape_string($value) . "'"; return "'" . $this->link->real_escape_string($value) . "'";
} }
function query($sql, $fetchMode = MYSQLI_STORE_RESULT) function query($sql, $streamResult = false)
{ {
if (!$this->link) if (!$this->link)
$this->connect(); $this->connect();
@ -181,7 +182,7 @@ class DatabaseMysql implements Database
{ {
$begin = explode(' ', microtime(), 2); $begin = explode(' ', microtime(), 2);
} }
$r = $this->link->query($sql, $fetchMode); $r = $this->link->query($sql, $streamResult ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
if (!$r) if (!$r)
$r = $this->check_reconnect('query', [ $sql, $fetchMode ]); $r = $this->check_reconnect('query', [ $sql, $fetchMode ]);
if ($this->queryLogFile) if ($this->queryLogFile)
@ -380,7 +381,6 @@ class DatabaseMysql implements Database
{ {
if (!$v) 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"); throw new DatabaseMysqlException("Error: empty array for '$k IN (...)', don't know what to do");
} }
else else
@ -425,7 +425,7 @@ class DatabaseMysql implements Database
else else
$wh[] = "$k IS NULL"; $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 // Sphinx supports neither brackets nor OR operator as of 2.0.6-release O_o
$where = join(' AND ', $wh); $where = join(' AND ', $wh);
@ -502,13 +502,13 @@ class DatabaseMysql implements Database
{ {
$sql .= " ORDER BY ".$this->order_option($options['ORDER BY']); $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 // Sphinx Search extension
$sql .= " WITHIN GROUP ORDER BY ".$this->order_option($options['WITHIN GROUP ORDER BY']); $sql .= " WITHIN GROUP ORDER BY ".$this->order_option($options['WITHIN GROUP ORDER BY']);
} }
$sql .= $this->limit_option($options); $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 // Sphinx Search extension
$opt = array(); $opt = array();
@ -754,9 +754,8 @@ class DatabaseMysql implements Database
$rs[] = $this->quote($r[$k]); $rs[] = $this->quote($r[$k]);
$r = "(".implode(",", $rs).")"; $r = "(".implode(",", $rs).")";
} }
$sphinx = !$this->username && !$this->password && !$this->dbname;
foreach ($key as &$k) foreach ($key as &$k)
if (strpos($k, '`') === false && (!$sphinx || $k !== 'id')) if (strpos($k, '`') === false && (!$this->isSphinx || $k !== 'id'))
$k = $this->quoteId($k); $k = $this->quoteId($k);
$sql = ($action == "REPLACE" ? "REPLACE" : "INSERT" . ($action == "IGNORE" ? " IGNORE" : "")). $sql = ($action == "REPLACE" ? "REPLACE" : "INSERT" . ($action == "IGNORE" ? " IGNORE" : "")).
" INTO $table (".implode(",",$key).") VALUES ".implode(",",$rows); " INTO $table (".implode(",",$key).") VALUES ".implode(",",$rows);