-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix #51499 PDO: add mysql-specific warning count function #6677
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
Conversation
I feel like this should go through RFC first. I am not a fan of adding driver specific methods. Isn't there any other way we could implement this? |
I'll happily do that if you still think it warrants it
I feel like the options are:
|
Update: Here's the RFC: https://wiki.php.net/rfc/pdo-mysql-get-warning-count |
Can you rebase it against the master, please? |
aa3e57d
to
77d45ef
Compare
77d45ef
to
1b2df98
Compare
Ok, rebased onto |
Looks like that CI failure doesn't have anything to do with this pull and is happening across many other pulls. |
MySQL supplies the count of warnings along with the result set and provides a handy function to retrieve it. Without this, the only way to discover if there has been warnings in the last query is to run *another* query: "SHOW WARNINGS" OR "SELECT @@warning_count". This requires another round-trip across the network which makes warning discovery or reporting a drag on performance. Instead, we use the existing get_driver_methods function that others (postgres, sqlite) use to provide custom functionality outside the standard PDO interface. Thinking about the PDO interface, I looked into several other PDO drivers but couldn't find an analog for mysql_warning_count(). Thus I determined that this may well stay mysql-specific. Fixes php bug: #51499 (https://bugs.php.net/bug.php?id=51499)
1b2df98
to
6d42e9f
Compare
Note that PHP 8.1 feature freeze is on July 20th. |
I never got any negative feedback with my RFC, just a few kind folks privately saying "I like your solution, but some others likely don't" and have no idea how to initiate voting. A tiny function to get the number of warnings in the last query. Without this, there is no sensible way in production to see if your PDO query generated warnings. Yes, it's possible without this change, but doubling the number of queries and thus network round trips is not sensible. |
See https://wiki.php.net/rfc/howto for details about the RFC process. The problem with this PR is not the functionality per se (which appears to be useful), but rather that many people are opposed to adding driver specific methods to the general PDO objects. |
Sorry for being late to the conversation. I do see your point about being vendor specific, but even still I would prefer not to have to know about this being a vendor specific feature. If the method would have the signature: Then, to check this in userland, whether the driver supports it or not, the following expression always works: if ($stmt->getWarningCount() > 0) {
// Handle warnings
} Using this same approach, the vendor specific driver could implement a method That way there is a generic interface, regardless of vendor, as what PDO should be all about. If the code is really cool about it, if ($warnings = $stmt->getWarnings()) {
// Handle warnings.
} |
I'm all for that idea. I chose a smaller scale implementation because I thought a generic function on the PDO interface (and the maintenance that entails) and only one driver that supports it would be a harder pill to swallow. |
@johmanx10 I would be against implementing a function like Getting the warning count from the connection status is much easier, but I don't know how useful that is in real life. A standard PDO method would be a better choice, and it could be implemented for other drivers too. So +1 for |
@kamil-tekiela I understand there is less use for a method like If a new vendor is introduced, with the warnings functionality, or an existing vendor gets this feature, but it doesn't implement it the same as MySQL does, then using if ($pdo->getWarningCount() > 0) {
$query = match($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)) {
'mysql' => 'SHOW WARNINGS;',
'acme' => 'SELECT Severity as Level, Code, Description as Message FROM acme.warnings;'
default => null
};
if ($query !== null && $warnings = $pdo->query($query)) {
// Handle warnings
}
} Versus: if ($warnings = $pdo->getWarnings()) {
// Handle warnings.
} So this mostly benefits the complexity and maintenance in userland. Again, I don't think that feature will be added, but I also don't think there is no point to it. |
And after thinking over my previous comments a bit, the default signature of the public function getWarnings(int|null $fetchMode = PDO::FETCH_CLASS, string $class = PDOWarning): false|PDOStatement; So that the following class could be the default: final class PDOWarning
{
public function __construct(
public readonly string $level,
public readonly int $code,
public readonly string $message
) {}
} |
Since the respective RFC has been declined, I'm closing this PR. |
MySQL supplies the count of warnings along with the result set and
provides a handy function to retrieve it.
Without this, the only way to discover if there has been warnings
in the last query is to run another query: "SHOW WARNINGS" OR
"SELECT @@warning_count". This requires another round-trip across
the network which makes warning discovery or reporting a drag on
performance.
Instead, we use the existing get_driver_methods function that others
(postgres, sqlite) use to provide custom functionality outside the
standard PDO interface.
Thinking about the PDO interface, I looked into several other PDO
drivers but couldn't find an analog for mysql_warning_count(). Thus I
determined that this may well stay mysql-specific.
Fixes php bug: #51499 (https://bugs.php.net/bug.php?id=51499)