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