5 use Doctrine\DBAL\Query\Expression\CompositeExpression;
27 $count_expr = $this->options->distinct ?
"DISTINCT {$qb->getTableAlias()}.id" :
'*';
28 $qb->select(
"COUNT({$count_expr}) AS total");
51 public function calculate($function, $property, $property_type =
null) {
53 throw new DomainException(
"'{$function}' is not a valid numeric function");
56 if (!isset($property_type)) {
57 $property_type =
'metadata';
62 switch ($property_type) {
68 $alias =
$qb->joinEntitiesTable(
$qb->getTableAlias(),
'entity_guid',
'inner',
'e');
69 $qb->addSelect(
"{$function}({$alias}.{$property}) AS calculation");
74 if (!empty($this->options->metadata_name_value_pairs) && $this->options->metadata_name_value_pairs[0]->names != $property) {
75 $alias =
$qb->joinMetadataTable(
$qb->getTableAlias(),
'entity_guid', $property);
78 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
83 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
106 $distinct = $this->options->distinct ?
'DISTINCT ' :
'';
107 $qb->select(
"{$distinct}{$qb->getTableAlias()}.*");
114 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
115 if (empty($this->options->order_by) && $original_order !==
false) {
116 $qb->addOrderBy(
"{$qb->getTableAlias()}.time_created",
'asc');
117 $qb->addOrderBy(
"{$qb->getTableAlias()}.id",
'asc');
125 $callback = $callback ?: $this->options->callback;
126 if (!isset($callback)) {
127 $callback =
function ($row) {
128 return new \ElggMetadata($row);
142 if ($this->options->annotation_calculation) {
143 $clauses = $this->options->annotation_name_value_pairs;
144 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
145 throw new LogicException(
'Annotation calculation can not be performed on multiple annotation name value pairs merged with AND');
148 $clause = array_shift($clauses);
150 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
151 }
else if ($this->options->metadata_calculation) {
152 $clauses = $this->options->metadata_name_value_pairs;
153 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
154 throw new LogicException(
'Metadata calculation can not be performed on multiple metadata name value pairs merged with AND');
157 $clause = array_shift($clauses);
159 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
160 }
else if ($this->options->count) {
161 return $this->
count();
162 }
else if ($this->options->batch) {
163 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
165 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
179 foreach ($this->options->joins as $join) {
180 $join->prepare(
$qb,
$qb->getTableAlias());
183 foreach ($this->options->wheres as $where) {
184 $ands[] = $where->prepare(
$qb,
$qb->getTableAlias());
187 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
190 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
193 $ands =
$qb->merge($ands);
196 $qb->andWhere($ands);
214 return EntityWhereClause::factory($this->options)->prepare(
$qb, $joined_alias);
230 foreach ($clauses as $clause) {
231 $parts[] = $clause->prepare(
$qb,
$qb->getTableAlias());
234 return $qb->merge($parts, $boolean);
250 foreach ($clauses as $clause) {
251 if (strtoupper($boolean) ===
'OR' ||
count($clauses) === 1) {
254 $joined_alias =
$qb->joinAnnotationTable(
$qb->getTableAlias(),
'entity_guid', $clause->names);
257 $parts[] = $clause->prepare(
$qb, $joined_alias);
260 return $qb->merge($parts, $boolean);
278 foreach ($clauses as $clause) {
279 if (strtoupper($boolean) ===
'OR' ||
count($clauses) === 1) {
282 $joined_alias =
$qb->joinRelationshipTable(
$qb->getTableAlias(),
'entity_guid', $clause->names, $clause->inverse);
285 $parts[] = $clause->prepare(
$qb, $joined_alias);
288 return $qb->merge($parts, $boolean);
Builds queries for matching annotations against their properties.
Builds queries for filtering entities by their properties in the entities table.
Builds clauses for filtering entities by their properties in entity_relationships table.
Database abstraction query builder.
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.
batch($limit=null, $offset=null, $callback=null)
Fetch rows as an ElggBatch.
static fromTable(string $table, ?string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Exception thrown if a value does not adhere to a defined valid data domain.
Exception that represents error in the program logic.
_elgg_services()
Get the global service provider.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.