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');
122 $qb->setMaxResults((
int)
$limit);
123 $qb->setFirstResult((
int) $offset);
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) {
222 foreach ($this->options->wheres as $where) {
227 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
229 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
232 $ands = $qb->
merge($ands);
235 $qb->andWhere($ands);
250 return EntityWhereClause::factory($this->options)->prepare($qb, $qb->
getTableAlias());
266 foreach ($clauses as $clause) {
274 $parts[] = $clause->prepare($qb, $joined_alias);
278 return $qb->
merge($parts, $boolean);
294 foreach ($clauses as $clause) {
301 $parts[] = $clause->prepare($qb, $joined_alias);
304 return $qb->
merge($parts, $boolean);
319 foreach ($clauses as $clause) {
326 $parts[] = $clause->prepare($qb, $joined_alias);
329 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.
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.
batch($limit=null, $offset=null, $callback=null)
Fetch rows as an ElggBatch.
buildQuery(QueryBuilder $qb)
Build a database query.
calculate($function, $property, $property_type=null)
Performs a mathematical calculation on a set of entity properties.
Exception thrown if a value does not adhere to a defined valid data domain.
Database abstraction query builder.
Entities repository contains methods for fetching entities from database or performing calculations o...
buildEntityClause(QueryBuilder $qb)
Process entity attribute wheres Applies entity attribute constrains on the selected entities table...
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.
buildPairedMetadataClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process metadata name value pairs Joins the metadata table on entity guid in the entities table and a...
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.
buildPairedRelationshipClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process relationship pairs.
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.
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.
getDates()
Returns a list of months in which entities were updated or created.