Skip to content
Merged
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
36 changes: 0 additions & 36 deletions analyzer-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2346,12 +2346,6 @@ code = "less-specific-nested-argument-type"
message = "Argument type mismatch for argument #2 of `str_replace`: expected `array<array-key, string>|string`, but provided type `array<array-key, mixed>|null|string` is less specific."
count = 1

[[issues]]
file = "src/Sql/Delete.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\AbstractSql::resolveTable`: expected `PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|null|string`, but possibly received `PhpDb\Sql\TableIdentifier|array<string, PhpDb\Sql\TableIdentifier|string>|string`.'
count = 1

[[issues]]
file = "src/Sql/Delete.php"
code = "possibly-invalid-argument"
Expand Down Expand Up @@ -2514,12 +2508,6 @@ code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\AbstractSql::processSubSelect`: expected `PhpDb\Sql\Select`, but possibly received `PhpDb\Sql\Select|non-empty-array<array-key, mixed>`.'
count = 1

[[issues]]
file = "src/Sql/Insert.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\AbstractSql::resolveTable`: expected `PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|null|string`, but possibly received `PhpDb\Sql\TableIdentifier|array<string, PhpDb\Sql\TableIdentifier|string>|string`.'
count = 2

[[issues]]
file = "src/Sql/Insert.php"
code = "possibly-invalid-argument"
Expand Down Expand Up @@ -3120,30 +3108,12 @@ code = "overly-wide-return-type"
message = 'Declared return type `PhpDb\Sql\Platform\PlatformDecoratorInterface|null` for `getsqlplatform` has unused branches: `null`.'
count = 1

[[issues]]
file = "src/Sql/Sql.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\Delete::__construct`: expected `PhpDb\Sql\TableIdentifier|null|string`, but possibly received `PhpDb\Sql\TableIdentifier|array<string, PhpDb\Sql\TableIdentifier|string>|null|string`.'
count = 1

[[issues]]
file = "src/Sql/Sql.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\Exception\InvalidArgumentException::forForeignTable`: expected `string`, but possibly received `PhpDb\Sql\TableIdentifier|array<string, PhpDb\Sql\TableIdentifier|string>|string`.'
count = 4

[[issues]]
file = "src/Sql/Sql.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\Insert::__construct`: expected `PhpDb\Sql\TableIdentifier|null|string`, but possibly received `PhpDb\Sql\TableIdentifier|array<string, PhpDb\Sql\TableIdentifier|string>|null|string`.'
count = 1

[[issues]]
file = "src/Sql/Sql.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\Update::__construct`: expected `PhpDb\Sql\TableIdentifier|null|string`, but possibly received `PhpDb\Sql\TableIdentifier|array<string, PhpDb\Sql\TableIdentifier|string>|null|string`.'
count = 1

[[issues]]
file = "src/Sql/Sql.php"
code = "unhandled-thrown-type"
Expand Down Expand Up @@ -3222,12 +3192,6 @@ code = "possible-method-access-on-null"
message = "Attempting to call a method on `null`."
count = 1

[[issues]]
file = "src/Sql/Update.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\AbstractSql::resolveTable`: expected `PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|null|string`, but possibly received `PhpDb\Sql\TableIdentifier|array<string, PhpDb\Sql\TableIdentifier|string>|string`.'
count = 1

[[issues]]
file = "src/Sql/Update.php"
code = "possibly-invalid-argument"
Expand Down
16 changes: 16 additions & 0 deletions src/Sql/AbstractPreparableSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use PhpDb\Adapter\ParameterContainer;
use PhpDb\Adapter\StatementContainerInterface;

use function array_values;
use function is_array;

abstract class AbstractPreparableSql extends AbstractSql implements PreparableSqlInterface
{
#[Override]
Expand All @@ -30,4 +33,17 @@ public function prepareStatement(

return $statementContainer;
}

/**
* Returns the table to render, dropping the alias from an aliased table array
*
* TableGateway stores an aliased table on data-changing statements, and alias syntax in those
* statements is not portable, so only the table itself is rendered.
*
* @param array<string, string|TableIdentifier>|string|TableIdentifier $table
*/
protected function unaliasTable(array|string|TableIdentifier $table): string|TableIdentifier
{
return is_array($table) ? array_values($table)[0] ?? '' : $table;
}
}
7 changes: 5 additions & 2 deletions src/Sql/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ class Delete extends AbstractPreparableSql

/**
* Constructor
*
* @param string|TableIdentifier|array<string, string|TableIdentifier>|null $table An aliased table
* array is kept for TableGateway and rendered as the bare table
*/
public function __construct(string|TableIdentifier|null $table = null)
public function __construct(string|TableIdentifier|array|null $table = null)
{
if ($table) {
$this->from($table);
Expand Down Expand Up @@ -101,7 +104,7 @@ protected function processDelete(
): string {
return str_replace(
'%1$s',
$this->resolveTable($this->table, $platform, $driver, $parameterContainer),
$this->resolveTable($this->unaliasTable($this->table), $platform, $driver, $parameterContainer),
$this->specifications[static::SPECIFICATION_DELETE],
);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Sql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ class Insert extends AbstractPreparableSql

/**
* Constructor
*
* @param string|TableIdentifier|array<string, string|TableIdentifier>|null $table An aliased table
* array is kept for TableGateway and rendered as the bare table
*/
public function __construct(string|TableIdentifier|null $table = null)
public function __construct(string|TableIdentifier|array|null $table = null)
{
if ($table) {
$this->into($table);
Expand Down Expand Up @@ -186,7 +189,7 @@ protected function processInsert(
return str_replace(
['%1$s', '%2$s', '%3$s'],
[
$this->resolveTable($this->table, $platform, $driver, $parameterContainer),
$this->resolveTable($this->unaliasTable($this->table), $platform, $driver, $parameterContainer),
implode(', ', $columns),
implode(', ', $values),
],
Expand All @@ -211,7 +214,7 @@ protected function processSelect(
return str_replace(
['%1$s', '%2$s', '%3$s'],
[
$this->resolveTable($this->table, $platform, $driver, $parameterContainer),
$this->resolveTable($this->unaliasTable($this->table), $platform, $driver, $parameterContainer),
$columns ? "({$columns})" : '',
$selectSql,
],
Expand Down
7 changes: 5 additions & 2 deletions src/Sql/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ class Update extends AbstractPreparableSql

/**
* Constructor
*
* @param string|TableIdentifier|array<string, string|TableIdentifier>|null $table An aliased table
* array is kept for TableGateway and rendered as the bare table
*/
public function __construct(string|TableIdentifier|null $table = null)
public function __construct(string|TableIdentifier|array|null $table = null)
{
if ($table) {
$this->table($table);
Expand Down Expand Up @@ -227,7 +230,7 @@ protected function processUpdate(
): string {
return str_replace(
'%1$s',
$this->resolveTable($this->table, $platform, $driver, $parameterContainer),
$this->resolveTable($this->unaliasTable($this->table), $platform, $driver, $parameterContainer),
$this->specifications[static::SPECIFICATION_UPDATE],
);
}
Expand Down
24 changes: 24 additions & 0 deletions test/unit/Sql/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Override;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Driver\StatementInterface;
use PhpDb\Sql\AbstractPreparableSql;
use PhpDb\Sql\Argument\Identifier;
use PhpDb\Sql\Argument\Value;
use PhpDb\Sql\Delete;
Expand All @@ -25,6 +26,7 @@
use PhpDbTest\TestAsset\DeleteIgnore;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -41,13 +43,23 @@
#[CoversMethod(Delete::class, 'processDelete')]
#[CoversMethod(Delete::class, 'processWhere')]
#[CoversMethod(Delete::class, '__get')]
#[CoversMethod(AbstractPreparableSql::class, 'unaliasTable')]
final class DeleteTest extends TestCase
{
use AdapterTestTrait;
use DeprecatedAssertionsTrait;

protected Delete $delete;

/** @return array<string, array{array<string, string|TableIdentifier>, string}> */
public static function aliasedTableProvider(): array
{
return [
'table name' => [['f' => 'foo'], '"foo"'],
'table identifier' => [['f' => new TableIdentifier('foo', 'sch')], '"sch"."foo"'],
];
}

#[Test]
public function constructorWithTable(): void
{
Expand Down Expand Up @@ -118,6 +130,18 @@ public function getSqlString(): void
static::assertSame('DELETE FROM "sch"."foo" WHERE x = y', $this->delete->getSqlString());
}

/**
* @param array<string, string|TableIdentifier> $table
*/
#[Test]
#[DataProvider('aliasedTableProvider')]
public function getSqlStringRendersBareTableForAliasedTable(array $table, string $expected): void
{
$this->delete->from($table)->where('x = y');

static::assertSame("DELETE FROM {$expected} WHERE x = y", $this->delete->getSqlString());
}

#[Test]
public function getSqlStringWithEmptyWhere(): void
{
Expand Down
27 changes: 27 additions & 0 deletions test/unit/Sql/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpDb\Adapter\Driver\StatementInterface;
use PhpDb\Adapter\ParameterContainer;
use PhpDb\Adapter\StatementContainer;
use PhpDb\Sql\AbstractPreparableSql;
use PhpDb\Sql\Exception\InvalidArgumentException;
use PhpDb\Sql\Expression;
use PhpDb\Sql\Insert;
Expand All @@ -21,6 +22,7 @@
use PhpDbTest\TestAsset\TrustingSql92Platform;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RequiresPhp;
Expand All @@ -47,13 +49,23 @@
#[CoversMethod(Insert::class, '__unset')]
#[CoversMethod(Insert::class, '__isset')]
#[CoversMethod(Insert::class, '__get')]
#[CoversMethod(AbstractPreparableSql::class, 'unaliasTable')]
final class InsertTest extends TestCase
{
use AdapterTestTrait;
use DeprecatedAssertionsTrait;

protected Insert $insert;

/** @return array<string, array{array<string, string|TableIdentifier>, string}> */
public static function aliasedTableProvider(): array
{
return [
'table name' => [['f' => 'foo'], '"foo"'],
'table identifier' => [['f' => new TableIdentifier('foo', 'sch')], '"sch"."foo"'],
];
}

// @codingStandardsIgnoreStart
#[Test]
public function _get(): void
Expand Down Expand Up @@ -176,6 +188,21 @@ public function getSqlString(): void
);
}

/**
* @param array<string, string|TableIdentifier> $table
*/
#[Test]
#[DataProvider('aliasedTableProvider')]
public function getSqlStringRendersBareTableForAliasedTable(array $table, string $expected): void
{
$this->insert->into($table)->values(['bar' => 'baz']);

static::assertSame(
"INSERT INTO {$expected} (\"bar\") VALUES ('baz')",
$this->insert->getSqlString(new TrustingSql92Platform()),
);
}

#[Test]
public function getSqlStringThrowsExceptionWhenNoValuesOrSelect(): void
{
Expand Down
23 changes: 23 additions & 0 deletions test/unit/Sql/SqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PhpDb\Sql\Update;
use PhpDbTest\TestAsset;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -49,6 +50,16 @@ final class SqlTest extends TestCase
*/
protected Sql $sql;

/** @return array<string, array{'delete'|'insert'|'update'}> */
public static function dataChangingStatementProvider(): array
{
return [
'delete' => ['delete'],
'insert' => ['insert'],
'update' => ['update'],
];
}

// @codingStandardsIgnoreStart
#[Test]
public function _construct(): void
Expand Down Expand Up @@ -96,6 +107,18 @@ public function buildSqlStringThrowsWhenPlatformNotSqlInterface(): void
$sql->buildSqlString($this->sql->select());
}

/**
* @param 'delete'|'insert'|'update' $method
*/
#[Test]
#[DataProvider('dataChangingStatementProvider')]
public function dataChangingStatementKeepsAliasedTable(string $method): void
{
$sql = new Sql($this->mockAdapter, ['f' => 'foo']);

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

#[Test]
public function delete(): void
{
Expand Down
26 changes: 26 additions & 0 deletions test/unit/Sql/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PhpDbTest\TestAsset\UpdateIgnore;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RequiresPhp;
Expand All @@ -57,13 +58,23 @@
#[CoversMethod(Update::class, 'processSet')]
#[CoversMethod(Update::class, 'processWhere')]
#[CoversMethod(Update::class, 'processJoins')]
#[CoversMethod(AbstractPreparableSql::class, 'unaliasTable')]
final class UpdateTest extends TestCase
{
use AdapterTestTrait;
use DeprecatedAssertionsTrait;

protected Update $update;

/** @return array<string, array{array<string, string|TableIdentifier>, string}> */
public static function aliasedTableProvider(): array
{
return [
'table name' => [['f' => 'foo'], '"foo"'],
'table identifier' => [['f' => new TableIdentifier('foo', 'sch')], '"sch"."foo"'],
];
}

#[Test]
public function cloneDeepCopiesSetWhereAndJoins(): void
{
Expand Down Expand Up @@ -212,6 +223,21 @@ public function getSqlStringForFalseUpdateValueParameter(): void
);
}

/**
* @param array<string, string|TableIdentifier> $table
*/
#[Test]
#[DataProvider('aliasedTableProvider')]
public function getSqlStringRendersBareTableForAliasedTable(array $table, string $expected): void
{
$this->update->table($table)->set(['bar' => 'baz']);

static::assertSame(
"UPDATE {$expected} SET \"bar\" = 'baz'",
$this->update->getSqlString(new TrustingSql92Platform()),
);
}

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