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
72 changes: 0 additions & 72 deletions analyzer-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -282,60 +282,6 @@ code = "missing-return-type"
message = "Function `__unset` is missing a return type hint."
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "class-must-be-final"
message = 'Class `PhpDb\Sql\Platform\Platform` should be declared `final`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "mixed-array-assignment"
message = "Unsafe array assignment on type `mixed`."
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "mixed-return-statement"
message = 'Could not infer a precise return type for function `PhpDb\Sql\Platform\Platform::getDecorators`. Saw type `mixed`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "non-existent-method"
message = 'Method `getSqlString` does not exist on type `PhpDb\Sql\Platform\PlatformDecoratorInterface`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "non-existent-method"
message = 'Method `getSqlString` does not exist on type `PhpDb\Sql\PreparableSqlInterface`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "non-existent-method"
message = 'Method `prepareStatement` does not exist on type `PhpDb\Sql\Platform\PlatformDecoratorInterface`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "non-existent-method"
message = 'Method `prepareStatement` does not exist on type `PhpDb\Sql\SqlInterface`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "possibly-undefined-string-array-index"
message = "Possibly undefined array key `string` accessed on `array<array-key, mixed>`."
count = 1

[[issues]]
file = "src/Sql/Platform/PlatformDecoratorInterface.php"
code = "missing-api-or-internal"
message = 'Interface `PhpDb\Sql\Platform\PlatformDecoratorInterface` is missing an `@api` or `@internal` annotation.'
count = 1

[[issues]]
file = "src/Sql/Select.php"
code = "missing-return-type"
Expand Down Expand Up @@ -408,12 +354,6 @@ code = "invalid-array-access"
message = 'Cannot access array index on object `PhpDb\Sql\TableIdentifier` that does not implement `ArrayAccess`.'
count = 3

[[issues]]
file = "src/TableGateway/AbstractTableGateway.php"
code = "invalid-property-assignment-value"
message = "Invalid type for property `$lastInsertValue`: expected `int`, but got `false|int|null|string`."
count = 1

[[issues]]
file = "src/TableGateway/AbstractTableGateway.php"
code = "invalid-return-statement"
Expand Down Expand Up @@ -588,12 +528,6 @@ code = "nullable-return-statement"
message = 'Function `PhpDb\TableGateway\AbstractTableGateway::getTable` is declared to return `PhpDb\Sql\TableIdentifier|array<array-key, mixed>|string` but possibly returns a nullable value (inferred as `PhpDb\Sql\TableIdentifier|array<array-key, mixed>|null|string`).'
count = 1

[[issues]]
file = "src/TableGateway/AbstractTableGateway.php"
code = "overly-wide-return-type"
message = "Declared return type `false|int|null|string` for `getlastinsertvalue` has unused branches: `false`, `string`, `null`."
count = 1

[[issues]]
file = "src/TableGateway/AbstractTableGateway.php"
code = "possible-method-access-on-null"
Expand Down Expand Up @@ -630,12 +564,6 @@ code = "possibly-undefined-int-array-index"
message = 'Possibly undefined array index accessed on `list<PhpDb\Sql\ExpressionInterface|PhpDb\Sql\Select|null|scalar>`.'
count = 3

[[issues]]
file = "src/TableGateway/AbstractTableGateway.php"
code = "possibly-undefined-string-array-index"
message = '''Possibly undefined array key `string('table')` accessed on `array<string, PhpDb\Sql\TableIdentifier|array<array-key, mixed>|string>`.'''
count = 1

[[issues]]
file = "src/TableGateway/AbstractTableGateway.php"
code = "possibly-undefined-string-array-index"
Expand Down
5 changes: 5 additions & 0 deletions docs/book/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ echo $platform->quoteIdentifierInFragment('foo as bar');
echo $platform->quoteIdentifierInFragment('(foo.bar = boo.baz)', ['(', ')', '=']);
```

Value quoting requires a driver. The platform asks the driver to quote the value, and a platform
without one throws `PhpDb\Adapter\Exception\RuntimeException` rather than guess. Driverless
quoting is not supported; `quoteTrustedValue()` is the only path that does not consult a driver,
for values the caller has already established are safe.

## Using The Parameter Container

The `ParameterContainer` object is a container for the various parameters that
Expand Down
19 changes: 9 additions & 10 deletions docs/book/sql-ddl/alter-drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,18 @@ foreach ($tables as $tableName) {

### Current Status

**Important:** Platform-specific DDL decorators have been **removed during
refactoring**. The decorator infrastructure exists in the codebase but specific
platform implementations (MySQL, SQL Server, Oracle, SQLite) have been
deprecated and removed.
A dialect renders DDL through its own statement class. `Sql` obtains those classes from the
adapter platform's statement factory, so `$sql->createTable()`, `$sql->alterTable()` and
`$sql->dropTable()` return the class the driver supplies. Where a driver has nothing to add, the
standard `PhpDb\Sql\Ddl\*` class is returned, which is what a directly constructed statement
renders with.

### What This Means

1. **Platform specialization is handled at the Adapter Platform level**,
not the SQL DDL level
2. **DDL objects are platform-agnostic** - they define the structure,
and the platform renders it appropriately
3. **The decorator system can be used manually** if needed via
`setTypeDecorator()`, but this is advanced usage
1. **Platform specialization lives in the driver's statement classes**, which subclass the
standard ones and override the render hooks they need
2. **DDL objects define the structure**; the dialect class decides how that structure is written
3. **Statements obtained from `$sql` carry the dialect**; a `new CreateTable()` does not

### Platform-Agnostic Approach

Expand Down
2 changes: 1 addition & 1 deletion docs/book/sql-ddl/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $column->setOption('length', 11); // INTEGER(11)

The `length` option is a display width rendered directly after the type, as `INTEGER(11)`. It
accepts an `int` or a string of digits. MySQL deprecated integer display widths in 8.0.17, and the
MySQL platform decorator drops the attribute (see phpdb-mysql#81); other platforms render it as
MySQL statement class drops the attribute (see phpdb-mysql#81); other platforms render it as
given.

**Constructor:**
Expand Down
31 changes: 26 additions & 5 deletions docs/book/sql/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,29 @@ interface PreparableSqlInterface
interface SqlInterface
{
public function getSqlString(
PlatformInterface $adapterPlatform = null
PlatformInterface $adapterPlatform
) : string;
}
```

Use these functions to produce either (a) a prepared statement,
or (b) a string to execute.

`getSqlString()` requires the platform, because a statement does not know which dialect renders
it. Value quoting needs a driver, so a driverless platform cannot render a statement that carries
values, and no standard SQL fallback exists. `$sql->buildSqlString()` supplies the adapter's
platform for you; calling a statement directly means passing one yourself.

```php
$select = $sql->select('users')->where(['id' => 5]);

// through Sql, with the adapter's platform
$sqlString = $sql->buildSqlString($select);

// or directly, naming the platform
$sqlString = $select->getSqlString($adapter->getPlatform());
```

## SQL Arguments and Argument Types

`PhpDb\Sql` provides individual `Argument\<type>` types as well as an
Expand Down Expand Up @@ -233,12 +248,18 @@ $sqlString = $sql->buildSqlString($select);
Note: Direct string building bypasses parameter binding.
Use with caution and never with user input.

```php title="Getting the SQL Platform"
$platform = $sql->getSqlPlatform();
```php title="Getting the Statement Factory"
$factory = $sql->getStatementFactory();
```

The platform object handles database-specific SQL generation and can be
used for custom query building.
The factory builds the statements this adapter renders. A driver whose dialect renders a
statement differently returns its own subclass from the matching factory method, so a statement
obtained from `$sql` carries the dialect from the moment it is created, including through
TableGateway events, RowGateway operations and subselects. The same applies to DDL through
`createTable()`, `alterTable()` and `dropTable()`.

A statement built with `new Select()` renders with the standard SQL rules, because nothing has
told it which dialect it belongs to.

## TableIdentifier

Expand Down
6 changes: 0 additions & 6 deletions lint-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ code = "no-isset"
message = "Use of the `isset` construct."
count = 4

[[issues]]
file = "test/unit/Sql/AbstractSqlFunctionalTestCase.php"
code = "halstead"
message = "Method has a high halstead volume"
count = 1

[[issues]]
file = "test/unit/Sql/InsertIgnoreTest.php"
code = "no-isset"
Expand Down
4 changes: 3 additions & 1 deletion mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ baseline = "lint-baseline.toml"
# implements, so their method count is the size of that contract. Pdo\Result implements Iterator,
# Countable and ResultInterface, and ParameterContainer implements Iterator, ArrayAccess and
# Countable beside its errata and max-length accessors: in both, the count is fixed by the
# interfaces and the public API.
# interfaces and the public API. Sql is the statement entry point: one method per statement type
# it builds, beside the render and table accessors.
# "test/" repeats the phpdb-qa-tools default, which this key replaces.
too-many-methods = { exclude = [
"test/",
Expand All @@ -32,6 +33,7 @@ too-many-methods = { exclude = [
"src/Adapter/Platform/PlatformInterface.php",
"src/Sql/Exception/",
"src/Sql/Predicate/Exception/",
"src/Sql/Sql.php",
"src/TableGateway/Exception/",
"src/Adapter/Exception/",
"src/Sql/Predicate/Predicate.php",
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public function getHelpers(): array
self::FUNCTION_QUOTE_IDENTIFIER => static fn(string $value): string => $platform->quoteIdentifier(
$value,
),
self::FUNCTION_QUOTE_VALUE => $platform->quoteValue(...),
default => null,
self::FUNCTION_QUOTE_VALUE => $platform->quoteValue(...),
default => null,
};
if (null !== $function) {
$functions[] = $function;
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/Driver/Pdo/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
throw Exception\InvalidArgumentException::forInvalidFetchMode();
}

$this->fetchMode = (int) $fetchMode;

Check warning on line 280 in src/Adapter/Driver/Pdo/Result.php

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "CastInt": @@ @@ throw Exception\InvalidArgumentException::forInvalidFetchMode(); } - $this->fetchMode = (int) $fetchMode; + $this->fetchMode = $fetchMode; } public function setStatementMode(string $statementMode = self::STATEMENT_MODE_FORWARD): void

Check warning on line 280 in src/Adapter/Driver/Pdo/Result.php

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "CastInt": @@ @@ throw Exception\InvalidArgumentException::forInvalidFetchMode(); } - $this->fetchMode = (int) $fetchMode; + $this->fetchMode = $fetchMode; } public function setStatementMode(string $statementMode = self::STATEMENT_MODE_FORWARD): void
}

public function setStatementMode(string $statementMode = self::STATEMENT_MODE_FORWARD): void
Expand Down Expand Up @@ -307,9 +307,9 @@
$rowCount = $this->rowCount;

return match (true) {
is_int($rowCount) => $rowCount,
is_int($rowCount) => $rowCount,
$rowCount instanceof Closure => (int) $rowCount(),
default => $this->resource->rowCount(),
default => $this->resource->rowCount(),
};
}
}
2 changes: 1 addition & 1 deletion src/Adapter/Driver/Pdo/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private function toPdoType(mixed $errata, mixed $value): int
is_int($value) => PDO::PARAM_INT,
is_bool($value) => PDO::PARAM_BOOL,
null === $value => PDO::PARAM_NULL,
default => PDO::PARAM_STR,
default => PDO::PARAM_STR,
};
}

Expand Down
6 changes: 2 additions & 4 deletions src/Adapter/ParameterContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ public function offsetSet(mixed $offset, mixed $value, mixed $errata = null, mix
? substr($offset, offset: 1)
: $offset]
?? $offset,
null === $offset => (string) count($this->data),
default => throw Exception\InvalidArgumentException::forInvalidKeyType(),
null === $offset => (string) count($this->data),
default => throw Exception\InvalidArgumentException::forInvalidKeyType(),
};

$isNewPosition = is_int($offset)
Expand Down Expand Up @@ -310,8 +310,6 @@ public function offsetSetMaxLength(string|int $name, mixed $maxLength): void
/**
* Bind a parameter to another by reference. Referencing a name that is not set creates it
* as null, so the two stay bound when either is set later.
*
* @mago-expect analysis:possibly-undefined-string-array-index
*/
public function offsetSetReference(string|int $name, string|int $from): void
{
Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/Platform/PlatformInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PhpDb\Adapter\Platform;

use PhpDb\Sql\Platform\PlatformDecoratorInterface;
use PhpDb\Sql\Platform\StatementFactoryInterface;

/**
* @api
Expand Down Expand Up @@ -32,12 +32,12 @@ public function getQuoteIdentifierSymbol(): string;
public function getQuoteValueSymbol(): string;

/**
* Get Sql platform decorator
* Get the statement factory for this platform's dialect
*
* Returns a PhpDb\Sql\Platform\* instance
* PhpDb\Sql\Platform\AbstractPlatform implements this interface
*/
public function getSqlPlatformDecorator(): PlatformDecoratorInterface;
public function getStatementFactory(): StatementFactoryInterface;

/**
* Quote identifier
Expand Down
15 changes: 11 additions & 4 deletions src/Adapter/Platform/Sql92.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
use Override;
use PhpDb\Adapter\Exception;
use PhpDb\Sql\Platform\Platform;
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
use PhpDb\Sql\Platform\StatementFactoryInterface;

use function addcslashes;

/**
* @api
* Standard SQL conformance platform.
*
* The test suite renders statements through it, and extends it to specialize quoting per dialect,
* which is why it is not final. Nothing in the library selects it and no statement falls back to it.
*
* @internal
*
* @mago-expect analysis:class-must-be-final
*/
class Sql92 extends AbstractPlatform
{
Expand All @@ -31,9 +38,9 @@ public function getName(): string
* {@inheritDoc}
*/
#[Override]
public function getSqlPlatformDecorator(): PlatformDecoratorInterface
public function getStatementFactory(): StatementFactoryInterface
{
return new Platform($this);
return new Platform();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Profiler/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getLastProfile(): ?array
{
$lastIndex = array_key_last($this->profiles);

return null === $lastIndex ? null : $this->profiles[$lastIndex] ?? null;
return null === $lastIndex ? null : $this->profiles[$lastIndex];
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Container/AdapterInterfaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function is_array;

final class AdapterInterfaceFactory
{
/**
Expand All @@ -32,8 +34,11 @@
throw ContainerException::forMissingConfigService($requestedName, self::class);
}

/** @var array<array-key, mixed> $config */
$config = $container->get('config') ?? [];
/** @var mixed $config */
$config = $container->get('config');
if (! is_array($config)) {
$config = [];
}

/** @var array{driver?: class-string<DriverInterface>, ...} $adapterConfig */
$adapterConfig = $config[AdapterInterface::class] ?? $config[Adapter::class] ?? [];
Expand All @@ -50,7 +55,7 @@

$driver = $container->build($driverClass, $adapterConfig);

$adapterPlatform = $container->build(PlatformInterface::class, ['driver' => $driver]);

Check warning on line 58 in src/Container/AdapterInterfaceFactory.php

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ $driver = $container->build($driverClass, $adapterConfig); - $adapterPlatform = $container->build(PlatformInterface::class, ['driver' => $driver]); + $adapterPlatform = $container->build(PlatformInterface::class, []); $profilerInterface = $container->has(ProfilerInterface::class) ? $container->build(ProfilerInterface::class)

Check warning on line 58 in src/Container/AdapterInterfaceFactory.php

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ $driver = $container->build($driverClass, $adapterConfig); - $adapterPlatform = $container->build(PlatformInterface::class, ['driver' => $driver]); + $adapterPlatform = $container->build(PlatformInterface::class, []); $profilerInterface = $container->has(ProfilerInterface::class) ? $container->build(ProfilerInterface::class)

$profilerInterface = $container->has(ProfilerInterface::class)
? $container->build(ProfilerInterface::class)
Expand Down
Loading
Loading