diff --git a/dibi/dibi.php b/dibi/dibi.php index 6211fe06..4b8a058c 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -300,6 +300,19 @@ class dibi + /** + * Generates and returns SQL query as DibiDataSource - Monostate for DibiConnection::test(). + * @param array|mixed one or more arguments + * @return DibiDataSource + */ + public static function dataSource($args) + { + $args = func_get_args(); + return self::getConnection()->dataSource($args); + } + + + /** * Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch(). * @param array|mixed one or more arguments diff --git a/dibi/libs/DibiDataSource.php b/dibi/libs/DibiDataSource.php index 7d2d2387..4b823504 100644 --- a/dibi/libs/DibiDataSource.php +++ b/dibi/libs/DibiDataSource.php @@ -41,6 +41,9 @@ class DibiDataSource extends DibiObject implements IDataSource /** @var int */ private $count; + /** @var int */ + private $totalCount; + /** @var array */ private $cols = array(); @@ -91,11 +94,29 @@ class DibiDataSource extends DibiObject implements IDataSource public function count() { if ($this->count === NULL) { - $this->count = (int) $this->connection->nativeQuery( + $this->count = $this->conds || $this->offset || $this->limit + ? (int) $this->connection->nativeQuery( + 'SELECT COUNT(*) FROM (' . $this->__toString() . ') AS t' + )->fetchSingle() + : $this->getTotalCount(); + } + return $this->count; + } + + + + /** + * Returns the number of rows in a given data source. + * @return int + */ + public function getTotalCount() + { + if ($this->totalCount === NULL) { + $this->totalCount = (int) $this->connection->nativeQuery( 'SELECT COUNT(*) FROM ' . $this->sql )->fetchSingle(); } - return $this->count; + return $this->totalCount; } @@ -120,7 +141,7 @@ class DibiDataSource extends DibiObject implements IDataSource */ public function toFluent() { - return $this->connection->select('*')->from('(%SQL) AS [t]', $this->__toString()); + return $this->connection->select('*')->from('(%SQL) AS t', $this->__toString()); } @@ -185,7 +206,7 @@ class DibiDataSource extends DibiObject implements IDataSource } else { $this->conds[] = func_get_args(); } - $this->result = NULL; + $this->result = $this->count = NULL; return $this; } @@ -220,7 +241,7 @@ class DibiDataSource extends DibiObject implements IDataSource { $this->limit = $limit; $this->offset = $offset; - $this->result = NULL; + $this->result = $this->count = NULL; return $this; } diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index d278752c..1dd6bc04 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -202,7 +202,7 @@ final class DibiTranslator extends DibiObject case 'and': case 'or': // key=val AND key IS NULL AND ... if (empty($value)) { - return '1'; + return $this->driver->escape(TRUE, 'b'); } foreach ($value as $k => $v) { @@ -265,7 +265,7 @@ final class DibiTranslator extends DibiObject case 'by': // key ASC, key DESC if (empty($value)) { - return '1'; + return $this->driver->escape(TRUE, 'b'); } foreach ($value as $k => $v) {