5 use Doctrine\DBAL\Query\Expression\CompositeExpression;
27 $count_expr = $this->options->distinct ?
"DISTINCT {$qb->getTableAlias()}.guid" :
'*';
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)) {
58 $property_type =
'attribute';
60 $property_type =
'metadata';
66 switch ($property_type) {
72 $qb->addSelect(
"{$function}({$qb->getTableAlias()}.{$property}) AS calculation");
77 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
82 $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",
'desc');
118 $qb->addOrderBy(
"{$qb->getTableAlias()}.guid",
'desc');
126 $options = $this->options->getArrayCopy();
130 $options[
'callback'] = $callback ?: $this->options->callback;
152 $qb->select(
"DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME({$qb->getTableAlias()}.time_created)) AS yearmonth");
158 $options = $this->options->getArrayCopy();
169 return array_map(
function ($e) {
170 return $e->yearmonth;
181 if ($this->options->annotation_calculation) {
182 $clauses = $this->options->annotation_name_value_pairs;
183 if (count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
184 throw new LogicException(
'Annotation calculation can not be performed on multiple annotation name value pairs merged with AND');
187 $clause = array_shift($clauses);
189 return $this->calculate($this->options->annotation_calculation, $clause->names,
'annotation');
190 }
else if ($this->options->metadata_calculation) {
191 $clauses = $this->options->metadata_name_value_pairs;
192 if (count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
193 throw new LogicException(
'Metadata calculation can not be performed on multiple metadata name value pairs merged with AND');
196 $clause = array_shift($clauses);
198 return $this->calculate($this->options->metadata_calculation, $clause->names,
'metadata');
199 }
else if ($this->options->count) {
200 return $this->count();
201 }
else if ($this->options->batch) {
202 return $this->batch($this->options->limit, $this->options->offset, $this->options->callback);
204 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
218 foreach ($this->options->joins as $join) {
219 $join->prepare(
$qb,
$qb->getTableAlias());
222 foreach ($this->options->wheres as $where) {
223 $ands[] = $where->prepare(
$qb,
$qb->getTableAlias());
226 $ands[] = $this->buildEntityClause(
$qb);
227 $ands[] = $this->buildPairedMetadataClause(
$qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
228 $ands[] = $this->buildPairedMetadataClause(
$qb, $this->options->search_name_value_pairs,
'OR');
229 $ands[] = $this->buildPairedAnnotationClause(
$qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
230 $ands[] = $this->buildPairedRelationshipClause(
$qb, $this->options->relationship_pairs);
232 $ands =
$qb->merge($ands);
235 $qb->andWhere($ands);
250 return EntityWhereClause::factory($this->options)->prepare(
$qb,
$qb->getTableAlias());
266 foreach ($clauses as $clause) {
268 if (strtoupper($boolean) ===
'OR' || count($clauses) === 1) {
271 $joined_alias =
$qb->joinMetadataTable(
$qb->getTableAlias(),
'guid', $clause->names);
274 $parts[] = $clause->prepare(
$qb, $joined_alias);
278 return $qb->merge($parts, $boolean);
294 foreach ($clauses as $clause) {
295 if (strtoupper($boolean) ===
'OR' || count($clauses) === 1) {
298 $joined_alias =
$qb->joinAnnotationTable(
$qb->getTableAlias(),
'guid', $clause->names);
301 $parts[] = $clause->prepare(
$qb, $joined_alias);
304 return $qb->merge($parts, $boolean);
319 foreach ($clauses as $clause) {
320 if (strtoupper($boolean) ===
'OR' || count($clauses) === 1) {
323 $joined_alias =
$qb->joinRelationshipTable(
$qb->getTableAlias(), $clause->join_on, $clause->names, $clause->inverse);
326 $parts[] = $clause->prepare(
$qb, $joined_alias);
329 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.
Entities repository contains methods for fetching entities from database or performing calculations o...
buildPairedMetadataClause(QueryBuilder $qb, $clauses, $boolean='AND')
Process metadata name value pairs Joins the metadata table on entity guid in the entities table and a...
buildEntityClause(QueryBuilder $qb)
Process entity attribute wheres Applies entity attribute constrains on the selected entities table.
buildPairedRelationshipClause(QueryBuilder $qb, $clauses, $boolean='AND')
Process relationship pairs.
buildPairedAnnotationClause(QueryBuilder $qb, $clauses, $boolean='AND')
Process annotation name value pairs Joins the annotation table on entity guid in the entities table a...
execute()
Execute the query resolving calculation, count and/or batch options.
buildQuery(QueryBuilder $qb)
Build a database query.
getDates()
Returns a list of months in which entities were updated or created.
calculate($function, $property, $property_type=null)
Performs a mathematical calculation on a set of entity properties.
Entity table database service.
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.
Query builder for fetching data from the database.
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.