35 $count_expr = $this->options->distinct ?
"DISTINCT n_table.id" :
"*";
36 $qb->select(
"COUNT({$count_expr}) AS total");
59 public function calculate($function, $property, $property_type = null) {
65 if (!isset($property_type)) {
66 $property_type =
'metadata';
71 switch ($property_type) {
80 $alias = $qb->joinEntitiesTable(
'n_table',
'entity_guid',
'inner',
'e');
81 $qb->addSelect(
"{$function}({$alias}.{$property}) AS calculation");
86 if (!empty($this->options->metadata_name_value_pairs) && $this->options->metadata_name_value_pairs[0]->names != $property) {
87 $alias = $qb->joinMetadataTable(
'n_table',
'entity_guid', $property);
89 $qb->addSelect(
"{$function}($alias.value) AS calculation");
93 $alias = $qb->joinAnnotationTable(
'n_table',
'entity_guid', $property);
94 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
97 case 'private_setting' :
98 $alias = $qb->joinPrivateSettingsTable(
'n_table',
'entity_guid', $property);
99 $qb->addSelect(
"{$function}({$alias}.value) AS calculation");
111 return (
int)
$result->calculation;
127 $distinct = $this->options->distinct ?
"DISTINCT" :
"";
128 $qb->select(
"$distinct n_table.*");
135 $original_order =
elgg_extract(
'order_by', $this->options->__original_options);
136 if (empty($original_order) && $original_order !==
false) {
137 $qb->addOrderBy(
'n_table.time_created',
'asc');
138 $qb->addOrderBy(
'n_table.id',
'asc');
142 $qb->setMaxResults((
int)
$limit);
143 $qb->setFirstResult((
int) $offset);
146 $callback = $callback ? : $this->options->callback;
147 if (!isset($callback)) {
148 $callback =
function ($row) {
164 if ($this->options->annotation_calculation) {
165 $clauses = $this->options->annotation_name_value_pairs;
166 if (
count($clauses) > 1 && $this->options->annotation_name_value_pairs_operator !==
'OR') {
167 throw new LogicException(
"Annotation calculation can not be performed on multiple annotation name value pairs merged with AND");
170 $clause = array_shift($clauses);
172 return $this->
calculate($this->options->annotation_calculation, $clause->names,
'annotation');
173 }
else if ($this->options->metadata_calculation) {
174 $clauses = $this->options->metadata_name_value_pairs;
175 if (
count($clauses) > 1 && $this->options->metadata_name_value_pairs_operator !==
'OR') {
176 throw new LogicException(
"Metadata calculation can not be performed on multiple metadata name value pairs merged with AND");
179 $clause = array_shift($clauses);
181 return $this->
calculate($this->options->metadata_calculation, $clause->names,
'metadata');
182 }
else if ($this->options->count) {
183 return $this->
count();
184 }
else if ($this->options->batch) {
185 return $this->
batch($this->options->limit, $this->options->offset, $this->options->callback);
187 return $this->
get($this->options->limit, $this->options->offset, $this->options->callback);
202 foreach ($this->options->joins as $join) {
203 $join->prepare($qb,
'n_table');
206 foreach ($this->options->wheres as $where) {
207 $ands[] = $where->prepare($qb,
'n_table');
210 $ands[] = $this->
buildPairedMetadataClause($qb, $this->options->metadata_name_value_pairs, $this->options->metadata_name_value_pairs_operator);
213 $ands[] = $this->
buildPairedAnnotationClause($qb, $this->options->annotation_name_value_pairs, $this->options->annotation_name_value_pairs_operator);
214 $ands[] = $this->
buildPairedPrivateSettingsClause($qb, $this->options->private_setting_name_value_pairs, $this->options->private_setting_name_value_pairs_operator);
217 $ands = $qb->
merge($ands);
220 $qb->andWhere($ands);
240 return EntityWhereClause::factory($this->options)->prepare($qb, $joined_alias);
257 foreach ($clauses as $clause) {
258 $parts[] = $clause->prepare($qb,
'n_table');
261 return $qb->
merge($parts, $boolean);
277 foreach ($clauses as $clause) {
278 if (strtoupper($boolean) ===
'OR' ||
count($clauses) > 1) {
283 $parts[] = $clause->prepare($qb, $joined_alias);
286 return $qb->
merge($parts, $boolean);
302 foreach ($clauses as $clause) {
303 if (strtoupper($boolean) ===
'OR' ||
count($clauses) > 1) {
308 $parts[] = $clause->prepare($qb, $joined_alias);
311 return $qb->
merge($parts, $boolean);
329 foreach ($clauses as $clause) {
330 if (strtoupper($boolean) ==
'OR' ||
count($clauses) > 1) {
333 $joined_alias = $qb->
joinRelationshipTable(
'n_table',
'entity_guid', $clause->names, $clause->inverse);
335 $parts[] = $clause->prepare($qb, $joined_alias);
338 return $qb->
merge($parts, $boolean);
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.
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.
static $primary_attr_names
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
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.