diff --git a/dibi/dibi.php b/dibi/dibi.php index d71bb01d..bbe0089e 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -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$)'; diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index 314a317b..7180f90a 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -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) { diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 8448f551..1e90ab8a 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -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.'); diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 7a7f7f61..7133bea0 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -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) { diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index be4f0ed1..9181ea91 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -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; } diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 2c74228b..57b9d85a 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -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()); diff --git a/dibi/libs/DibiTable.php b/dibi/libs/DibiTable.php index 16674bdf..328e075e 100644 --- a/dibi/libs/DibiTable.php +++ b/dibi/libs/DibiTable.php @@ -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; } diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index aa86dc6a..fc009c2f 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -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'), diff --git a/dibi/libs/interfaces.php b/dibi/libs/interfaces.php index f737c801..2034e256 100644 --- a/dibi/libs/interfaces.php +++ b/dibi/libs/interfaces.php @@ -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); }