-
-
Notifications
You must be signed in to change notification settings - Fork 138
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
PdoDriver: fix pdo connection in exception mode (target >=4.1.0) #293
base: master
Are you sure you want to change the base?
Conversation
src/Dibi/Drivers/PdoDriver.php
Outdated
} | ||
|
||
$this->affectedRows = null; | ||
throw $this->createException($this->connection->errorInfo(), $sql, null); |
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 do not like this handling of error cases. Is is done twice, any ideas?
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.
Is your intention to make it work regardless of ATTR_ERRMODE, ie. with ERRMODE_WARNING and ERRMODE_EXCEPTION together?
Maybe you can use finally:
try {
$res = $this->connection->query($sql);
...
} catch (\PDOException $e) {
} finally {
throw $this->createException(isset($e) ? $e->errorInfo : $this->connection->errorInfo(), $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.
Finally will be executed also on successful case, I have extracted this in main body of the function.
src/Dibi/Drivers/PdoDriver.php
Outdated
|
||
} catch (\PDOException $pdoException) { | ||
$this->affectedRows = null; | ||
throw $this->createException($pdoException->errorInfo, $sql, $pdoException); |
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 do not like that I need to pass $previous
all around. Any ideas how to do better?
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 think the PDOException does not contain any additional information, so there is no need to pass on the previous one.
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, as it is implementation detail, no code ever should depend on that. Gonna remove that
1ac208e
to
9840c31
Compare
There is one more problem to fix, there is also WARNING mode in which dibi probably should mute warnings and handle them by iteself consistently = throwing exception. I would like to write few notes into comments into this function because it handles three things simulatanously and it is not obvious what is does for the first look. |
One more thing - I will need to help a little with tests. What approach to go? Run all tests also in warning mode and exception mode? Or create just unit tests for this one function. I'm not shure what happend whan connection dies before |
Run all tests in warning mode and exception mode seems better to me.
It's hard to test, I would probably let it go. |
How to do that? Tester does not support running more |
Running all tests with different configuration can be made by passing evironemnt variables to tester runner and passing that to individual tests. These evironment variables can be then listened in |
@dg It is more complicated then it looked at first time. There are still unhandled self::try(function() {return $this->connection->getAttribute();}, function () {success}, function() {failure}); However this leads into weakly typed code as PHP does not support generics. |
Another option is to have two drivers, one for exception mode and one for warnings mode. And forbid silent mode (it is nonsense mode). And another one option: to switch PDO to warning mode and back before and after each operation. |
As PDO can provide consistent API when configured, I will try to go for switching error modes. Hopefuly that will hot have any side effects. There should be none - as long as there will be no nesting or callbacks from inside of the driver. BTW problem with silent mode is that it is default. Current implementation of PdoDriver relays on silent mode. API that changes by configuration is hell🔥. |
e5e6a36
to
7a49609
Compare
17866e0
to
82c45c3
Compare
dd46ea7
to
9e71132
Compare
4947fa5
to
04f006a
Compare
see #292