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

This commit is contained in:
Loz Calver 2016-05-10 11:34:44 +01:00
parent 32f0a637b4
commit d1df67d308
No known key found for this signature in database
GPG Key ID: 7701A92D5D6E8976
2 changed files with 35 additions and 6 deletions

View File

@ -542,7 +542,7 @@ class SQLSelect extends SQLConditionalExpression {
$countQuery->setFrom(array('(' . $clone->sql($innerParameters) . ') all_distinct')); $countQuery->setFrom(array('(' . $clone->sql($innerParameters) . ') all_distinct'));
$sql = $countQuery->sql($parameters); // $parameters should be empty $sql = $countQuery->sql($parameters); // $parameters should be empty
$result = DB::prepared_query($sql, $innerParameters); $result = DB::prepared_query($sql, $innerParameters);
return $result->value(); return (int)$result->value();
} else { } else {
$clone->setSelect(array("count(*)")); $clone->setSelect(array("count(*)"));
} }
@ -551,7 +551,7 @@ class SQLSelect extends SQLConditionalExpression {
} }
$clone->setGroupBy(array()); $clone->setGroupBy(array());
return $clone->execute()->value(); return (int)$clone->execute()->value();
} }
/** /**
@ -594,7 +594,7 @@ class SQLSelect extends SQLConditionalExpression {
$clone->orderby = null; $clone->orderby = null;
$clone->groupby = 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 there's a limit set, then that limit is going to heavily affect the count
if($this->limit) { if($this->limit) {
if($this->limit['limit'] !== null && $count >= ($this->limit['start'] + $this->limit['limit'])) { if($this->limit['limit'] !== null && $count >= ($this->limit['start'] + $this->limit['limit'])) {

View File

@ -30,14 +30,43 @@ class SQLQueryTest extends SapphireTest {
//basic counting //basic counting
$qry = SQLQueryTest_DO::get()->dataQuery()->getFinalisedQuery(); $qry = SQLQueryTest_DO::get()->dataQuery()->getFinalisedQuery();
$qry->setGroupBy('Common'); $qry->setGroupBy('"Common"');
$ids = $this->allFixtureIDs('SQLQueryTest_DO'); $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` //test with `having`
if (DB::get_conn() instanceof MySQLDatabase) { if (DB::get_conn() instanceof MySQLDatabase) {
$qry->setHaving('"Date" > 2012-02-01'); $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);
} }
} }