Skip to content

Commit

Permalink
Merge 0c31b00 into ee96dd6
Browse files Browse the repository at this point in the history
  • Loading branch information
courtney-miles committed Aug 26, 2018
2 parents ee96dd6 + 0c31b00 commit 1ee4bf3
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 193 deletions.
74 changes: 74 additions & 0 deletions src/Load/DatabaseLoader/AbstractBatchStmt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Author: Courtney Miles
* Date: 26/08/18
* Time: 9:07 AM
*/

namespace MilesAsylum\Slurp\Load\DatabaseLoader;

use MilesAsylum\Slurp\Load\DatabaseLoader\Exception\MissingValueException;

abstract class AbstractBatchStmt implements BatchStmtInterface
{
/**
* @var \PDO
*/
protected $pdo;

/**
* @var string
*/
protected $table;

/**
* @var array
*/
protected $columns;

public function __construct(\PDO $pdo, string $table, array $columns)
{
$this->pdo = $pdo;
$this->table = $table;
$this->columns = $columns;
}

protected function ensureColumnMatch($rowValues): void
{
$missingValues = array_keys(
array_diff_key(array_flip($this->columns), $rowValues)
);

if (count($missingValues)) {
throw new MissingValueException(
sprintf(
'The supplied row is missing values for %s.',
implode(',', $missingValues)
)
);
}
}

protected function convertRowCollectionToParams(array $rowCollection):array
{
$params = [];

foreach ($rowCollection as $row) {
$this->ensureColumnMatch($row);
$params = array_merge($params, $this->convertRowToParams($row));
}

return $params;
}

protected function convertRowToParams($row):array
{
$params = [];

foreach ($this->columns as $col) {
$params[] = $row[$col];
}

return $params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace MilesAsylum\Slurp\Load\DatabaseLoader;

class InsertUpdateSql
class BatchInsUpdQueryFactory
{
public function createSql($table, array $columns, $batchSize = 1)
public function createQuery($table, array $columns, $batchSize = 1)
{
$colsStr = '`' . implode('`, `', $columns) . '`';
$valueStr = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
Expand Down
53 changes: 53 additions & 0 deletions src/Load/DatabaseLoader/BatchInsUpdStmt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Author: Courtney Miles
* Date: 26/08/18
* Time: 8:53 AM
*/

namespace MilesAsylum\Slurp\Load\DatabaseLoader;

class BatchInsUpdStmt extends AbstractBatchStmt
{
/**
* @var BatchInsUpdQueryFactory
*/
private $queryFactory;

/**
* @var \PDOStatement[]
*/
private $preparedBatchStmts = [];

public function __construct(\PDO $pdo, string $table, array $columns, BatchInsUpdQueryFactory $queryFactory)
{
parent::__construct($pdo, $table, $columns);
$this->queryFactory = $queryFactory;
}

/**
* @param array[] $rows
*/
public function write(array $rows)
{
$this->getPreparedBatchStmt(count($rows))
->execute(
$this->convertRowCollectionToParams($rows)
);
}

protected function getPreparedBatchStmt($count): \PDOStatement
{
if (!isset($this->preparedBatchStmts[$count])) {
$this->preparedBatchStmts[$count] = $this->pdo->prepare(
$this->queryFactory->createQuery(
$this->table,
$this->columns,
$count
)
);
}

return $this->preparedBatchStmts[$count];
}
}
13 changes: 13 additions & 0 deletions src/Load/DatabaseLoader/BatchStmtInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Author: Courtney Miles
* Date: 26/08/18
* Time: 8:52 AM
*/

namespace MilesAsylum\Slurp\Load\DatabaseLoader;

interface BatchStmtInterface
{
public function write(array $rows);
}
97 changes: 9 additions & 88 deletions src/Load/DatabaseLoader/DatabaseLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,38 @@

namespace MilesAsylum\Slurp\Load\DatabaseLoader;

use MilesAsylum\Slurp\Load\DatabaseLoader\Exception\ColumnMismatchException;
use MilesAsylum\Slurp\Load\LoaderInterface;

class DatabaseLoader implements LoaderInterface
{
/**
* @var \PDO
*/
private $pdo;

/**
* @var InsertUpdateSql
*/
private $queryFactory;

/**
* @var array
*/
private $columns;

/**
* @var \PDOStatement
*/
protected $batchStmt;

/**
* @var \PDOStatement
* @var int
*/
protected $singleStmt;
protected $batchSize;

protected $rowCollection = [];
/**
* @var string
* @var array[]
*/
private $table;
/**
* @var int
*/
private $batchSize;
protected $rowCollection = [];

public function __construct(
\PDO $pdo,
InsertUpdateSql $queryFactory,
string $table,
array $columns,
BatchStmtInterface $batchStmt,
int $batchSize = 100
) {
$this->pdo = $pdo;
$this->queryFactory = $queryFactory;
$this->table = $table;
$this->columns = $columns;
$this->batchSize = $batchSize;
$this->batchStmt = $this->pdo->prepare(
$this->queryFactory->createSql($this->table, $this->columns, $this->batchSize)
);
$this->singleStmt = $this->pdo->prepare(
$this->queryFactory->createSql($this->table, $this->columns)
);
$this->batchStmt = $batchStmt;
}

public function loadValues(array $values): void
{
$this->ensureColumnMatch($values);
$this->rowCollection[] = $values;

if (count($this->rowCollection) == $this->batchSize) {
if (count($this->rowCollection) >= $this->batchSize) {
$this->flush();
}
}
Expand All @@ -84,52 +50,7 @@ public function finalise(): void

protected function flush(): void
{
if (count($this->rowCollection) == $this->batchSize) {
$params = $this->convertRowCollectionToParams($this->rowCollection);

$this->batchStmt->execute($params);
$this->rowCollection = [];
} else {
foreach ($this->rowCollection as $row) {
$this->singleStmt->execute($this->convertRowToParams($row));
}
}
}

protected function ensureColumnMatch($rowValues): void
{
$expectedCount = count($this->columns);

if (count(array_intersect_key(array_flip($this->columns), $rowValues)) < $expectedCount) {
throw new ColumnMismatchException(
sprintf(
'The supplied row has values for %s where it is expected to have values for %s.',
implode(',', array_keys($rowValues)),
implode(',', $this->columns)
)
);
}
}

protected function convertRowCollectionToParams(array $rowCollection):array
{
$params = [];

foreach ($rowCollection as $row) {
$params = array_merge($params, $this->convertRowToParams($row));
}

return $params;
}

protected function convertRowToParams($row):array
{
$params = [];

foreach ($this->columns as $col) {
$params[] = $row[$col];
}

return $params;
$this->batchStmt->write($this->rowCollection);
$this->rowCollection = [];
}
}
14 changes: 0 additions & 14 deletions src/Load/DatabaseLoader/Exception/ColumnMismatchException.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Load/DatabaseLoader/Exception/MissingValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Author: Courtney Miles
* Date: 20/08/18
* Time: 10:57 PM
*/

namespace MilesAsylum\Slurp\Load\DatabaseLoader\Exception;

class MissingValueException extends \InvalidArgumentException implements ExceptionInterface
{
}
21 changes: 0 additions & 21 deletions src/Load/DatabaseLoader/LoaderFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

namespace MilesAsylum\Slurp\Tests\Slurp\Load\DatabaseLoader;

use MilesAsylum\Slurp\Load\DatabaseLoader\InsertUpdateSql;
use MilesAsylum\Slurp\Load\DatabaseLoader\BatchInsUpdQueryFactory;
use PHPUnit\Framework\TestCase;

class InsertUpdateSqlTest extends TestCase
class BatchInsUpdQueryFactoryTest extends TestCase
{
public function testCreateInsertQuery()
{
$queryFactory = new InsertUpdateSql();
$queryFactory = new BatchInsUpdQueryFactory();

$expectedInsSql = <<<SQL
INSERT INTO `foo` (`col_alpha`, `col_beta`)
Expand All @@ -23,13 +23,13 @@ public function testCreateInsertQuery()
SQL;
$this->assertSame(
$expectedInsSql,
$queryFactory->createSql('foo', ['col_alpha', 'col_beta'])
$queryFactory->createQuery('foo', ['col_alpha', 'col_beta'])
);
}

public function testCreateInsertQueryBatch()
{
$queryFactory = new InsertUpdateSql();
$queryFactory = new BatchInsUpdQueryFactory();

$expectedInsSql = <<<SQL
INSERT INTO `foo` (`col_alpha`, `col_beta`)
Expand All @@ -40,7 +40,7 @@ public function testCreateInsertQueryBatch()
SQL;
$this->assertSame(
$expectedInsSql,
$queryFactory->createSql('foo', ['col_alpha', 'col_beta'], 3)
$queryFactory->createQuery('foo', ['col_alpha', 'col_beta'], 3)
);
}
}
Loading

0 comments on commit 1ee4bf3

Please sign in to comment.