Skip to content
Draft
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
5 changes: 3 additions & 2 deletions src/Column/AbstractMultiRangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Pgsql\Expression\MultiRangeValue;
use Yiisoft\Db\Pgsql\Expression\RangeValueInterface;
use Yiisoft\Db\Schema\Column\AbstractColumn;
use Yiisoft\Db\Schema\Column\ColumnInterface;

Expand All @@ -15,7 +16,7 @@
use function is_string;

/**
* @template T of ExpressionInterface
* @template T of RangeValueInterface
*/
abstract class AbstractMultiRangeColumn extends AbstractColumn
{
Expand Down Expand Up @@ -53,7 +54,7 @@ public function dbTypecast(mixed $value): mixed
/**
* @inheritDoc
*
* @return ?ExpressionInterface[]
* @return ?RangeValueInterface[]
*
* @psalm-return ?T[]
*/
Expand Down
7 changes: 5 additions & 2 deletions src/Column/AbstractRangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Pgsql\Expression\RangeValueInterface;
use Yiisoft\Db\Schema\Column\AbstractColumn;
use Yiisoft\Db\Schema\Column\ColumnInterface;

Expand All @@ -16,7 +17,7 @@
use function sprintf;

/**
* @template T of ExpressionInterface
* @template T of RangeValueInterface
*/
abstract class AbstractRangeColumn extends AbstractColumn
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public function dbTypecast(mixed $value): mixed
/**
* @inheritDoc
*
* @return ?ExpressionInterface
* @return ?RangeValueInterface
*
* @psalm-return ?T
*/
Expand Down Expand Up @@ -88,6 +89,8 @@ abstract protected function getBoundColumn(): ColumnInterface;
/**
* @throws NotSupportedException
*
* @return RangeValueInterface
*
* @psalm-return T
*/
abstract protected function createRangeValue(
Expand Down
19 changes: 17 additions & 2 deletions src/Expression/DateRangeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
namespace Yiisoft\Db\Pgsql\Expression;

use DateTimeImmutable;
use Yiisoft\Db\Expression\ExpressionInterface;

final class DateRangeValue implements ExpressionInterface
/**
* @implements RangeValueInterface<DateTimeImmutable>
*/
final class DateRangeValue implements RangeValueInterface
{
public function __construct(
public readonly ?DateTimeImmutable $lower = null,
public readonly ?DateTimeImmutable $upper = null,
public readonly bool $includeLower = true,
public readonly bool $includeUpper = true,
) {}

public function getBounds(): array
{
$lower = $this->lower === null || $this->includeLower
? $this->lower
: $this->lower->modify('+1 day');

$upper = $this->upper === null || $this->includeUpper
? $this->upper
: $this->upper->modify('-1 day');

return [$lower, $upper];
}
}
20 changes: 17 additions & 3 deletions src/Expression/Int4RangeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@

namespace Yiisoft\Db\Pgsql\Expression;

use Yiisoft\Db\Expression\ExpressionInterface;

final class Int4RangeValue implements ExpressionInterface
/**
* @implements RangeValueInterface<int>
*/
final class Int4RangeValue implements RangeValueInterface
{
public function __construct(
public readonly ?int $lower = null,
public readonly ?int $upper = null,
public readonly bool $includeLower = true,
public readonly bool $includeUpper = true,
) {}

public function getBounds(): array
{
$lower = $this->lower === null || $this->includeLower
? $this->lower
: $this->lower + 1;

$upper = $this->upper === null || $this->includeUpper
? $this->upper
: $this->upper - 1;

return [$lower, $upper];
}
}
35 changes: 33 additions & 2 deletions src/Expression/Int8RangeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,45 @@

namespace Yiisoft\Db\Pgsql\Expression;

use Yiisoft\Db\Expression\ExpressionInterface;
use RuntimeException;

final class Int8RangeValue implements ExpressionInterface
use const PHP_INT_MAX;
use const PHP_INT_MIN;

/**
* @implements RangeValueInterface<int|string>
*/
final class Int8RangeValue implements RangeValueInterface
{
public function __construct(
public readonly int|string|null $lower = null,
public readonly int|string|null $upper = null,
public readonly bool $includeLower = true,
public readonly bool $includeUpper = true,
) {}

public function getBounds(): array
{
$lower = $this->lower === null || $this->includeLower
? $this->lower
: (
PHP_INT_MIN <= $this->lower && $this->lower < PHP_INT_MAX
Comment thread
vjik marked this conversation as resolved.
? (int) $this->lower + 1
: throw new RuntimeException(
'Lower bound cannot be determined from the excluded value of a bigint range.',
)
);

$upper = $this->upper === null || $this->includeUpper
? $this->upper
: (
PHP_INT_MIN < $this->upper && $this->upper <= PHP_INT_MAX
? (int) $this->upper - 1
: throw new RuntimeException(
'Upper bound cannot be determined from the excluded value of a bigint range.',
)
);

return [$lower, $upper];
}
}
24 changes: 22 additions & 2 deletions src/Expression/NumRangeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@

namespace Yiisoft\Db\Pgsql\Expression;

use Yiisoft\Db\Expression\ExpressionInterface;
use RuntimeException;

final class NumRangeValue implements ExpressionInterface
/**
* @implements RangeValueInterface<int|float>
*/
final class NumRangeValue implements RangeValueInterface
{
public function __construct(
public readonly int|float|null $lower = null,
public readonly int|float|null $upper = null,
public readonly bool $includeLower = true,
public readonly bool $includeUpper = true,
) {}

public function getBounds(): array
{
$lower = $this->lower === null || $this->includeLower
? $this->lower
: throw new RuntimeException(
'Lower bound cannot be determined from the excluded value of a numeric range.',
);

$upper = $this->upper === null || $this->includeUpper
? $this->upper
: throw new RuntimeException(
'Upper bound cannot be determined from the excluded value of a numeric range.',
);

return [$lower, $upper];
}
}
24 changes: 24 additions & 0 deletions src/Expression/RangeValueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Expression;

use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Pgsql\Column\AbstractMultiRangeColumn;
use Yiisoft\Db\Pgsql\Column\AbstractRangeColumn;

/**
* Represents a range value of {@see AbstractRangeColumn} and {@see AbstractMultiRangeColumn} column types.
*
* @template T
*/
interface RangeValueInterface extends ExpressionInterface

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this interface be used in the application? It seems we will always be working with a specific implementation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only required to limit return types of AbstractRangeColumn and AbstractMultiRangeColumn, currently using annotations, but in the next major release changing method signatures.
Also user-defined range types can realize it.

{
/**
* Returns the lower and upper bounds of a range, inclusive.
*
* @psalm-return array{0: ?T, 1: ?T}
*/
public function getBounds(): array;
}
19 changes: 17 additions & 2 deletions src/Expression/TsRangeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
namespace Yiisoft\Db\Pgsql\Expression;

use DateTimeImmutable;
use Yiisoft\Db\Expression\ExpressionInterface;

final class TsRangeValue implements ExpressionInterface
/**
* @implements RangeValueInterface<DateTimeImmutable>
*/
final class TsRangeValue implements RangeValueInterface
{
public function __construct(
public readonly ?DateTimeImmutable $lower = null,
public readonly ?DateTimeImmutable $upper = null,
public readonly bool $includeLower = true,
public readonly bool $includeUpper = true,
) {}

public function getBounds(): array
{
$lower = $this->lower === null || $this->includeLower
? $this->lower
: $this->lower->modify('+1 second');

$upper = $this->upper === null || $this->includeUpper
? $this->upper
: $this->upper->modify('-1 second');

return [$lower, $upper];
}
}
19 changes: 17 additions & 2 deletions src/Expression/TsTzRangeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
namespace Yiisoft\Db\Pgsql\Expression;

use DateTimeImmutable;
use Yiisoft\Db\Expression\ExpressionInterface;

final class TsTzRangeValue implements ExpressionInterface
/**
* @implements RangeValueInterface<DateTimeImmutable>
*/
final class TsTzRangeValue implements RangeValueInterface
{
public function __construct(
public readonly ?DateTimeImmutable $lower = null,
public readonly ?DateTimeImmutable $upper = null,
public readonly bool $includeLower = true,
public readonly bool $includeUpper = true,
) {}

public function getBounds(): array
{
$lower = $this->lower === null || $this->includeLower
? $this->lower
: $this->lower->modify('+1 second');

$upper = $this->upper === null || $this->includeUpper
? $this->upper
: $this->upper->modify('-1 second');

return [$lower, $upper];
}
}