From d1df67d3089a51c0db63fbaed869738453bee482 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Tue, 10 May 2016 11:34:44 +0100 Subject: [PATCH] FIX: SQLSelect count methods now cast to int (fixes #5498) --- model/queries/SQLSelect.php | 6 +++--- tests/model/SQLQueryTest.php | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/model/queries/SQLSelect.php b/model/queries/SQLSelect.php index 7af2c32a5..9eb4ca065 100644 --- a/model/queries/SQLSelect.php +++ b/model/queries/SQLSelect.php @@ -542,7 +542,7 @@ class SQLSelect extends SQLConditionalExpression { $countQuery->setFrom(array('(' . $clone->sql($innerParameters) . ') all_distinct')); $sql = $countQuery->sql($parameters); // $parameters should be empty $result = DB::prepared_query($sql, $innerParameters); - return $result->value(); + return (int)$result->value(); } else { $clone->setSelect(array("count(*)")); } @@ -551,7 +551,7 @@ class SQLSelect extends SQLConditionalExpression { } $clone->setGroupBy(array()); - return $clone->execute()->value(); + return (int)$clone->execute()->value(); } /** @@ -594,7 +594,7 @@ class SQLSelect extends SQLConditionalExpression { $clone->orderby = null; $clone->groupby = null; - $count = $clone->execute()->value(); + $count = (int)$clone->execute()->value(); // If there's a limit set, then that limit is going to heavily affect the count if($this->limit) { if($this->limit['limit'] !== null && $count >= ($this->limit['start'] + $this->limit['limit'])) { diff --git a/tests/model/SQLQueryTest.php b/tests/model/SQLQueryTest.php index 6449fa2fd..1b1a173bc 100755 --- a/tests/model/SQLQueryTest.php +++ b/tests/model/SQLQueryTest.php @@ -30,14 +30,43 @@ class SQLQueryTest extends SapphireTest { //basic counting $qry = SQLQueryTest_DO::get()->dataQuery()->getFinalisedQuery(); - $qry->setGroupBy('Common'); + $qry->setGroupBy('"Common"'); $ids = $this->allFixtureIDs('SQLQueryTest_DO'); - $this->assertEquals(count($ids), $qry->count('"SQLQueryTest_DO"."ID"')); + + $count = $qry->count('"SQLQueryTest_DO"."ID"'); + $this->assertEquals(count($ids), $count); + $this->assertInternalType("int", $count); //test with `having` if (DB::get_conn() instanceof MySQLDatabase) { $qry->setHaving('"Date" > 2012-02-01'); - $this->assertEquals(1, $qry->count('"SQLQueryTest_DO"."ID"')); + $count = $qry->count('"SQLQueryTest_DO"."ID"'); + $this->assertEquals(1, $count); + $this->assertInternalType("int", $count); + } + } + + public function testUnlimitedRowCount() { + //basic counting + $qry = SQLQueryTest_DO::get()->dataQuery()->getFinalisedQuery(); + $ids = $this->allFixtureIDs('SQLQueryTest_DO'); + $qry->setLimit(1); + + $count = $qry->unlimitedRowCount('"SQLQueryTest_DO"."ID"'); + $this->assertEquals(count($ids), $count); + $this->assertInternalType("int", $count); + + // Test without column - SQLSelect has different logic for this + $count = $qry->unlimitedRowCount(); + $this->assertEquals(2, $count); + $this->assertInternalType("int", $count); + + //test with `having` + if (DB::get_conn() instanceof MySQLDatabase) { + $qry->setHaving('"Date" > 2012-02-01'); + $count = $qry->unlimitedRowCount('"SQLQueryTest_DO"."ID"'); + $this->assertEquals(1, $count); + $this->assertInternalType("int", $count); } }