Framework  3.9
iterated_query.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2012 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 
49 class DataItemIterator implements Iterator, Countable, ArrayAccess
50 {
51  var $query;
52  var $result;
53  var $current = null;
54  var $position = -1;
55  var $item;
56 
57  public function __construct($query)
58  {
59  $this->query = $query;
60  $this->item = new $this->query->class;
61  }
62 
63  public function count()
64  {
65  $this->rewind();
66  trace("Row Count: {$this->query->rowCount}", 4);
67  return $this->query->rowCount;
68  }
69 
70  public function rewind()
71  {
72  if ($this->position == 0) return;
73 
74  $this->current = null;
75  $this->position = 0;
76 
77  try
78  {
79  $this->result = $this->query->_runQuery();
80  $this->current = $this->result->fetch();
81  }
82  catch(PDOException $e)
83  {
84  throw new FakoliException($e->getMessage());
85  }
86  }
87 
88  public function current()
89  {
90  $this->item->populate($this->current);
91  return $this->item;
92  }
93 
94  public function valid()
95  {
96  return ($this->current != null);
97  }
98 
99  public function next()
100  {
101  $this->current = $this->result->fetch();
102  ++$this->position;
103  }
104 
105  public function key()
106  {
107  return $this->position;
108  }
109 
110  public function offsetExists($offset)
111  {
112  if (!is_numeric($offset)) return false;
113  return ($offset >= 0 && $offset < $this->query->rowCount);
114  }
115 
116  public function offsetGet ($offset)
117  {
118  if ($offset == $this->position) return $this->item;
119 
120  if ($offset < $this->position)
121  {
122  $this->rewind();
123  }
124  else
125  {
126  $offset -= $this->position;
127  }
128 
129  while($offset--)
130  {
131  $this->next();
132  }
133  return $this->item;
134  }
135 
136  public function offsetSet($offset, $value )
137  {
138  throw new FakoliException("Attempt to write to iterated data query");
139  }
140 
141  public function offsetUnset($offset)
142  {
143  throw new FakoliException("Attempt to write to iterated data query");
144  }
145 }
146 
164 {
165  public function __construct($class, $constraints = "")
166  {
167  parent::__construct($class, $constraints);
168  }
169 
170  function _runQuery()
171  {
172  $prototype = new $this->class;
173  $prototype->filter = $this->filter;
174 
175  $order_by_idx = strpos(strtoupper($this->constraints), "ORDER BY");
176  $orderBy = "";
177 
178  if ($order_by_idx !== false)
179  {
180  $orderBy = substr($this->constraints, $order_by_idx);
181 
182  $this->constraints = substr($this->constraints, 0, $order_by_idx);
183  }
184 
185  if ($this->constraints == "") $this->constraints = "WHERE 1=1"; //TODO - tidy this up some day
186  $this->constraints .= " ".$prototype->getIdentityConstraint();
187 
188  $query = "SELECT ".$prototype->getFieldList()." FROM {$prototype->table} {$this->tableAlias} {$this->constraints} $orderBy";
189 
190  trace("$query", 3);
191 
192  try
193  {
195 
196  $result = $db->prepare($query);
197  $result->execute($this->params);
198  $this->rowCount = $result->rowCount();
199  return $result;
200  }
201  catch(PDOException $e)
202  {
203  throw new FakoliException($e->getMessage());
204  }
205  }
206 
207  function execute()
208  {
209  return new DataItemIterator($this);
210  }
211 
212  static function create($class, $constraints = "")
213  {
214  return new IteratedQuery($class, $constraints);
215  }
216 }
217 
218 
239 function iteratedQuery($class)
240 {
241  $query = new IteratedQuery($class);
242 
243  if (func_num_args() > 1)
244  {
245  $query->constraints(func_get_arg(1));
246 
247  if (func_num_args() > 2)
248  {
249  $query->filter(func_get_arg(3));
250  }
251  }
252 
253  return $query->execute();
254 }?>
constraints($constraints)
Sets the constraint clause for the Query.
Definition: query.inc:68
params($params)
Sets the bound parameters array.
Definition: query.inc:91
static getConnection()
Retrieves a reference to the global database connection.
DataItemIterator is a memory-efficient iterator class that can be used when rendering large data sets...
offsetSet($offset, $value)
IteratedQuery provides a memory-efficient way to query and return large data sets.
__construct($class, $constraints="")
static create($class, $constraints="")
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
iteratedQuery($class)
iteratedQuery() provides a memory-efficient way to query and return large data sets.
query($class)
Performs a query against the database, returning an array of DataItem objects of the specified class.
Definition: query.inc:373