-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
353 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
src/Load/DatabaseLoader/Exception/ColumnMismatchException.php
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/Load/DatabaseLoader/Exception/MissingValueException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.