1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 13:17:58 +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 * @throws DibiDriverException
*/ */
protected function throwException($sql=NULL) protected function throwException($sql = NULL)
{ {
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection), $sql); 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 * @throws DibiDriverException
*/ */
protected function throwException($sql=NULL) protected function throwException($sql = NULL)
{ {
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql); 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 * @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); 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 * @throws DibiDriverException
*/ */
protected function throwException($sql=NULL) protected function throwException($sql = NULL)
{ {
$err = oci_error($this->connection); $err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code'], $sql); throw new DibiDriverException($err['message'], $err['code'], $sql);

View File

@@ -50,6 +50,13 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
private $resultset; private $resultset;
/**
* Affected rows
* @var int
*/
private $affectedRows = FALSE;
/** /**
* Connects to a database * Connects to a database
@@ -101,13 +108,30 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/ */
public function query($sql) 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) { if (isset($list[$cmd])) {
$this->throwException($sql); $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() 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 * @throws DibiDriverException
*/ */
protected function throwException($sql=NULL) protected function throwException($sql = NULL)
{ {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql); throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);

View File

@@ -291,7 +291,7 @@ class DibiConnection extends NObject
{ {
$id = $this->driver->insertId($sequence); $id = $this->driver->insertId($sequence);
if ($id < 1) throw new DibiException('Cannot retrieve last generated ID'); if ($id < 1) throw new DibiException('Cannot retrieve last generated ID');
return $id; return (int) $id;
} }

View File

@@ -1,46 +0,0 @@
<style>
pre.dibi { padding-bottom: 10px; }
</style>
<h1>dibi conditional SQL example</h1>
<pre>
<?php
require_once '../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite',
'database' => 'sample.sdb',
));
$cond1 = rand(0,2) < 1;
$cond2 = rand(0,2) < 1;
$name = $cond1 ? 'K%' : NULL;
// if & end
dibi::test('
SELECT *
FROM [customers]
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
);
// if & else & end (last end is optional)
dibi::test('
SELECT *
FROM %if', $cond1, '[customers] %else [products]'
);
// nested condition
dibi::test('
SELECT *
FROM [customers]
WHERE
%if', isset($name), '[name] LIKE %s', $name, '
%if', $cond2, 'AND [admin]=1 %end
%else 1 LIMIT 10 %end'
);