mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 13:47:33 +02:00
- DibiPostgreDriver - workaround for bug in pg_affected_rows
- added DibiResult::setObjects([TRUE | FALSE | class name])
This commit is contained in:
@@ -79,9 +79,12 @@ class dibi
|
||||
|
||||
// special
|
||||
FIELD_COUNTER = 'C', // counter or autoincrement, is integer
|
||||
IDENTIFIER = 'n',
|
||||
IDENTIFIER = 'n';
|
||||
|
||||
// dibi version
|
||||
/**
|
||||
* dibi version
|
||||
*/
|
||||
const
|
||||
VERSION = '0.9 (Revision: $WCREV$, Date: $WCDATE$)';
|
||||
|
||||
|
||||
|
@@ -53,7 +53,7 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
|
||||
|
||||
/**
|
||||
* Affected rows.
|
||||
* @var int
|
||||
* @var int|FALSE
|
||||
*/
|
||||
private $affectedRows = FALSE;
|
||||
|
||||
|
@@ -58,6 +58,13 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
|
||||
private $escMethod = FALSE;
|
||||
|
||||
|
||||
/**
|
||||
* Affected rows.
|
||||
* @var int|FALSE
|
||||
*/
|
||||
private $affectedRows = FALSE;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @throws DibiException
|
||||
@@ -144,9 +151,12 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
|
||||
$this->resultset = @pg_query($this->connection, $sql);
|
||||
|
||||
if ($this->resultset === FALSE) {
|
||||
$this->affectedRows = FALSE;
|
||||
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql);
|
||||
}
|
||||
|
||||
$this->affectedRows = pg_affected_rows($this->resultset); // retrieve immediately due PHP bug
|
||||
|
||||
return is_resource($this->resultset);
|
||||
}
|
||||
|
||||
@@ -159,7 +169,7 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
|
||||
*/
|
||||
public function affectedRows()
|
||||
{
|
||||
return pg_affected_rows($this->resultset);
|
||||
return $this->affectedRows;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -74,10 +74,10 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
|
||||
private $withTables = FALSE;
|
||||
|
||||
/**
|
||||
* Fetch as object?
|
||||
* @var bool
|
||||
* Fetch as objects or arrays?
|
||||
* @var mixed TRUE | FALSE | class name
|
||||
*/
|
||||
public $asObjects = FALSE;
|
||||
private $objects = FALSE;
|
||||
|
||||
|
||||
|
||||
@@ -98,8 +98,14 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
|
||||
public function __construct($driver, $config)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
$this->setWithTables(!empty($config['result:withtables']));
|
||||
$this->asObjects = !empty($config['result:objects']);
|
||||
|
||||
if (!empty($config['result:withtables'])) {
|
||||
$this->setWithTables(TRUE);
|
||||
}
|
||||
|
||||
if (isset($config['result:objects'])) {
|
||||
$this->setObjects($config['result:objects']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -215,14 +221,39 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns rows as arrays or objects?
|
||||
*
|
||||
* @param mixed TRUE | FALSE | class name
|
||||
* @return void
|
||||
*/
|
||||
public function setObjects($type)
|
||||
{
|
||||
$this->objects = is_string($type) ? $type : (bool) $type;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns rows as arrays or objects?
|
||||
*
|
||||
* @return mixed TRUE | FALSE | class name
|
||||
*/
|
||||
public function getObjects()
|
||||
{
|
||||
return $this->objects;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Fetches the row at current position, process optional type conversion.
|
||||
* and moves the internal cursor to the next position
|
||||
*
|
||||
* @param bool fetch as object? Overrides $this->asObjects
|
||||
* @param mixed fetch as object? Overrides $this->setObjects()
|
||||
* @return array|FALSE array on success, FALSE if no next record
|
||||
*/
|
||||
final public function fetch($asObject = NULL)
|
||||
final public function fetch($objects = NULL)
|
||||
{
|
||||
if ($this->withTables === FALSE) {
|
||||
$row = $this->getDriver()->fetch(TRUE);
|
||||
@@ -245,8 +276,16 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
|
||||
}
|
||||
}
|
||||
|
||||
if ($asObject || ($asObject === NULL && $this->asObjects)) {
|
||||
if ($objects === NULL) {
|
||||
$objects = $this->objects;
|
||||
}
|
||||
|
||||
if ($objects) {
|
||||
if ($objects === TRUE) {
|
||||
$row = (object) $row;
|
||||
} else {
|
||||
$row = new $objects($row);
|
||||
}
|
||||
}
|
||||
|
||||
return $row;
|
||||
@@ -286,18 +325,18 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
|
||||
final function fetchAll()
|
||||
{
|
||||
$this->seek(0);
|
||||
$row = $this->fetch(FALSE);
|
||||
$row = $this->fetch();
|
||||
if (!$row) return array(); // empty resultset
|
||||
|
||||
$data = array();
|
||||
if (count($row) === 1) {
|
||||
if (!$this->objects && count($row) === 1) {
|
||||
// special case: one-column result set
|
||||
$key = key($row);
|
||||
do {
|
||||
$data[] = $row[$key];
|
||||
} while ($row = $this->fetch(FALSE));
|
||||
} while ($row = $this->fetch());
|
||||
|
||||
} else {
|
||||
if ($this->asObjects) $row = (object) $row;
|
||||
do {
|
||||
$data[] = $row;
|
||||
} while ($row = $this->fetch());
|
||||
|
@@ -252,9 +252,15 @@ abstract class DibiTable extends /*Nette::*/Object
|
||||
{
|
||||
$row = $this->blankRow;
|
||||
$row[$this->primary] = NULL;
|
||||
if ($this->connection->getConfig('result:objects')) {
|
||||
|
||||
if ($class = $this->connection->getConfig('result:objects')) {
|
||||
if ($class === TRUE) {
|
||||
$row = (object) $row;
|
||||
} else {
|
||||
$row = new $class($row);
|
||||
}
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
|
@@ -119,7 +119,7 @@ final class DibiTranslator extends /*Nette::*/Object
|
||||
} else {
|
||||
$sql[] = substr($arg, 0, $toSkip)
|
||||
/*
|
||||
. preg_replace_callback('/
|
||||
preg_replace_callback('/
|
||||
(?=`|\[|\'|"|%) ## speed-up
|
||||
(?:
|
||||
`(.+?)`| ## 1) `identifier`
|
||||
|
@@ -32,7 +32,7 @@ interface IDibiVariable
|
||||
* @param string optional modifier
|
||||
* @return string SQL code
|
||||
*/
|
||||
public function toSql(DibiTranslator $translator, $modifier);
|
||||
function toSql(DibiTranslator $translator, $modifier);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user