Framework  3.9
grouped_query.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
37 require_once realpath(dirname(__FILE__)."/query.inc");
38 
40 {
41  var $groupBy;
42 
43  public function __construct($class, $constraints = "", $groupBy = "")
44  {
45  parent::__construct($class, $constraints);
46  $this->groupBy = $groupBy;
47  }
48 
49  function groupBy($groupBy)
50  {
51  $this->groupBy = $groupBy;
52  return $this;
53  }
54 
55  function execute()
56  {
57  $prototype = new $this->class;
58 
59  if (!$this->groupBy) $this->groupBy = $prototype->getPrimaryKey();
60 
61  if ($this->filter)
62  {
63  $prototype->filter = $this->filter;
64  }
65 
66  $order_by_idx = strpos(strtoupper($this->constraints), "ORDER BY");
67  if ($order_by_idx !== false)
68  {
69  $orderBy = substr($this->constraints, $order_by_idx);
70 
71  $this->constraints = substr($this->constraints, 0, $order_by_idx);
72  }
73 
74  $fields = $prototype->getFieldList();
75 
76  if ($this->constraints == "") $this->constraints = "WHERE 1=1"; //TODO - tidy this up some day
77 
78  $query = "SELECT $fields FROM {$prototype->table} {$this->constraints} ".$prototype->getIdentityConstraint()." $orderBy";;
79  trace($query, 3);
80 
81  $items = array();
82 
83  $field = $this->groupBy;
84 
85  $useFormat = preg_match("/^[A-Za-z0-9_]*$/", $field) ? false : true;
86 
87  try
88  {
90 
91  $result = $db->prepare($query);
92  $result->execute($this->params);
93 
94  while($line = $result->fetch())
95  {
96  $item = new $this->class; //Hack to work around PHP's stupid implementation of get_class()
97  $item->populate($line);
98  $val = ($useFormat) ? $item->format($field) : $item->get($field);
99 
100  $items[$val][] = $item;
101  }
102 
103  unset($result);
104  }
105  catch(PDOException $e)
106  {
107  $err = "groupedQuery() failed - " . $e->getMessage();
108  trace($err, 2);
109  throw new DataItemException($err);
110  }
111 
112  return $items;
113  }
114 
115 
122  function executeValue($func)
123  {
124  $prototype = new $this->class;
125 
126  if ($this->constraints == "") $this->constraints = "WHERE 1=1"; //TODO - tidy this up some day
127  $this->constraints .= " ".$prototype->getIdentityConstraint();
128 
129  $query = "SELECT {$this->groupBy}, $func as result FROM {$prototype->table} {$this->tableAlias} {$this->constraints} GROUP BY {$this->groupBy}";
130 
131  trace($query, 3);
132 
133  $values = array();
134 
135  try
136  {
138 
139  $result = $db->prepare($query);
140  $result->execute($this->params);
141 
142  while ($row = $result->fetch())
143  {
144  $values[$row[$this->groupBy]] = $row['result'];
145  }
146 
147  unset($result);
148  }
149  catch(PDOException $e)
150  {
151  $err = "Query::executeValue() failed - " . $e->getMessage();
152  trace($err, 2);
153  throw new DataItemException($err);
154  }
155 
156  return $values;
157  }
158 
159  static function create($class, $constraints = "", $groupBy = "")
160  {
162  }
163 }
164 
165 
175 function groupedQuery($class)
176 {
177  $field = $prototype->primary_key;
178 
179  $filter = null;
180 
181  $query = new GroupedQuery($class);
182 
183  if (func_num_args() > 1)
184  {
185  $query->constraints(func_get_arg(1));
186 
187  if (func_num_args() > 2)
188  {
189  $query->groupBy(func_get_arg(2));
190 
191  if (func_num_args() > 3)
192  {
193  $query->filter(func_get_arg(3));
194  }
195  }
196  }
197 
198  return $query->execute();
199 }
200 ?>
constraints($constraints)
Sets the constraint clause for the Query.
Definition: query.inc:68
filter($filter)
Sets a filter to constrain the fields retrieved when the query is executed.
Definition: query.inc:79
params($params)
Sets the bound parameters array.
Definition: query.inc:91
static getConnection()
Retrieves a reference to the global database connection.
groupBy($groupBy)
static create($class, $constraints="", $groupBy="")
__construct($class, $constraints="", $groupBy="")
executeValue($func)
Query the database to calculate aggregate values by grouping.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
groupedQuery($class)
Performs a query against the database, returning an array of arrays of DataItem objects of the specif...