Skip to content
Browse files

added aggregation

  • Loading branch information...
1 parent 4835973 commit 51921e028ad5d6b27a19e0616cd434f3f035799c @tthomas48 committed Mar 9, 2013
Showing with 63 additions and 9 deletions.
  1. +30 −7 BuyPlayTix/DataBean/DBAdapter.php
  2. +33 −2 BuyPlayTix/DataBean/ObjectAdapter.php
View
37 BuyPlayTix/DataBean/DBAdapter.php
@@ -190,15 +190,15 @@ function raw_insert($table, $insert_fields = array()) {
$values = array();
foreach($insert_fields as $name => $value) {
$fields[] = $name;
- $values[] = $db->quote($value);
+ $placeholders[] = "?";
+ $values[] = $value;
}
$sql = "insert
into " . $table . "
- (" . implode(',', $fields) . ") values (" . implode(',', $values) . ")";
- return $db->query($sql);
-
-
+ (" . implode(',', $fields) . ") values (" . implode(',', $placeholders) . ")";
+ $sth = $db->prepare($sql);
+ return $db->execute($sth, $values);
}
function raw_update($table, $fields = array(), $where_fields = array()) {
@@ -235,7 +235,7 @@ function raw_select($table, $fields = array(), $where_fields = array(), $cast_cl
$db = DB::getInstance();
$values = array();
- $where_clause = "";
+ $where_clause = array();
foreach($where_fields as $name => $v) {
$condition = '=';
$value = $v;
@@ -246,7 +246,30 @@ function raw_select($table, $fields = array(), $where_fields = array(), $cast_cl
$where_clause[] = $name . " $condition ? ";
$values[] = $value;
}
- $sql = "SELECT " . implode(",", $fields) . " FROM " . $table . " WHERE " . implode(" AND ", $where_clause) . (count($order) ? ' ORDER BY ' . implode(",", $order) : '') . (count($group) ? ' GROUP BY ' . implode(",", $group) : '');
+ $formatted_fields = array();
+ foreach($fields as $field) {
+ if(is_array($field)) {
+ $field_name = $field['name'];
+ $field_alias = $field['alias'];
+ switch($field['aggregation']) {
+ case 'count':
+ $formatted_fields[] = "count(" . $field_name . ")" . (empty($field_alias) ? '' : ' ' + $field_alias);
+ break;
+ case 'sum':
+ $formatted_fields[] = "sum(" . $field_name . ")" . (empty($field_alias) ? '' : ' ' + $field_alias);
+ break;
+ default:
+ throw new \Exception("Unknown aggregation type for field $field_name.");
+ }
+ } else {
+ $formatted_fields[] = $field;
+ }
+ }
+ $sql = "SELECT " . implode(",", $formatted_fields) .
+ " FROM " . $table .
+ " WHERE " . implode(" AND ", $where_clause) .
+ (count($order) ? ' ORDER BY ' . implode(",", $order) : '') .
+ (count($group) ? ' GROUP BY ' . implode(",", $group) : '');
$sth = $db->prepare($sql);
$result = $db->execute($sth, $values);
View
35 BuyPlayTix/DataBean/ObjectAdapter.php
@@ -224,7 +224,7 @@ function raw_insert($table, $fields = array()) {
}
$this->tables[$table][] = $fields;
}
- // TODO: Add order and grouping
+ // TODO: Add order and grouping, aggregate
function raw_select($table, $fields = array(), $where_fields = array(), $cast_class = NULL, $order = array(), $group = array()) {
if(!isset($this->tables[$table])) {
$this->tables[$table] = array();
@@ -278,8 +278,39 @@ function raw_select($table, $fields = array(), $where_fields = array(), $cast_cl
$results[] = $cast_class($row[0]);
} else {
$ret_row = array();
+
+ $aggregation = array();
foreach($fields as $field) {
- $ret_row[$field] = $row[$field];
+ if(is_array($field)) {
+ $field_name = $field['name'];
+ $field_alias = $field['alias'];
+ if(empty($field_alias)) {
+ $field_alias = $field_name;
+ }
+ switch($field['aggregation']) {
+ case 'count':
+ if(isset($aggregation[$field_alias])) {
+ $aggregation[$field_alias] = $aggregation[$field_alias]++;
+ break;
+ }
+ $aggregation[$field_alias] = 1;
+ break;
+ case 'sum':
+ if(isset($aggregation[$field_alias])) {
+ $aggregation[$field_alias] = $aggregation[$field_alias] += $row[$field_name];
+ break;
+ }
+ $aggregation[$field_alias] = $row[$field_name];
+ break;
+ default:
+ throw new \Exception("Unknown aggregation type for field $field_name.");
+ }
+ } else {
+ $ret_row[$field] = $row[$field];
+ }
+ }
+ foreach($aggregation as $field_name => $field_value) {
+ $ret_row[$field_name] = $field_value;
}
$results[] = $ret_row;
}

0 comments on commit 51921e0

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