46 $this->
class = $class;
53 private function massageSQL($sql)
58 $sql = str_replace(array(
"[[:<:]]",
"[[:>:]]"), array(
'\\\\b',
'\\\\b'), $sql);
103 $num = func_num_args();
104 for($i = 0; $i < $num; $i += 2)
106 $this->
params[func_get_arg($i)] = $this->massageSQL(func_get_arg($i + 1));
183 $pk = $prototype->getPrimaryKey();
185 $query =
"SELECT $pk FROM {$prototype->table} {$this->tableAlias} {$this->constraints} LIMIT 1";
193 $result = $db->prepare($query);
194 $result->execute($this->
params);
196 if ($result->fetch()) $exists =
true;
200 catch(PDOException $e)
219 $order_by_idx = strpos(strtoupper($this->
constraints),
"ORDER BY");
222 if ($order_by_idx !==
false)
224 $orderBy = substr($this->
constraints, $order_by_idx);
230 $this->
constraints .=
" ".$prototype->getIdentityConstraint();
232 $query =
"SELECT ".$prototype->getFieldList().
" FROM {$prototype->table} {$this->tableAlias} {$this->constraints} $orderBy";
235 trace(
"Page: $this->page Size: $this->size", 3);
244 $result = $db->prepare($query);
245 $result->execute($this->
params);
249 $count = ($this->
page - 1) * $this->size;
256 while($line = $result->fetch())
260 $item->populate($line);
264 if (
$size == 0)
break;
269 catch(PDOException $e)
271 $err =
"query() failed - " . $e->getMessage();
289 if (
count($results) == 1)
294 if (
count($results) == 0)
314 $this->
constraints .=
" ".$prototype->getIdentityConstraint();
316 $query =
"SELECT $func as result FROM {$prototype->table} {$this->tableAlias} {$this->constraints}";
324 $result = $db->prepare($query);
325 $result->execute($this->
params);
327 if ($row = $result->fetch())
329 $value = $row[
'result'];
334 catch(PDOException $e)
336 $err =
"Query::executeValue() failed - " . $e->getMessage();
375 $query =
new Query($class);
377 if (func_num_args() > 1)
381 if (is_object(func_get_arg(1)))
383 $query->filter($filter);
387 if (is_array(func_get_arg($idx)))
389 $query->params(func_get_arg($idx++));
392 if (func_num_args() > $idx)
394 $query->constraints(func_get_arg($idx));
396 if (func_num_args() == $idx + 3)
398 $query->page(func_get_arg($idx + 1), func_get_arg($idx + 2));
403 return $query->execute();
bind()
Binds placeholders to parameter values.
__construct($class, $constraints="")
constraints($constraints)
Sets the constraint clause for the Query.
filter($filter)
Sets a filter to constrain the fields retrieved when the query is executed.
params($params)
Sets the bound parameters array.
static getVersion()
Determine the version of the connected database.
static getConnection()
Retrieves a reference to the global database connection.
Query provides an object-oriented interface for database queries.
executeSingle()
Executes the query and returns a singleton result row.
exists()
Return true if the query matches one or more rows in the database.
tableAlias($alias)
Sometimes when you are creating abonimable SQL constraints you might need to refer back to the main o...
__construct($class, $constraints="")
Creates a new Query object.
executeValue($func)
Query the database to calculate an aggregate value.
static create($class, $constraints="")
Static factory method to create a new Query.
execute()
Executes the query and returns an array of DataItems containing the results (the class of items retur...
count()
Return the count of matching results.
page($page, $size)
Sets the page number and size for constraining the result set by page.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
startsWith($text, $start)
Tests whether a string starts with a given sub-string.
query($class)
Performs a query against the database, returning an array of DataItem objects of the specified class.