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 =
'metadata';
65 switch ($property_type) {
74 $alias =
$qb->joinEntitiesTable(
'n_table',
'entity_guid',
'inner',
'e');
75 $qb->addSelect(
"{$function}({$alias}.{$property}) AS calculation");
80 if (!empty($this->options->metadata_name_value_pairs) && $this->options->metadata_name_value_pairs[0]->names != $property) {
81 $alias =
$qb->joinMetadataTable(
'n_table',
'entity_guid', $property);
83 $qb->addSelect(
"{$function}($alias.value) AS calculation");
87 $alias =
$qb->joinAnnotationTable(
'n_table',
'entity_guid', $property);
88 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
91 case 'private_setting' :
92 $alias =
$qb->joinPrivateSettingsTable(
'n_table',
'entity_guid', $property);
93 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
105 return (
int)
$result->calculation;
121 $distinct = $this->options->distinct ?
"DISTINCT" :
"";
122 $qb->select(
"$distinct n_table.*");
129 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
130 if (empty($original_order) && $original_order !==
false) {
131 $qb->addOrderBy(
'n_table.time_created',
'asc');
132 $qb->addOrderBy(
'n_table.id',
'asc');
137 $qb->setFirstResult((
int) $offset);
140 $callback = $callback ? : $this->options->callback;
141 if (!isset($callback)) {
142 $callback =
function ($row) {
143 return new \ElggMetadata($row);
158 if ($this->options->annotation_calculation) {
159 $clauses = $this->options->annotation_name_value_pairs;
160 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
161 throw new LogicException(
"Annotation calculation can not be performed on multiple annotation name value pairs merged with AND");
164 $clause = array_shift($clauses);
166 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
167 }
else if ($this->options->metadata_calculation) {
168 $clauses = $this->options->metadata_name_value_pairs;
169 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
170 throw new LogicException(
"Metadata calculation can not be performed on multiple metadata name value pairs merged with AND");
173 $clause = array_shift($clauses);
175 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
176 }
else if ($this->options->count) {
177 return $this->
count();
178 }
else if ($this->options->batch) {
179 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
181 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
196 foreach ($this->options->joins as $join) {
197 $join->prepare($qb,
'n_table');
200 foreach ($this->options->wheres as $where) {
201 $ands[] = $where->prepare($qb,
'n_table');
204 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
207 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
208 $ands[] = $this->
buildPairedPrivateSettingsClause($qb, $this->options->private_setting_name_value_pairs, $this->options->private_setting_name_value_pairs_operator);
211 $ands = $qb->
merge($ands);
214 $qb->andWhere($ands);
234 return EntityWhereClause::factory($this->options)->prepare($qb, $joined_alias);
251 foreach ($clauses as $clause) {
252 $parts[] = $clause->prepare($qb,
'n_table');
255 return $qb->
merge($parts, $boolean);
271 foreach ($clauses as $clause) {
277 $parts[] = $clause->prepare($qb, $joined_alias);
280 return $qb->
merge($parts, $boolean);
296 foreach ($clauses as $clause) {
302 $parts[] = $clause->prepare($qb, $joined_alias);
305 return $qb->
merge($parts, $boolean);
323 foreach ($clauses as $clause) {
327 $joined_alias = $qb->
joinRelationshipTable(
'n_table',
'entity_guid', $clause->names, $clause->inverse);
329 $parts[] = $clause->prepare($qb, $joined_alias);
332 return $qb->
merge($parts, $boolean);
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...
Database abstraction query builder.
if($pagination &&($position== 'after'||$position== 'both')) $limit
joinEntitiesTable($from_alias= '', $from_column= 'guid', $join_type= 'inner', $joined_alias=null)
Join entity table from alias and return joined table alias.
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.
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.
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.