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
49 changes: 49 additions & 0 deletions src/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PicoMapper;

use Override;
use PicoDb\Database;
use PicoDb\Table;

Expand Down Expand Up @@ -96,6 +97,54 @@
return parent::count($column);
}

/**
* Fetches all values for a single column.
*
* @param string $column
* @return mixed
*/
#[Override]
public function findAllByColumn($column)
{
if ($this->definition->getDeletionTimestamp()) {

Check notice

Code scanning / Psalm

RiskyTruthyFalsyComparison Note

Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
$this->isNull($this->prefixTableNameTo($this->definition->getDeletionTimestamp()));
}

return parent::findAllByColumn($column);
}

/**
* Fetches a single column value from the first matching row.
*
* @param string $column
* @return string|bool
*/
#[Override]
public function findOneColumn($column)
{
if ($this->definition->getDeletionTimestamp()) {

Check notice

Code scanning / Psalm

RiskyTruthyFalsyComparison Note

Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
$this->isNull($this->prefixTableNameTo($this->definition->getDeletionTimestamp()));
}

return parent::findOneColumn($column);
}

/**
* Sums a column across all matching rows.
*
* @param string $column
* @return float
*/
#[Override]
public function sum(string $column)
{
if ($this->definition->getDeletionTimestamp()) {

Check notice

Code scanning / Psalm

RiskyTruthyFalsyComparison Note

Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
$this->isNull($this->prefixTableNameTo($this->definition->getDeletionTimestamp()));
}

return parent::sum($column);
}

/**
* Maps the provided array into the database.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,42 @@ public function testRemove()
);
}

public function testFindAllByColumnExcludesSoftDeletedRecords()
{
$this->getMapping()->eq('id', 1)->remove();

$names = $this->getMapping()->findAllByColumn('name');

$this->assertCount(1, $names);
$this->assertNotContains('John Doe', $names);
$this->assertContains('Jane Doe', $names);
}

public function testFindOneColumnExcludesSoftDeletedRecords()
{
$this->getMapping()->eq('id', 1)->remove();

$name = $this->getMapping()->eq('id', 1)->findOneColumn('name');

$this->assertFalse($name);
}

public function testSumExcludesSoftDeletedRecords()
{
$this->db->execute('CREATE TABLE scores (id INTEGER PRIMARY KEY, customer_id INTEGER, points INTEGER, date_deleted TEXT)');
$this->db->execute('INSERT INTO scores (id, customer_id, points) VALUES (1, 1, 100), (2, 2, 200)');

$score = (new Definition('scores'))
->withColumns('id', 'customer_id', 'points')
->withDeletionTimestamp('date_deleted');

$this->assertSame(300.0, (new Mapping($this->db, $score))->sum('points'));

(new Mapping($this->db, $score))->eq('id', 1)->remove();

$this->assertSame(200.0, (new Mapping($this->db, $score))->sum('points'));
}

public function testReadOnlyInsert()
{
$customer = [
Expand Down
Loading