-
-
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?
Changes from 1 commit
411ce51
1472f06
7d87a70
b9d0ba1
98e10a4
1e1d8b5
39f03e9
f55f22f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,31 +89,44 @@ public function disconnect(): void | |
*/ | ||
public function query(string $sql): ?Dibi\ResultDriver | ||
{ | ||
$res = $this->connection->query($sql); | ||
if ($res) { | ||
$this->affectedRows = $res->rowCount(); | ||
return $res->columnCount() ? $this->createResultDriver($res) : null; | ||
try { | ||
$res = $this->connection->query($sql); | ||
|
||
if ($res) { | ||
$this->affectedRows = $res->rowCount(); | ||
return $res->columnCount() ? $this->createResultDriver($res) : null; | ||
} | ||
|
||
} catch (\PDOException $pdoException) { | ||
$this->affectedRows = null; | ||
throw $this->createException($pdoException->errorInfo, $sql, $pdoException); | ||
} | ||
|
||
$this->affectedRows = null; | ||
throw $this->createException($this->connection->errorInfo(), $sql, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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. |
||
|
||
} | ||
|
||
|
||
private function createException(array $errorInfo, string $sql, ?\Throwable $previous): Dibi\DriverException { | ||
[$sqlState, $code, $message] = $errorInfo; | ||
|
||
[$sqlState, $code, $message] = $this->connection->errorInfo(); | ||
$message = "SQLSTATE[$sqlState]: $message"; | ||
switch ($this->driverName) { | ||
case 'mysql': | ||
throw MySqliDriver::createException($message, $code, $sql); | ||
return MySqliDriver::createException($message, $code, $sql, $previous); | ||
|
||
case 'oci': | ||
throw OracleDriver::createException($message, $code, $sql); | ||
return OracleDriver::createException($message, $code, $sql, $previous); | ||
|
||
case 'pgsql': | ||
throw PostgreDriver::createException($message, $sqlState, $sql); | ||
return PostgreDriver::createException($message, $sqlState, $sql, $previous); | ||
|
||
case 'sqlite': | ||
throw SqliteDriver::createException($message, $code, $sql); | ||
return SqliteDriver::createException($message, $code, $sql, $previous); | ||
|
||
default: | ||
throw new Dibi\DriverException($message, $code, $sql); | ||
return new Dibi\DriverException($message, $code, $sql, $previous); | ||
} | ||
} | ||
|
||
|
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