28 $count_expr = $this->options->distinct ?
'DISTINCT n_table.id' :
'*';
29 $qb->select(
"COUNT({$count_expr}) AS total");
52 public function calculate($function, $property, $property_type = null) {
54 throw new DomainException(
"'{$function}' is not a valid numeric function");
57 if (!isset($property_type)) {
58 $property_type =
'metadata';
63 switch ($property_type) {
72 $alias =
$qb->joinEntitiesTable(
'n_table',
'entity_guid',
'inner',
'e');
73 $qb->addSelect(
"{$function}({$alias}.{$property}) AS calculation");
78 if (!empty($this->options->metadata_name_value_pairs) && $this->options->metadata_name_value_pairs[0]->names != $property) {
79 $alias =
$qb->joinMetadataTable(
'n_table',
'entity_guid', $property);
82 $qb->addSelect(
"{$function}($alias.value) AS calculation");
86 $alias =
$qb->joinAnnotationTable(
'n_table',
'entity_guid', $property);
87 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
110 $distinct = $this->options->distinct ?
'DISTINCT' :
'';
111 $qb->select(
"{$distinct} n_table.*");
118 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
119 if (empty($original_order) && $original_order !==
false) {
120 $qb->addOrderBy(
'n_table.time_created',
'asc');
121 $qb->addOrderBy(
'n_table.id',
'asc');
126 $qb->setFirstResult((
int) $offset);
129 $callback = $callback ?: $this->options->callback;
130 if (!isset($callback)) {
131 $callback =
function ($row) {
132 return new \ElggMetadata($row);
146 if ($this->options->annotation_calculation) {
147 $clauses = $this->options->annotation_name_value_pairs;
148 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
149 throw new LogicException(
'Annotation calculation can not be performed on multiple annotation name value pairs merged with AND');
152 $clause = array_shift($clauses);
154 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
155 }
else if ($this->options->metadata_calculation) {
156 $clauses = $this->options->metadata_name_value_pairs;
157 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
158 throw new LogicException(
'Metadata calculation can not be performed on multiple metadata name value pairs merged with AND');
161 $clause = array_shift($clauses);
163 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
164 }
else if ($this->options->count) {
165 return $this->
count();
166 }
else if ($this->options->batch) {
167 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
169 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
183 foreach ($this->options->joins as $join) {
184 $join->prepare($qb,
'n_table');
187 foreach ($this->options->wheres as $where) {
188 $ands[] = $where->prepare($qb,
'n_table');
191 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
194 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
197 $ands = $qb->
merge($ands);
200 $qb->andWhere($ands);
218 return EntityWhereClause::factory($this->options)->prepare($qb, $joined_alias);
234 foreach ($clauses as $clause) {
235 $parts[] = $clause->prepare($qb,
'n_table');
238 return $qb->
merge($parts, $boolean);
254 foreach ($clauses as $clause) {
261 $parts[] = $clause->prepare($qb, $joined_alias);
264 return $qb->
merge($parts, $boolean);
282 foreach ($clauses as $clause) {
286 $joined_alias = $qb->
joinRelationshipTable(
'n_table',
'entity_guid', $clause->names, $clause->inverse);
289 $parts[] = $clause->prepare($qb, $joined_alias);
292 return $qb->
merge($parts, $boolean);
batch($limit=null, $offset=null, $callback=null)
Fetch rows as an ElggBatch.
Exception thrown if a value does not adhere to a defined valid data domain.
Database abstraction query builder.
joinEntitiesTable($from_alias= '', $from_column= 'guid', $join_type= 'inner', $joined_alias=null)
Join entity table from alias and return joined table alias.
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.
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.
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.
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.