-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
updateObject method in DatabaseDriver.php always returns true #320
Comments
The code change which caused this issue was once made with PR #33 by @wilsonge . However, the PgsqlDriver still has similar code as the (abstract) base driver had before that change: https://github.com/joomla-framework/database/blob/3.x-dev/src/Pgsql/PgsqlDriver.php#L1041-L1044 And the doc blocks of the "execute" method in both the (abstract) base driver and the PDO driver say that the "execute" method returns boolean. That seems inconsistent to me. @wilsonge Any idea what's the right way? |
In my opinion a choice has to be made between:
|
I would always go for exceptions. |
There are certain ground rules for the choice between the use of exceptions or the use of return values to determine the outcome of programming logic. I have to admit that I didn't know them by heart, so I asked ChatGPT. Its answer confirms what I thought I remembered:
The thing I struggle with most myself, is 3. But I think consistency, as mentioned in 4, is an important one iin the context of this issue. |
The decision about exception has already been made a long time ago. In this case, I would expect, the return true is only for b/c reasons. Only way to solve this would be a new method, not sure if it's worth to do this. |
Why not do it the same as in PgsqlDriver.php? // Set the query and execute the update.
$this->setQuery(sprintf($statement, implode(',', $fields), implode(' AND ', $where)));
return $this->execute(); If execute() fails you still have the exception thrown but also a return false. |
In my opinion it still is a bad idea to use different ways of trying to achieve the same thing. It is confusing. b/c is one of the reasons why these confusing constructs remain. |
because you will never came to the You as developer would expect that in case of an error you get false, which is actually not true. You would need to check the docs to see that you get an exception instead of an false. |
I've just checked the "execute" method of the DatabaseDriver, which is inherited e.g. my the MySQLi driver, and the "execute" method of the PdoDriver, which is inherited by the PgsqlDriver. Both of them return true in case of success, but in case of error they throw exceptions and they never return false, so the "return true" in these methods seems to be just there for b/c. So it seems the change from PR #33 for the MySQLi driver was right, and it should be done in the same way in the PgsqlDriver. |
Steps to reproduce the issue
Can only be reproduced if a database update fails.
Expected result
The updateObject method runs the query and should return the result of the execute() statement.
Actual result
The updateObject method runs the query and regardless of the result of the execute statement, it always returns true.
System information (as much as possible)
See the implemention of the updateObject method in PgsqlDriver.php.
Additional comments
Although the execute statement might trigger an exception, it would be better if the updateObject method itself returns a boolean value that represents the result of the update.
It might also be good to look at the insertObject method in DatabaseDriver.php. That too always returns true, whether or not the execute statement failed or not.
The text was updated successfully, but these errors were encountered: