1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 06:07:39 +02:00

- fixed bug in DibiDataSource

- added DibiDataSource::getTotalCount()
- PostgreSql compatibility
This commit is contained in:
David Grudl
2009-02-08 21:15:54 +00:00
parent 60662bdae1
commit 8df67d0de2
3 changed files with 41 additions and 7 deletions

View File

@@ -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(). * Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch().
* @param array|mixed one or more arguments * @param array|mixed one or more arguments

View File

@@ -41,6 +41,9 @@ class DibiDataSource extends DibiObject implements IDataSource
/** @var int */ /** @var int */
private $count; private $count;
/** @var int */
private $totalCount;
/** @var array */ /** @var array */
private $cols = array(); private $cols = array();
@@ -91,11 +94,29 @@ class DibiDataSource extends DibiObject implements IDataSource
public function count() public function count()
{ {
if ($this->count === NULL) { 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 'SELECT COUNT(*) FROM ' . $this->sql
)->fetchSingle(); )->fetchSingle();
} }
return $this->count; return $this->totalCount;
} }
@@ -120,7 +141,7 @@ class DibiDataSource extends DibiObject implements IDataSource
*/ */
public function toFluent() 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 { } else {
$this->conds[] = func_get_args(); $this->conds[] = func_get_args();
} }
$this->result = NULL; $this->result = $this->count = NULL;
return $this; return $this;
} }
@@ -220,7 +241,7 @@ class DibiDataSource extends DibiObject implements IDataSource
{ {
$this->limit = $limit; $this->limit = $limit;
$this->offset = $offset; $this->offset = $offset;
$this->result = NULL; $this->result = $this->count = NULL;
return $this; return $this;
} }

View File

@@ -202,7 +202,7 @@ final class DibiTranslator extends DibiObject
case 'and': case 'and':
case 'or': // key=val AND key IS NULL AND ... case 'or': // key=val AND key IS NULL AND ...
if (empty($value)) { if (empty($value)) {
return '1'; return $this->driver->escape(TRUE, 'b');
} }
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
@@ -265,7 +265,7 @@ final class DibiTranslator extends DibiObject
case 'by': // key ASC, key DESC case 'by': // key ASC, key DESC
if (empty($value)) { if (empty($value)) {
return '1'; return $this->driver->escape(TRUE, 'b');
} }
foreach ($value as $k => $v) { foreach ($value as $k => $v) {