1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 09:53:11 +01:00

PdoDriver: improved detection of query result [Closes #279]

This commit is contained in:
David Grudl 2018-03-28 18:03:39 +02:00
parent bc5e4e378c
commit 6deb7f7d08

View File

@ -101,23 +101,14 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
*/
public function query(string $sql): ?Dibi\ResultDriver
{
// must detect if SQL returns result set or num of affected rows
$cmd = strtoupper(substr(ltrim($sql), 0, 6));
static $list = ['UPDATE' => 1, 'DELETE' => 1, 'INSERT' => 1, 'REPLAC' => 1];
$this->affectedRows = null;
if (isset($list[$cmd])) {
$this->affectedRows = Helpers::false2Null($this->connection->exec($sql));
if ($this->affectedRows !== null) {
return null;
}
} else {
$res = $this->connection->query($sql);
if ($res) {
return $this->createResultDriver($res);
}
$res = $this->connection->query($sql);
if ($res) {
$this->affectedRows = $res->rowCount();
return $res->columnCount() ? $this->createResultDriver($res) : null;
}
$this->affectedRows = null;
[$sqlState, $code, $message] = $this->connection->errorInfo();
$message = "SQLSTATE[$sqlState]: $message";
switch ($this->driverName) {