mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 21:28:02 +02:00
* added support for affectedRows in DibiPdoDriver
This commit is contained in:
@@ -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)
|
||||||
{
|
{
|
||||||
|
// 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 (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->resultset = $this->connection->query($sql);
|
||||||
|
$this->affectedRows = FALSE;
|
||||||
|
|
||||||
if ($this->resultset === FALSE) {
|
if ($this->resultset === FALSE) {
|
||||||
$this->throwException($sql);
|
$this->throwException($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->resultset instanceof PDOStatement;
|
return TRUE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -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'
|
|
||||||
);
|
|
Reference in New Issue
Block a user