BUG Fixed issue with SQLQuery::lastRow crashing on empty set. Added test cases for lastRow and firstRow.

Quoted table / column names to make test cases work in postgres

BUG Fixed issue with SQLQuery::lastRow crashing on empty set. Added test cases for lastRow and firstRow.

Quoted table / column names to make test cases work in postgres

Merge branch '3.0-sqlquery-lastrow-fix' of github.com:tractorcow/sapphire into 3.0-sqlquery-lastrow-fix
This commit is contained in:
Damian Mooyman 2012-11-16 13:27:51 +13:00
parent 8c2e3230c8
commit 76c63fe4a4
3 changed files with 78 additions and 2 deletions

View File

@ -1080,7 +1080,10 @@ class SQLQuery {
public function lastRow() {
$query = clone $this;
$offset = $this->limit ? $this->limit['start'] : 0;
$query->setLimit(1, $this->count() + $offset - 1);
// Limit index to start in case of empty results
$index = max($this->count() + $offset - 1, 0);
$query->setLimit(1, $index);
return $query;
}

View File

@ -2,7 +2,7 @@
class SQLQueryTest extends SapphireTest {
static $fixture_file = null;
public static $fixture_file = 'SQLQueryTest.yml';
protected $extraDataObjects = array(
'SQLQueryTest_DO',
@ -301,6 +301,72 @@ class SQLQueryTest extends SapphireTest {
$this->assertEquals("SELECT * FROM MyTable WHERE (Monkey = 'Chimp' OR Color = 'Brown')",$query->sql());
}
public function testSelectFirst() {
// Test first from sequence
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('"Name"');
$result = $query->firstRow()->execute();
$this->assertCount(1, $result);
foreach($result as $row) {
$this->assertEquals('Object 1', $row['Name']);
}
// Test first from empty sequence
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('"Name"');
$query->setWhere(array("\"Name\" = 'Nonexistent Object'"));
$result = $query->firstRow()->execute();
$this->assertCount(0, $result);
// Test that given the last item, the 'first' in this list matches the last
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('"Name"');
$query->setLimit(1, 1);
$result = $query->firstRow()->execute();
$this->assertCount(1, $result);
foreach($result as $row) {
$this->assertEquals('Object 2', $row['Name']);
}
}
public function testSelectLast() {
// Test last in sequence
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('"Name"');
$result = $query->lastRow()->execute();
$this->assertCount(1, $result);
foreach($result as $row) {
$this->assertEquals('Object 2', $row['Name']);
}
// Test last from empty sequence
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('"Name"');
$query->setWhere(array("\"Name\" = 'Nonexistent Object'"));
$result = $query->lastRow()->execute();
$this->assertCount(0, $result);
// Test that given the first item, the 'last' in this list matches the first
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
$query->setOrderBy('"Name"');
$query->setLimit(1);
$result = $query->lastRow()->execute();
$this->assertCount(1, $result);
foreach($result as $row) {
$this->assertEquals('Object 1', $row['Name']);
}
}
}
class SQLQueryTest_DO extends DataObject implements TestOnly {

View File

@ -0,0 +1,7 @@
SQLQueryTest_DO:
test1:
Name: 'Object 1'
Meta: 'Details 1'
test2:
Name: 'Object 2'
Meta: 'Details 2'