mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
8c2e3230c8
commit
76c63fe4a4
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
7
tests/model/SQLQueryTest.yml
Normal file
7
tests/model/SQLQueryTest.yml
Normal file
@ -0,0 +1,7 @@
|
||||
SQLQueryTest_DO:
|
||||
test1:
|
||||
Name: 'Object 1'
|
||||
Meta: 'Details 1'
|
||||
test2:
|
||||
Name: 'Object 2'
|
||||
Meta: 'Details 2'
|
Loading…
x
Reference in New Issue
Block a user