Skip to content
This repository
Browse code

DataSet supports Countable

  • Loading branch information...
commit cbc2f9d0cb0c8990881316bbbd1b62167a5cc717 1 parent 04cbea7
Artazor authored
36  sql/builder/builder.class.php
@@ -86,6 +86,7 @@ public static function buildColumns($columns) {
86 86
 		}
87 87
 
88 88
         public static function buildGroup($columns) {
  89
+        	if ($columns === false) return false;
89 90
             $result = '';
90 91
             foreach($columns as $alias => $value) {
91 92
                 $result .= $result === ''
@@ -109,7 +110,7 @@ public static function buildFilter($predicate) {
109 110
                 } else {
110 111
                     $c = $key . '=\'' . mysql_real_escape_string($value) . '\'';
111 112
                 }
112  
-                $res .= $res === '' 
  113
+                $res .= $res === ''
113 114
                     ? ''
114 115
                     : ' and '
115 116
                 ;
@@ -134,15 +135,36 @@ public static function buildOffset($offset) {
134 135
 
135 136
 		}
136 137
 
137  
-		public function buildSql($meta, $intent) {
  138
+		public function getRealGroup($group, $keys) {
  139
+			if ($group === false) return false;
  140
+			foreach($keys as $key) {
  141
+				// if group contains all subparts of any key we can throw it away
  142
+				if(count(array_intersect($group, $key))===count(count($key))) {
  143
+					return false;
  144
+				}
  145
+			}
  146
+			return $group;
  147
+		}
  148
+
  149
+		public function buildSql($meta, $intent, $onlyCount = false) {
  150
+			$realGroup = $this->getRealGroup($meta['group'],$meta['keys']);
  151
+			if ($onlyCount) {
  152
+				if($realGroup === false) {
  153
+					$select = ' count(*) as count ';
  154
+				} else {
  155
+					$select = ' 1 ';
  156
+				}
  157
+			} else {
  158
+				$select  = self::buildColumns($meta['select'][$intent]);
  159
+			}
138 160
 
139 161
             $from = self::buildDomain($meta['from']);
140 162
 
141 163
 			$parts = array(
142  
-				'select'   => self::buildColumns($meta['select'][$intent]),
  164
+				'select'   => $select,
143 165
 				'from'     => self::buildDomain($meta['from']),
144 166
 				'where'    => self::buildFilter($meta['where'][$intent]),
145  
-				'group by' => self::buildGroup($meta['keys'][0]),
  167
+				'group by' => self::buildGroup($realGroup),
146 168
 				'having'   => self::buildFilter($meta['having']),
147 169
 				'order by' => self::buildOrder($meta['order']),
148 170
 				'limit'    => self::buildLimit($meta['limit']),
@@ -154,7 +176,11 @@ public function buildSql($meta, $intent) {
154 176
 					$sql .= $section . "\n" . self::indent($content) . "\n";
155 177
 				}
156 178
 			}
157  
-			return $sql;
  179
+			if ($onlyCount && $select === ' 1 ') {
  180
+				return "select count(*) as count from ($sql) as t";
  181
+			} else {
  182
+				return $sql;
  183
+			}
158 184
 		}
159 185
 
160 186
 		public function buildSelect($base, $projection) {
7  sql/data_set/data_set.class.php
@@ -36,8 +36,8 @@ public function getMeta() {
36 36
 		}
37 37
 
38 38
 		public function count() {
39  
-			$this->flash('Count');
40  
-			return 0;
  39
+			$res = $this->connection->rawQueryArray($this->sql['count']);
  40
+			return $res[0]['count'];
41 41
 		}
42 42
 
43 43
 		public function getColumnNames() {	}
@@ -86,7 +86,7 @@ public function offsetExists($offset) {
86 86
 		public function offsetUnset($offset) {
87 87
 			throw $this->scope->Exception('Delete via DataSet is not implemented yet');
88 88
 		}
89  
-        
  89
+
90 90
 		public function __construct($meta) {
91 91
             $this->mainAlias = key($meta['from']);
92 92
 			$this->meta = array_merge(self::$defaults, $meta);
@@ -96,6 +96,7 @@ public function __complete() {
96 96
 			$this->connection = $this->scope->connection;
97 97
 			$this->builder = $this->connection->builder;
98 98
 			$this->sql['select'] = $this->builder->buildSql($this->meta,'select');
  99
+			$this->sql['count'] = $this->builder->buildSql($this->meta,'select',true);
99 100
 		}
100 101
 
101 102
 		public function makeRow($data) {

0 notes on commit cbc2f9d

Please sign in to comment.
Something went wrong with that request. Please try again.