From ad88ec5f8c277e67c2683953fd1ba44f063e675f Mon Sep 17 00:00:00 2001 From: ucorina Date: Fri, 2 Mar 2012 15:17:18 +0000 Subject: [PATCH] Issue 1023 - Bug when paging query with outer join and distinct. git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@10299 201d5d3c-b55e-5fd7-737f-ddc643e51545 --- classes/db/DBMysql.class.php | 11 ++++++++++- classes/xml/xmlquery/DBParser.class.php | 1 + .../db/db/xml_query/mysql/MysqlSelectTest.php | 11 +++++++++++ .../xml_query/mysql/data/distinct_outer_join.xml | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/classes/db/db/xml_query/mysql/data/distinct_outer_join.xml diff --git a/classes/db/DBMysql.class.php b/classes/db/DBMysql.class.php index dafff7aba..70c59d30a 100644 --- a/classes/db/DBMysql.class.php +++ b/classes/db/DBMysql.class.php @@ -467,7 +467,16 @@ // Total count $temp_where = $queryObject->getWhereString(true, false); $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(); + if(strpos(strtolower($temp_select), "distinct") !== false) { + $count_query = sprintf('select %s %s %s', 'FROM ' . $queryObject->getFromString(), $temp_select, ($temp_where === '' ? '' : ' WHERE '. $temp_where)); + $uses_distinct = true; + } + + // 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); } diff --git a/classes/xml/xmlquery/DBParser.class.php b/classes/xml/xmlquery/DBParser.class.php index 54591f3a2..7fd962dcd 100644 --- a/classes/xml/xmlquery/DBParser.class.php +++ b/classes/xml/xmlquery/DBParser.class.php @@ -103,6 +103,7 @@ if($this->isStarFunction($column_name)){ return $column_name; } + if(strpos(strtolower($column_name), 'distinct') !== false) return $column_name; return $this->escapeColumn($column_name); } } diff --git a/tests/classes/db/db/xml_query/mysql/MysqlSelectTest.php b/tests/classes/db/db/xml_query/mysql/MysqlSelectTest.php index 296b9635d..53292ea33 100644 --- a/tests/classes/db/db/xml_query/mysql/MysqlSelectTest.php +++ b/tests/classes/db/db/xml_query/mysql/MysqlSelectTest.php @@ -229,4 +229,15 @@ order by `comments`.`list_order` asc limit 0, 5'; $this->_test($xml_file, $argsString, $expected); } + + + function test_distinct_outer_join(){ + $xml_file = _TEST_PATH_ . "db/xml_query/mysql/data/distinct_outer_join.xml"; + $argsString = '$args->site_srl = 0;'; + $expected = 'select distinct `modules`.`module_srl` as `module_site_srl` + from `xe_sites` as `sites` + left join `xe_modules` as `modules` on `modules`.`module_srl` = `sites`.`index_module_srl` + where `sites`.`site_srl` = 0'; + $this->_test($xml_file, $argsString, $expected); + } } \ No newline at end of file diff --git a/tests/classes/db/db/xml_query/mysql/data/distinct_outer_join.xml b/tests/classes/db/db/xml_query/mysql/data/distinct_outer_join.xml new file mode 100644 index 000000000..0c04f8a1a --- /dev/null +++ b/tests/classes/db/db/xml_query/mysql/data/distinct_outer_join.xml @@ -0,0 +1,16 @@ + + + +
+ + + +
+
+ + + + + + +