Use the query's iterator instead fetchAll() on CSV export

fixes #12723
This commit is contained in:
Eric Lippmann 2016-10-11 09:53:21 +02:00
parent 14cb499c1b
commit 529ba8ed8a
2 changed files with 13 additions and 15 deletions

View File

@ -3,15 +3,17 @@
namespace Icinga\File;
use Traversable;
class Csv
{
protected $query;
protected function __construct() {}
public static function fromQuery($query)
public static function fromQuery(Traversable $query)
{
$csv = new Csv();
$csv = new static();
$csv->query = $query;
return $csv;
}
@ -26,7 +28,7 @@ class Csv
{
$first = true;
$csv = '';
foreach ($this->query->fetchAll() as $row) {
foreach ($this->query as $row) {
if ($first) {
$csv .= implode(',', array_keys((array) $row)) . "\r\n";
$first = false;

View File

@ -3,7 +3,7 @@
namespace Tests\Icinga\File;
use Mockery;
use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\File\Csv;
use Icinga\Test\BaseTestCase;
@ -11,19 +11,15 @@ class CsvTest extends BaseTestCase
{
public function testWhetherValidCsvIsRendered()
{
$queryMock = Mockery::mock(
'Icinga\Data\SimpleQuery',
array(
'fetchAll' => array(
array('col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3', 'col4' => 'val4'),
array('col1' => 'val5', 'col2' => 'val6', 'col3' => 'val7', 'col4' => 'val8')
)
)
);
$csv = Csv::fromQuery($queryMock);
$data = new ArrayDatasource(array(
array('col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3', 'col4' => 'val4'),
array('col1' => 'val5', 'col2' => 'val6', 'col3' => 'val7', 'col4' => 'val8')
));
$csv = Csv::fromQuery($data->select());
$this->assertEquals(
join(
implode(
"\r\n",
array(
'col1,col2,col3,col4',