Skip to content

Commit

Permalink
do not perform update/delete without primary key. fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M. Jones committed Sep 7, 2020
1 parent 5fe5987 commit 2d2a2be
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public static function primaryValueMissing(string $col)
return new Exception($message);
}

public static function primaryValueChanged(string $col) : Exception
{
$message = "Primary key value for '$col' changed";
return new Exception($message);
}

public static function cannotPerformWithoutPrimaryKey(string $operation, string $table) : Exception
{
$message = "Cannot {$operation} on table '$table' without primary key.";
return new Exception($message);
}

public static function tableAlreadySet() : Exception
{
return new Exception("Table already set.");
Expand Down
11 changes: 9 additions & 2 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ public function updateRowPrepare(Row $row) : Update
$update = $this->update();
foreach (static::PRIMARY_KEY as $primaryCol) {
if (array_key_exists($primaryCol, $diff)) {
$message = "Primary key value for '$primaryCol' changed";
throw new Exception($message);
throw Exception::primaryValueChanged($primaryCol);
}
$update->where("{$primaryCol} = ", $row->$primaryCol);
unset($diff[$primaryCol]);
Expand All @@ -199,6 +198,10 @@ public function updateRowPerform(Row $row, Update $update) : ?PDOStatement
return null;
}

if (empty(static::PRIMARY_KEY)) {
throw Exception::cannotPerformWithoutPrimaryKey('update row', static::NAME);
}

$pdoStatement = $update->perform();

$rowCount = $pdoStatement->rowCount();
Expand Down Expand Up @@ -245,6 +248,10 @@ public function deleteRowPerform(Row $row, Delete $delete) : ?PDOStatement
return null;
}

if (empty(static::PRIMARY_KEY)) {
throw Exception::cannotPerformWithoutPrimaryKey('delete row', static::NAME);
}

$pdoStatement = $delete->perform();

$rowCount = $pdoStatement->rowCount();
Expand Down
34 changes: 34 additions & 0 deletions tests/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Atlas\Testing\CompositeDataSourceFixture;
use Atlas\Testing\DataSource\Employee\EmployeeRow;
use Atlas\Testing\DataSource\Employee\EmployeeTable;
use Atlas\Testing\DataSource\Nopkey\NopkeyRow;
use Atlas\Testing\DataSource\Nopkey\NopkeyTable;
use Atlas\Testing\DataSourceFixture;
use PDO;
use PDOStatement;
Expand Down Expand Up @@ -389,4 +391,36 @@ protected function silenceErrors()
$conn = $this->table->getWriteConnection();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
}

public function testUpdateRow_noPrimaryKey()
{
$table = $this->tableLocator->get(NopkeyTable::CLASS);
$row = $table->newRow([
'name' => 'Bolivar Shagnasty',
'email' => '[email protected]',
]);
$table->insertRow($row);

$row->email = '[email protected]';
$this->expectException(Exception::CLASS);
$this->expectExceptionMessage("Cannot update row on table 'nopkeys' without primary key.");

$table->updateRow($row);
}

public function testDeleteRow_noPrimaryKey()
{
$table = $this->tableLocator->get(NopkeyTable::CLASS);
$row = $table->newRow([
'name' => 'Bolivar Shagnasty',
'email' => '[email protected]',
]);
$table->insertRow($row);

$row->email = '[email protected]';
$this->expectException(Exception::CLASS);
$this->expectExceptionMessage("Cannot delete row on table 'nopkeys' without primary key.");

$table->deleteRow($row);
}
}

0 comments on commit 2d2a2be

Please sign in to comment.