[DBAL-3079] Reworked the usage of PDO in PDOConnection from inheritance to composition#3080
Conversation
| * @throws DBALException | ||
| */ | ||
| public function query(); | ||
| public function query(string $sql); |
There was a problem hiding this comment.
I didn't add the return type since the wrapper connection will have to declare that it returns a driver connection. This will likely change in the future.
There was a problem hiding this comment.
@morozov I didn't understand this comment: if we change this in future, wouldn't it be better to let this become a loud BC break via a change of the signature?
| * {@inheritdoc} | ||
| */ | ||
| public function prepare($prepareString, $driverOptions = []) | ||
| public function prepare($prepareString) |
There was a problem hiding this comment.
No longer needed to follow the PDO signature. Will add to the changelog later.
| */ | ||
| public function beginTransaction() | ||
| { | ||
| return $this->conn->beginTransaction(); |
There was a problem hiding this comment.
Not wrapping this into a driver exception. Could be resolved in a different ticket.
| * @throws DBALException | ||
| */ | ||
| public function query(); | ||
| public function query(string $sql); |
There was a problem hiding this comment.
@morozov I didn't understand this comment: if we change this in future, wouldn't it be better to let this become a loud BC break via a change of the signature?
| /** | ||
| * @var PDO | ||
| */ | ||
| private $conn; |
There was a problem hiding this comment.
Suggest renaming this to connection or even pdoConnection to avoid abbreviation and confusion overall
fd275f8 to
c78e0e5
Compare
a06e9c0 to
9c8441a
Compare
|
@Ocramius want to take a look again? |
Ocramius
left a comment
There was a problem hiding this comment.
Besides nits, this looks really good 👍
lib/Doctrine/DBAL/Connection.php
Outdated
| * @return \Doctrine\DBAL\Driver\Statement The executed statement. | ||
| * @param string $query The SQL query to execute. | ||
| * @param mixed[] $params The parameters to bind to the query, if any. | ||
| * @param mixed[] $types The types the previous parameters are in. |
There was a problem hiding this comment.
Types should be (int|string)[]
There was a problem hiding this comment.
When iterating over (int|string)[], PhpStorm will recognize elements as mixed, but if it's int[]|string[] (not exactly correct), it will recognize them as int|string. This was previously brought up in #3025 (comment).
I'm still in favor of the proper annotation instead of making PhpStorm happy, especially if it's about scalar types (@Majkl578).
There was a problem hiding this comment.
Please let's not introduce types like (int|string)[]. Although unfortunate, it doesn't work in PhpStorm, our CS and most of other tools either.
There was a problem hiding this comment.
Hmm I see you actually did the change... :/
I am strongly against and it should be reverted given the current state of PHP ecosystem.
There was a problem hiding this comment.
We need to achieve some agreement before reverting it. The state of the PHP ecosystem is PHP 7.1 = ~37%. Yet, we don't support the older versions.
There was a problem hiding this comment.
The state of PHP ecosystem is: PHPStan supports this, any other software doesn't.
lib/Doctrine/DBAL/Connection.php
Outdated
| * @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp The query cache profile. | ||
| * @param string $query The SQL query to execute. | ||
| * @param mixed[] $params The parameters to bind to the query, if any. | ||
| * @param mixed[] $types The types the previous parameters are in. |
There was a problem hiding this comment.
Types should be (int|string)[]
lib/Doctrine/DBAL/Connection.php
Outdated
| * raw PDOStatement instances. | ||
| * @param DriverStatement $stmt The statement to bind the values to. | ||
| * @param mixed[] $params The map/list of named/positional parameters. | ||
| * @param mixed[] $types The parameter types. |
There was a problem hiding this comment.
Types should be (int|string)[]
| { | ||
| try { | ||
| $pdo = new PDOConnection( | ||
| $conn = new PDOConnection( |
There was a problem hiding this comment.
Can we rename $conn here? Let's avoid abbreviations
|
|
||
| try { | ||
| $pdo = new PDOConnection( | ||
| $conn = new PDOConnection( |
There was a problem hiding this comment.
Same: can we avoid the abbreviation?
|
|
||
| use Doctrine\DBAL\Cache\QueryCacheProfile; | ||
| use Doctrine\DBAL\ColumnCase; | ||
| use Doctrine\DBAL\Driver\PDOConnection; |
There was a problem hiding this comment.
Unrelated to this patch, but do we still need the portability connection in 3.x?
There was a problem hiding this comment.
What's wrong with it? The column case conversion is very helpful when dealing with Oracle and IBM DB2.
There was a problem hiding this comment.
On the other hand, I agree that PORTABILITY_RTRIM is just a crutch for badly designed code which happens to work on MySQL. And as for PORTABILITY_EMPTY_TO_NULL — no idea what it could be used for.
| * @param QueryCacheProfile $qcp The query cache profile. | ||
| * | ||
| * @throws \Doctrine\DBAL\Cache\CacheException | ||
| * @throws DBALException |
There was a problem hiding this comment.
CacheException is a subclass of DBALException which can happen underneath. Therefore, replaced.
|
Thanks @morozov! |
|
Could we have a way for DBAL's PDOStatement to expose the underlying PDOStatement ? PDO has a With DBAL 2.x, I can access this info thanks to the inheritance. But I don't see any way to achieve this in current state of 3.x now that composition is used. The PDOConnection allows accessing the wrapped connection, but the PDOStatement does not have the equivalent API. |
|
@stof please file a separate ticket. For a use case like yours, it'd make sense to expose the underlying statement not only for PDO drivers. |
Fixes #3079.
PDOConnectionno longer extendsPDO.PDOobject can be obtained viaPDOStatement::getWrappedConnection(). It's required to be able to call PDO-specific methods likesetAttribute().PDOStatement::query()doesn't have to be compatible withPDO::query(), it's updated toPDOStatement::query(string $sql) : ResultStatement. This is the primary goal of this pull request.Connectionhave been updated with typed arguments for consistency:prepare(),executeQuery(),executeUpdate(),exec(),Driver\Statement::rowCount()was updated with: intsince it's invoked from some methods above.PDOmethods onPDOStatementhave been reworked to usePDOStatement::getWrappedConnection().DriverConnectionMockhas been removed since it required an update but wasn't use anywhere.