Issue 1891: groupby bug

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@10648 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2012-05-02 14:48:41 +00:00
parent 40a2b98b6c
commit d9edd9b205
3 changed files with 561 additions and 532 deletions

View file

@ -657,7 +657,7 @@
$query = $this->getInsertSql($queryObject, $with_values); $query = $this->getInsertSql($queryObject, $with_values);
if(is_a($query, 'Object')) return; if(is_a($query, 'Object')) return;
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):''; $query .= (__DEBUG_QUERY__&1 && $queryObject->queryID)?sprintf (' '.$this->comment_syntax, $queryObject->queryID):'';
$result = $this->_query ($query); $result = $this->_query ($query);
if ($result && !$this->transaction_started) { if ($result && !$this->transaction_started) {
@ -730,7 +730,7 @@
if (is_a($query, 'Object')) if (is_a($query, 'Object'))
return; return;
$query .= (__DEBUG_QUERY__ & 1 && $queryObject->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $queryObject->queryID) ? sprintf(' ' . $this->comment_syntax, $queryObject->queryID) : '';
$result = $this->_query($query, $connection); $result = $this->_query($query, $connection);
if ($this->isError()) if ($this->isError())
@ -764,11 +764,24 @@
// Total count // Total count
$temp_where = $queryObject->getWhereString($with_values, false); $temp_where = $queryObject->getWhereString($with_values, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString($with_values), ($temp_where === '' ? '' : ' WHERE '. $temp_where)); $count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString($with_values), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
if ($queryObject->getGroupByString() != '') {
// Check for distinct query and if found update count query structure
$temp_select = $queryObject->getSelectString($with_values);
$uses_distinct = strpos(strtolower($temp_select), "distinct") !== false;
$uses_groupby = $queryObject->getGroupByString() != '';
if($uses_distinct || $uses_groupby) {
$count_query = sprintf('select %s %s %s %s'
, $temp_select
, 'FROM ' . $queryObject->getFromString($with_values)
, ($temp_where === '' ? '' : ' WHERE '. $temp_where)
, ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '')
);
// If query uses grouping or distinct, count from original select
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }
$count_query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):''; $count_query .= (__DEBUG_QUERY__&1 && $queryObject->queryID)?sprintf (' '.$this->comment_syntax, $queryObject->queryID):'';
$result = $this->_query($count_query, $connection); $result = $this->_query($count_query, $connection);
$count_output = $this->_fetch($result); $count_output = $this->_fetch($result);
$total_count = (int)(isset($count_output->count) ? $count_output->count : NULL); $total_count = (int)(isset($count_output->count) ? $count_output->count : NULL);

View file

@ -565,11 +565,24 @@
// Total count // Total count
$temp_where = $queryObject->getWhereString(true, false); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE ' . $temp_where)); $count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE ' . $temp_where));
if ($queryObject->getGroupByString() != '') {
// Check for distinct query and if found update count query structure
$temp_select = $queryObject->getSelectString(true);
$uses_distinct = strpos(strtolower($temp_select), "distinct") !== false;
$uses_groupby = $queryObject->getGroupByString() != '';
if($uses_distinct || $uses_groupby) {
$count_query = sprintf('select %s %s %s %s'
, $temp_select
, 'FROM ' . $queryObject->getFromString(true)
, ($temp_where === '' ? '' : ' WHERE '. $temp_where)
, ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '')
);
// If query uses grouping or distinct, count from original select
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }
$count_query .= (__DEBUG_QUERY__ & 1 && $output->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $count_query .= (__DEBUG_QUERY__ & 1 && $queryObject->queryID) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : '';
$this->param = $queryObject->getArguments(); $this->param = $queryObject->getArguments();
$result_count = $this->_query($count_query, $connection); $result_count = $this->_query($count_query, $connection);
$count_output = $this->_fetch($result_count); $count_output = $this->_fetch($result_count);

View file

@ -1,5 +1,5 @@
<?php <?php
/** /**
* @class DBMysql * @class DBMysql
* @author NHN (developers@xpressengine.com) * @author NHN (developers@xpressengine.com)
* @brief Class to use MySQL DBMS * @brief Class to use MySQL DBMS
@ -10,7 +10,7 @@
* Does not use prepared statements, since mysql driver does not support them * Does not use prepared statements, since mysql driver does not support them
**/ **/
class DBMysql extends DB { class DBMysql extends DB {
/** /**
* @brief Connection information for Mysql DB * @brief Connection information for Mysql DB
@ -410,7 +410,7 @@
$query = $this->getSelectSql($queryObject, $with_values); $query = $this->getSelectSql($queryObject, $with_values);
if (is_a($query, 'Object')) if (is_a($query, 'Object'))
return; return;
$query .= (__DEBUG_QUERY__ & 1 && $queryObject->query_id) ? sprintf(' ' . $this->comment_syntax, $this->query_id) : ''; $query .= (__DEBUG_QUERY__ & 1 && $queryObject->queryID) ? sprintf(' ' . $this->comment_syntax, $queryObject->queryID) : '';
$result = $this->_query($query, $connection); $result = $this->_query($query, $connection);
if ($this->isError()) if ($this->isError())
@ -465,18 +465,21 @@
// Check for distinct query and if found update count query structure // Check for distinct query and if found update count query structure
$temp_select = $queryObject->getSelectString($with_values); $temp_select = $queryObject->getSelectString($with_values);
$uses_distinct = false; $uses_distinct = strpos(strtolower($temp_select), "distinct") !== false;
if(strpos(strtolower($temp_select), "distinct") !== false) { $uses_groupby = $queryObject->getGroupByString() != '';
$count_query = sprintf('select %s %s %s', 'FROM ' . $queryObject->getFromString($with_values), $temp_select, ($temp_where === '' ? '' : ' WHERE '. $temp_where)); if($uses_distinct || $uses_groupby) {
$uses_distinct = true; $count_query = sprintf('select %s %s %s %s'
} , $temp_select
, 'FROM ' . $queryObject->getFromString($with_values)
, ($temp_where === '' ? '' : ' WHERE '. $temp_where)
, ($uses_groupby ? ' GROUP BY ' . $queryObject->getGroupByString() : '')
);
// If query uses grouping or distinct, count from original select // If query uses grouping or distinct, count from original select
if ($queryObject->getGroupByString() != '' || $uses_distinct) {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }
$count_query .= (__DEBUG_QUERY__&1 && $queryObject->query_id)?sprintf (' '.$this->comment_syntax, $this->query_id):''; $count_query .= (__DEBUG_QUERY__&1 && $queryObject->queryID)?sprintf (' '.$this->comment_syntax, $queryObject->queryID):'';
$result_count = $this->_query($count_query, $connection); $result_count = $this->_query($count_query, $connection);
$count_output = $this->_fetch($result_count); $count_output = $this->_fetch($result_count);
$total_count = (int)(isset($count_output->count) ? $count_output->count : NULL); $total_count = (int)(isset($count_output->count) ? $count_output->count : NULL);
@ -549,7 +552,7 @@
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }
} }
return new DBMysql; return new DBMysql;
?> ?>