28 $count_expr = $this->options->distinct ?
"DISTINCT {$qb->getTableAlias()}.id" :
'*';
29 $qb->select(
"COUNT({$count_expr}) AS total");
52 public function calculate($function, $property, $property_type = null) {
54 throw new DomainException(
"'{$function}' is not a valid numeric function");
57 if (!isset($property_type)) {
58 $property_type =
'metadata';
63 switch ($property_type) {
69 $alias =
$qb->joinEntitiesTable(
$qb->getTableAlias(),
'entity_guid',
'inner',
'e');
70 $qb->addSelect(
"{$function}({$alias}.{$property}) AS calculation");
75 if (!empty($this->options->metadata_name_value_pairs) && $this->options->metadata_name_value_pairs[0]->names != $property) {
76 $alias =
$qb->joinMetadataTable(
$qb->getTableAlias(),
'entity_guid', $property);
79 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
84 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
107 $distinct = $this->options->distinct ?
'DISTINCT ' :
'';
108 $qb->select(
"{$distinct}{$qb->getTableAlias()}.*");
115 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
116 if (empty($this->options->order_by) && $original_order !==
false) {
117 $qb->addOrderBy(
"{$qb->getTableAlias()}.time_created",
'asc');
118 $qb->addOrderBy(
"{$qb->getTableAlias()}.id",
'asc');
122 $qb->setMaxResults((
int)
$limit);
123 $qb->setFirstResult((
int) $offset);
126 $callback = $callback ?: $this->options->callback;
127 if (!isset($callback)) {
128 $callback =
function ($row) {
129 return new \ElggMetadata($row);
143 if ($this->options->annotation_calculation) {
144 $clauses = $this->options->annotation_name_value_pairs;
145 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
146 throw new LogicException(
'Annotation calculation can not be performed on multiple annotation name value pairs merged with AND');
149 $clause = array_shift($clauses);
151 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
152 }
else if ($this->options->metadata_calculation) {
153 $clauses = $this->options->metadata_name_value_pairs;
154 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
155 throw new LogicException(
'Metadata calculation can not be performed on multiple metadata name value pairs merged with AND');
158 $clause = array_shift($clauses);
160 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
161 }
else if ($this->options->count) {
162 return $this->
count();
163 }
else if ($this->options->batch) {
164 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
166 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
180 foreach ($this->options->joins as $join) {
184 foreach ($this->options->wheres as $where) {
188 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
191 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
194 $ands = $qb->
merge($ands);
197 $qb->andWhere($ands);
215 return EntityWhereClause::factory($this->options)->prepare($qb, $joined_alias);
231 foreach ($clauses as $clause) {
235 return $qb->
merge($parts, $boolean);
251 foreach ($clauses as $clause) {
258 $parts[] = $clause->prepare($qb, $joined_alias);
261 return $qb->
merge($parts, $boolean);
279 foreach ($clauses as $clause) {
286 $parts[] = $clause->prepare($qb, $joined_alias);
289 return $qb->
merge($parts, $boolean);
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.
Exception thrown if a value does not adhere to a defined valid data domain.
Database abstraction query builder.
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.
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.
joinRelationshipTable(string $from_alias= '', string $from_column= 'guid', $name=null, bool $inverse=false,?string $join_type= 'inner', string $joined_alias=null)
Join relationship 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.