Framework  3.9
Query Class Reference

Query provides an object-oriented interface for database queries. More...

+ Inheritance diagram for Query:
+ Collaboration diagram for Query:

Public Member Functions

 __construct ($class, $constraints="")
 Creates a new Query object. More...
 
 page ($page, $size)
 Sets the page number and size for constraining the result set by page. More...
 
 tableAlias ($alias)
 Sometimes when you are creating abonimable SQL constraints you might need to refer back to the main object table by a specific alias. More...
 
 exists ()
 Return true if the query matches one or more rows in the database. More...
 
 execute ()
 Executes the query and returns an array of DataItems containing the results (the class of items returned is as specified in the Query constructor). More...
 
 executeSingle ()
 Executes the query and returns a singleton result row. More...
 
 executeValue ($func)
 Query the database to calculate an aggregate value. More...
 
 count ()
 Return the count of matching results. More...
 
- Public Member Functions inherited from AbstractQuery
 constraints ($constraints)
 Sets the constraint clause for the Query. More...
 
 filter ($filter)
 Sets a filter to constrain the fields retrieved when the query is executed. More...
 
 params ($params)
 Sets the bound parameters array. More...
 
 bind ()
 Binds placeholders to parameter values. More...
 

Static Public Member Functions

static create ($class, $constraints="")
 Static factory method to create a new Query. More...
 

Public Attributes

 $page
 
 $size
 
 $tableAlias
 
- Public Attributes inherited from AbstractQuery
 $class
 
 $constraints
 
 $filter
 
 $params
 

Detailed Description

Query provides an object-oriented interface for database queries.

By instantiating Query objects you can specify a full or partial set of criteria and parameters, and then subsequently execute the query to retrieve the results. You can pass around the constructed Query object and execute it multiple times, allowing for lazy evaluation and other performance enhancing tricks.

Author
andy

Definition at line 125 of file query.inc.

Constructor & Destructor Documentation

◆ __construct()

Query::__construct (   $class,
  $constraints = "" 
)

Creates a new Query object.

Parameters
string$classthe DataItem class to be queried
string$constraints[optional] constraint clause for the query
Returns
a new Query object

Reimplemented from AbstractQuery.

Definition at line 137 of file query.inc.

138  {
139  parent::__construct($class, $constraints);
140 
141  $this->page = -1;
142  $this->size = -1;
143 
144  $this->tableAlias = "";
145  }
tableAlias($alias)
Sometimes when you are creating abonimable SQL constraints you might need to refer back to the main o...
Definition: query.inc:171
page($page, $size)
Sets the page number and size for constraining the result set by page.
Definition: query.inc:155

Member Function Documentation

◆ count()

Query::count ( )

Return the count of matching results.

Definition at line 347 of file query.inc.

348  {
349  return $this->executeValue("COUNT(1)");
350  }
executeValue($func)
Query the database to calculate an aggregate value.
Definition: query.inc:309

◆ create()

static Query::create (   $class,
  $constraints = "" 
)
static

Static factory method to create a new Query.

Parameters
string$classthe DataItem class to be queried
string$constraints[optional] constraint clause for the query
Returns
a new Query object

Definition at line 358 of file query.inc.

359  {
360  return new Query($class, $constraints);
361  }
Query provides an object-oriented interface for database queries.
Definition: query.inc:126

◆ execute()

Query::execute ( )

Executes the query and returns an array of DataItems containing the results (the class of items returned is as specified in the Query constructor).

Returns
array an array of DataItems containing the results.

Reimplemented from AbstractQuery.

Definition at line 214 of file query.inc.

215  {
216  $prototype = new $this->class;
217  $prototype->filter = $this->filter;
218 
219  $order_by_idx = strpos(strtoupper($this->constraints), "ORDER BY");
220  $orderBy = "";
221 
222  if ($order_by_idx !== false)
223  {
224  $orderBy = substr($this->constraints, $order_by_idx);
225 
226  $this->constraints = substr($this->constraints, 0, $order_by_idx);
227  }
228 
229  if ($this->constraints == "") $this->constraints = "WHERE 1=1"; //TODO - tidy this up some day
230  $this->constraints .= " ".$prototype->getIdentityConstraint();
231 
232  $query = "SELECT ".$prototype->getFieldList()." FROM {$prototype->table} {$this->tableAlias} {$this->constraints} $orderBy";
233 
234  trace("$query", 3);
235  trace("Page: $this->page Size: $this->size", 3);
236  $items = array();
237 
238  $size = $this->size;
239 
240  try
241  {
243 
244  $result = $db->prepare($query);
245  $result->execute($this->params);
246 
247  if ($this->page > 0)
248  {
249  $count = ($this->page - 1) * $this->size;
250  while($count--)
251  {
252  $result->fetch();
253  }
254  }
255 
256  while($line = $result->fetch())
257  {
258  $item = new $this->class;
259  $item->filter = $this->filter;
260  $item->populate($line);
261  $items[] = $item;
262 
263  --$size;
264  if ($size == 0) break;
265  }
266 
267  unset($result);
268  }
269  catch(PDOException $e)
270  {
271  $err = "query() failed - " . $e->getMessage();
272  trace($err, 2);
273  throw new DataItemException($err);
274  }
275 
276  trace(count($items)." items found", 3);
277  return $items;
278  }
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.
$size
Definition: query.inc:128
count()
Return the count of matching results.
Definition: query.inc:347
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ executeSingle()

Query::executeSingle ( )

Executes the query and returns a singleton result row.

If no results match then a DataNotFoundException is thrown. If more than one row matches, then a DataItemException is thrown.

Returns
DataItem the matching DataItem from the database

Definition at line 286 of file query.inc.

287  {
288  $results = $this->execute();
289  if (count($results) == 1)
290  {
291  return $results[0];
292  }
293 
294  if (count($results) == 0)
295  {
296  throw new DataNotFoundException();
297  }
298  else
299  {
300  throw new DataItemException("Ambiguous singleton query");
301  }
302  }
execute()
Executes the query and returns an array of DataItems containing the results (the class of items retur...
Definition: query.inc:214

◆ executeValue()

Query::executeValue (   $func)

Query the database to calculate an aggregate value.

The database table associated with the specified class is used as the source for the data.

Parameters
string$functhe value or function to retrieve

Definition at line 309 of file query.inc.

310  {
311  $prototype = new $this->class;
312 
313  if ($this->constraints == "") $this->constraints = "WHERE 1=1"; //TODO - tidy this up some day
314  $this->constraints .= " ".$prototype->getIdentityConstraint();
315 
316  $query = "SELECT $func as result FROM {$prototype->table} {$this->tableAlias} {$this->constraints}";
317 
318  trace($query, 3);
319 
320  try
321  {
323 
324  $result = $db->prepare($query);
325  $result->execute($this->params);
326 
327  if ($row = $result->fetch())
328  {
329  $value = $row['result'];
330  }
331 
332  unset($result);
333  }
334  catch(PDOException $e)
335  {
336  $err = "Query::executeValue() failed - " . $e->getMessage();
337  trace($err, 2);
338  throw new DataItemException($err);
339  }
340 
341  return $value;
342  }

◆ exists()

Query::exists ( )

Return true if the query matches one or more rows in the database.

Definition at line 180 of file query.inc.

181  {
182  $prototype = new $this->class;
183  $pk = $prototype->getPrimaryKey();
184 
185  $query = "SELECT $pk FROM {$prototype->table} {$this->tableAlias} {$this->constraints} LIMIT 1";
186 
187  $exists = false;
188 
189  try
190  {
192 
193  $result = $db->prepare($query);
194  $result->execute($this->params);
195 
196  if ($result->fetch()) $exists = true;
197 
198  unset($result);
199  }
200  catch(PDOException $e)
201  {
202  throw new DataItemException($e->getMessage());
203  }
204 
205  return $exists;
206 
207  }

◆ page()

Query::page (   $page,
  $size 
)

Sets the page number and size for constraining the result set by page.

By default results are not paged.

Parameters
$pageinteger the page number
$sizeinteger the number of items per page
Returns
Query reference to the Query object, to allow for call chaining.

Definition at line 155 of file query.inc.

156  {
157  $this->page = $page;
158  $this->size = $size;
159  return $this;
160  }
$page
Definition: query.inc:127

◆ tableAlias()

Query::tableAlias (   $alias)

Sometimes when you are creating abonimable SQL constraints you might need to refer back to the main object table by a specific alias.

In those cases, set the table alias for the main object using this message.

Parameters
$aliasString the table alias name
Returns
Query reference to the Query object, to allow for call chaining.

Definition at line 171 of file query.inc.

172  {
173  $this->tableAlias = $alias;
174  return $this;
175  }

Member Data Documentation

◆ $page

Query::$page

Definition at line 127 of file query.inc.

◆ $size

Query::$size

Definition at line 128 of file query.inc.

◆ $tableAlias

Query::$tableAlias

Definition at line 129 of file query.inc.


The documentation for this class was generated from the following file: