1
0
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:
David Grudl
2008-05-12 22:55:51 +00:00
parent 7bb5684d71
commit 40e9f313c3
9 changed files with 91 additions and 33 deletions

View File

@@ -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$)';

View File

@@ -217,7 +217,7 @@ class DibiMsSqlDriver extends /*Nette::*/Object implements IDibiDriver
{
// offset suppot is missing...
if ($limit >= 0) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
}
if ($offset) {

View File

@@ -231,7 +231,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
{
// offset suppot is missing...
if ($limit >= 0) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
}
if ($offset) throw new InvalidArgumentException('Offset is not implemented in driver odbc.');

View File

@@ -53,7 +53,7 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
/**
* Affected rows.
* @var int
* @var int|FALSE
*/
private $affectedRows = FALSE;
@@ -92,7 +92,7 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
$this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
} catch (PDOException $e) {
throw new DibiDriverException($e->getMessage(), $e->getCode());
throw new DibiDriverException($e->getMessage(), $e->getCode());
}
if (!$this->connection) {

View File

@@ -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;
}

View File

@@ -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,10 +276,18 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
}
}
if ($asObject || ($asObject === NULL && $this->asObjects)) {
$row = (object) $row;
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());

View File

@@ -252,9 +252,15 @@ abstract class DibiTable extends /*Nette::*/Object
{
$row = $this->blankRow;
$row[$this->primary] = NULL;
if ($this->connection->getConfig('result:objects')) {
$row = (object) $row;
if ($class = $this->connection->getConfig('result:objects')) {
if ($class === TRUE) {
$row = (object) $row;
} else {
$row = new $class($row);
}
}
return $row;
}

View File

@@ -119,16 +119,16 @@ final class DibiTranslator extends /*Nette::*/Object
} else {
$sql[] = substr($arg, 0, $toSkip)
/*
. preg_replace_callback('/
(?=`|\[|\'|"|%) ## speed-up
(?:
`(.+?)`| ## 1) `identifier`
\[(.+?)\]| ## 2) [identifier]
(\')((?:\'\'|[^\'])*)\'| ## 3,4) string
(")((?:""|[^"])*)"| ## 5,6) "string"
(\'|") ## 7) lone-quote
%([a-zA-Z]{1,4})(?![a-zA-Z])|## 8) modifier
)/xs',
preg_replace_callback('/
(?=`|\[|\'|"|%) ## speed-up
(?:
`(.+?)`| ## 1) `identifier`
\[(.+?)\]| ## 2) [identifier]
(\')((?:\'\'|[^\'])*)\'| ## 3,4) string
(")((?:""|[^"])*)"| ## 5,6) "string"
(\'|") ## 7) lone-quote
%([a-zA-Z]{1,4})(?![a-zA-Z])|## 8) modifier
)/xs',
*/ // note: this can change $this->args & $this->cursor & ...
. preg_replace_callback('/(?=`|\[|\'|"|%)(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s',
array($this, 'cb'),

View File

@@ -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);
}