Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
},
"require": {
"php": "^5.3",
"eldnp/export": "^0.1.2",
"ext-pdo": "*"
"ext-pdo": "*",
"eldnp/export.map": "^0.2.1"
},
"require-dev": {
"phpunit/PHPUnit": "^4.8",
Expand Down
22 changes: 5 additions & 17 deletions lib/PaginatedDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

use Eldnp\Export\DataSource\Pdo\Exception\LogicException;
use Eldnp\Export\DataSource\Pdo\Exception\RuntimeException;
use Eldnp\Export\DataSourceInterface;
use Eldnp\Export\Map\AbstractMapDataSource;

/**
* Class PaginatedDataSource
*
* @package Eldnp\Export\DataSource\Pdo
*/
class PaginatedDataSource implements DataSourceInterface
class PaginatedDataSource extends AbstractMapDataSource
{
/**
* @var \PDO
Expand All @@ -58,13 +58,6 @@ class PaginatedDataSource implements DataSourceInterface
*/
private $batchSize;

/**
* @see constants \PDO::FETCH_...
*
* @var int|null
*/
private $fetchStyle;

/**
* @var \PDOStatement
*/
Expand Down Expand Up @@ -98,16 +91,14 @@ class PaginatedDataSource implements DataSourceInterface
* @param string $leftPlaceholder
* @param string $rightPlaceholder
* @param int $batchSize
* @param int|null $fetchStyle
*/
public function __construct(\PDO $pdo, $query, $leftPlaceholder, $rightPlaceholder, $batchSize, $fetchStyle = null)
public function __construct(\PDO $pdo, $query, $leftPlaceholder, $rightPlaceholder, $batchSize)
{
$this->pdo = $pdo;
$this->query = $query;
$this->leftPlaceholder = $leftPlaceholder;
$this->rightPlaceholder = $rightPlaceholder;
$this->setBatchSize($batchSize);
$this->fetchStyle = $fetchStyle;
}

/**
Expand Down Expand Up @@ -169,10 +160,7 @@ private function buildNextStatement()
return $statement;
}

/**
* @inheritdoc
*/
public function current()
protected function currentMap()
{
return $this->currentRawData;
}
Expand All @@ -183,7 +171,7 @@ public function current()
public function next()
{
$allowNextStatement = true;
while (false === $this->currentRawData = $this->currentStatement->fetch($this->fetchStyle)) {
while (false === $this->currentRawData = $this->currentStatement->fetch(\PDO::FETCH_ASSOC)) {
if (!$allowNextStatement) {
return;
}
Expand Down
22 changes: 8 additions & 14 deletions lib/PdoStatementDataSource.php → lib/StatementDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,20 @@
namespace Eldnp\Export\DataSource\Pdo;

use Eldnp\Export\DataSource\Pdo\Exception\LogicException;
use Eldnp\Export\DataSourceInterface;
use Eldnp\Export\Map\AbstractMapDataSource;

/**
* Class PdoStatementDataSource
*
* @package Eldnp\Export\DataSource\Pdo
*/
class PdoStatementDataSource implements DataSourceInterface
class StatementDataSource extends AbstractMapDataSource
{
/**
* @var \PDOStatement
*/
private $statement;

/**
* @see constants \PDO::FETCH_...
*
* @var int|null
*/
private $fetchStyle;

/**
* @var mixed|false
*/
Expand All @@ -63,15 +56,16 @@ class PdoStatementDataSource implements DataSourceInterface
* PdoStatementDataSource constructor.
*
* @param \PDOStatement $statement
* @param int|null $fetchStyle
*/
public function __construct(\PDOStatement $statement, $fetchStyle = null)
public function __construct(\PDOStatement $statement)
{
$this->statement = $statement;
$this->fetchStyle = $fetchStyle;
}

public function current()
/**
* @inheritdoc
*/
public function currentMap()
{
return $this->rawData;
}
Expand All @@ -81,7 +75,7 @@ public function current()
*/
public function next()
{
$this->rawData = $this->statement->fetch($this->fetchStyle);
$this->rawData = $this->statement->fetch(\PDO::FETCH_ASSOC);
}

/**
Expand Down
91 changes: 91 additions & 0 deletions test/FixturePdoFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/*
* This file is part of Eldnp/export.data-source.pdo.
*
* Eldnp/export.data-source.pdo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Eldnp/export.data-source.pdo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Eldnp/export.data-source.pdo. If not, see <http://www.gnu.org/licenses/>.
*
* @see https://github.com/eldnp/export.data-source.pdo for the canonical source repository
* @copyright Copyright (c) 2017 Oleg Verevskoy <verevskoy@gmail.com>
* @license https://github.com/eldnp/export.data-source.pdo/blob/master/LICENSE GNU GENERAL PUBLIC LICENSE Version 3
*/

namespace EldnpTest\Export\DataSource\Pdo;

/**
* Class FixturePdoFactory
*
* @package EldnpTest\Export\DataSource\Pdo
*/
class FixturePdoFactory
{
/**
* @param \PDO $pdo
* @param string $query
*
* @throws \RuntimeException
*/
private static function executeQuery(\PDO $pdo, $query)
{
if (false === $pdo->exec($query)) {
throw new \RuntimeException(sprintf(
'An error occurred while running the request. Error: %s. Query: %s',
implode(', ', $pdo->errorInfo()),
$query
));
}
}

/**
* @param \PDO $pdo
*/
private static function createScheme(\PDO $pdo)
{
self::executeQuery($pdo, <<< SQL
create table awesome_table
(
id INT not null primary key,
field_one INT not null,
field_two INT
)
SQL
);
}

/**
* @param \PDO $pdo
* @param int $numRows
*/
private static function populateAwesomeTable(\PDO $pdo, $numRows)
{
for ($i = 1; $i <= $numRows; $i++) {
$query = 'insert into awesome_table (id, field_one, field_two) values '
. sprintf('(%d, %d, %s)', $i, rand(0, $numRows), rand(0, 10) > 3 ? rand(0, $numRows) : 'null')
;
self::executeQuery($pdo, $query);
}
}

/**
* @param int $numRows
*
* @return \PDO
*/
public static function factory($numRows)
{
$pdo = new \PDO("sqlite::memory:");
self::createScheme($pdo);
self::populateAwesomeTable($pdo, $numRows);
return $pdo;
}
}
54 changes: 0 additions & 54 deletions test/Generator.php

This file was deleted.

35 changes: 1 addition & 34 deletions test/PaginatedDataSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,9 @@ class PaginatedDataSourceTest extends TestCase
*/
private $pdo;

/**
* @param string $query
*
* @throws \RuntimeException
*/
private function executeQuery($query)
{
if (false === $this->pdo->exec($query)) {
throw new \RuntimeException(sprintf(
'An error occurred while running the request. Error: %s. Query: %s',
implode(', ', $this->pdo->errorInfo()),
$query
));
}
}

protected function setUp()
{
parent::setUp();
$this->pdo = new \PDO("sqlite::memory:");
$this->executeQuery(<<< SQL
create table awesome_table
(
id INT not null primary key,
field_one INT not null,
field_two INT
)
SQL
);
$maxRows = self::TEST_ROWS_COUNT;
for ($i = 1; $i <= $maxRows; $i++) {
$this->executeQuery(
'insert into awesome_table (id, field_one, field_two) values ' .
sprintf('(%d, %d, %s)', $i, rand(0, $maxRows), rand(0, 10) > 3 ? rand(0, $maxRows) : 'null')
);
}
$this->pdo = FixturePdoFactory::factory(self::TEST_ROWS_COUNT);
}

private function buildDataSource(
Expand Down
Loading