From bf6dc1cbd12e97f3f770ea1b5ce76c4b06a71d7f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 5 Feb 2009 01:26:08 +0000 Subject: [PATCH] - DibiFluent implements Countable, IteratorAggregate - DibiDataSource is deprecated - DibiTranslator - fixed DateTime class support --- dibi/dibi.php | 6 ++- dibi/libs/DibiDataSource.php | 14 +++++++ dibi/libs/DibiFluent.php | 78 ++++++++++++++++++++++++++---------- dibi/libs/DibiResult.php | 36 +---------------- dibi/libs/DibiRow.php | 52 ++++++++++++++++++++++++ dibi/libs/DibiTableX.php | 1 + dibi/libs/DibiTranslator.php | 9 +++-- dibi/libs/interfaces.php | 14 ------- 8 files changed, 136 insertions(+), 74 deletions(-) create mode 100644 dibi/libs/DibiRow.php diff --git a/dibi/dibi.php b/dibi/dibi.php index b6571d34..c39ba2af 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -67,6 +67,7 @@ require_once dirname(__FILE__) . '/libs/DibiException.php'; require_once dirname(__FILE__) . '/libs/DibiConnection.php'; require_once dirname(__FILE__) . '/libs/DibiResult.php'; require_once dirname(__FILE__) . '/libs/DibiResultIterator.php'; +require_once dirname(__FILE__) . '/libs/DibiRow.php'; require_once dirname(__FILE__) . '/libs/DibiTranslator.php'; require_once dirname(__FILE__) . '/libs/DibiVariable.php'; require_once dirname(__FILE__) . '/libs/DibiTableX.php'; @@ -116,10 +117,13 @@ class dibi const REVISION = '$WCREV$ released on $WCDATE$'; /**#@-*/ - /** + /**#@+ * Configuration options */ const RESULT_WITH_TABLES = 'resultWithTables'; // for MySQL + const ROW_CLASS = 'rowClass'; + const ASC = 'ASC', DESC = 'DESC'; + /**#@-*/ /** @var DibiConnection[] Connection registry storage for DibiConnection objects */ private static $registry = array(); diff --git a/dibi/libs/DibiDataSource.php b/dibi/libs/DibiDataSource.php index 20ad30e6..8b328b18 100644 --- a/dibi/libs/DibiDataSource.php +++ b/dibi/libs/DibiDataSource.php @@ -20,12 +20,26 @@ +/** + * Provides an interface between a dataset and data-aware components. + * @package dibi + * @deprecated + */ +interface IDataSource extends Countable, IteratorAggregate +{ + //function IteratorAggregate::getIterator(); + //function Countable::count(); +} + + + /** * Default implementation of IDataSource for dibi. * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi + * @deprecated */ class DibiDataSource extends DibiObject implements IDataSource { diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index e627604a..77297505 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -27,18 +27,18 @@ * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi */ -class DibiFluent extends DibiObject +class DibiFluent extends DibiObject implements Countable, IteratorAggregate { /** @var array */ public static $masks = array( 'SELECT' => array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY', - 'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET', '%end'), - 'UPDATE' => array('UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT', '%end'), - 'INSERT' => array('INSERT', 'INTO', 'VALUES', 'SELECT', '%end'), - 'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT', '%end'), + 'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET'), + 'UPDATE' => array('UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT'), + 'INSERT' => array('INSERT', 'INTO', 'VALUES', 'SELECT'), + 'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT'), ); - /** @var array */ + /** @var array default modifiers for arrays */ public static $modifiers = array( 'SELECT' => '%n', 'FROM' => '%n', @@ -51,7 +51,7 @@ class DibiFluent extends DibiObject 'GROUP BY' => '%by', ); - /** @var array */ + /** @var array clauses separators */ public static $separators = array( 'SELECT' => ',', 'FROM' => FALSE, @@ -258,9 +258,10 @@ class DibiFluent extends DibiObject public function fetch() { if ($this->command === 'SELECT') { - $this->clauses['LIMIT'] = array(1); + return $this->connection->query($this->_export(NULL, array('%lmt', 1)))->fetch(); + } else { + return $this->connection->query($this->_export())->fetch(); } - return $this->execute()->fetch(); } @@ -272,9 +273,10 @@ class DibiFluent extends DibiObject public function fetchSingle() { if ($this->command === 'SELECT') { - $this->clauses['LIMIT'] = array(1); + return $this->connection->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle(); + } else { + return $this->connection->query($this->_export())->fetchSingle(); } - return $this->execute()->fetchSingle(); } @@ -287,7 +289,7 @@ class DibiFluent extends DibiObject */ public function fetchAll($offset = NULL, $limit = NULL) { - return $this->execute()->fetchAll($offset, $limit); + return $this->connection->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->fetchAll(); } @@ -302,7 +304,7 @@ class DibiFluent extends DibiObject */ public function fetchAssoc($assoc) { - return $this->execute()->fetchAssoc($assoc); + return $this->connection->query($this->_export())->fetchAssoc($assoc); } @@ -316,7 +318,32 @@ class DibiFluent extends DibiObject */ public function fetchPairs($key = NULL, $value = NULL) { - return $this->execute()->fetchPairs($key, $value); + return $this->connection->query($this->_export())->fetchPairs($key, $value); + } + + + + /** + * Required by the IteratorAggregate interface. + * @param int offset + * @param int limit + * @return DibiResultIterator + */ + public function getIterator($offset = NULL, $limit = NULL) + { + return $this->connection->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->getIterator(); + } + + + + /** + * @return int + */ + public function count() + { + return (int) $this->connection->query( + 'SELECT COUNT(*) FROM (%ex', $this->_export(), ') AS [data]' + )->fetchSingle(); } @@ -338,7 +365,7 @@ class DibiFluent extends DibiObject * @param string clause name * @return array */ - protected function _export($clause = NULL) + protected function _export($clause = NULL, $args = array()) { if ($clause === NULL) { $data = $this->clauses; @@ -352,18 +379,16 @@ class DibiFluent extends DibiObject } } - $args = array(); foreach ($data as $clause => $statement) { if ($statement !== NULL) { - if ($clause[0] !== '%') { - $args[] = $clause; - if ($clause === $this->command) { - $args[] = implode(' ', array_keys($this->flags)); - } + $args[] = $clause; + if ($clause === $this->command) { + $args[] = implode(' ', array_keys($this->flags)); } array_splice($args, count($args), 0, $statement); } } + return $args; } @@ -395,6 +420,17 @@ class DibiFluent extends DibiObject return $this->connection->sql($this->_export()); } + + + /** + * Returns the dibi connection. + * @return DibiConnection + */ + final public function getConnection() + { + return $this->connection; + } + } diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index c8f87946..fb25350e 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -40,7 +40,7 @@ * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi */ -class DibiResult extends DibiObject implements IDataSource +class DibiResult extends DibiObject implements Countable, IteratorAggregate { /** @var array IDibiDriver */ private $driver; @@ -639,37 +639,3 @@ class DibiResult extends DibiObject implements IDataSource } } - - - - -/** - * dibi result-set row - * - * @author David Grudl - * @copyright Copyright (c) 2005, 2009 David Grudl - * @package dibi - */ -class DibiRow extends ArrayObject -{ - - /** - * @param array - */ - public function __construct($arr) - { - parent::__construct($arr, 2); - } - - - - /** - * PHP < 5.3 workaround - * @return void - */ - public function __wakeup() - { - $this->setFlags(2); - } - -} diff --git a/dibi/libs/DibiRow.php b/dibi/libs/DibiRow.php new file mode 100644 index 00000000..c0903cde --- /dev/null +++ b/dibi/libs/DibiRow.php @@ -0,0 +1,52 @@ +setFlags(2); + } + +} diff --git a/dibi/libs/DibiTableX.php b/dibi/libs/DibiTableX.php index d0051fe5..43a5e4be 100644 --- a/dibi/libs/DibiTableX.php +++ b/dibi/libs/DibiTableX.php @@ -25,6 +25,7 @@ * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl * @package dibi + * @deprecated */ abstract class DibiTableX extends DibiObject { diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index 8aac22b3..43d79272 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -296,9 +296,11 @@ final class DibiTranslator extends DibiObject if ($value instanceof IDibiVariable) { return $value->toSql($this, $modifier); - } - if (!is_scalar($value)) { // array is already processed + } elseif ($value instanceof DateTime) { + $value = $value->format('U'); + + } elseif (!is_scalar($value)) { // array is already processed $this->hasError = TRUE; return '**Unexpected type ' . gettype($value) . '**'; } @@ -329,7 +331,7 @@ final class DibiTranslator extends DibiObject case 'd': // date case 't': // datetime - $value = is_numeric($value) ? (int) $value : ($value instanceof DateTime ? $value->format('U') : strtotime($value)); + $value = is_numeric($value) ? (int) $value : strtotime($value); return $this->driver->escape($value, $modifier); case 'by': @@ -355,6 +357,7 @@ final class DibiTranslator extends DibiObject case 'a': case 'l': case 'v': + case 'by': $this->hasError = TRUE; return '**Unexpected type ' . gettype($value) . '**'; diff --git a/dibi/libs/interfaces.php b/dibi/libs/interfaces.php index ab57163a..36a6e6c1 100644 --- a/dibi/libs/interfaces.php +++ b/dibi/libs/interfaces.php @@ -39,20 +39,6 @@ interface IDibiVariable -/** - * Provides an interface between a dataset and data-aware components. - * @package dibi - */ -interface IDataSource extends Countable, IteratorAggregate -{ - //function IteratorAggregate::getIterator(); - //function Countable::count(); -} - - - - - /** * Defines method that must profiler implement. * @package dibi