Skip to content
Permalink
Browse files

FIX: SQLSelect count methods now cast to int (fixes #5498)

  • Loading branch information...
kinglozzer committed May 10, 2016
1 parent 32f0a63 commit d1df67d3089a51c0db63fbaed869738453bee482
Showing with 35 additions and 6 deletions.
  1. +3 −3 model/queries/SQLSelect.php
  2. +32 −3 tests/model/SQLQueryTest.php
@@ -542,7 +542,7 @@ public function unlimitedRowCount($column = null) {
$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 @@ public function unlimitedRowCount($column = null) {
}
$clone->setGroupBy(array());
return $clone->execute()->value();
return (int)$clone->execute()->value();
}
/**
@@ -594,7 +594,7 @@ public function count($column = null) {
$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'])) {
@@ -30,14 +30,43 @@ public function testCount() {
//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');
$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');
$this->assertEquals(1, $qry->count('"SQLQueryTest_DO"."ID"'));
$count = $qry->unlimitedRowCount('"SQLQueryTest_DO"."ID"');
$this->assertEquals(1, $count);
$this->assertInternalType("int", $count);
}
}

0 comments on commit d1df67d

Please sign in to comment.
You can’t perform that action at this time.