Permalink
Browse files

DataSet supports Countable

  • Loading branch information...
1 parent 04cbea7 commit cbc2f9d0cb0c8990881316bbbd1b62167a5cc717 @Artazor Artazor committed May 26, 2012
Showing with 35 additions and 8 deletions.
  1. +31 −5 sql/builder/builder.class.php
  2. +4 −3 sql/data_set/data_set.class.php
View
36 sql/builder/builder.class.php
@@ -86,6 +86,7 @@ public static function buildColumns($columns) {
}
public static function buildGroup($columns) {
+ if ($columns === false) return false;
$result = '';
foreach($columns as $alias => $value) {
$result .= $result === ''
@@ -109,7 +110,7 @@ public static function buildFilter($predicate) {
} else {
$c = $key . '=\'' . mysql_real_escape_string($value) . '\'';
}
- $res .= $res === ''
+ $res .= $res === ''
? ''
: ' and '
;
@@ -134,15 +135,36 @@ public static function buildOffset($offset) {
}
- public function buildSql($meta, $intent) {
+ public function getRealGroup($group, $keys) {
+ if ($group === false) return false;
+ foreach($keys as $key) {
+ // if group contains all subparts of any key we can throw it away
+ if(count(array_intersect($group, $key))===count(count($key))) {
+ return false;
+ }
+ }
+ return $group;
+ }
+
+ public function buildSql($meta, $intent, $onlyCount = false) {
+ $realGroup = $this->getRealGroup($meta['group'],$meta['keys']);
+ if ($onlyCount) {
+ if($realGroup === false) {
+ $select = ' count(*) as count ';
+ } else {
+ $select = ' 1 ';
+ }
+ } else {
+ $select = self::buildColumns($meta['select'][$intent]);
+ }
$from = self::buildDomain($meta['from']);
$parts = array(
- 'select' => self::buildColumns($meta['select'][$intent]),
+ 'select' => $select,
'from' => self::buildDomain($meta['from']),
'where' => self::buildFilter($meta['where'][$intent]),
- 'group by' => self::buildGroup($meta['keys'][0]),
+ 'group by' => self::buildGroup($realGroup),
'having' => self::buildFilter($meta['having']),
'order by' => self::buildOrder($meta['order']),
'limit' => self::buildLimit($meta['limit']),
@@ -154,7 +176,11 @@ public function buildSql($meta, $intent) {
$sql .= $section . "\n" . self::indent($content) . "\n";
}
}
- return $sql;
+ if ($onlyCount && $select === ' 1 ') {
+ return "select count(*) as count from ($sql) as t";
+ } else {
+ return $sql;
+ }
}
public function buildSelect($base, $projection) {
View
7 sql/data_set/data_set.class.php
@@ -36,8 +36,8 @@ public function getMeta() {
}
public function count() {
- $this->flash('Count');
- return 0;
+ $res = $this->connection->rawQueryArray($this->sql['count']);
+ return $res[0]['count'];
}
public function getColumnNames() { }
@@ -86,7 +86,7 @@ public function offsetExists($offset) {
public function offsetUnset($offset) {
throw $this->scope->Exception('Delete via DataSet is not implemented yet');
}
-
+
public function __construct($meta) {
$this->mainAlias = key($meta['from']);
$this->meta = array_merge(self::$defaults, $meta);
@@ -96,6 +96,7 @@ public function __complete() {
$this->connection = $this->scope->connection;
$this->builder = $this->connection->builder;
$this->sql['select'] = $this->builder->buildSql($this->meta,'select');
+ $this->sql['count'] = $this->builder->buildSql($this->meta,'select',true);
}
public function makeRow($data) {

0 comments on commit cbc2f9d

Please sign in to comment.