Skip to content
Open
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
19 changes: 0 additions & 19 deletions src/Adapter/Platform/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace PhpDb\Adapter\Platform;

use Override;
use PDO;
use PhpDb\Adapter\Driver;
use PhpDb\Adapter\Exception;

use function addcslashes;
use function array_map;
Expand All @@ -21,8 +18,6 @@

/**
* @api
*
* @property Driver\DriverInterface|Driver\PdoDriverInterface|PDO $driver
*/
abstract class AbstractPlatform implements PlatformInterface
{
Expand Down Expand Up @@ -138,20 +133,6 @@ public function quoteTrustedValue(int|float|string|bool $value): ?string
return '\'' . addcslashes((string) $value, characters: "\x00\n\r\\'\"\x1a") . '\'';
}

/**
* {@inheritDoc}
*
* @throws Exception\RuntimeException When no driver is available to quote safely.
*/
#[Override]
public function quoteValue(string $value): string
{
if (null === ($this->driver ?? null)) {
throw Exception\RuntimeException::forVulnerablePlatformQuote(static::class, __METHOD__);
}
return '\'' . addcslashes($value, characters: "\x00\n\r\\'\"\x1a") . '\'';
}

/**
* {@inheritDoc}
*/
Expand Down
9 changes: 2 additions & 7 deletions src/Adapter/Platform/Sql92.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use PhpDb\Sql\Platform\Platform;
use PhpDb\Sql\Platform\StatementFactoryInterface;

use function addcslashes;

/**
* Standard SQL conformance platform.
*
Expand Down Expand Up @@ -46,14 +44,11 @@ public function getStatementFactory(): StatementFactoryInterface
/**
* {@inheritDoc}
*
* @throws Exception\RuntimeException When no driver is available to quote safely.
* @throws Exception\RuntimeException Always: this platform has no driver to quote with.
*/
#[Override]
public function quoteValue(string $value): string
{
if (null === ($this->driver ?? null)) {
throw Exception\RuntimeException::forVulnerablePlatformQuote(static::class, __METHOD__);
}
return '\'' . addcslashes($value, characters: "\x00\n\r\\'\"\x1a") . '\'';
throw Exception\RuntimeException::forVulnerablePlatformQuote(static::class, __METHOD__);
}
}
11 changes: 9 additions & 2 deletions src/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,12 @@ protected function processJoins(
/**
* Platform overrides may return the raw limit or offset unquoted; any scalar is rendered as vsprintf's %s would.
*
* A limit is a non-negative integer, not a value: quoting it relies on the engine coercing a
* string, which SQL Server and Oracle reject in their OFFSET and FETCH clauses.
*
* @return array{0: SqlParameterValue}|null
*
* @mago-expect analysis:unused-parameter
*/
protected function processLimit(
PlatformInterface $platform,
Expand All @@ -571,13 +576,15 @@ protected function processLimit(
return [$driver->formatParameterName("{$paramPrefix}limit")];
}

return [$platform->quoteValue((string) $this->limit)];
return [(string) $this->limit];
}

/**
* Platform overrides may return the raw limit or offset unquoted; any scalar is rendered as vsprintf's %s would.
*
* @return array{0: SqlParameterValue}|null
*
* @mago-expect analysis:unused-parameter
*/
protected function processOffset(
PlatformInterface $platform,
Expand All @@ -594,7 +601,7 @@ protected function processOffset(
return [$driver->formatParameterName("{$paramPrefix}offset")];
}

return [$platform->quoteValue((string) $this->offset)];
return [(string) $this->offset];
}

/**
Expand Down
37 changes: 0 additions & 37 deletions test/unit/Adapter/Platform/Sql92Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace PhpDbTest\Adapter\Platform;

use Override;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Exception\RuntimeException;
use PhpDb\Adapter\Platform\AbstractPlatform;
use PhpDb\Adapter\Platform\Sql92;
Expand Down Expand Up @@ -65,23 +64,6 @@ public function abstractPlatformQuoteIdentifierDoublesEmbeddedQuoteCharacters(
static::assertSame($expected, (new TestPlatform())->quoteIdentifier($identifier));
}

#[Test]
public function abstractPlatformQuoteValueEscapesWithDriver(): void
{
$platform = new TestPlatform($this->createStub(DriverInterface::class));

static::assertSame("'test\\'value'", $platform->quoteValue("test'value"));
}

#[Test]
public function abstractPlatformQuoteValueThrowsWithoutDriver(): void
{
$platform = new TestPlatform();

$this->expectException(RuntimeException::class);
$platform->quoteValue('value');
}

#[Test]
public function getIdentifierSeparator(): void
{
Expand Down Expand Up @@ -219,32 +201,13 @@ public function quoteTrustedValueEscapesSpecialCharacters(): void
);
}

#[Test]
public function quoteValueEscapesSpecialCharacters(): void
{
$platform = new TestSql92Platform(driver: $this->createStub(DriverInterface::class));

$quoted = $platform->quoteValue("test'value");

static::assertStringContainsString('test', $quoted);
static::assertStringStartsWith("'", $quoted);
static::assertStringEndsWith("'", $quoted);
}

#[Test]
public function quoteValueListThrowsWithoutDriver(): void
{
$this->expectException(RuntimeException::class);
static::assertSame("'Foo O\\'Bar'", $this->platform->quoteValueList("Foo O'Bar"));
}

#[Test]
public function quoteValueRaisesNoticeWithoutPlatformSupport(): void
{
$this->expectException(RuntimeException::class);
$this->platform->quoteValue('value');
}

#[Test]
public function quoteValueThrowsWithoutDriver(): void
{
Expand Down
12 changes: 10 additions & 2 deletions test/unit/Adapter/Platform/TestAsset/TestPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace PhpDbTest\Adapter\Platform\TestAsset;

use Override;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Exception;
use PhpDb\Adapter\Platform\AbstractPlatform;
use PhpDb\Sql\Platform\Platform;
use PhpDb\Sql\Platform\StatementFactoryInterface;
Expand All @@ -14,7 +14,6 @@ final class TestPlatform extends AbstractPlatform
{
/** @param array{string, string} $quoteIdentifier */
public function __construct(
protected ?DriverInterface $driver = null,
protected array $quoteIdentifier = ['"', '"'],
) {}

Expand All @@ -29,4 +28,13 @@ public function getStatementFactory(): StatementFactoryInterface
{
return new Platform();
}

/**
* A bare platform has no driver to quote values with, which is what the exception reports.
*/
#[Override]
public function quoteValue(string $value): string
{
throw Exception\RuntimeException::forVulnerablePlatformQuote(static::class, __METHOD__);
}
}
2 changes: 1 addition & 1 deletion test/unit/Sql/AbstractSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ public function processSubSelectRendersTheSubselectWithItsOwnClass(): void

$sql = $outer->getSqlString(new TrustingSql92Platform());

static::assertStringContainsString("DIALECT_LIMIT '5'", $sql);
static::assertStringContainsString('DIALECT_LIMIT 5', $sql);
static::assertStringContainsString('SELECT "foo"', $sql);
}

Expand Down
5 changes: 5 additions & 0 deletions test/unit/Sql/Platform/AbstractPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public static function statementProvider(): array
public function aDialectOverrideReturnsItsOwnSubclassThroughTheStandardReturnType(): void
{
$platform = new class extends AbstractPlatform {
public function quoteValue(string $value): string
{
return $this->quoteTrustedValue($value);
}

/** @param TableIdentifier|string|array<string, string|TableIdentifier>|null $table */
public function select(array|string|TableIdentifier|null $table = null): Select
{
Expand Down
14 changes: 7 additions & 7 deletions test/unit/Sql/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public static function providerData(): array
$select26 = new Select();
$select26->from('foo')->limit(5);
$sqlPrep26 = 'SELECT "foo".* FROM "foo" LIMIT ?';
$sqlStr26 = 'SELECT "foo".* FROM "foo" LIMIT \'5\'';
$sqlStr26 = 'SELECT "foo".* FROM "foo" LIMIT 5';
$params26 = ['limit' => 5];
$internalTests26 = [
'processSelect' => [[['"foo".*']], '"foo"'],
Expand All @@ -469,7 +469,7 @@ public static function providerData(): array
$select27 = new Select();
$select27->from('foo')->limit(5)->offset(10);
$sqlPrep27 = 'SELECT "foo".* FROM "foo" LIMIT ? OFFSET ?';
$sqlStr27 = 'SELECT "foo".* FROM "foo" LIMIT \'5\' OFFSET \'10\'';
$sqlStr27 = 'SELECT "foo".* FROM "foo" LIMIT 5 OFFSET 10';
$params27 = ['limit' => 5, 'offset' => 10];
$internalTests27 = [
'processSelect' => [[['"foo".*']], '"foo"'],
Expand Down Expand Up @@ -696,7 +696,7 @@ public static function providerData(): array
$select45 = new Select();
$select45->from('foo')->limit('5')->offset('10');
$sqlPrep45 = 'SELECT "foo".* FROM "foo" LIMIT ? OFFSET ?';
$sqlStr45 = 'SELECT "foo".* FROM "foo" LIMIT \'5\' OFFSET \'10\'';
$sqlStr45 = 'SELECT "foo".* FROM "foo" LIMIT 5 OFFSET 10';
$params45 = ['limit' => 5, 'offset' => 10];
$internalTests45 = [
'processSelect' => [[['"foo".*']], '"foo"'],
Expand All @@ -719,7 +719,7 @@ public static function providerData(): array
$select47 = new Select();
$select47->from('foo')->limit('10000000000000000000')->offset('10000000000000000000');
$sqlPrep47 = 'SELECT "foo".* FROM "foo" LIMIT ? OFFSET ?';
$sqlStr47 = 'SELECT "foo".* FROM "foo" LIMIT \'10000000000000000000\' OFFSET \'10000000000000000000\'';
$sqlStr47 = 'SELECT "foo".* FROM "foo" LIMIT 10000000000000000000 OFFSET 10000000000000000000';
$params47 = ['limit' => 10_000_000_000_000_000_000, 'offset' => 10_000_000_000_000_000_000];
$internalTests47 = [
'processSelect' => [[['"foo".*']], '"foo"'],
Expand Down Expand Up @@ -795,7 +795,7 @@ public static function providerData(): array
$select53->from('foo')->where(new In('bar_id', $subSelect53))->limit(11)->offset(12);
$params53 = ['limit' => 11, 'offset' => 12, 'subselect1limit' => 10, 'subselect1offset' => 9];
$sqlPrep53 = 'SELECT "foo".* FROM "foo" WHERE "bar_id" IN (SELECT "bar"."id" AS "id" FROM "bar" LIMIT :subselect1limit OFFSET :subselect1offset) LIMIT :limit OFFSET :offset';
$sqlStr53 = 'SELECT "foo".* FROM "foo" WHERE "bar_id" IN (SELECT "bar"."id" AS "id" FROM "bar" LIMIT \'10\' OFFSET \'9\') LIMIT \'11\' OFFSET \'12\'';
$sqlStr53 = 'SELECT "foo".* FROM "foo" WHERE "bar_id" IN (SELECT "bar"."id" AS "id" FROM "bar" LIMIT 10 OFFSET 9) LIMIT 11 OFFSET 12';
$internalTests53 = [
'processSelect' => [[['"foo".*']], '"foo"'],
'processWhere' => ['"bar_id" IN (SELECT "bar"."id" AS "id" FROM "bar" LIMIT ? OFFSET ?)'],
Expand Down Expand Up @@ -1358,13 +1358,13 @@ public function limit(): void
}

#[Test]
public function limitIsQuotedWhenContainerHasNoDriver(): void
public function limitIsRenderedAsAnIntegerWhenContainerHasNoDriver(): void
{
$select = (new Select('a'))->limit(5);
$method = new ReflectionMethod($select, 'processLimit');

static::assertSame(
["'5'"],
['5'],
$method->invoke($select, new TrustingSql92Platform(), null, new ParameterContainer()),
);
}
Expand Down
2 changes: 0 additions & 2 deletions test/unit/TestAsset/TestSql92Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace PhpDbTest\TestAsset;

use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Platform\Sql92;

class TestSql92Platform extends Sql92
{
public function __construct(
protected ?DriverInterface $driver = null,
protected bool $quoteIdentifiers = true,
) {}
}
Loading