Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added aggregation

  • Loading branch information...
commit 51921e028ad5d6b27a19e0616cd434f3f035799c 1 parent 4835973
Tim Thomas authored
37 BuyPlayTix/DataBean/DBAdapter.php
@@ -190,15 +190,15 @@ function raw_insert($table, $insert_fields = array()) {
190 190 $values = array();
191 191 foreach($insert_fields as $name => $value) {
192 192 $fields[] = $name;
193   - $values[] = $db->quote($value);
  193 + $placeholders[] = "?";
  194 + $values[] = $value;
194 195 }
195 196
196 197 $sql = "insert
197 198 into " . $table . "
198   - (" . implode(',', $fields) . ") values (" . implode(',', $values) . ")";
199   - return $db->query($sql);
200   -
201   -
  199 + (" . implode(',', $fields) . ") values (" . implode(',', $placeholders) . ")";
  200 + $sth = $db->prepare($sql);
  201 + return $db->execute($sth, $values);
202 202 }
203 203 function raw_update($table, $fields = array(), $where_fields = array()) {
204 204
@@ -235,7 +235,7 @@ function raw_select($table, $fields = array(), $where_fields = array(), $cast_cl
235 235 $db = DB::getInstance();
236 236
237 237 $values = array();
238   - $where_clause = "";
  238 + $where_clause = array();
239 239 foreach($where_fields as $name => $v) {
240 240 $condition = '=';
241 241 $value = $v;
@@ -246,7 +246,30 @@ function raw_select($table, $fields = array(), $where_fields = array(), $cast_cl
246 246 $where_clause[] = $name . " $condition ? ";
247 247 $values[] = $value;
248 248 }
249   - $sql = "SELECT " . implode(",", $fields) . " FROM " . $table . " WHERE " . implode(" AND ", $where_clause) . (count($order) ? ' ORDER BY ' . implode(",", $order) : '') . (count($group) ? ' GROUP BY ' . implode(",", $group) : '');
  249 + $formatted_fields = array();
  250 + foreach($fields as $field) {
  251 + if(is_array($field)) {
  252 + $field_name = $field['name'];
  253 + $field_alias = $field['alias'];
  254 + switch($field['aggregation']) {
  255 + case 'count':
  256 + $formatted_fields[] = "count(" . $field_name . ")" . (empty($field_alias) ? '' : ' ' + $field_alias);
  257 + break;
  258 + case 'sum':
  259 + $formatted_fields[] = "sum(" . $field_name . ")" . (empty($field_alias) ? '' : ' ' + $field_alias);
  260 + break;
  261 + default:
  262 + throw new \Exception("Unknown aggregation type for field $field_name.");
  263 + }
  264 + } else {
  265 + $formatted_fields[] = $field;
  266 + }
  267 + }
  268 + $sql = "SELECT " . implode(",", $formatted_fields) .
  269 + " FROM " . $table .
  270 + " WHERE " . implode(" AND ", $where_clause) .
  271 + (count($order) ? ' ORDER BY ' . implode(",", $order) : '') .
  272 + (count($group) ? ' GROUP BY ' . implode(",", $group) : '');
250 273 $sth = $db->prepare($sql);
251 274 $result = $db->execute($sth, $values);
252 275
35 BuyPlayTix/DataBean/ObjectAdapter.php
@@ -224,7 +224,7 @@ function raw_insert($table, $fields = array()) {
224 224 }
225 225 $this->tables[$table][] = $fields;
226 226 }
227   - // TODO: Add order and grouping
  227 + // TODO: Add order and grouping, aggregate
228 228 function raw_select($table, $fields = array(), $where_fields = array(), $cast_class = NULL, $order = array(), $group = array()) {
229 229 if(!isset($this->tables[$table])) {
230 230 $this->tables[$table] = array();
@@ -278,8 +278,39 @@ function raw_select($table, $fields = array(), $where_fields = array(), $cast_cl
278 278 $results[] = $cast_class($row[0]);
279 279 } else {
280 280 $ret_row = array();
  281 +
  282 + $aggregation = array();
281 283 foreach($fields as $field) {
282   - $ret_row[$field] = $row[$field];
  284 + if(is_array($field)) {
  285 + $field_name = $field['name'];
  286 + $field_alias = $field['alias'];
  287 + if(empty($field_alias)) {
  288 + $field_alias = $field_name;
  289 + }
  290 + switch($field['aggregation']) {
  291 + case 'count':
  292 + if(isset($aggregation[$field_alias])) {
  293 + $aggregation[$field_alias] = $aggregation[$field_alias]++;
  294 + break;
  295 + }
  296 + $aggregation[$field_alias] = 1;
  297 + break;
  298 + case 'sum':
  299 + if(isset($aggregation[$field_alias])) {
  300 + $aggregation[$field_alias] = $aggregation[$field_alias] += $row[$field_name];
  301 + break;
  302 + }
  303 + $aggregation[$field_alias] = $row[$field_name];
  304 + break;
  305 + default:
  306 + throw new \Exception("Unknown aggregation type for field $field_name.");
  307 + }
  308 + } else {
  309 + $ret_row[$field] = $row[$field];
  310 + }
  311 + }
  312 + foreach($aggregation as $field_name => $field_value) {
  313 + $ret_row[$field_name] = $field_value;
283 314 }
284 315 $results[] = $ret_row;
285 316 }

0 comments on commit 51921e0

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