-
-
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
[DBAL-3079] Reworked the usage of PDO in PDOConnection from inheritance to composition #3080
Conversation
*/ | ||
public function query(); | ||
public function query(string $sql); |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not wrapping this into a driver exception. Could be resolved in a different ticket.
*/ | ||
public function query(); | ||
public function query(string $sql); |
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types should be (int|string)[]
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types should be (int|string)[]
@@ -22,7 +22,7 @@ class Driver extends AbstractPostgreSQLDriver | |||
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) | |||
{ | |||
try { | |||
$pdo = new PDOConnection( | |||
$conn = new PDOConnection( |
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.
Can we rename $conn
here? Let's avoid abbreviations
@@ -36,7 +36,7 @@ public function connect(array $params, $username = null, $password = null, array | |||
} | |||
|
|||
try { | |||
$pdo = new PDOConnection( | |||
$conn = new PDOConnection( |
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.
Same: can we avoid the abbreviation?
@@ -4,9 +4,11 @@ | |||
|
|||
use Doctrine\DBAL\Cache\QueryCacheProfile; | |||
use Doctrine\DBAL\ColumnCase; | |||
use Doctrine\DBAL\Driver\PDOConnection; |
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.
Unrelated to this patch, but do we still need the portability connection in 3.x?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
* | ||
* @throws \Doctrine\DBAL\Cache\CacheException | ||
* @throws DBALException |
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.
CacheException
is a subclass of DBALException
which can happen underneath. Therefore, replaced.
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.
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.
PDOConnection
no longer extendsPDO
.PDO
object 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.Connection
have been updated with typed arguments for consistency:prepare()
,executeQuery()
,executeUpdate()
,exec()
,Driver\Statement::rowCount()
was updated with: int
since it's invoked from some methods above.PDO
methods onPDOStatement
have been reworked to usePDOStatement::getWrappedConnection()
.DriverConnectionMock
has been removed since it required an update but wasn't use anywhere.