-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap pdo instances #3544
Closed
Closed
Wrap pdo instances #3544
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Driver; | ||
|
||
use Doctrine\DBAL\ParameterType; | ||
use PDO; | ||
use function assert; | ||
use function func_get_args; | ||
|
||
final class WrappedPDOConnection implements Connection, ServerInfoAwareConnection | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
/** @var PDO */ | ||
private $connection; | ||
|
||
private function __construct(PDO $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
public static function fromInstance(PDO $connection) : self | ||
{ | ||
try { | ||
$connection->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]); | ||
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
||
return new self($connection); | ||
} catch (\PDOException $exception) { | ||
throw new PDOException($exception); | ||
} | ||
} | ||
|
||
public function setAttribute(int $attribute, $value) : bool | ||
{ | ||
return $this->connection->setAttribute($attribute, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function prepare($prepareString) | ||
{ | ||
try { | ||
$stmt = $this->connection->prepare($prepareString); | ||
assert($stmt instanceof PDOStatement); | ||
|
||
return $stmt; | ||
} catch (\PDOException $exception) { | ||
throw new PDOException($exception); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function query() | ||
{ | ||
$args = func_get_args(); | ||
|
||
try { | ||
$stmt = $this->connection->query(...$args); | ||
assert($stmt instanceof PDOStatement); | ||
|
||
return $stmt; | ||
} catch (\PDOException $exception) { | ||
throw new PDOException($exception); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function quote($input, $type = ParameterType::STRING) | ||
{ | ||
return $this->connection->quote($input, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function exec($statement) | ||
{ | ||
try { | ||
return $this->connection->exec($statement); | ||
} catch (\PDOException $exception) { | ||
throw new PDOException($exception); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function lastInsertId($name = null) | ||
{ | ||
try { | ||
if ($name === null) { | ||
return $this->connection->lastInsertId(); | ||
} | ||
|
||
return $this->connection->lastInsertId($name); | ||
} catch (\PDOException $exception) { | ||
throw new PDOException($exception); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function beginTransaction() | ||
{ | ||
return $this->connection->beginTransaction(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit() | ||
{ | ||
return $this->connection->commit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rollBack() | ||
{ | ||
return $this->connection->rollBack(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function errorCode() | ||
{ | ||
return $this->connection->errorCode(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function errorInfo() | ||
{ | ||
return $this->connection->errorInfo(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getServerVersion() | ||
{ | ||
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function requiresQueryForServerVersion() | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/Doctrine/Tests/DBAL/Functional/WrappedPDOConnectionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DBAL\Functional; | ||
|
||
use Doctrine\DBAL\DriverManager; | ||
use PDO; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @group GH-3487 | ||
*/ | ||
final class WrappedPDOConnectionTest extends TestCase | ||
{ | ||
/** @var PDO */ | ||
private $pdo; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function createInMemoryConnection() : void | ||
{ | ||
$this->pdo = new PDO('sqlite::memory:'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function queriesAndPreparedStatementsShouldWork() : void | ||
{ | ||
$connection = DriverManager::getConnection(['pdo' => $this->pdo]); | ||
|
||
self::assertTrue($connection->ping()); | ||
|
||
$connection->query('CREATE TABLE testing (id INTEGER NOT NULL PRIMARY KEY)'); | ||
$connection->query('INSERT INTO testing VALUES (1), (2), (3)'); | ||
|
||
$statement = $connection->prepare('SELECT id FROM testing WHERE id >= ?'); | ||
$statement->execute([2]); | ||
|
||
self::assertSame([['id' => '2'], ['id' => '3']], $statement->fetchAll(PDO::FETCH_ASSOC)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function autoIncrementIdsShouldBeFetched() : void | ||
{ | ||
$connection = DriverManager::getConnection(['pdo' => $this->pdo]); | ||
|
||
$connection->query('CREATE TABLE testing (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(10) NOT NULL)'); | ||
$connection->query('INSERT INTO testing (name) VALUES ("Testing")'); | ||
|
||
self::assertSame('1', $connection->lastInsertId()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function transactionControlShouldHappenNormally() : void | ||
{ | ||
$connection = DriverManager::getConnection(['pdo' => $this->pdo]); | ||
$connection->query('CREATE TABLE testing (id INTEGER NOT NULL PRIMARY KEY)'); | ||
|
||
$connection->beginTransaction(); | ||
$connection->query('INSERT INTO testing VALUES (1), (2), (3)'); | ||
$connection->rollBack(); | ||
|
||
self::assertSame([], $connection->fetchAll('SELECT * FROM testing')); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still would be a breaking change since it's no longer a PDO instance (won't match a type hint, can't call PDO-specific methods, etc).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not according to our docblocks...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We never expect to handle raw PDO connection in the internals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. I didn't notice it was about the internal API. But still, even if we can afford introducing this class, I don't think we should. Having two PDO connections which share most of the code is not a proper solution. I'd like to propose #3549 instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@morozov I'm fine with not adding this adapter 😄 I'd suggest to port the tests to your PR, just to ensure that the basic stuff works fine.