/** * Overrides OgSelectionHandler::entityFieldQueryAlter(). * * Add the user's groups along with the rest of the "public" groups. */ public function entityFieldQueryAlter(SelectQueryInterface $query) { $gids = og_get_entity_groups(); if (empty($gids['node'])) { return; } $conditions =& $query->conditions(); // Find the condition for the "field_data_field_privacy_settings" query, and // the one for the "node.nid", so we can later db_or() them. $public_condition = array(); foreach ($conditions as $key => $condition) { if ($key !== '#conjunction' && is_string($condition['field'])) { if (strpos($condition['field'], 'field_data_field_og_subscribe_settings') === 0) { $public_condition = $condition; unset($conditions[$key]); } if ($condition['field'] === 'node.nid') { unset($conditions[$key]); } } } if (!$public_condition) { return; } $or = db_or(); $or->condition($public_condition['field'], $public_condition['value'], $public_condition['operator']); $or->condition('node.nid', $gids['node'], 'IN'); $query->condition($or); }
/** * Preprocesses a search's database query before it is executed. * * @param SelectQueryInterface $db_query * The database query to be executed for the search. Will have "item_id" and * "score" columns in its result. * @param SearchApiQueryInterface $query * The search query that is being executed. * * @see SearchApiDbService::preQuery() */ function hook_search_api_db_query_alter(SelectQueryInterface &$db_query, SearchApiQueryInterface $query) { // If the option was set on the query, add additional SQL conditions. if ($custom = $query->getOption('custom_sql_conditions')) { foreach ($custom as $condition) { $db_query->condition($condition['field'], $condition['value'], $condition['operator']); } } }
/** * Preprocesses and validates the query. * * @return * TRUE if the validation was successful, FALSE if not. * * @throws FieldsOverlapException * @throws NoFieldsException */ public function preExecute() { // Confirm that the user did not try to specify an identical // field and default field. if (array_intersect($this->insertFields, $this->defaultFields)) { throw new FieldsOverlapException('You may not specify the same field to have a value and a schema-default value.'); } if (!empty($this->fromQuery)) { // We have to assume that the used aliases match the insert fields. // Regular fields are added to the query before expressions, maintain the // same order for the insert fields. // This behavior can be overridden by calling fields() manually as only the // first call to fields() does have an effect. $this->fields(array_merge(array_keys($this->fromQuery->getFields()), array_keys($this->fromQuery->getExpressions()))); } else { // Don't execute query without fields. if (count($this->insertFields) + count($this->defaultFields) == 0) { throw new NoFieldsException('There are no fields available to insert with.'); } } // If no values have been added, silently ignore this query. This can happen // if values are added conditionally, so we don't want to throw an // exception. if (!isset($this->insertValues[0]) && count($this->insertFields) > 0 && empty($this->fromQuery)) { return FALSE; } return TRUE; }
protected function applySorts(\SelectQueryInterface $query, array $sorts) { if (empty($sorts)) { $query->orderBy('s.id', 'ASC'); } else { foreach ($sorts as $sort => $order) { if ($order === CursorInterface::SORT_DESC) { $direction = 'DESC'; } else { $direction = 'ASC'; } switch ($sort) { case Field::SUB_ID: $query->orderBy('s.id', $direction); break; case Field::SUB_STATUS: $query->orderBy('s.status', $direction); break; case Field::SUB_CREATED_TS: $query->orderBy('s.created', $direction); break; case Field::CHAN_ID: $query->orderBy('c.name', $direction); break; case Field::SUBER_NAME: $query->orderBy('s.name', $direction); break; default: throw new \InvalidArgumentException("Unsupported sort field"); } } } }
public function addOverrideJoin(\SelectQueryInterface $query, $base_alias, $base_id, $overrides_alias, $type) { $alias = $query->addJoin("LEFT OUTER", 'test_override', $overrides_alias, "{$base_alias}.{$base_id} = {$overrides_alias}.id"); $tables =& $query->getTables(); $new_tables = array(); $found_base = FALSE; foreach ($tables as $key => $table) { if ($table['alias'] == $base_alias) { $new_tables[$key] = $table; $new_tables[$alias] = $tables[$alias]; } else { if ($key == $alias) { } else { $new_tables[$key] = $table; } } } $tables = $new_tables; return $alias; }
/** * {@inheritdoc} */ protected function applySorts(\SelectQueryInterface $query, array $sorts) { if (empty($sorts)) { // Messages need a default ordering for fetching. If time for // more than one message is the same, ordering by message // identifier as second choice will lower unpredictable // behavior chances to happen (still possible thought since // serial fields don't guarantee order, even thought in real // life they do until very high values) $query->orderBy('q.created', 'ASC')->orderBy('q.msg_id', 'ASC'); } else { foreach ($sorts as $sort => $order) { if ($order === CursorInterface::SORT_DESC) { $direction = 'DESC'; } else { $direction = 'ASC'; } switch ($sort) { case Field::MSG_ID: case Field::MSG_SENT: $query->orderBy('q.created', $direction)->orderBy('q.msg_id', $direction); break; case Field::MSG_ORIGIN: $query->orderBy('m.origin', $direction); break; case Field::MSG_TYPE: $query->orderBy('m.type', $direction); break; case Field::MSG_READ_TS: $query->orderBy('m.read_at', $direction); break; case Field::MSG_UNREAD: $query->orderBy('q.msg_id', $direction); break; case Field::MSG_LEVEL: $query->orderBy('m.level', $direction); break; case Field::SUB_ID: $query->orderBy('q.sub_id', $direction); break; default: throw new \InvalidArgumentException("Unsupported sort field"); } } } }
public function entityFieldQueryAlter(SelectQueryInterface $query) { // Core forces us to know about 'permanent' vs. 'temporary' files. $tables = $query->getTables(); $base_table = key($tables); $query->condition('status', FILE_STATUS_PERMANENT); // Access control to files is a very difficult business. For now, we are not // going to give it a shot. // @todo: fix this when core access control is less insane. return $query; }
public function entityFieldQueryAlter(SelectQueryInterface $query) { // The Taxonomy module doesn't implement any proper taxonomy term access, // and as a consequence doesn't make sure that taxonomy terms cannot be viewed // when the user doesn't have access to the vocabulary. $tables = $query->getTables(); $base_table = key($tables); $vocabulary_alias = $query->innerJoin('taxonomy_vocabulary', 'n', '%alias.vid = ' . $base_table . '.vid'); $query->addMetadata('base_table', $vocabulary_alias); // Pass the query to the taxonomy access control. $this->reAlterQuery($query, 'taxonomy_vocabulary_access', $vocabulary_alias); // Also, the taxonomy term entity exposes a bundle, but doesn't have a bundle // column in the database. We have to alter the query ourself to go fetch // the bundle. $conditions =& $query->conditions(); foreach ($conditions as $key => &$condition) { if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'vocabulary_machine_name') { $condition['field'] = $vocabulary_alias . '.machine_name'; break; } } }
/** * @param \Molino\SelectQueryInterface $query * @return \Vespolina\Entity\Taxonomy\TaxonomyNodeInterface */ public function findTaxonomyNode(SelectQueryInterface $query = null) { return $query->one(); }
/** * Check a query for tables that match our entities tables * and return the alais for everyone that was found * * @param $query \SelectQueryInterface * a SelectQuery is expected * * @return array * an array whose keys are table names and values are alias */ protected function extractAlias(\SelectQueryInterface $query) { $tables = $query->getTables(); $aliases = array(); foreach ($tables as $alias => $table) { foreach ($this->entities as $entity) { if ($table['table'] == $entity['base_table']) { $aliases[$entity['base_table']] = $alias; } if ($table['table'] == $entity['revision_table']) { $aliases[$entity['revision_table']] = $alias; } } } return $aliases; }
public function notExists(SelectQueryInterface $select) { $this->query->notExists($select); return $this; }
/** * Apply the operator onto the query * * @param \SelectQueryInterface $query * @param string $statement * @param string $value */ protected final function applyOperator(\SelectQueryInterface $query, $statement, $value) { // Check if $value contains an operator (i.e. if is associative array) if (is_array($value) && !Misc::isIndexed($value)) { // First key will be the operator $keys = array_keys($value); $operator = $keys[0]; $value = $value[$operator]; switch ($operator) { case '<>': if (is_array($value)) { $query->condition($statement, $value, 'NOT IN'); } else { $query->condition($statement, $value, '<>'); } break; case 'exists': $query->exists($value); break; case 'in': $query->condition($statement, $value, 'IN'); break; default: $query->condition($statement, $value, $operator); break; } } else { if (null === $value) { $query->isNull($statement); } else { $query->condition($statement, $value); } } }