Framework  3.9
indexed_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 $indexBy;
42 
43  public function __construct($class, $constraints = "", $indexBy = "")
44  {
45  parent::__construct($class, $constraints);
46  $this->indexBy = $indexBy;
47  }
48 
49  function indexBy($indexBy)
50  {
51  $this->indexBy = $indexBy;
52  return $this;
53  }
54 
55  function execute()
56  {
57  $prototype = new $this->class;
58 
59  if (!$this->indexBy) $this->indexBy = $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";
77  $query = "SELECT $fields FROM {$prototype->table} {$this->constraints} ".$prototype->getIdentityConstraint()." $orderBy";
78  trace("DataItem indexedQuery(): $query", 3);
79 
80  $items = array();
81 
82  $field = $this->indexBy;
83 
84  $useFormat = preg_match("/^[A-Za-z0-9_]*$/", $field) ? false : true;
85 
86  try
87  {
89 
90  $result = $db->prepare($query);
91  $result->execute($this->params);
92 
93  while($line = $result->fetch())
94  {
95  $item = new $this->class;
96  $item->populate($line);
97 
98  $val = ($useFormat) ? $item->format($field) : $item->get($field);
99 
100  if (array_key_exists($val, $items))
101  {
102  // Implicitly promote to array if there is a collision
103  if (!is_array($items[$val]))
104  {
105  $items[$val] = array($items[$val]);
106  }
107  $items[$val][] = $item;
108  }
109  else
110  {
111  $items[$val] = $item;
112  }
113  }
114 
115  unset($result);
116  }
117  catch(PDOException $e)
118  {
119  $err = "indexedQuery() failed - " . $e->getMessage();
120  trace($err, 2);
121  throw new DataItemException($err);
122  }
123 
124  return $items;
125  }
126 
127  static function create($class, $constraints = "", $indexBy = "")
128  {
130  }
131 }
132 
133 
145 function indexedQuery($class)
146 {
147  $field = $prototype->primary_key;
148  $filter = null;
149 
150  $query = new IndexedQuery($class);
151 
152  if (func_num_args() > 1)
153  {
154  $query->constraints(func_get_arg(1));
155 
156  if (func_num_args() > 2)
157  {
158  $query->indexBy(func_get_arg(2));
159 
160  if (func_num_args() > 3)
161  {
162  $query->filter(func_get_arg(3));
163  }
164  }
165  }
166 
167  return $query->execute();
168 }?>
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.
__construct($class, $constraints="", $indexBy="")
static create($class, $constraints="", $indexBy="")
indexBy($indexBy)
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
indexedQuery($class)
Performs a query against the database, returning an array of DataItem objects of the specified class,...