Skip to content
Merged
26 changes: 15 additions & 11 deletions src/Sql/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class Sql
{
protected AdapterInterface $adapter;

/** @var TableIdentifier|string|array<string, string|TableIdentifier>|null */
/** @var TableIdentifier|string|array<string, TableIdentifier|string>|null */
protected TableIdentifier|string|array|null $table;

protected Platform\PlatformDecoratorInterface $sqlPlatform;

/** @param TableIdentifier|string|array<string, string|TableIdentifier>|null $table */
/** @param TableIdentifier|string|array<string, TableIdentifier|string>|null $table */
public function __construct(
AdapterInterface $adapter,
array|string|TableIdentifier|null $table = null,
TableIdentifier|array|string|null $table = null,
) {
$this->table = $table;
$this->adapter = $adapter;
Expand All @@ -46,7 +46,7 @@ public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapt
);
}

public function delete(string|TableIdentifier|null $table = null): Delete
public function delete(TableIdentifier|string|null $table = null): Delete
{
if (null !== $this->table && null !== $table) {
throw Exception\InvalidArgumentException::forForeignTable($this->table);
Expand All @@ -65,8 +65,8 @@ public function getSqlPlatform(): Platform\PlatformDecoratorInterface
return $this->sqlPlatform;
}

/** @return TableIdentifier|string|array<string, string|TableIdentifier>|null */
public function getTable(): array|string|TableIdentifier|null
/** @return TableIdentifier|string|array<string, TableIdentifier|string>|null */
public function getTable(): TableIdentifier|array|string|null
{
return $this->table;
}
Expand All @@ -76,7 +76,7 @@ public function hasTable(): bool
return null !== $this->table;
}

public function insert(string|TableIdentifier|null $table = null): Insert
public function insert(TableIdentifier|string|null $table = null): Insert
{
if (null !== $this->table && null !== $table) {
throw Exception\InvalidArgumentException::forForeignTable($this->table);
Expand Down Expand Up @@ -106,7 +106,11 @@ public function prepareStatementForSqlObject(
return $statement;
}

public function select(string|TableIdentifier|null $table = null): Select
/**
* @param TableIdentifier|string|array<string, TableIdentifier|string>|null $table
* @throws Exception\InvalidArgumentException
*/
public function select(TableIdentifier|array|string|null $table = null): Select
{
if (null !== $this->table && null !== $table) {
throw Exception\InvalidArgumentException::forForeignTable($this->table);
Expand All @@ -116,17 +120,17 @@ public function select(string|TableIdentifier|null $table = null): Select
}

/**
* @param TableIdentifier|string|array<string, string|TableIdentifier> $table
* @param TableIdentifier|string|array<string, TableIdentifier|string> $table
* @throws Exception\InvalidArgumentException
*/
public function setTable(array|string|TableIdentifier $table): self
public function setTable(TableIdentifier|array|string $table): self
{
$this->table = $table;

return $this;
}

public function update(string|TableIdentifier|null $table = null): Update
public function update(TableIdentifier|string|null $table = null): Update
{
if (null !== $this->table && null !== $table) {
throw Exception\InvalidArgumentException::forForeignTable($this->table);
Expand Down
28 changes: 28 additions & 0 deletions test/unit/Sql/SqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpDbTest\TestAsset;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -41,6 +42,7 @@
#[CoversMethod(Sql::class, 'delete')]
#[CoversMethod(Sql::class, 'prepareStatementForSqlObject')]
#[CoversMethod(Sql::class, 'buildSqlString')]
#[Group('unit')]
final class SqlTest extends TestCase
{
protected MockObject&Adapter $mockAdapter;
Expand Down Expand Up @@ -229,6 +231,32 @@ public function select(): void
$this->sql->select('bar');
}

#[Test]
public function selectAcceptsAliasedTableArray(): void
{
$sql = new Sql($this->mockAdapter);

static::assertSame(['f' => 'foo'], $sql->select(['f' => 'foo'])->getRawState('table'));
}

#[Test]
public function selectRejectsAliasedTableWhenTableIsFixed(): void
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage(
'This Sql object is intended to work with only the table "foo" provided at construction time.',
);
$this->sql->select(['f' => 'foo']);
}

#[Test]
public function selectRendersAliasedTable(): void
{
$sql = new Sql($this->mockAdapter);

static::assertSame('SELECT "f".* FROM "foo" AS "f"', $sql->buildSqlString($sql->select(['f' => 'foo'])));
}

#[Test]
public function selectThrowsWhenTableConflicts(): void
{
Expand Down
Loading