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 // special
FIELD_COUNTER = 'C', // counter or autoincrement, is integer FIELD_COUNTER = 'C', // counter or autoincrement, is integer
IDENTIFIER = 'n', IDENTIFIER = 'n';
// dibi version /**
* dibi version
*/
const
VERSION = '0.9 (Revision: $WCREV$, Date: $WCDATE$)'; VERSION = '0.9 (Revision: $WCREV$, Date: $WCDATE$)';

View File

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

View File

@@ -231,7 +231,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
{ {
// offset suppot is missing... // offset suppot is missing...
if ($limit >= 0) { 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.'); 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. * Affected rows.
* @var int * @var int|FALSE
*/ */
private $affectedRows = 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']); $this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
} catch (PDOException $e) { } catch (PDOException $e) {
throw new DibiDriverException($e->getMessage(), $e->getCode()); throw new DibiDriverException($e->getMessage(), $e->getCode());
} }
if (!$this->connection) { if (!$this->connection) {

View File

@@ -58,6 +58,13 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
private $escMethod = FALSE; private $escMethod = FALSE;
/**
* Affected rows.
* @var int|FALSE
*/
private $affectedRows = FALSE;
/** /**
* @throws DibiException * @throws DibiException
@@ -144,9 +151,12 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
$this->resultset = @pg_query($this->connection, $sql); $this->resultset = @pg_query($this->connection, $sql);
if ($this->resultset === FALSE) { if ($this->resultset === FALSE) {
$this->affectedRows = FALSE;
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql); 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); return is_resource($this->resultset);
} }
@@ -159,7 +169,7 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
*/ */
public function affectedRows() 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; private $withTables = FALSE;
/** /**
* Fetch as object? * Fetch as objects or arrays?
* @var bool * @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) public function __construct($driver, $config)
{ {
$this->driver = $driver; $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. * Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position * 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 * @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) { if ($this->withTables === FALSE) {
$row = $this->getDriver()->fetch(TRUE); $row = $this->getDriver()->fetch(TRUE);
@@ -245,10 +276,18 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
} }
} }
if ($asObject || ($asObject === NULL && $this->asObjects)) { if ($objects === NULL) {
$row = (object) $row; $objects = $this->objects;
} }
if ($objects) {
if ($objects === TRUE) {
$row = (object) $row;
} else {
$row = new $objects($row);
}
}
return $row; return $row;
} }
@@ -286,18 +325,18 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
final function fetchAll() final function fetchAll()
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetch(FALSE); $row = $this->fetch();
if (!$row) return array(); // empty resultset if (!$row) return array(); // empty resultset
$data = array(); $data = array();
if (count($row) === 1) { if (!$this->objects && count($row) === 1) {
// special case: one-column result set
$key = key($row); $key = key($row);
do { do {
$data[] = $row[$key]; $data[] = $row[$key];
} while ($row = $this->fetch(FALSE)); } while ($row = $this->fetch());
} else { } else {
if ($this->asObjects) $row = (object) $row;
do { do {
$data[] = $row; $data[] = $row;
} while ($row = $this->fetch()); } while ($row = $this->fetch());

View File

@@ -252,9 +252,15 @@ abstract class DibiTable extends /*Nette::*/Object
{ {
$row = $this->blankRow; $row = $this->blankRow;
$row[$this->primary] = NULL; $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; return $row;
} }

View File

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

View File

@@ -32,7 +32,7 @@ interface IDibiVariable
* @param string optional modifier * @param string optional modifier
* @return string SQL code * @return string SQL code
*/ */
public function toSql(DibiTranslator $translator, $modifier); function toSql(DibiTranslator $translator, $modifier);
} }