Skip to content

Commit

Permalink
Merge pull request #9098 from sminnee/fix-9097
Browse files Browse the repository at this point in the history
FIX: Repeated iteration should return all records
  • Loading branch information
Guy Marriott authored Jul 3, 2019
2 parents ad05099 + 4043669 commit d0b4f61
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/ORM/Connect/MySQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public function __destruct()
public function seek($row)
{
if (is_object($this->handle)) {
// Fix for https://github.com/silverstripe/silverstripe-framework/issues/9097 without breaking the seek() API
$this->handle->data_seek($row);
return $this->nextRecord();
$result = $this->nextRecord();
$this->handle->data_seek($row);
return $result;
}
return null;
}
Expand Down
11 changes: 5 additions & 6 deletions src/ORM/Connect/MySQLStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ public function __destruct()
public function seek($row)
{
$this->rowNum = $row - 1;

// Fix for https://github.com/silverstripe/silverstripe-framework/issues/9097 without breaking the seek() API
$this->statement->data_seek($row);
$result = $this->next();
$this->statement->data_seek($row);
return $this->next();
return $result;
}

public function numRecords()
Expand All @@ -132,9 +136,4 @@ public function nextRecord()
}
return $row;
}

public function rewind()
{
return $this->seek(0);
}
}
5 changes: 2 additions & 3 deletions src/ORM/Connect/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,15 @@ public function table()
* Iterator function implementation. Rewind the iterator to the first item and return it.
* Makes use of {@link seek()} and {@link numRecords()}, takes care of the plumbing.
*
* @return array
* @return void
*/
public function rewind()
{
if ($this->queryHasBegun && $this->numRecords() > 0) {
$this->queryHasBegun = false;
$this->currentRecord = null;
return $this->seek(0);
$this->seek(0);
}
return null;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/php/ORM/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,25 @@ public function testFieldTypes()
$result = DB::query('SELECT TRUE')->first();
$this->assertInternalType('int', reset($result));
}

/**
* Test that repeated iteration of a query returns all records.
* See https://github.com/silverstripe/silverstripe-framework/issues/9097
*/
public function testRepeatedIteration()
{
$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"');

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

0 comments on commit d0b4f61

Please sign in to comment.