Newer
Older
<?php
/*********************************************************************
class.orm.php
Simple ORM (Object Relational Mapper) for PHP5 based on Django's ORM,
except that complex filter operations are not supported. The ORM simply
supports ANDed filter operations without any GROUP BY support.
Jared Hancock <jared@osticket.com>
Copyright (c) 2006-2013 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
class OrmException extends Exception {}
class OrmConfigurationException extends Exception {}
// Database fields/tables do not match codebase
class InconsistentModelException extends OrmException {
function __construct() {
// Drop the model cache (just incase)
ModelMeta::flushModelCache();
call_user_func_array(array('parent', '__construct'), func_get_args());
}
}
/**
* Meta information about a model including edges (relationships), table
* name, default sorting information, database fields, etc.
*
* This class is constructed and built automatically from the model's
* ::_inspect method using a class's ::$meta array.
*/
class ModelMeta implements ArrayAccess {
static $base = array(
'pk' => false,
'table' => false,
'defer' => array(),
'select_related' => array(),
function __construct($model) {
// Merge ModelMeta from parent model (if inherited)
if (is_subclass_of($parent, 'VerySimpleModel')) {
$parent::_inspect();
$meta = $parent::$meta->extend($model::$meta);
}
else {
$meta = $model::$meta + self::$base;
}
if (!$meta['table'])
throw new OrmConfigurationException(
sprintf(__('%s: Model does not define meta.table'), $model));
elseif (!$meta['pk'])
throw new OrmConfigurationException(
sprintf(__('%s: Model does not define meta.pk'), $model));
// Ensure other supported fields are set and are arrays
foreach (array('pk', 'ordering', 'defer') as $f) {
if (!isset($meta[$f]))
$meta[$f] = array();
elseif (!is_array($meta[$f]))
$meta[$f] = array($meta[$f]);
}
// Break down foreign-key metadata
if (!isset($meta['joins']))
$meta['joins'] = array();
foreach ($meta['joins'] as $field => &$j) {
if ($j['local'])
$meta['foreign_keys'][$j['local']] = $field;
$this->base = $meta;
}
function extend($meta) {
if ($meta instanceof self)
$meta = $meta->base;
return $meta + $this->base + self::$base;
}
function processJoin(&$j) {
if (isset($j['reverse'])) {
list($fmodel, $key) = explode('.', $j['reverse']);
$info = $fmodel::$meta['joins'][$key];
if (!is_array($info['constraint']))
throw new OrmConfigurationException(sprintf(__(
// `reverse` here is the reverse of an ORM relationship
'%s: Reverse does not specify any constraints'),
$j['reverse']));
foreach ($info['constraint'] as $foreign => $local) {
list($L,$field) = is_array($local) ? $local : explode('.', $local);
$constraint[$field ?: $L] = array($fmodel, $foreign);
}
if (!isset($j['list']))
$j['list'] = true;
if (!isset($j['null']))
// By default, reverse releationships can be empty lists
$j['null'] = true;
else {
foreach ($j['constraint'] as $local => $foreign) {
list($class, $field) = $constraint[$local]
= is_array($foreign) ? $foreign : explode('.', $foreign);
if ($j['list'] && !isset($j['broker'])) {
$j['broker'] = 'InstrumentedList';
}
if ($j['broker'] && !class_exists($j['broker'])) {
throw new OrmException($j['broker'] . ': List broker does not exist');
}
foreach ($constraint as $local => $foreign) {
list($class, $field) = $foreign;
if ($local[0] == "'" || $field[0] == "'" || !class_exists($class))
continue;
function offsetGet($field) {
if (!isset($this->base[$field]))
$this->setupLazy($field);
return $this->base[$field];
}
function offsetSet($field, $what) {
$this->base[$field] = $what;
}
function offsetExists($field) {
return isset($this->base[$field]);
}
function offsetUnset($field) {
throw new Exception('Model MetaData is immutable');
}
function setupLazy($what) {
switch ($what) {
case 'fields':
$this->base['fields'] = self::inspectFields();
break;
case 'newInstance':
$class_repr = sprintf(
'O:%d:"%s":0:{}',
strlen($this->model), $this->model
);
$this->base['newInstance'] = function() use ($class_repr) {
return unserialize($class_repr);
};
break;
default:
throw new Exception($what . ': No such meta-data');
}
}
function inspectFields() {
if (!isset(self::$model_cache))
self::$model_cache = function_exists('apc_fetch');
if (self::$model_cache) {
$key = md5(SECRET_SALT . GIT_VERSION . $this['table']);
if ($fields = apc_fetch($key)) {
return $fields;
}
}
$fields = DbEngine::getCompiler()->inspectTable($this['table']);
apc_store($key, $fields);
}
return $fields;
static function flushModelCache() {
if (self::$model_cache)
@apc_clear_cache('user');
}
class VerySimpleModel {
static $meta = array(
'table' => false,
'ordering' => false,
'pk' => false
);
var $dirty = array();
var $__deferred__ = array();
function __construct($row) {
$this->ht = $row;
}
function get($field, $default=false) {
if (array_key_exists($field, $this->ht))
return $this->ht[$field];
elseif (isset(static::$meta['joins'][$field])) {
if (!static::$meta instanceof ModelMeta)
static::_inspect();
$j = static::$meta['joins'][$field];
// Support instrumented lists and such
if (isset($j['list']) && $j['list']) {
$class = $j['fkey'][0];
$fkey = array();
// Localize the foreign key constraint
foreach ($j['constraint'] as $local=>$foreign) {
list($_klas,$F) = $foreign;
$fkey[$F ?: $_klas] = ($local[0] == "'")
? trim($local, "'") : $this->ht[$local];
}
$v = $this->ht[$field] = new $j['broker'](
// Send Model, [Foriegn-Field => Local-Id]
array($class, $fkey)
);
return $v;
}
// Support relationships
elseif (isset($j['fkey'])) {
$criteria = array();
foreach ($j['constraint'] as $local => $foreign) {
if (class_exists($klas))
$class = $klas;
if ($local[0] == "'") {
$criteria[$F] = trim($local,"'");
}
// Does not affect the local model
continue;
}
else {
$criteria[$F] = $this->ht[$local];
}
}
$v = $this->ht[$field] = $class::lookup($criteria);
}
catch (DoesNotExist $e) {
$v = null;
}
elseif (isset($this->__deferred__[$field])) {
// Fetch deferred field
$row = static::objects()->filter($this->getPk())
// XXX: Seems like all the deferred fields should be fetched
->values_flat($field)
->one();
if ($row)
return $this->ht[$field] = $row[0];
}
elseif ($field == 'pk') {
return $this->getPk();
}
if (isset($default))
return $default;
// TODO: Inspect fields from database before throwing this error
throw new OrmException(sprintf(__('%s: %s: Field not defined'),
get_class($this), $field));
}
function __get($field) {
return $this->get($field, null);
}
function __isset($field) {
return array_key_exists($field, $this->ht)
|| isset(static::$meta['joins'][$field]);
function __unset($field) {
if ($this->__isset($field))
unset($this->ht[$field]);
else
unset($this->{$field});
// Update of foreign-key by assignment to model instance
if (isset(static::$meta['joins'][$field])) {
// XXX: This is likely not necessary
if (!isset(static::$meta['joins'][$field]['fkey']))
static::_inspect();
$j = static::$meta['joins'][$field];
if ($j['list'] && ($value instanceof InstrumentedList)) {
// Magic list property
$this->ht[$field] = $value;
return;
}
if (in_array($j['local'], static::$meta['pk'])) {
// Reverse relationship — don't null out local PK
return;
}
// Pass. Set local field to NULL in logic below
}
elseif ($value instanceof $j['fkey'][0]) {
// Capture the object under the object's field name
$this->ht[$field] = $value;
if ($value->__new__)
// save() will be performed when saving this object
$value = null;
else
$value = $value->get($j['fkey'][1]);
// Fall through to the standard logic below
}
else
throw new InvalidArgumentException(
sprintf(__('Expecting NULL or instance of %s. Got a %s instead'),
$j['fkey'][0], get_class($value)));
// Capture the foreign key id value
$field = $j['local'];
}
// elseif $field is in a relationship, adjust the relationship
elseif (isset(static::$meta['foreign_keys'][$field])) {
// meta->foreign_keys->{$field} points to the property of the
// foreign object. For instance 'object_id' points to 'object'
$related = static::$meta['foreign_keys'][$field];
}
$old = isset($this->ht[$field]) ? $this->ht[$field] : null;
if ($old != $value) {
// isset should not be used here, because `null` should not be
// replaced in the dirty array
if (!array_key_exists($field, $this->dirty))
$this->dirty[$field] = $old;
if ($related)
// $related points to a foreign object propery. If setting a
// new object_id value, the relationship to object should be
// cleared and rebuilt
unset($this->ht[$related]);
$this->ht[$field] = $value;
}
function __set($field, $value) {
return $this->set($field, $value);
}
function setAll($props) {
foreach ($props as $field=>$value)
$this->set($field, $value);
}
static function __oninspect() {}
static function _inspect() {
if (!static::$meta instanceof ModelMeta) {
static::$meta = new ModelMeta(get_called_class());
// Let the model participate
static::__oninspect();
/**
* objects
*
* Retrieve a QuerySet for this model class which can be used to fetch
* models from the connected database. Subclasses can override this
* method to apply forced constraints on the QuerySet.
*/
static function objects() {
return new QuerySet(get_called_class());
}
/**
* lookup
*
* Retrieve a record by its primary key. This method may be short
* circuited by model caching if the record has already been loaded by
* the database. In such a case, the database will not be consulted for
* the model's data.
*
* This method can be called with an array of keyword arguments matching
* the PK of the object or the values of the primary key. Both of these
* usages are correct:
*
* >>> User::lookup(1)
* >>> User::lookup(array('id'=>1))
*
* For composite primary keys and the first usage, pass the values in
* the order they are given in the Model's 'pk' declaration in its meta
* data.
*
* Parameters:
* $criteria - (mixed) primary key for the sought model either as
* arguments or key/value array as the function's first argument
*
* Returns:
* (Object<Model>|null) a single instance of the sought model or null if
* no such instance exists.
// Autoinsepct model
static::_inspect();
// Model::lookup(1), where >1< is the pk value
if (!is_array($criteria)) {
$criteria = array();
foreach (func_get_args() as $i=>$f)
$criteria[static::$meta['pk'][$i]] = $f;
// Only consult cache for PK lookup, which is assumed if the
// values are passed as args rather than an array
if ($cached = ModelInstanceManager::checkCache(get_called_class(),
$criteria))
return $cached;
try {
return static::objects()->filter($criteria)->one();
}
catch (DoesNotExist $e) {
return null;
}
$ex = DbEngine::delete($this);
try {
$ex->execute();
if ($ex->affected_rows() != 1)
return false;
Signal::send('model.deleted', $this);
}
catch (OrmException $e) {
return false;
}
if ($this->__deleted__)
throw new OrmException('Trying to update a deleted object');
$pk = static::$meta['pk'];
$wasnew = $this->__new__;
// First, if any foreign properties of this object are connected to
// another *new* object, then save those objects first and set the
// local foreign key field values
foreach (static::$meta['joins'] as $prop => $j) {
if (isset($this->ht[$prop])
&& ($foreign = $this->ht[$prop])
&& $foreign instanceof VerySimpleModel
&& !in_array($j['local'], $pk)
&& null === $this->get($j['local'])
) {
if ($foreign->__new__ && !$foreign->save())
return false;
$this->set($j['local'], $foreign->get($j['fkey'][1]));
}
}
// If there's nothing in the model to be saved, then we're done
$ex = DbEngine::save($this);
try {
$ex->execute();
if ($ex->affected_rows() != 1) {
// This doesn't really signify an error. It just means that
// the database believes that the row did not change. For
// inserts though, it's a deal breaker
if ($this->__new__)
return false;
else
// No need to reload the record if requested — the
// database didn't update anything
$refetch = false;
}
catch (OrmException $e) {
return false;
// XXX: Ensure AUTO_INCREMENT is set for the field
$this->ht[$pk[0]] = $ex->insert_id();
Signal::send('model.created', $this);
}
else {
$data = array('dirty' => $this->dirty);
Signal::send('model.updated', $this, $data);
}
# Refetch row from database
if ($refetch) {
// Preserve non database information such as list relationships
// across the refetch
static::objects()->filter($this->getPk())->values()->one()
+ $this->ht;
}
if ($wasnew) {
// Attempt to update foreign, unsaved objects with the PK of
// this newly created object
foreach (static::$meta['joins'] as $prop => $j) {
if (isset($this->ht[$prop])
&& ($foreign = $this->ht[$prop])
&& in_array($j['local'], $pk)
) {
if ($foreign instanceof VerySimpleModel
&& null === $foreign->get($j['fkey'][1])
) {
$foreign->set($j['fkey'][1], $this->get($j['local']));
}
elseif ($foreign instanceof InstrumentedList) {
foreach ($foreign as $item) {
if (null === $item->get($j['fkey'][1]))
$item->set($j['fkey'][1], $this->get($j['local']));
}
}
}
}
}
static function create($ht=false) {
if (!$ht) $ht=array();
$class = get_called_class();
$i = new $class(array());
$i->__new__ = true;
foreach ($ht as $field=>$value)
if (!is_array($value))
$i->set($field, $value);
return $i;
}
private function getPk() {
$pk = array();
foreach ($this::$meta['pk'] as $f)
$pk[$f] = $this->ht[$f];
return $pk;
}
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/**
* AnnotatedModel
*
* Simple wrapper class which allows wrapping and write-protecting of
* annotated fields retrieved from the database. Instances of this class
* will delegate most all of the heavy lifting to the wrapped Model instance.
*/
class AnnotatedModel {
var $model;
var $annotations;
function __construct($model, $annotations) {
$this->model = $model;
$this->annotations = $annotations;
}
function __get($what) {
return $this->get($what);
}
function get($what) {
if (isset($this->annotations[$what]))
return $this->annotations[$what];
return $this->model->get($what, null);
}
function __set($what, $to) {
return $this->set($what, $to);
}
function set($what, $to) {
if (isset($this->annotations[$what]))
throw new OrmException('Annotated fields are read-only');
return $this->model->set($what, $to);
}
// Delegate everything else to the model
function __call($what, $how) {
return call_user_func_array(array($this->model, $what), $how);
}
}
function SqlFunction($name) {
$this->func = $name;
$this->args = array_slice(func_get_args(), 1);
}
function input($what, $compiler, $model) {
if ($what instanceof SqlFunction)
$A = $what->toSql($compiler, $model);
elseif ($what instanceof Q)
$A = $compiler->compileQ($what, $model);
else
$A = $compiler->input($what);
return $A;
}
function toSql($compiler, $model=false, $alias=false) {
$args = array();
foreach ($this->args as $A) {
$args[] = $this->input($A, $compiler, $model);
}
return sprintf('%s(%s)%s', $this->func, implode(',', $args),
$alias && $this->alias ? ' AS '.$compiler->quote($this->alias) : '');
}
function getAlias() {
return $this->alias;
}
function setAlias($alias) {
$this->alias = $alias;
static function __callStatic($func, $args) {
$I = new static($func);
$I->args = $args;
return $I;
}
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
class SqlCase extends SqlFunction {
var $cases = array();
var $else = false;
static function N() {
return new static('CASE');
}
function when($expr, $result) {
$this->cases[] = array($expr, $result);
return $this;
}
function otherwise($result) {
$this->else = $result;
return $this;
}
function toSql($compiler, $model=false, $alias=false) {
$cases = array();
foreach ($this->cases as $A) {
list($expr, $result) = $A;
$expr = $this->input($expr, $compiler, $model);
$result = $this->input($result, $compiler, $model);
$cases[] = "WHEN {$expr} THEN {$result}";
}
if ($this->else) {
$else = $this->input($this->else, $compiler, $model);
$cases[] = "ELSE {$else}";
}
return sprintf('CASE %s END%s', implode(' ', $cases),
$alias && $this->alias ? ' AS '.$compiler->quote($this->alias) : '');
}
}
class SqlExpr extends SqlFunction {
function __construct($args) {
$this->args = $args;
}
function toSql($compiler, $model=false, $alias=false) {
$O = array();
foreach ($this->args as $field=>$value) {
list($field, $op) = $compiler->getField($field, $model);
if (is_callable($op))
$O[] = call_user_func($op, $field, $value, $model);
else
$O[] = sprintf($op, $field, $compiler->input($value));
}
return implode(' ', $O) . ($alias ? ' AS ' . $alias : '');
}
}
class SqlExpression extends SqlFunction {
var $operator;
var $operands;
function toSql($compiler, $model=false, $alias=false) {
$O = array();
foreach ($this->args as $operand) {
if ($operand instanceof SqlFunction)
$O[] = $operand->toSql($compiler, $model);
else
$O[] = $compiler->input($operand);
}
return implode(' '.$this->func.' ', $O)
. ($alias ? ' AS '.$compiler->quote($alias) : '');
}
static function __callStatic($operator, $operands) {
switch ($operator) {
case 'minus':
$operator = '-'; break;
case 'plus':
$operator = '+'; break;
case 'times':
$operator = '*'; break;
case 'bitand':
$operator = '&'; break;
case 'bitor':
$operator = '|'; break;
default:
throw new InvalidArgumentException('Invalid operator specified');
}
return parent::__callStatic($operator, $operands);
}
}
class SqlInterval extends SqlFunction {
var $type;
function toSql($compiler, $model=false, $alias=false) {
$A = $this->args[0];
if ($A instanceof SqlFunction)
$A = $A->toSql($compiler, $model);
else
$A = $compiler->input($A);
return sprintf('INTERVAL %s %s',
$A,
$this->func)
. ($alias ? ' AS '.$compiler->quote($alias) : '');
}
static function __callStatic($interval, $args) {
if (count($args) != 1) {
throw new InvalidArgumentException("Interval expects a single interval value");
}
return parent::__callStatic($interval, $args);
}
}
class SqlField extends SqlFunction {
function __construct($field) {
$this->field = $field;
}
function toSql($compiler, $model=false, $alias=false) {
list($field) = $compiler->getField($this->field, $model);
return $field;
}
}
class SqlCode extends SqlFunction {
function __construct($code) {
$this->code = $code;
}
function toSql($compiler, $model=false, $alias=false) {
return $this->code;
}
}
class SqlAggregate extends SqlFunction {
var $func;
var $expr;
var $distinct=false;
var $constraint=false;
function __construct($func, $expr, $distinct=false, $constraint=false) {
$this->func = $func;
$this->expr = $expr;
$this->distinct = $distinct;
if ($constraint instanceof Q)
$this->constraint = $constraint;
elseif ($constraint)
$this->constraint = new Q($constraint);
}
static function __callStatic($func, $args) {
$distinct = @$args[1] ?: false;
$constraint = @$args[2] ?: false;
return new static($func, $args[0], $distinct, $constraint);
}
function toSql($compiler, $model=false, $alias=false) {
$options = array('constraint' => $this->constraint, 'model' => true);
// For DISTINCT, require a field specification — not a relationship
// specification.
$E = $this->expr;
if ($E instanceof SqlFunction) {
$field = $E->toSql($compiler, $model, $alias);
}
else {
list($field, $rmodel) = $compiler->getField($E, $model, $options);
if ($this->distinct) {
$pk = false;
foreach ($rmodel::$meta['pk'] as $f) {
$pk |= false !== strpos($field, $f);
}
if (!$pk) {
// Try and use the foriegn primary key
if (count($rmodel::$meta['pk']) == 1) {
list($field) = $compiler->getField(
$this->expr . '__' . $rmodel::$meta['pk'][0],
$model, $options);
}
else {
throw new OrmException(
sprintf('%s :: %s', $rmodel, $field) .
': DISTINCT aggregate expressions require specification of a single primary key field of the remote model'
);
}
}
}
return sprintf('%s(%s%s)%s', $this->func,
$this->distinct ? 'DISTINCT ' : '', $field,
$alias && $this->alias ? ' AS '.$compiler->quote($this->alias) : '');
}
function getFieldName() {
return strtolower(sprintf('%s__%s', $this->args[0], $this->func));
}
}
class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countable {
var $model;
var $constraints = array();
var $ordering = array();
var $limit = false;
var $offset = 0;
var $related = array();
var $values = array();
var $defer = array();
var $extra = array();
var $distinct = array();
var $lock = false;
const LOCK_EXCLUSIVE = 1;
const LOCK_SHARED = 2;
function __construct($model) {
$this->model = $model;
}
function filter() {
// Multiple arrays passes means OR
foreach (func_get_args() as $Q) {
$this->constraints[] = $Q instanceof Q ? $Q : new Q($Q);
return $this;
}
function exclude() {
foreach (func_get_args() as $Q) {
$this->constraints[] = $Q instanceof Q ? $Q->negate() : Q::not($Q);
function defer() {
foreach (func_get_args() as $f)
$this->defer[$f] = true;
return $this;
}
function order_by($order) {
$this->ordering = array_merge($this->ordering,
is_array($order) ? $order : func_get_args());
function getSortFields() {
$ordering = $this->ordering;
if ($this->extra['order_by'])
$ordering = array_merge($ordering, $this->extra['order_by']);
return $ordering;
}
function lock($how=false) {
$this->lock = $how ?: self::LOCK_EXCLUSIVE;
return $this;
}
function limit($count) {
$this->limit = $count;
return $this;
}
function offset($at) {
$this->offset = $at;
return $this;
}
function isWindowed() {
return $this->limit || $this->offset;
}
function select_related() {
$this->related = array_merge($this->related, func_get_args());
return $this;
}
function extra(array $extra) {
foreach ($extra as $section=>$info) {
$this->extra[$section] = array_merge($this->extra[$section] ?: array(), $info);
}
return $this;
}
function distinct() {
foreach (func_get_args() as $D)
$this->distinct[] = $D;
return $this;
}
function models() {
$this->iterator = 'ModelInstanceManager';
$this->values = $this->related = array();
return $this;
}
foreach (func_get_args() as $A)
$this->values[$A] = $A;
// This disables related models
$this->related = false;
function values_flat() {
$this->values = func_get_args();
$this->iterator = 'FlatArrayIterator';
// This disables related models
$this->related = false;
function all() {
return $this->getIterator()->asArray();
}
$list = $this->limit(1)->all();
/**
* one
*
* Finds and returns a single model instance based on the criteria in
* this QuerySet instance.
*
* Throws:
* DoesNotExist - if no such model exists with the given criteria
* ObjectNotUnique - if more than one model matches the given criteria
*
* Returns:
* (Object<Model>) a single instance of the sought model is guarenteed.
* If no such model or multiple models exist, an exception is thrown.
*/
function one() {
$list = $this->all();
if (count($list) == 0)
throw new DoesNotExist();
elseif (count($list) > 1)
throw new ObjectNotUnique('One object was expected; however '
.'multiple objects in the database matched the query. '
.sprintf('In fact, there are %d matching objects.', count($list))
);
// TODO: Throw error if more than one result from database
// Defer to the iterator if fetching already started
if (isset($this->_iterator)) {
return $this->_iterator->count();
}
elseif (isset($this->_count)) {
return $this->_count;
}
$class = $this->compiler;
$compiler = new $class();
return $this->_count = $compiler->compileCount($this);
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
/**
* exists
*
* Determines if there are any rows in this QuerySet. This can be
* achieved either by evaluating a SELECT COUNT(*) query or by
* attempting to fetch the first row from the recordset and return
* boolean success.
*
* Parameters:
* $fetch - (bool) TRUE if a compile and fetch should be attempted
* instead of a SELECT COUNT(*). This would be recommended if an
* accurate count is not required and the records would be fetched
* if this method returns TRUE.
*
* Returns:
* (bool) TRUE if there would be at least one record in this QuerySet
*/
function exists($fetch=false) {
if ($fetch) {
return (bool) $this[0];
}
return $this->count() > 0;
}
function annotate($annotations) {
if (!is_array($annotations))
$annotations = func_get_args();
foreach ($annotations as $name=>$A) {
if (is_int($name))
$name = $A->getFieldName();
$A->setAlias($name);
}
function delete() {
$class = $this->compiler;
$compiler = new $class();
// XXX: Mark all in-memory cached objects as deleted
$ex = $compiler->compileBulkDelete($this);
$ex->execute();
return $ex->affected_rows();
}
function update(array $what) {
$class = $this->compiler;
$compiler = new $class;
$ex = $compiler->compileBulkUpdate($this, $what);
$ex->execute();
return $ex->affected_rows();
}
function __clone() {
unset($this->_iterator);
unset($this->query);
}
// IteratorAggregate interface
function getIterator() {
$class = $this->iterator;
$this->_iterator = new $class($this);
return $this->_iterator;
}
// ArrayAccess interface
function offsetExists($offset) {
return $this->getIterator()->offsetExists($offset);
}
function offsetGet($offset) {
return $this->getIterator()->offsetGet($offset);
}
function offsetUnset($a) {
throw new Exception(__('QuerySet is read-only'));
throw new Exception(__('QuerySet is read-only'));
return (string) $this->getQuery();
function getQuery($options=array()) {
if (isset($this->query))
return $this->query;
// Load defaults from model
$model = $this->model;
$query = clone $this;
if (!$query->ordering && isset($model::$meta['ordering']))
$query->ordering = $model::$meta['ordering'];
if (false !== $query->related && !$query->values && $model::$meta['select_related'])
$query->related = $model::$meta['select_related'];
if (!$query->defer && $model::$meta['defer'])
$query->defer = $model::$meta['defer'];
$class = $this->compiler;
$compiler = new $class($options);
$this->query = $compiler->compileSelect($query);
/**
* Fetch a model class which can be used to render the QuerySet as a
* subquery to be used as a JOIN.
*/
function asView() {
$unique = spl_object_hash($this);
$classname = "QueryView{$unique}";
$class = <<<EOF
class {$classname} extends VerySimpleModel {
static \$meta = array(
'view' => true,
);
static \$queryset;
static function getQuery(\$compiler) {
return ' ('.static::\$queryset->getQuery().') ';
}
}
EOF;
eval($class); // Ugh
$classname::$queryset = $this;
return $classname;
}
function serialize() {
$info = get_object_vars($this);
unset($info['query']);
unset($info['limit']);
unset($info['offset']);
unset($info['_iterator']);
return serialize($info);
}
function unserialize($data) {
$data = unserialize($data);
foreach ($data as $name => $val) {
$this->{$name} = $val;
}
}
class DoesNotExist extends Exception {}
class ObjectNotUnique extends Exception {}
abstract class ResultSet implements Iterator, ArrayAccess, Countable {
var $resource;
var $position = 0;
var $queryset;
function __construct($queryset=false) {
$this->queryset = $queryset;
if ($queryset) {
$this->model = $queryset->model;
$this->resource = $queryset->getQuery();
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
}
}
abstract function fillTo($index);
function asArray() {
$this->fillTo(PHP_INT_MAX);
return $this->cache;
}
// Iterator interface
function rewind() {
$this->position = 0;
}
function current() {
$this->fillTo($this->position);
return $this->cache[$this->position];
}
function key() {
return $this->position;
}
function next() {
$this->position++;
}
function valid() {
$this->fillTo($this->position);
return count($this->cache) > $this->position;
}
// ArrayAccess interface
function offsetExists($offset) {
$this->fillTo($offset);
return $this->position >= $offset;
}
function offsetGet($offset) {
$this->fillTo($offset);
return $this->cache[$offset];
}
function offsetUnset($a) {
throw new Exception(sprintf(__('%s is read-only'), get_class($this)));
}
function offsetSet($a, $b) {
throw new Exception(sprintf(__('%s is read-only'), get_class($this)));
}
// Countable interface
function count() {
return count($this->asArray());
}
}
class ModelInstanceManager extends ResultSet {
var $model;
var $map;
static $objectCache = array();
function __construct($queryset=false) {
parent::__construct($queryset);
if ($queryset) {
$this->map = $this->resource->getMap();
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
function cache($model) {
$model::_inspect();
$key = sprintf('%s.%s',
$model::$meta->model, implode('.', $model->pk));
self::$objectCache[$key] = $model;
}
/**
* uncache
*
* Drop the cached reference to the model. If the model is deleted
* database-side. Lookups for the same model should not be short
* circuited to retrieve the cached reference.
*/
static function uncache($model) {
$key = sprintf('%s.%s',
$model::$meta->model, implode('.', $model->pk));
unset(self::$objectCache[$key]);
}
static function checkCache($modelClass, $fields) {
foreach ($modelClass::$meta['pk'] as $f)
$key .= '.'.$fields[$f];
return @self::$objectCache[$key];
}
/**
* getOrBuild
*
* Builds a new model from the received fields or returns the model
* already stashed in the model cache. Caching helps to ensure that
* multiple lookups for the same model identified by primary key will
* fetch the exact same model. Therefore, changes made to the model
* anywhere in the project will be reflected everywhere.
*
* For annotated models (models build from querysets with annotations),
* the built or cached model is wrapped in an AnnotatedModel instance.
* The annotated fields are in the AnnotatedModel instance and the
* database-backed fields are managed by the Model instance.
*/
function getOrBuild($modelClass, $fields, $cache=true) {
// Check for NULL primary key, used with related model fetching. If
// the PK is NULL, then consider the object to also be NULL
foreach ($modelClass::$meta['pk'] as $pkf) {
if (!isset($fields[$pkf])) {
return null;
}
}
$annotations = $this->queryset->annotations;
$extras = array();
// For annotations, drop them from the $fields list and add them to
// an $extras list. The fields passed to the root model should only
// be the root model's fields. The annotated fields will be wrapped
// using an AnnotatedModel instance.
if ($annotations && $modelClass == $this->model) {
if (isset($fields[$name])) {
$extras[$name] = $fields[$name];
unset($fields[$name]);
}
}
}
// Check the cache for the model instance first
if (!($m = self::checkCache($modelClass, $fields))) {
// Construct and cache the object
$m = new $modelClass($fields);
// XXX: defer may refer to fields not in this model
$m->__deferred__ = $this->queryset->defer;
$m->__onload();
if ($cache)
$this->cache($m);
}
// Wrap annotations in an AnnotatedModel
if ($extras) {
$m = new AnnotatedModel($m, $extras);
// TODO: If the model has deferred fields which are in $fields,
// those can be resolved here
return $m;
}
/**
* buildModel
*
* This method builds the model including related models from the record
* received. For related recordsets, a $map should be setup inside this
* object prior to using this method. The $map is assumed to have this
* configuration:
*
* array(array(<fieldNames>, <modelClass>, <relativePath>))
*
* Where $modelClass is the name of the foreign (with respect to the
* root model ($this->model), $fieldNames is the number and names of
* fields in the row for this model, $relativePath is the path that
* describes the relationship between the root model and this model,
* 'user__account' for instance.
function buildModel($row) {
// TODO: Traverse to foreign keys
if ($this->map) {
if ($this->model != $this->map[0][1])
throw new OrmException('Internal select_related error');
$offset = 0;
foreach ($this->map as $info) {
@list($fields, $model_class, $path) = $info;
$values = array_slice($row, $offset, count($fields));
$record = array_combine($fields, $values);
if (!$path) {
// Build the root model
$model = $this->getOrBuild($this->model, $record);
$i = 0;
// Traverse the declared path and link the related model
$tail = array_pop($path);
$m = $model;
foreach ($path as $field) {
$m = $m->get($field);
}
$m->set($tail, $this->getOrBuild($model_class, $record));
$model = $this->getOrBuild($this->model, $row);
$func = ($this->map) ? 'getRow' : 'getArray';
while ($this->resource && $index >= count($this->cache)) {
if ($row = $this->resource->{$func}()) {
$this->cache[] = $this->buildModel($row);
} else {
$this->resource->close();
$this->resource = null;
break;
}
}
}
}
class FlatArrayIterator extends ResultSet {
function __construct($queryset) {
$this->resource = $queryset->getQuery();
}
function fillTo($index) {
while ($this->resource && $index >= count($this->cache)) {
if ($row = $this->resource->getRow()) {
} else {
$this->resource->close();
$this->resource = null;
break;
}
}
}
}
class HashArrayIterator extends ResultSet {
function __construct($queryset) {
$this->resource = $queryset->getQuery();
}
function fillTo($index) {
while ($this->resource && $index >= count($this->cache)) {
if ($row = $this->resource->getArray()) {
$this->cache[] = $row;
} else {
$this->resource->close();
$this->resource = null;
break;
}
}
}
}
class InstrumentedList extends ModelInstanceManager {
function __construct($fkey, $queryset=false) {
list($model, $this->key) = $fkey;
$queryset = $model::objects()->filter($this->key);
parent::__construct($queryset);
function add($object, $at=false) {
if (!$object || !$object instanceof $this->model)
throw new Exception(sprintf(
'Attempting to add invalid object to list. Expected <%s>, but got <%s>',
$this->model,
get_class($object)
));
foreach ($this->key as $field=>$value)
$object->set($field, $value);
if (!$object->__new__)
$object->save();
if ($at !== false)
$this->cache[$at] = $object;
else
$this->cache[] = $object;
function remove($object, $delete=true) {
if ($delete)
$object->delete();
else
$object->set($this->key, null);
function reset() {
$this->cache = array();
}
/**
* Reduce the list to a subset using a simply key/value constraint. New
* items added to the subset will have the constraint automatically
* added to all new items.
*/
function window($constraint) {
$model = $this->model;
$meta = $model::$meta;
$key = $this->key;
foreach ($constraint as $field=>$value) {
if (!is_string($field) || false === in_array($field, $meta['fields']))
throw new OrmException('InstrumentedList windowing must be performed on local fields only');
$key[$field] = $value;
}
return new static(array($this->model, $key), $this->filter($constraint));
}
// QuerySet delegates
function count() {
return $this->objects()->count();
}
function exists() {
return $this->queryset->exists();
}
function expunge() {
if ($this->queryset->delete())
$this->reset();
function update(array $what) {
return $this->queryset->update($what);
}
// Fetch a new QuerySet
function objects() {
return clone $this->queryset;
function offsetUnset($a) {
$this->fillTo($a);
$this->cache[$a]->delete();
}
function offsetSet($a, $b) {
$this->fillTo($a);
$this->cache[$a]->delete();
$this->add($b, $a);
function __call($what, $how) {
return call_user_func_array(array($this->objects(), $what), $how);
var $joins = array();
var $aliases = array();
var $alias_num = 1;
function __construct($options=false) {
if ($options)
$this->options = array_merge($this->options, $options);
}
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
/**
* Handles breaking down a field or model search descriptor into the
* model search path, field, and operator parts. When used in a queryset
* filter, an expression such as
*
* user__email__hostname__contains => 'foobar'
*
* would be broken down to search from the root model (passed in,
* perhaps a ticket) to the user and email models by inspecting the
* model metadata 'joins' property. The 'constraint' value found there
* will be used to build the JOIN sql clauses.
*
* The 'hostname' will be the field on 'email' model that should be
* compared in the WHERE clause. The comparison should be made using a
* 'contains' function, which in MySQL, might be implemented using
* something like "<lhs> LIKE '%foobar%'"
*
* This function will rely heavily on the pushJoin() function which will
* handle keeping track of joins made previously in the query and
* therefore prevent multiple joins to the same table for the same
* reason. (Self joins are still supported).
*
* Comparison functions supported by this function are defined for each
* respective SqlCompiler subclass; however at least these functions
* should be defined:
*
* function a__function => b
* ----------+------------------------------------------------
* exact | a is exactly equal to b
* gt | a is greater than b
* lte | b is greater than a
* lt | a is less than b
* gte | b is less than a
* ----------+------------------------------------------------
* contains | (string) b is contained within a
* statswith | (string) first len(b) chars of a are exactly b
* endswith | (string) last len(b) chars of a are exactly b
* like | (string) a matches pattern b
* ----------+------------------------------------------------
* in | a is in the list or the nested queryset b
* ----------+------------------------------------------------
* isnull | a is null (if b) else a is not null
*
* If no comparison function is declared in the field descriptor,
* 'exact' is assumed.
*
* Parameters:
* $field - (string) name of the field to join
* $model - (VerySimpleModel) root model for references in the $field
* parameter
* $options - (array) extra options for the compiler
* 'table' => return the table alias rather than the field-name
* 'model' => return the target model class rather than the operator
* 'constraint' => extra constraint for join clause
*
* Returns:
* (mixed) Usually array<field-name, operator> where field-name is the
* name of the field in the destination model, and operator is the
* requestion comparison method.
*/
function getField($field, $model, $options=array()) {
// Break apart the field descriptor by __ (double-underbars). The
// first part is assumed to be the root field in the given model.
// The parts after each of the __ pieces are links to other tables.
// The last item (after the last __) is allowed to be an operator
// specifiction.
$parts = explode('__', $field);
$operator = static::$operators['exact'];
if (!isset($options['table'])) {
if (isset(static::$operators[$field])) {
$operator = static::$operators[$field];
$field = array_pop($parts);
}
$rootModel = $model;
// Call pushJoin for each segment in the join path. A new JOIN
// fragment will need to be emitted and/or cached
$joins = array();
$push = function($p, $path, $model) use (&$joins) {
$model::_inspect();
if (!($info = $model::$meta['joins'][$p])) {
throw new OrmException(sprintf(
'Model `%s` does not have a relation called `%s`',
$model, $p));
}
$crumb = implode('__', $path);
$tip = ($crumb) ? "{$crumb}__{$p}" : $p;
$joins[] = array($crumb, $tip, $model, $info);
// Roll to foreign model
return $info['fkey'];
foreach ($parts as $p) {
list($model) = $push($p, $path, $model);
$path[] = $p;
}
// If comparing a relationship, join the foreign table
// This is a comparison with a relationship — use the foreign key
if (isset($model::$meta['joins'][$field])) {
list($model, $field) = $push($field, $path, $model);
}
// Add the conststraint as the last arg to the last join
if (isset($options['constraint'])) {
$joins[count($joins)-1][] = $options['constraint'];
}
// Apply the joins list to $this->pushJoin
foreach ($joins as $A) {
$alias = call_user_func_array(array($this, 'pushJoin'), $A);
}
if (!isset($alias)) {
// Determine the alias for the root model table
$alias = (isset($this->joins['']))
? $this->joins['']['alias']
: $this->quote($rootModel::$meta['table']);
if (isset($options['table']) && $options['table'])
elseif (isset($this->annotations[$field]))
$field = $this->annotations[$field];
elseif ($alias)
$field = $alias.'.'.$this->quote($field);
if (isset($options['model']) && $options['model'])
$operator = $model;
/**
* Uses the compiler-specific `compileJoin` function to compile the join
* statement fragment, and caches the result in the local $joins list. A
* new alias is acquired using the `nextAlias` function which will be
* associated with the join. If the same path is requested again, the
* algorithm is short-circuited and the originally-assigned table alias
* is returned immediately.
*/
function pushJoin($tip, $path, $model, $info, $constraint=false) {
// TODO: Build the join statement fragment and return the table
// alias. The table alias will be useful where the join is used in
// the WHERE and ORDER BY clauses
// If the join already exists for the statement-being-compiled, just
// return the alias being used.
if (!$constraint && isset($this->joins[$path]))
return $this->joins[$path]['alias'];
// TODO: Support only using aliases if necessary. Use actual table
// names for everything except oddities like self-joins
$alias = $this->nextAlias();
// Keep an association between the table alias and the model. This
// will make model construction much easier when we have the data
// and the table alias from the database.
$this->aliases[$alias] = $model;
// TODO: Stash joins and join constraints into local ->joins array.
// This will be useful metadata in the executor to construct the
// final models for fetching
// TODO: Always use a table alias. This will further help with
// coordination between the data returned from the database (where
// table alias is available) and the corresponding data.
$T = array('alias' => $alias);
$this->joins[$path] = $T;
$this->joins[$path]['sql'] = $this->compileJoin($tip, $model, $alias, $info, $constraint);
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
/**
* compileQ
*
* Build a constraint represented in an arbitrarily nested Q instance.
* The placement of the compiled constraint is also considered and
* represented in the resulting CompiledExpression instance.
*
* Parameters:
* $Q - (Q) constraint represented in a Q instance
* $model - (VerySimpleModel) root model for all the field references in
* the Q instance
* $slot - (int) slot for inputs to be placed. Useful to differenciate
* inputs placed in the joins and where clauses for SQL engines
* which do not support named parameters.
*
* Returns:
* (CompiledExpression) object containing the compiled expression (with
* AND, OR, and NOT operators added). Furthermore, the $type attribute
* of the CompiledExpression will allow the compiler to place the
* constraint properly in the WHERE or HAVING clause appropriately.
*/
function compileQ(Q $Q, $model, $slot=false) {
$type = CompiledExpression::TYPE_WHERE;
foreach ($Q->constraints as $field=>$value) {
// Handle nested constraints
if ($value instanceof Q) {
$filter[] = $T = $this->compileQ($value, $model, $slot);
// Bubble up HAVING constraints
if ($T instanceof CompiledExpression
&& $T->type == CompiledExpression::TYPE_HAVING)
$type = $T->type;
// Handle relationship comparisons with model objects
elseif ($value instanceof VerySimpleModel) {
$criteria = array();
foreach ($value->pk as $f=>$v) {
$f = $field . '__' . $f;
$criteria[$f] = $v;
}
$filter[] = $this->compileQ(new Q($criteria), $model, $slot);
}
// Handle simple field = <value> constraints
list($field, $op) = $this->getField($field, $model);
if ($field instanceof SqlAggregate) {
// This constraint has to go in the HAVING clause
$field = $field->toSql($this, $model);
$type = CompiledExpression::TYPE_HAVING;
}
if ($value === null)
$filter[] = sprintf('%s IS NULL', $field);
// Allow operators to be callable rather than sprintf
// strings
$filter[] = call_user_func($op, $field, $value, $model);
$filter[] = sprintf($op, $field, $this->input($value, $slot));
$glue = $Q->isOred() ? ' OR ' : ' AND ';
$clause = implode($glue, $filter);
if (count($filter) > 1)
$clause = '(' . $clause . ')';
if ($Q->isNegated())
$clause = 'NOT '.$clause;
return new CompiledExpression($clause, $type);
}
function compileConstraints($where, $model) {
$constraints = array();
foreach ($where as $Q) {
$constraints[] = $this->compileQ($Q, $model);
}
}
function getParams() {
return $this->params;
}
function getJoins($queryset) {
$sql = '';
foreach ($this->joins as $j)
$sql .= $j['sql'];
// Add extra items from QuerySet
if (isset($queryset->extra['tables'])) {
foreach ($queryset->extra['tables'] as $S) {
$join = ' JOIN ';
// Left joins require an ON () clause
if ($lastparen = strrpos($S, '(')) {
if (preg_match('/\bon\b/i', substr($S, $lastparen - 4, 4)))
$join = ' LEFT' . $join;
}
$sql .= $join.$S;
}
}
return $sql;
}
function nextAlias() {
// Use alias A1-A9,B1-B9,...
$alias = chr(65 + (int)($this->alias_num / 9)) . $this->alias_num % 9;
$this->alias_num++;
return $alias;
}
}
class CompiledExpression /* extends SplString */ {
const TYPE_WHERE = 0x0001;
const TYPE_HAVING = 0x0002;
var $text = '';
function __construct($clause, $type=self::TYPE_WHERE) {
$this->text = $clause;
$this->type = $type;
}
function __toString() {
return $this->text;
}
}
static $compiler = 'MySqlCompiler';
function __construct($info) {
}
function connect() {
}
// Gets a compiler compatible with this database engine that can compile
// and execute a queryset or DML request.
static function getCompiler() {
$class = static::$compiler;
return new $class();
static function delete(VerySimpleModel $model) {
return static::getCompiler()->compileDelete($model);
}
static function save(VerySimpleModel $model) {
$compiler = static::getCompiler();
if ($model->__new__)
return $compiler->compileInsert($model);
else
return $compiler->compileUpdate($model);
}
}
class MySqlCompiler extends SqlCompiler {
static $operators = array(
'exact' => '%1$s = %2$s',
'contains' => array('self', '__contains'),
'startswith' => array('self', '__startswith'),
'endswith' => array('self', '__endswith'),
'gt' => '%1$s > %2$s',
'lt' => '%1$s < %2$s',
'gte' => '%1$s >= %2$s',
'lte' => '%1$s <= %2$s',
'isnull' => array('self', '__isnull'),
'intersect' => array('self', '__find_in_set'),
// Thanks, http://stackoverflow.com/a/3683868
function like_escape($what, $e='\\') {
return str_replace(array($e, '%', '_'), array($e.$e, $e.'%', $e.'_'), $what);
}
function __contains($a, $b) {
# {%a} like %{$b}%
# Escape $b
$b = $this->like_escape($b);
return sprintf('%s LIKE %s', $a, $this->input("%$b%"));
}
function __startswith($a, $b) {
$b = $this->like_escape($b);
return sprintf('%s LIKE %s', $a, $this->input("$b%"));
}
function __endswith($a, $b) {
$b = $this->like_escape($b);
return sprintf('%s LIKE %s', $a, $this->input("%$b"));
}
function __in($a, $b) {
if (is_array($b)) {
$vals = array_map(array($this, 'input'), $b);
$b = implode(', ', $vals);
}
// MySQL doesn't support LIMIT or OFFSET in subqueries. Instead, add
// the query as a JOIN and add the join constraint into the WHERE
// clause.
elseif ($b instanceof QuerySet && $b->isWindowed()) {
$f1 = $b->values[0];
$view = $b->asView();
$alias = $this->pushJoin($view, $a, $view, array('constraint'=>array()));
return sprintf('%s = %s.%s', $a, $alias, $this->quote($f1));
}
else {
$b = $this->input($b);
}
function __isnull($a, $b) {
return $b
? sprintf('%s IS NULL', $a)
: sprintf('%s IS NOT NULL', $a);
}
function __find_in_set($a, $b) {
if (is_array($b)) {
$sql = array();
foreach (array_map(array($this, 'input'), $b) as $b) {
$sql[] = sprintf('FIND_IN_SET(%s, %s)', $b, $a);
}
$parens = count($sql) > 1;
$sql = implode(' OR ', $sql);
return $parens ? ('('.$sql.')') : $sql;
}
return sprintf('FIND_IN_SET(%s, %s)', $b, $a);
}
function compileJoin($tip, $model, $alias, $info, $extra=false) {
$constraints = array();
$join = ' JOIN ';
if (isset($info['null']) && $info['null'])
$join = ' LEFT'.$join;
if (isset($this->joins[$tip]))
$table = $this->joins[$tip]['alias'];
else
$table = $this->quote($model::$meta['table']);
foreach ($info['constraint'] as $local => $foreign) {
// Support a constant constraint with
// "'constant'" => "Model.field_name"
if ($local[0] == "'") {
$constraints[] = sprintf("%s.%s = %s",
$alias, $this->quote($right),
$this->input(trim($local, '\'"'))
// Support local constraint
// field_name => "'constant'"
elseif ($rmodel[0] == "'" && !$right) {
$constraints[] = sprintf("%s.%s = %s",
$table, $this->quote($local),
else {
$constraints[] = sprintf("%s.%s = %s.%s",
$table, $this->quote($local), $alias,
$this->quote($right)
);
}
// Support extra join constraints
if ($extra instanceof Q) {
$constraints[] = $this->compileQ($extra, $model);
if (!isset($rmodel))
$rmodel = $model;
// Support inline views
$table = ($rmodel::$meta['view'])
// XXX: Support parameters from the nested query
? $rmodel::getQuery($this)
: $this->quote($rmodel::$meta['table']);
if ($constraints)
$base .= ' ON ('.implode(' AND ', $constraints).')';
return $base;
/**
* input
*
* Generate a parameterized input for a database query. Input value is
* received by reference to avoid copying.
*
* Parameters:
* $what - (mixed) value to be sent to the database. No escaping is
* necessary. Pass a raw value here.
*
* Returns:
* (string) token to be placed into the compiled SQL statement. For
* MySQL, this is always the string '?'.
*/
function input($what, $slot=false) {
if ($what instanceof QuerySet) {
$q = $what->getQuery(array('nosort'=>true));
$this->params = array_merge($this->params, $q->params);
return $q->sql;
elseif ($what instanceof SqlFunction) {
return $what->toSql($this);
elseif (!isset($what)) {
return 'NULL';
}
$this->params[] = $what;
return ':'.(count($this->params));
}
function quote($what) {
return "`$what`";
}
/**
* getWhereClause
*
* This builds the WHERE ... part of a DML statement. This should be
* called before ::getJoins(), because it may add joins into the
* statement based on the relationships used in the where clause
*/
protected function getWhereHavingClause($queryset) {
$constraints = $this->compileConstraints($queryset->constraints, $model);
$where = $having = array();
foreach ($constraints as $C) {
if ($C->type == CompiledExpression::TYPE_WHERE)
$where[] = $C;
else
$having[] = $C;
}
if (isset($queryset->extra['where'])) {
foreach ($queryset->extra['where'] as $S) {
$where[] = "($S)";
}
}
$where = ' WHERE '.implode(' AND ', $where);
if ($having)
$having = ' HAVING '.implode(' AND ', $having);
return array($where ?: '', $having ?: '');
}
function compileCount($queryset) {
$model = $queryset->model;
$table = $model::$meta['table'];
list($where, $having) = $this->getWhereHavingClause($queryset);
$joins = $this->getJoins($queryset);
$sql = 'SELECT COUNT(*) AS count FROM '.$this->quote($table).$joins.$where;
$exec = new MysqlExecutor($sql, $this->params);
$row = $exec->getArray();
return $row['count'];
}
function compileSelect($queryset) {
$model = $queryset->model;
// Use an alias for the root model table
$this->joins[''] = array('alias' => ($rootAlias = $this->nextAlias()));
// Compile the WHERE clause
$this->annotations = $queryset->annotations ?: array();
list($where, $having) = $this->getWhereHavingClause($queryset);
// Compile the ORDER BY clause
if (($columns = $queryset->getSortFields()) && !isset($this->options['nosort'])) {
if ($sort instanceof SqlFunction) {
$field = $sort->toSql($this, $model);
}
else {
if ($sort[0] == '-') {
$dir = 'DESC';
$sort = substr($sort, 1);
}
list($field) = $this->getField($sort, $model);
// TODO: Throw exception if $field can be indentified as
// invalid
if ($field instanceof SqlFunction)
$field = $field->toSql($this, $model);
$orders[] = $field.' '.$dir;
}
$sort = ' ORDER BY '.implode(', ', $orders);
}
// Compile the field listing
$table = $this->quote($model::$meta['table']).' '.$rootAlias;
$defer = $queryset->defer ?: array();
// Add local fields first
$model::_inspect();
foreach ($model::$meta['fields'] as $f) {
// Handle deferreds
if (isset($defer[$f]))
continue;
$fields[$rootAlias . '.' . $this->quote($f)] = true;
$fieldMap[] = array($theseFields, $model);
// Add the JOINs to this query
foreach ($queryset->related as $sr) {
// XXX: Sort related by the paths so that the shortest paths
// are resolved first when building out the models.
$full_path = '';
$parts = array();
// Track each model traversal and fetch data for each of the
// models in the path of the related table
foreach (explode('__', $sr) as $field) {
$full_path .= $field;
$parts[] = $field;
list($alias, $fmodel) = $this->getField($full_path, $model,
array('table'=>true, 'model'=>true));
$fmodel::_inspect();
foreach ($fmodel::$meta['fields'] as $f) {
// Handle deferreds
if (isset($defer[$sr . '__' . $f]))
continue;
elseif (isset($fields[$alias.'.'.$this->quote($f)]))
continue;
$fields[$alias . '.' . $this->quote($f)] = true;
if ($theseFields) {
$fieldMap[] = array($theseFields, $fmodel, $parts);
}
$full_path .= '__';
}
}
}
// Support retrieving only a list of values rather than a model
elseif ($queryset->values) {
foreach ($queryset->values as $alias=>$v) {
list($f) = $this->getField($v, $model);
$fields[$f->toSql($this, $model, $alias)] = true;
else {
if (!is_int($alias))
$f .= ' AS '.$this->quote($alias);
// If there are annotations, add in these fields to the
// GROUP BY clause
if ($queryset->annotations)
$group_by[] = $unaliased;
}
// Simple selection from one table
else {
if ($queryset->defer) {
foreach ($model::$meta['fields'] as $f) {
if (isset($queryset->defer[$f]))
continue;
$fields[$rootAlias .'.'. $this->quote($f)] = true;
}
}
else {
$fields[$rootAlias.'.*'] = true;
$fields = array_keys($fields);
// Add in annotations
if ($queryset->annotations) {
// The root model will receive the annotations, add in the
// annotation after the root model's fields
$T = $A->toSql($this, $model, $alias);
if ($fieldMap) {
array_splice($fields, count($fieldMap[0][0]), 0, array($T));
$fieldMap[0][0][] = $A->getAlias();
}
else {
// No field map — just add to end of field list
$fields[] = $T;
Loading
Loading full blame...