-
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.
Provide an option to run a DML query (i.e a DELETE) before committing…
… the staged data.
- Loading branch information
1 parent
466b4bd
commit 5b38e58
Showing
7 changed files
with
226 additions
and
6 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
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,20 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 22/09/18 | ||
* Time: 6:26 AM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Load\DatabaseLoader; | ||
|
||
|
||
/** | ||
* This class can been used to run a DML statement immediately prior to committing the loaded records. | ||
*/ | ||
interface PreCommitDmlInterface | ||
{ | ||
/** | ||
* @return int The number of affected rows. | ||
*/ | ||
public function execute(): int; | ||
} |
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,67 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 21/09/18 | ||
* Time: 7:18 PM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Load\DatabaseLoader; | ||
|
||
|
||
class PreCommitSimpleDelete implements PreCommitDmlInterface | ||
{ | ||
/** | ||
* @var \PDO | ||
*/ | ||
private $pdo; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $table; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $conditions; | ||
|
||
public function __construct(\PDO $pdo, string $table, array $conditions = []) | ||
{ | ||
$this->pdo = $pdo; | ||
$this->table = $table; | ||
$this->conditions = $conditions; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute(): int | ||
{ | ||
$conditionsStr = null; | ||
$qryParams = []; | ||
|
||
foreach ($this->conditions as $col => $value) { | ||
$valuePlaceholder = $this->colNameToPlaceholder($col); | ||
$conditions[] = sprintf( | ||
'`%s` = %s', | ||
$col, | ||
$valuePlaceholder | ||
); | ||
$qryParams[$valuePlaceholder] = $value; | ||
} | ||
|
||
if (!empty($conditions)) { | ||
$conditionsStr = ' WHERE ' . implode(' AND ', $conditions); | ||
} | ||
|
||
$stmt = $this->pdo->prepare("DELETE FROM `{$this->table}`$conditionsStr"); | ||
$stmt->execute($qryParams); | ||
|
||
return $stmt->rowCount(); | ||
} | ||
|
||
protected function colNameToPlaceholder($colName) | ||
{ | ||
return ':' . str_replace([' '], ['_'], $colName); | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
tests/Slurp/Load/DatabaseLoader/PreCommitSimpleDeleteTest.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,85 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 22/09/18 | ||
* Time: 7:01 AM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Tests\Slurp\Load\DatabaseLoader; | ||
|
||
use MilesAsylum\Slurp\Load\DatabaseLoader\PreCommitSimpleDelete; | ||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PreCommitSimpleDeleteTest extends TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
|
||
/** | ||
* @var \PDO|MockInterface | ||
*/ | ||
protected $mockPdo; | ||
|
||
/** | ||
* @var \PDOStatement|MockInterface | ||
*/ | ||
protected $mockDelStmt; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->mockPdo = \Mockery::mock(\PDO::class); | ||
$this->mockDelStmt = \Mockery::mock(\PDOStatement::class); | ||
} | ||
|
||
public function testExecuteWithoutConditions() | ||
{ | ||
$affectedRows = 3; | ||
$table = 'my_tbl'; | ||
|
||
$expectedQry = <<<SQL | ||
DELETE FROM `my_tbl` | ||
SQL; | ||
|
||
$this->mockPdo->shouldReceive('prepare') | ||
->with($expectedQry) | ||
->andReturn($this->mockDelStmt) | ||
->once(); | ||
$this->mockDelStmt->shouldReceive('execute') | ||
->with([]) | ||
->once(); | ||
$this->mockDelStmt->shouldReceive('rowCount') | ||
->andReturn($affectedRows); | ||
|
||
$delete = new PreCommitSimpleDelete($this->mockPdo, $table); | ||
|
||
$this->assertSame($affectedRows, $delete->execute()); | ||
} | ||
|
||
public function testExecuteWithConditions() | ||
{ | ||
$affectedRows = 3; | ||
$table = 'my_tbl'; | ||
$conditions = ['col 1' => 123, 'col2' => 'abc']; | ||
|
||
$expectedQry = <<<SQL | ||
DELETE FROM `my_tbl` WHERE `col 1` = :col_1 AND `col2` = :col2 | ||
SQL; | ||
|
||
$this->mockPdo->shouldReceive('prepare') | ||
->with($expectedQry) | ||
->andReturn($this->mockDelStmt) | ||
->once(); | ||
$this->mockDelStmt->shouldReceive('execute') | ||
->with([':col_1' => 123, ':col2' => 'abc']) | ||
->once(); | ||
$this->mockDelStmt->shouldReceive('rowCount') | ||
->andReturn($affectedRows); | ||
|
||
$delete = new PreCommitSimpleDelete($this->mockPdo, $table, $conditions); | ||
|
||
$this->assertSame($affectedRows, $delete->execute()); | ||
} | ||
} |
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