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

View File

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