From c2c63615fde17065566f852f2a63dce097ce5cbe Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 19 Jan 2012 05:02:06 +0100 Subject: [PATCH] DibiFluent: added setupResult() --- dibi/libs/DibiFluent.php | 53 +++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index dbf2e4a8..f9359b3d 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -82,6 +82,9 @@ class DibiFluent extends DibiObject implements IDataSource /** @var DibiConnection */ private $connection; + /** @var array */ + private $setups = array(); + /** @var string */ private $command; @@ -291,6 +294,20 @@ class DibiFluent extends DibiObject implements IDataSource + /** + * Adds DibiResult setup. + * @param string method + * @param mixed args + * @return DibiFluent provides a fluent interface + */ + public function setupResult($method) + { + $this->setups[] = func_get_args(); + return $this; + } + + + /********************* executing ****************d*g**/ @@ -303,7 +320,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function execute($return = NULL) { - $res = $this->connection->query($this->_export()); + $res = $this->query($this->_export()); return $return === dibi::IDENTIFIER ? $this->connection->getInsertId() : $res; } @@ -316,9 +333,9 @@ class DibiFluent extends DibiObject implements IDataSource public function fetch() { if ($this->command === 'SELECT') { - return $this->connection->query($this->_export(NULL, array('%lmt', 1)))->fetch(); + return $this->query($this->_export(NULL, array('%lmt', 1)))->fetch(); } else { - return $this->connection->query($this->_export())->fetch(); + return $this->query($this->_export())->fetch(); } } @@ -331,9 +348,9 @@ class DibiFluent extends DibiObject implements IDataSource public function fetchSingle() { if ($this->command === 'SELECT') { - return $this->connection->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle(); + return $this->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle(); } else { - return $this->connection->query($this->_export())->fetchSingle(); + return $this->query($this->_export())->fetchSingle(); } } @@ -347,7 +364,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function fetchAll($offset = NULL, $limit = NULL) { - return $this->connection->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->fetchAll(); + return $this->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->fetchAll(); } @@ -359,7 +376,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function fetchAssoc($assoc) { - return $this->connection->query($this->_export())->fetchAssoc($assoc); + return $this->query($this->_export())->fetchAssoc($assoc); } @@ -372,7 +389,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function fetchPairs($key = NULL, $value = NULL) { - return $this->connection->query($this->_export())->fetchPairs($key, $value); + return $this->query($this->_export())->fetchPairs($key, $value); } @@ -385,7 +402,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function getIterator($offset = NULL, $limit = NULL) { - return $this->connection->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->getIterator(); + return $this->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->getIterator(); } @@ -407,9 +424,23 @@ class DibiFluent extends DibiObject implements IDataSource */ public function count() { - return (int) $this->connection->query( + return (int) $this->query(array( 'SELECT COUNT(*) FROM (%ex', $this->_export(), ') AS [data]' - )->fetchSingle(); + ))->fetchSingle(); + } + + + + /** + * @return DibiResult + */ + private function query($args) + { + $res = $this->connection->query($args); + foreach ($this->setups as $setup) { + call_user_func_array(array($res, array_shift($setup)), $setup); + } + return $res; }