33 $count_expr = $this->options->distinct ?
"DISTINCT e.guid" :
"*";
34 $qb->select(
"COUNT({$count_expr}) AS total");
57 public function calculate($function, $property, $property_type = null) {
63 if (!isset($property_type)) {
65 $property_type =
'attribute';
67 $property_type =
'metadata';
73 switch ($property_type) {
79 $qb->addSelect(
"{$function}(e.{$property}) AS calculation");
83 $alias =
$qb->joinMetadataTable(
'e',
'guid', $property,
'inner',
'n_table');
84 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
88 $alias =
$qb->joinAnnotationTable(
'e',
'guid', $property,
'inner',
'n_table');
89 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
92 case 'private_setting' :
93 $alias =
$qb->joinPrivateSettingsTable(
'e',
'guid', $property,
'inner',
'ps');
94 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
119 $distinct = $this->options->distinct ?
"DISTINCT" :
"";
120 $qb->select(
"$distinct e.*");
127 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
128 if (empty($original_order) && $original_order !==
false) {
129 $qb->addOrderBy(
'e.time_created',
'desc');
131 $qb->addOrderBy(
'e.guid',
'desc');
136 $qb->setFirstResult((
int) $offset);
139 $options = $this->options->getArrayCopy();
143 $options[
'callback'] = $callback ? : $this->options->callback;
166 $qb->select(
"DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(e.time_created)) AS yearmonth");
172 $options = $this->options->getArrayCopy();
183 return array_map(
function ($e) {
184 return $e->yearmonth;
196 if ($this->options->annotation_calculation) {
197 $clauses = $this->options->annotation_name_value_pairs;
198 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
199 throw new LogicException(
"Annotation calculation can not be performed on multiple annotation name value pairs merged with AND");
202 $clause = array_shift($clauses);
204 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
205 }
else if ($this->options->metadata_calculation) {
206 $clauses = $this->options->metadata_name_value_pairs;
207 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
208 throw new LogicException(
"Metadata calculation can not be performed on multiple metadata name value pairs merged with AND");
211 $clause = array_shift($clauses);
213 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
214 }
else if ($this->options->count) {
215 return $this->
count();
216 }
else if ($this->options->batch) {
217 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
219 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
234 foreach ($this->options->joins as $join) {
235 $join->prepare($qb,
'e');
238 foreach ($this->options->wheres as $where) {
239 $ands[] = $where->prepare($qb,
'e');
243 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
245 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
246 $ands[] = $this->
buildPairedPrivateSettingsClause($qb, $this->options->private_setting_name_value_pairs, $this->options->private_setting_name_value_pairs_operator);
249 $ands = $qb->
merge($ands);
252 $qb->andWhere($ands);
267 return EntityWhereClause::factory($this->options)->prepare($qb,
'e');
284 foreach ($clauses as $clause) {
291 $parts[] = $clause->prepare($qb, $joined_alias);
295 return $qb->
merge($parts, $boolean);
311 foreach ($clauses as $clause) {
317 $parts[] = $clause->prepare($qb, $joined_alias);
320 return $qb->
merge($parts, $boolean);
336 foreach ($clauses as $clause) {
342 $parts[] = $clause->prepare($qb, $joined_alias);
345 return $qb->
merge($parts, $boolean);
360 foreach ($clauses as $clause) {
362 $joined_alias = $qb->
joinRelationshipTable(
'e', $clause->join_on, null, $clause->inverse,
'inner',
'r');
366 $parts[] = $clause->prepare($qb, $joined_alias);
369 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.
Exception thrown if an argument is not of the expected type.
batch($limit=null, $offset=null, $callback=null)
{Fetch rows as an ElggBatch.Number of rows to fetch Index of the first row Callback function to run d...
buildQuery(QueryBuilder $qb)
Build a database query.
calculate($function, $property, $property_type=null)
Performs a mathematical calculation on a set of entity properties.
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...
if($pagination &&($position== 'after'||$position== 'both')) $limit
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.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
static fromTable($table, $alias=null)
{}
joinPrivateSettingsTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join private settings table from alias and return joined table alias.
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.
buildPairedPrivateSettingsClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process private setting name value pairs Joins the private settings table on entity guid in the entit...