27 public function calculate($function, $property, $property_type = null) {
29 throw new DomainException(
"'{$function}' is not a valid numeric function");
32 if (!isset($property_type)) {
34 $property_type =
'attribute';
36 $property_type =
'metadata';
44 switch ($property_type) {
50 $alias = $select->joinEntitiesTable($select->getTableAlias(), $join_column,
'inner',
'e');
51 $select->select(
"{$function}({$alias}.{$property}) AS calculation");
56 if (!empty($this->options->annotation_name_value_pairs) && $this->options->annotation_name_value_pairs[0]->names != $property) {
57 $alias = $select->joinAnnotationTable($select->getTableAlias(), $join_column, $property);
60 $select->select(
"{$function}({$alias}.value) AS calculation");
64 $alias = $select->joinMetadataTable($select->getTableAlias(), $join_column, $property);
65 $select->select(
"{$function}({$alias}.value) AS calculation");
82 $count_expr = $this->options->distinct ?
"DISTINCT {$select->getTableAlias()}.id" :
'*';
83 $select->select(
"COUNT({$count_expr}) AS total");
99 if ($this->options->annotation_calculation) {
100 $clauses = $this->options->annotation_name_value_pairs;
101 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
102 throw new LogicException(
'Annotation calculation can not be performed on multiple annotation name value pairs merged with AND');
105 $clause = array_shift($clauses);
107 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
108 }
elseif ($this->options->metadata_calculation) {
109 $clauses = $this->options->metadata_name_value_pairs;
110 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
111 throw new LogicException(
'Metadata calculation can not be performed on multiple metadata name value pairs merged with AND');
114 $clause = array_shift($clauses);
116 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
117 }
elseif ($this->options->count) {
118 return $this->
count();
119 }
elseif ($this->options->batch) {
120 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
122 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
132 $distinct = $this->options->distinct ?
'DISTINCT ' :
'';
133 $select->select(
"{$distinct}{$select->getTableAlias()}.*");
135 $this->
expandInto($select, $select->getTableAlias());
140 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
141 if (empty($this->options->order_by) && $original_order !==
false) {
142 $select->addOrderBy(
"{$select->getTableAlias()}.time_created",
'desc');
143 $select->addOrderBy(
"{$select->getTableAlias()}.id",
'desc');
147 $select->setMaxResults((
int)
$limit);
148 $select->setFirstResult((
int) $offset);
151 $callback = $callback ?: $this->options->callback;
152 if (!isset($callback)) {
153 $callback =
function ($row) {
154 return new \ElggRelationship($row);
160 $preload = array_filter(
$results,
function($e) {
161 return $e instanceof \ElggRelationship;
183 foreach ($this->options->joins as $join) {
187 foreach ($this->options->wheres as $where) {
192 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
194 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
197 $ands = $qb->
merge($ands);
199 $qb->andWhere($ands);
215 return EntityWhereClause::factory($this->options)->prepare($qb, $joined_alias);
233 foreach ($clauses as $clause) {
241 $parts[] = $clause->prepare($qb, $joined_alias);
245 return $qb->
merge($parts, $boolean);
263 foreach ($clauses as $clause) {
270 $parts[] = $clause->prepare($qb, $joined_alias);
273 return $qb->
merge($parts, $boolean);
288 foreach ($clauses as $clause) {
292 return $qb->
merge($parts, $boolean);
301 if (!empty($this->options->inverse_relationship)) {
joinAnnotationTable(string $from_alias= '', string $from_column= 'guid', $name=null,?string $join_type= 'inner', string $joined_alias=null)
Join annotations table from alias and return joined table alias.
batch($limit=null, $offset=null, $callback=null)
Fetch rows as an ElggBatch.
buildPairedAnnotationClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process annotation name value pairs Joins the annotation table on guid_one|guid_two in relationships ...
calculate($function, $property, $property_type=null)
Exception thrown if a value does not adhere to a defined valid data domain.
Database abstraction query builder.
buildPairedRelationshipClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process relationship pairs.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Abstract methods for interfacing with the database.
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
buildQuery(QueryBuilder $qb)
Build a database query.
Exception that represents error in the program logic.
joinEntitiesTable(string $from_alias= '', string $from_column= 'guid',?string $join_type= 'inner', string $joined_alias=null)
Join entity table from alias and return joined table alias.
joinMetadataTable(string $from_alias= '', string $from_column= 'guid', $name=null,?string $join_type= 'inner', string $joined_alias=null)
Join metadata table from alias and return joined table alias.
getTableAlias()
Returns the alias of the primary table.
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
_elgg_services()
Get the global service provider.
buildPairedMetadataClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process metadata name value pairs Joins the metadata table on guid_one|guid_two in relationships tabl...
getJoinColumn()
Return the base column to use in joins.
buildEntityClause(QueryBuilder $qb)
Process entity attribute wheres Joins entities table on guid_one|guid_two in relationships table and ...