32 $count_expr = $this->options->distinct ?
'DISTINCT e.guid' :
'*';
33 $qb->select(
"COUNT({$count_expr}) AS total");
56 public function calculate($function, $property, $property_type = null) {
58 throw new DomainException(
"'{$function}' is not a valid numeric function");
61 if (!isset($property_type)) {
63 $property_type =
'attribute';
65 $property_type =
'metadata';
71 switch ($property_type) {
77 $qb->addSelect(
"{$function}(e.{$property}) AS calculation");
81 $alias =
$qb->joinMetadataTable(
'e',
'guid', $property,
'inner',
'n_table');
82 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
86 $alias =
$qb->joinAnnotationTable(
'e',
'guid', $property,
'inner',
'n_table');
87 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
111 $distinct = $this->options->distinct ?
'DISTINCT' :
'';
112 $qb->select(
"{$distinct} e.*");
119 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
120 if (empty($original_order) && $original_order !==
false) {
121 $qb->addOrderBy(
'e.time_created',
'desc');
123 $qb->addOrderBy(
'e.guid',
'desc');
128 $qb->setFirstResult((
int) $offset);
131 $options = $this->options->getArrayCopy();
135 $options[
'callback'] = $callback ?: $this->options->callback;
157 $qb->select(
'DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(e.time_created)) AS yearmonth');
163 $options = $this->options->getArrayCopy();
174 return array_map(
function ($e) {
175 return $e->yearmonth;
186 if ($this->options->annotation_calculation) {
187 $clauses = $this->options->annotation_name_value_pairs;
188 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
189 throw new LogicException(
'Annotation calculation can not be performed on multiple annotation name value pairs merged with AND');
192 $clause = array_shift($clauses);
194 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
195 }
else if ($this->options->metadata_calculation) {
196 $clauses = $this->options->metadata_name_value_pairs;
197 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
198 throw new LogicException(
'Metadata calculation can not be performed on multiple metadata name value pairs merged with AND');
201 $clause = array_shift($clauses);
203 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
204 }
else if ($this->options->count) {
205 return $this->
count();
206 }
else if ($this->options->batch) {
207 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
209 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
223 foreach ($this->options->joins as $join) {
224 $join->prepare($qb,
'e');
227 foreach ($this->options->wheres as $where) {
228 $ands[] = $where->prepare($qb,
'e');
232 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
234 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
237 $ands = $qb->
merge($ands);
240 $qb->andWhere($ands);
255 return EntityWhereClause::factory($this->options)->prepare($qb,
'e');
271 foreach ($clauses as $clause) {
279 $parts[] = $clause->prepare($qb, $joined_alias);
283 return $qb->
merge($parts, $boolean);
299 foreach ($clauses as $clause) {
306 $parts[] = $clause->prepare($qb, $joined_alias);
309 return $qb->
merge($parts, $boolean);
324 foreach ($clauses as $clause) {
326 $joined_alias = $qb->
joinRelationshipTable(
'e', $clause->join_on, null, $clause->inverse,
'inner',
'r');
331 $parts[] = $clause->prepare($qb, $joined_alias);
334 return $qb->
merge($parts, $boolean);
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.
joinAnnotationTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join annotations table from alias and return joined table alias.
static fromTable($table, $alias=null)
{}
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
_elgg_services()
Get the global service provider.
joinMetadataTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join metadata table from alias and return joined table alias.
joinRelationshipTable($from_alias= '', $from_column= 'guid', $name=null, $inverse=false, $join_type= 'inner', $joined_alias=null)
Join relationship table from alias and return joined table alias.
getDates()
Returns a list of months in which entities were updated or created.