function getOrderByFields(QuerySet $queryset) { $orders = array(); if (!($columns = $queryset->getSortFields())) { return $orders; } foreach ($columns as $sort) { $dir = 'ASC'; if (is_array($sort)) { list($sort, $dir) = $sort; } if ($sort instanceof Util\Expression) { $field = $sort->toSql($this, $model); } else { if ($sort[0] == '-') { $dir = 'DESC'; $sort = substr($sort, 1); } // If the field is already an annotation, then don't // compile the annotation again below. It's included in // the select clause, which is sufficient if (isset($this->annotations[$sort])) { $field = $this->quote($sort); } else { list($field) = $this->getField($sort, $model); } } if ($field instanceof Util\Expression) { $field = $field->toSql($this, $model); } // TODO: Throw exception if $field can be indentified as // invalid $orders[] = "{$field} {$dir}"; } return $orders; }
function union(QuerySet $other, $all = true) { // Values and values_list _must_ match for this to work if ($this->countSelectFields() != $other->countSelectFields()) { throw new Exception\OrmError('Union queries must have matching values counts'); } // TODO: Clear OFFSET and LIMIT in the $other query $this->chain[] = array($other, $all); return $this; }