Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
- distinctOn method for Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Sep 2, 2020
1 parent 5a0df79 commit ef94310
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
======================

v2.7.17 (02.09.2020)
-----
- added the ability to modify the database logger context by @Alexsisukin
- added distinctOn method to Postgres Select Query

v2.7.16 (28.08.2020)
-----
- fixes bug with invalid transaction level when transaction can't be started
Expand Down
6 changes: 5 additions & 1 deletion src/Driver/CompilerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ protected function hashInsertQuery(QueryParameters $params, array $tokens): stri
protected function hashSelectQuery(QueryParameters $params, array $tokens): string
{
// stable part of hash
$hash = 's_' . $tokens['forUpdate'] . '_' . $tokens['distinct'];
if (is_array($tokens['distinct']) && isset($tokens['distinct']['on'])) {
$hash = 's_' . $tokens['forUpdate'] . '_on_' . $tokens['distinct']['on'];
} else {
$hash = 's_' . $tokens['forUpdate'] . '_' . $tokens['distinct'];
}

foreach ($tokens['from'] as $table) {
if ($table instanceof SelectQuery) {
Expand Down
4 changes: 4 additions & 0 deletions src/Driver/Postgres/PostgresCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ protected function distinct(QueryParameters $params, Quoter $q, $distinct): stri
return '';
}

if (is_array($distinct) && isset($distinct['on'])) {
return sprintf('DISTINCT ON (%s)', $this->name($params, $q, $distinct['on']));
}

if (is_string($distinct)) {
return sprintf('DISTINCT (%s)', $this->name($params, $q, $distinct));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/Postgres/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Spiral\Database\Driver\Driver;
use Spiral\Database\Driver\Postgres\Query\PostgresInsertQuery;
use Spiral\Database\Driver\Postgres\Query\PostgresSelectQuery;
use Spiral\Database\Exception\DriverException;
use Spiral\Database\Exception\StatementException;
use Spiral\Database\Query\DeleteQuery;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __construct(array $options)
new PostgresHandler(),
new PostgresCompiler('""'),
new QueryBuilder(
new SelectQuery(),
new PostgresSelectQuery(),
new PostgresInsertQuery(),
new UpdateQuery(),
new DeleteQuery()
Expand Down
31 changes: 31 additions & 0 deletions src/Driver/Postgres/Query/PostgresSelectQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Database\Driver\Postgres\Query;

use Spiral\Database\Injection\FragmentInterface;
use Spiral\Database\Query\SelectQuery;

class PostgresSelectQuery extends SelectQuery
{
/**
* Apply distinct ON to the query.
*
* @param string|FragmentInterface $distinctOn
* @return self|$this
*/
public function distinctOn($distinctOn): SelectQuery
{
$this->distinct = ['on' => $distinctOn];

return $this;
}
}
38 changes: 38 additions & 0 deletions tests/Database/Driver/Postgres/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,42 @@ public function testGetColumns(): void

$this->assertSame($expected, $columns);
}

public function testSelectDistinct(): void
{
$table = $this->database->table('table');
$this->assertSame(0, $table->count());

$table->insertMultiple(
['name', 'value'],
[
['Anton', 10],
['Anton', 20],
['Bob', 15],
['Charlie', 10]
]
);

$data = $table->select('name', 'value')->distinct('name')->fetchAll();
$this->assertCount(4, $data);
}

public function testSelectDistinctOn(): void
{
$table = $this->database->table('table');
$this->assertSame(0, $table->count());

$table->insertMultiple(
['name', 'value'],
[
['Anton', 10],
['Anton', 20],
['Bob', 15],
['Charlie', 10]
]
);

$data = $table->select('name', 'value')->distinctOn('name')->fetchAll();
$this->assertCount(3, $data);
}
}
10 changes: 5 additions & 5 deletions tests/Database/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public function testAggregationByPass(): void
);

$this->assertSame(4, $table->count());
$this->assertSame(100, (int)$table->sum('value'));
$this->assertSame(100, (int) $table->sum('value'));
}

public function testAggregationMinByPass(): void
Expand All @@ -303,7 +303,7 @@ public function testAggregationMinByPass(): void
);

$this->assertSame(4, $table->count());
$this->assertSame(10, (int)$table->min('value'));
$this->assertSame(10, (int) $table->min('value'));
}

public function testAggregationMaxByPass(): void
Expand All @@ -322,7 +322,7 @@ public function testAggregationMaxByPass(): void
);

$this->assertSame(4, $table->count());
$this->assertSame(40, (int)$table->max('value'));
$this->assertSame(40, (int) $table->max('value'));
}

public function testAggregationAvgByPass(): void
Expand All @@ -341,7 +341,7 @@ public function testAggregationAvgByPass(): void
);

$this->assertSame(4, $table->count());
$this->assertSame(25, (int)$table->avg('value'));
$this->assertSame(25, (int) $table->avg('value'));
}

public function testAggregationAvgByPassFloat(): void
Expand All @@ -360,7 +360,7 @@ public function testAggregationAvgByPassFloat(): void
);

$this->assertSame(4, $table->count());
$this->assertSame(13.75, (float)$table->avg('value'));
$this->assertSame(13.75, (float) $table->avg('value'));
}

public function testDeleteWithWhere(): void
Expand Down

0 comments on commit ef94310

Please sign in to comment.