diff --git a/composer.lock b/composer.lock index a0b6714..4a539d9 100644 --- a/composer.lock +++ b/composer.lock @@ -263,12 +263,12 @@ "source": { "type": "git", "url": "https://github.com/php-db/phpdb.git", - "reference": "cb328d33d1290df05568793d519d2bac23faa34f" + "reference": "81844a5699bf397bf044bbe36ea90be361b4a021" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-db/phpdb/zipball/cb328d33d1290df05568793d519d2bac23faa34f", - "reference": "cb328d33d1290df05568793d519d2bac23faa34f", + "url": "https://api.github.com/repos/php-db/phpdb/zipball/81844a5699bf397bf044bbe36ea90be361b4a021", + "reference": "81844a5699bf397bf044bbe36ea90be361b4a021", "shasum": "" }, "require": { @@ -324,7 +324,7 @@ "issues": "https://github.com/php-db/phpdb/issues", "source": "https://github.com/php-db/phpdb" }, - "time": "2026-02-25T10:50:24+00:00" + "time": "2026-04-13T01:24:01+00:00" }, { "name": "psr/container", @@ -757,11 +757,11 @@ }, { "name": "phpstan/phpstan", - "version": "2.1.40", + "version": "2.1.46", "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9b2c7aeb83a75d8680ea5e7c9b7fca88052b766b", - "reference": "9b2c7aeb83a75d8680ea5e7c9b7fca88052b766b", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/a193923fc2d6325ef4e741cf3af8c3e8f54dbf25", + "reference": "a193923fc2d6325ef4e741cf3af8c3e8f54dbf25", "shasum": "" }, "require": { @@ -806,7 +806,7 @@ "type": "github" } ], - "time": "2026-02-23T15:04:35+00:00" + "time": "2026-04-01T09:25:14+00:00" }, { "name": "phpstan/phpstan-phpunit", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4be4a26..9d6182e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -54,12 +54,6 @@ parameters: count: 1 path: src/Driver.php - - - message: '#^Method PhpDb\\Mysql\\Driver\:\:getResultPrototype\(\) should return PhpDb\\Mysql\\Result but returns PhpDb\\Adapter\\Driver\\ResultInterface\.$#' - identifier: return.type - count: 1 - path: src/Driver.php - - message: '#^Parameter \#1 \$resource \(mysqli\|mysqli_stmt\|PhpDb\\Mysql\\mysqli_result\) of method PhpDb\\Mysql\\Driver\:\:createResult\(\) should be compatible with parameter \$resource \(resource\) of method PhpDb\\Adapter\\Driver\\DriverInterface\:\:createResult\(\)$#' identifier: method.childParameterType diff --git a/src/DatabasePlatformNameTrait.php b/src/DatabasePlatformNameTrait.php deleted file mode 100644 index 85b66bc..0000000 --- a/src/DatabasePlatformNameTrait.php +++ /dev/null @@ -1,30 +0,0 @@ -checkEnvironment(); @@ -44,6 +42,7 @@ public function __construct( if ($this->connection instanceof DriverAwareInterface) { $this->connection->setDriver($this); } + if ($this->statementPrototype instanceof DriverAwareInterface) { $this->statementPrototype->setDriver($this); } diff --git a/src/Pdo/Connection.php b/src/Pdo/Connection.php index 3aa5777..8c93155 100644 --- a/src/Pdo/Connection.php +++ b/src/Pdo/Connection.php @@ -14,12 +14,28 @@ use function array_diff_key; use function implode; +use function is_array; use function is_int; use function is_string; use function strtolower; class Connection extends AbstractPdoConnection { + /** + * Constructor + * + * @throws Exception\InvalidArgumentException + */ + public function __construct( + PDO|array $connectionParameters + ) { + if (is_array($connectionParameters)) { + $this->setConnectionParameters($connectionParameters); + } elseif ($connectionParameters instanceof PDO) { + $this->setResource($connectionParameters); + } + } + /** * {@inheritDoc} */ diff --git a/src/Pdo/Driver.php b/src/Pdo/Driver.php index 4bb69d8..ddfd553 100644 --- a/src/Pdo/Driver.php +++ b/src/Pdo/Driver.php @@ -5,15 +5,39 @@ namespace PhpDb\Mysql\Pdo; use Override; +use PDO; use PDOStatement; +use PhpDb\Adapter\Driver\Feature\DriverFeatureProviderInterface; use PhpDb\Adapter\Driver\Pdo\AbstractPdo; use PhpDb\Adapter\Driver\Pdo\Result; +use PhpDb\Adapter\Driver\PdoConnectionInterface; +use PhpDb\Adapter\Driver\PdoDriverAwareInterface; use PhpDb\Adapter\Driver\ResultInterface; -use PhpDb\Mysql\DatabasePlatformNameTrait; +use PhpDb\Adapter\Driver\StatementInterface; class Driver extends AbstractPdo { - use DatabasePlatformNameTrait; + public function __construct( + (PdoConnectionInterface&PdoDriverAwareInterface)|PDO $connection, + StatementInterface&PdoDriverAwareInterface $statementPrototype, + ResultInterface $resultPrototype, + array $features = [], + ) { + $this->connection = $connection; + $this->statementPrototype = $statementPrototype; + $this->resultPrototype = $resultPrototype; + + if (! $this->connection instanceof PDO) { + $this->connection->setDriver($this); + } + + $this->statementPrototype->setDriver($this); + + // $features is not constructor promoted because $this->features is defined in the trait + if ($features !== [] && $this instanceof DriverFeatureProviderInterface) { + $this->addFeatures($features); + } + } /** * @param PDOStatement $resource diff --git a/test/integration/TestFixtures/mysql.sql b/test/integration/TestFixtures/mysql.sql index ddddd7e..efeff46 100644 --- a/test/integration/TestFixtures/mysql.sql +++ b/test/integration/TestFixtures/mysql.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS test ( name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY (id) -); +) ENGINE=InnoDB; INSERT INTO test (name, value) VALUES ('foo', 'bar'), @@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS test_charset ( field$ VARCHAR(255) NOT NULL, field_ VARCHAR(255) NOT NULL, PRIMARY KEY (id) -); +) ENGINE=InnoDB; INSERT INTO test_charset (field$, field_) VALUES ('foo', 'bar'), @@ -32,7 +32,7 @@ CREATE TABLE IF NOT EXISTS test_audit_trail ( test_value_new VARCHAR(255) NOT NULL, changed TIMESTAMP, PRIMARY KEY (id) -); +) ENGINE=InnoDB; DROP VIEW IF EXISTS test_view; CREATE VIEW test_view diff --git a/test/unit/Pdo/DriverTest.php b/test/unit/Pdo/DriverTest.php index 934f4de..435ee94 100644 --- a/test/unit/Pdo/DriverTest.php +++ b/test/unit/Pdo/DriverTest.php @@ -40,14 +40,6 @@ protected function setUp(): void ); } - public function testGetDatabasePlatformName(): void - { - // Test platform name for SqlServer - //$this->pdo->getConnection()->setConnectionParameters(['driver' => 'pdo_mysql']); - self::assertEquals('Mysql', $this->pdo->getDatabasePlatformName()); - self::assertEquals('MySQL', $this->pdo->getDatabasePlatformName(DriverInterface::NAME_FORMAT_NATURAL)); - } - /** @psalm-return array */ public static function getParamsAndType(): array {