29 $count_expr = $this->options->distinct ?
"DISTINCT n_table.id" :
"*";
30 $qb->select(
"COUNT({$count_expr}) AS total");
53 public function calculate($function, $property, $property_type = null) {
59 if (!isset($property_type)) {
60 $property_type =
'annotation';
65 switch ($property_type) {
71 $alias =
$qb->joinEntitiesTable(
'n_table',
'entity_guid',
'inner',
'e');
72 $qb->select(
"{$function}({$alias}.{$property}) AS calculation");
77 if (!empty($this->options->annotation_name_value_pairs) && $this->options->annotation_name_value_pairs[0]->names != $property) {
78 $alias =
$qb->joinAnnotationTable(
'n_table',
'entity_guid', $property);
80 $qb->select(
"{$function}($alias.value) AS calculation");
84 $alias =
$qb->joinMetadataTable(
'n_table',
'entity_guid', $property);
85 $qb->select(
"{$function}({$alias}.value) AS calculation");
88 case 'private_setting' :
89 $alias =
$qb->joinPrivateSettingsTable(
'n_table',
'entity_guid', $property);
90 $qb->select(
"{$function}({$alias}.value) AS calculation");
102 return (
int)
$result->calculation;
118 $distinct = $this->options->distinct ?
"DISTINCT" :
"";
119 $qb->select(
"$distinct n_table.*");
126 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
127 if (empty($original_order) && $original_order !==
false) {
128 $qb->addOrderBy(
'n_table.time_created',
'asc');
129 $qb->addOrderBy(
'n_table.id',
'asc');
134 $qb->setFirstResult((
int) $offset);
137 $callback = $callback ? : $this->options->callback;
138 if (!isset($callback)) {
139 $callback =
function ($row) {
140 return new \ElggAnnotation($row);
145 if (!empty(
$results) && $this->options->preload_owners) {
160 if ($this->options->annotation_calculation) {
161 $clauses = $this->options->annotation_name_value_pairs;
162 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
163 throw new LogicException(
"Annotation calculation can not be performed on multiple annotation name value pairs merged with AND");
166 $clause = array_shift($clauses);
168 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
169 }
else if ($this->options->metadata_calculation) {
170 $clauses = $this->options->metadata_name_value_pairs;
171 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
172 throw new LogicException(
"Metadata calculation can not be performed on multiple metadata name value pairs merged with AND");
175 $clause = array_shift($clauses);
177 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
178 }
else if ($this->options->count) {
179 return $this->
count();
180 }
else if ($this->options->batch) {
181 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
183 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
198 foreach ($this->options->joins as $join) {
199 $join->prepare($qb,
'n_table');
202 foreach ($this->options->wheres as $where) {
203 $ands[] = $where->prepare($qb,
'n_table');
206 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
208 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
210 $ands[] = $this->
buildPairedPrivateSettingsClause($qb, $this->options->private_setting_name_value_pairs, $this->options->private_setting_name_value_pairs_operator);
213 $ands = $qb->
merge($ands);
216 $qb->andWhere($ands);
232 return EntityWhereClause::factory($this->options)->prepare($qb, $joined_alias);
248 if (empty($clauses)) {
253 foreach ($clauses as $clause) {
254 $parts[] = $clause->prepare($qb,
'n_table');
257 return $qb->
merge($parts, $boolean);
273 foreach ($clauses as $clause) {
277 $joined_alias = $qb->
joinMetadataTable(
'n_table',
'entity_guid', $clause->names);
279 $parts[] = $clause->prepare($qb, $joined_alias);
282 return $qb->
merge($parts, $boolean);
298 foreach ($clauses as $clause) {
304 $parts[] = $clause->prepare($qb, $joined_alias);
307 return $qb->
merge($parts, $boolean);
323 foreach ($clauses as $clause) {
324 $join_on = $clause->join_on ==
'guid' ?
'entity_guid' : $clause->join_on;
330 $parts[] = $clause->prepare($qb, $joined_alias);
333 return $qb->
merge($parts, $boolean);
buildPairedAnnotationClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process annotation name value pairs Applies where clauses to the selected annotation table...
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...
buildPairedRelationshipClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process relationship name value pairs Joins relationship table on entity_guid in the annotations tabl...
calculate($function, $property, $property_type=null)
Performs a mathematical calculation on metadata or metadata entity's properties.
Database abstraction query builder.
if($pagination &&($position== 'after'||$position== 'both')) $limit
Builds queries for matching annotations against their properties.
joinEntitiesTable($from_alias= '', $from_column= 'guid', $join_type= 'inner', $joined_alias=null)
Join entity table from alias and return joined table alias.
buildPairedMetadataClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process metadata name value pairs Joins metadata table on entity_guid in the annotations table and ap...
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.
Exception that represents error in the program logic.
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.
execute()
Execute the query resolving calculation, count and/or batch options.
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
buildQuery(QueryBuilder $qb)
Build a database query.
_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.
Annotation repository contains methods for fetching annotations from database or performing calculati...
buildEntityWhereClause(QueryBuilder $qb)
Process entity attribute wheres Joins entities table on entity guid in annotations table and applies ...
buildPairedPrivateSettingsClause(QueryBuilder $qb, $clauses, $boolean= 'AND')
Process private settings name value pairs Joins private settings table on entity_guid in the annotati...