Skip to content
Snippets Groups Projects
Verified Commit 70b1838d authored by Jan Kuchař's avatar Jan Kuchař Committed by Jiří Pudil
Browse files

Draft: implemented named identifier VO concept

parent 95f73fcc
No related branches found
No related tags found
1 merge request!40CompositeType: add explicit type cast to support composite values in WHERE clause
<?php
declare(strict_types = 1);
namespace Grifart\Tables;
final class NamedIdentifier
{
/** @var string[] */
private array $nameParts = [];
public function __construct(string ...$nameParts) {
$this->nameParts = $nameParts;
}
/**
* @return string SQL
*/
public function toSql(): string {
// todo: use pg_escape_identifier() or Dibi-equivalent
// todo: should not add quotes when not necessary!
$escapeIdentifier = static function($v) {
\assert(! str_contains($v, '"'), 'Whoops, this temporary escaping could not escape strings with a " character.');
return sprintf('"%s"', $v);
};
return implode(".", array_map($escapeIdentifier, $this->nameParts));
}
}
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Grifart\Tables\Types;
use Dibi\Literal;
use Grifart\Tables\NamedIdentifier;
use Grifart\Tables\Type;
use function Functional\map;
......@@ -17,22 +18,25 @@ abstract class CompositeType implements Type
/** @var Type<mixed>[] */
private array $types;
private NamedIdentifier $databaseType;
/**
* @param Type<mixed> $type
* @param Type<mixed> ...$rest
*/
protected function __construct(
private string $databaseType,
NamedIdentifier|string $databaseType,
Type $type,
Type ...$rest,
)
{
$this->types = [$type, ...$rest];
$this->databaseType = $databaseType instanceof NamedIdentifier ? $databaseType : new NamedIdentifier($databaseType);
}
final public function getDatabaseTypes(): array
{
return [$this->databaseType];
return [$this->databaseType->toSql()];
}
/**
......@@ -71,7 +75,7 @@ abstract class CompositeType implements Type
)
);
return new Literal(\sprintf('%s::%s', $dbValue, $this->databaseType));
return new Literal(\sprintf('%s::%s', $dbValue, $this->databaseType->toSql()));
}
/**
......
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Grifart\Tables\Tests\Fixtures;
use Grifart\ClassScaffolder\Definition\Types\Type as PhpType;
use Grifart\Tables\NamedIdentifier;
use Grifart\Tables\Types\CompositeType;
use Grifart\Tables\Types\IntType;
use function Grifart\ClassScaffolder\Definition\Types\tuple;
......@@ -17,7 +18,7 @@ final class VersionType extends CompositeType
public function __construct()
{
parent::__construct(
'version',
new NamedIdentifier('version'),
new IntType(),
new IntType(),
new IntType(),
......
......@@ -19,7 +19,7 @@ $composite = new class extends CompositeType {
public function __construct()
{
parent::__construct(
'"databaseType"',
'databaseType',
new IntType(),
new IntType(),
new TextType(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment