1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-11 08:34:59 +02:00

* added support for affectedRows in DibiPdoDriver

This commit is contained in:
David Grudl
2007-11-24 08:23:26 +00:00
parent c8fedf7692
commit a0febd3d50
7 changed files with 36 additions and 58 deletions

View File

@@ -387,7 +387,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
protected function throwException($sql = NULL)
{
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection), $sql);
}

View File

@@ -362,7 +362,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
protected function throwException($sql = NULL)
{
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql);
}

View File

@@ -339,7 +339,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
protected function throwException($sql = NULL)
{
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
}

View File

@@ -296,7 +296,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
protected function throwException($sql = NULL)
{
$err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code'], $sql);

View File

@@ -50,6 +50,13 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
private $resultset;
/**
* Affected rows
* @var int
*/
private $affectedRows = FALSE;
/**
* Connects to a database
@@ -101,13 +108,30 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function query($sql)
{
$this->resultset = $this->connection->query($sql);
// must detect if SQL returns resultset or num of affected rows
$cmd = strtoupper(substr(ltrim($sql), 0, 6));
$list = array('UPDATE'=>1, 'DELETE'=>1, 'INSERT'=>1, 'REPLAC'=>1);
if ($this->resultset === FALSE) {
$this->throwException($sql);
if (isset($list[$cmd])) {
$this->resultset = NULL;
$this->affectedRows = $this->connection->exec($sql);
if ($this->affectedRows === FALSE) {
$this->throwException($sql);
}
return FALSE;
} else {
$this->resultset = $this->connection->query($sql);
$this->affectedRows = FALSE;
if ($this->resultset === FALSE) {
$this->throwException($sql);
}
return TRUE;
}
return $this->resultset instanceof PDOStatement;
}
@@ -119,7 +143,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function affectedRows()
{
throw new BadMethodCallException(__METHOD__ . ' is not implemented');
return $this->affectedRows;
}
@@ -286,7 +310,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
protected function throwException($sql = NULL)
{
$err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);