Skip to content

Commit

Permalink
FIX Ensure consistent behaviour with repeat iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 15, 2022
1 parent 62ee637 commit e140c37
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/ORM/Connect/MySQLQuery.php
Expand Up @@ -63,6 +63,11 @@ public function getIterator()
}
yield $data;
}
// Check for the method first since $this->handle is a mixed type
if (method_exists($this->handle, 'data_seek')) {
// Reset so the query can be iterated over again
$this->handle->data_seek(0);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/php/ORM/DataListTest.php
Expand Up @@ -42,7 +42,6 @@ public static function getExtraDataObjects()
);
}


public function testFilterDataObjectByCreatedDate()
{
// create an object to test with
Expand Down
55 changes: 55 additions & 0 deletions tests/php/ORM/DatabaseTest.php
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\MSSQL\MSSQLDatabase;
use SilverStripe\Dev\SapphireTest;
use Exception;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\ORM\Tests\DatabaseTest\MyObject;

/**
Expand Down Expand Up @@ -293,4 +294,58 @@ public function testRepeatedIteration()
$this->assertEquals($inputData, $query->map());
$this->assertEquals($inputData, $query->map());
}

/**
* Test that repeated abstracted iteration of a query returns all records.
*/
public function testRepeatedIterationUsingAbstraction()
{
$inputData = ['one', 'two', 'three', 'four'];

foreach ($inputData as $i => $text) {
$x = new MyObject();
$x->MyField = $text;
$x->MyInt = $i;
$x->write();
}

$select = SQLSelect::create(['"MyInt"', '"MyField"'], '"DatabaseTest_MyObject"', orderby: ['"MyInt"']);

$this->assertEquals($inputData, $select->execute()->map());
$this->assertEquals($inputData, $select->execute()->map());
}

/**
* Test that stopping iteration part-way through produces predictable results
* on a subsequent iteration.
* This test is here to ensure consistency between implementations (e.g. mysql vs postgres, etc)
*/
public function testRepeatedPartialIteration()
{
$inputData = ['one', 'two', 'three', 'four'];

foreach ($inputData as $i => $text) {
$x = new MyObject();
$x->MyField = $text;
$x->MyInt = $i;
$x->write();
}

$query = DB::query('SELECT "MyInt", "MyField" FROM "DatabaseTest_MyObject" ORDER BY "MyInt"');

$i = 0;
foreach ($query as $record) {
$this->assertEquals($inputData[$i], $record['MyField']);
$i++;
if ($i > 1) {
break;
}
}

// Continue from where we left off, since we're using a Generator
foreach ($query as $record) {
$this->assertEquals($inputData[$i], $record['MyField']);
$i++;
}
}
}

0 comments on commit e140c37

Please sign in to comment.