40 $options = LegacyQueryOptionsAdapter::normalizePluralOptions(
$options, $singulars);
44 'subject_guids' => null,
45 'object_guids' => null,
46 'target_guids' => null,
47 'annotation_ids' => null,
49 'action_types' => null,
50 'posted_time_lower' => null,
51 'posted_time_upper' => null,
77 $count_expr = $this->options->distinct ?
"DISTINCT rv.id" :
"*";
78 $qb->select(
"COUNT({$count_expr}) AS total");
101 public function calculate($function, $property, $property_type =
'annotation') {
110 if (!empty($this->options->annotation_name_value_pairs) && $this->options->annotation_name_value_pairs[0]->names != $property) {
111 $alias = $qb->getNextJoinAlias();
118 $qb->join(
'rv',
'annotations', $alias,
"rv.annotation_id = $alias.id");
119 $qb->select(
"{$function}(n_table.value) AS calculation");
129 return (
int)
$result->calculation;
146 $distinct = $this->options->distinct ?
"DISTINCT" :
"";
147 $qb->select(
"$distinct rv.*");
154 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
155 if (empty($original_order) && $original_order !==
false) {
156 $qb->addOrderBy(
'rv.posted',
'desc');
160 $qb->setMaxResults((
int)
$limit);
161 $qb->setFirstResult((
int) $offset);
164 $callback = $callback ? : $this->options->callback;
165 if (!isset($callback)) {
166 $callback =
function ($row) {
174 $preload = array_filter(
$items,
function($e) {
196 if ($this->options->annotation_calculation) {
197 $clauses = $this->options->annotation_name_value_pairs;
198 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
199 throw new \LogicException(
"Annotation calculation can not be performed on multiple annotation name value pairs merged with AND");
202 $clause = array_shift($clauses);
204 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
205 }
else if ($this->options->count) {
206 return $this->
count();
207 }
else if ($this->options->batch) {
208 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
210 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
225 foreach ($this->options->joins as $join) {
226 $join->prepare($qb,
'rv');
229 foreach ($this->options->wheres as $where) {
230 $ands[] = $where->prepare($qb,
'rv');
235 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
238 $ands = $qb->
merge($ands);
241 $qb->andWhere($ands);
256 $where->ids = $this->options->ids;
257 $where->views = $this->options->views;
258 $where->action_types = $this->options->action_types;
259 $where->subject_guids = $this->options->subject_guids;
260 $where->object_guids = $this->options->object_guids;
261 $where->target_guids = $this->options->target_guids;
262 $where->created_after = $this->options->created_after;
263 $where->created_before = $this->options->created_before;
265 return $where->prepare($qb,
'rv');
278 $use_access_clause = !
_elgg_services()->userCapabilities->canBypassPermissionsCheck();
282 if (!empty($this->options->subject_guids) || $use_access_clause) {
283 $qb->joinEntitiesTable(
'rv',
'subject_guid',
'inner',
'se');
285 $subject->guids = $this->options->subject_guids;
286 $ands[] =
$subject->prepare($qb,
'se');
289 if (!empty($this->options->object_guids) || $use_access_clause || !empty($this->options->type_subtype_pairs)) {
290 $qb->joinEntitiesTable(
'rv',
'object_guid',
'inner',
'oe');
292 $object->guids = $this->options->object_guids;
293 $object->type_subtype_pairs = $this->options->type_subtype_pairs;
294 $ands[] =
$object->prepare($qb,
'oe');
297 if (!empty($this->options->target_guids) || $use_access_clause) {
299 $qb->joinEntitiesTable(
'rv',
'target_guid',
'left',
'te');
301 $target->guids = $this->options->target_guids;
302 $target_ors[] =
$target->prepare($qb,
'te');
304 $target_ors[] = $qb->compare(
'te.guid',
'IS NULL');
305 $ands[] = $qb->merge($target_ors,
'OR');
308 return $qb->merge($ands);
324 foreach ($clauses as $clause) {
325 if (strtoupper($boolean) ===
'OR' ||
count($clauses) === 1) {
326 $joined_alias =
'n_table';
330 $joins = $qb->getQueryPart(
'join');
332 if (!empty($joins[
'rv'])) {
333 foreach ($joins[
'rv'] as $join) {
334 if ($join[
'joinAlias'] === $joined_alias) {
341 $qb->
join(
'rv',
'annotations', $joined_alias,
"$joined_alias.id = rv.annotation_id");
344 $parts[] = $clause->prepare($qb, $joined_alias);
347 return $qb->
merge($parts, $boolean);
362 foreach ($clauses as $clause) {
363 $join_on = $clause->join_on ===
'guid' ?
'subject_guid' : $clause->join_on;
364 if (strtoupper($boolean) ==
'OR' ||
count($clauses) === 1) {
369 $parts[] = $clause->prepare($qb, $joined_alias);
372 return $qb->
merge($parts, $boolean);
if(!$item instanceof ElggRiverItem) $object
batch($limit=null, $offset=null, $callback=null)
{Fetch rows as an ElggBatch.Number of rows to fetch Index of the first row Callback function to run d...
$annotation
Elgg default annotation view.
calculate($function, $property, $property_type= 'annotation')
Performs a mathematical calculation on river annotations.
buildEntityClauses($qb)
Add subject, object and target clauses Make sure all three are accessible by the user.
Database abstraction query builder.
buildQuery(QueryBuilder $qb)
Build a database query.
Builds queries for matching annotations against their properties.
execute()
Execute the query resolving calculation, count and/or batch options.
buildPairedRelationshipClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process relationship pairs.
River repository contains methods for fetching/counting river items.
Abstract methods for interfacing with the database.
Builds queries for matching river items against their properties.
Builds queries for filtering entities by their properties in the entities table.
join($fromAlias, $join, $alias, $condition=null)
{}
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
buildPairedAnnotationClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process annotation name value pairs Joins the annotation table on entity guid in the entities table a...
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
getNextJoinAlias()
Get an index of the next available join alias.
static fromTable($table, $alias=null)
{}
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
buildRiverClause(QueryBuilder $qb)
Process river properties.
_elgg_services()
Get the global service provider.
joinRelationshipTable($from_alias= '', $from_column= 'guid', $name=null, $inverse=false, $join_type= 'inner', $joined_alias=null)
Join relationship table from alias and return joined table alias.
static find(array $options=[])
Build and execute a new query from an array of legacy options.
__construct(array $options=[])
{}