-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: specify next release add unique constraint fix values collision
- Loading branch information
Showing
5 changed files
with
163 additions
and
9 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
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,94 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Formal\AccessLayer\Query\Constraint; | ||
|
||
use Formal\AccessLayer\Table\{ | ||
Name, | ||
Column, | ||
}; | ||
use Innmind\Immutable\{ | ||
Maybe, | ||
Sequence, | ||
Str, | ||
Monoid\Concat, | ||
}; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class Unique | ||
{ | ||
private Column\Name $column; | ||
/** @var Sequence<Column\Name> */ | ||
private Sequence $columns; | ||
/** @var Maybe<non-empty-string> */ | ||
private Maybe $name; | ||
|
||
/** | ||
* @param Sequence<Column\Name> $columns | ||
* @param Maybe<non-empty-string> $name | ||
*/ | ||
private function __construct( | ||
Column\Name $column, | ||
Sequence $columns, | ||
Maybe $name, | ||
) { | ||
$this->column = $column; | ||
$this->columns = $columns; | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @psalm-pure | ||
* @no-named-arguments | ||
*/ | ||
public static function of( | ||
Column\Name $column, | ||
Column\Name ...$columns, | ||
): self { | ||
/** @var Maybe<non-empty-string> */ | ||
$name = Maybe::nothing(); | ||
|
||
return new self($column, Sequence::of(...$columns), $name); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $name | ||
*/ | ||
public function named(string $name): self | ||
{ | ||
return new self( | ||
$this->column, | ||
$this->columns, | ||
Maybe::just($name), | ||
); | ||
} | ||
|
||
/** | ||
* @return non-empty-string | ||
*/ | ||
public function sql(): string | ||
{ | ||
$columns = $this | ||
->columns | ||
->map(static fn($column) => ', '.$column->sql()) | ||
->map(Str::of(...)) | ||
->fold(new Concat) | ||
->toString(); | ||
|
||
return $this->name->match( | ||
fn($name) => \sprintf( | ||
'CONSTRAINT UC_%s UNIQUE (%s%s)', | ||
$name, | ||
$this->column->sql(), | ||
$columns, | ||
), | ||
fn() => \sprintf( | ||
'UNIQUE (%s%s)', | ||
$this->column->sql(), | ||
$columns, | ||
), | ||
); | ||
} | ||
} |
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