/** * {@inheritdoc} */ public function claimItem($lease_time = 30) { // Claim an item by updating its expire fields. If claim is not successful // another thread may have claimed the item in the meantime. Therefore loop // until an item is successfully claimed or we are reasonably sure there // are no unclaimed items left. while (TRUE) { $item = $this->connection->queryRange('SELECT data, created, item_id FROM {queue} q WHERE expire = 0 AND name = :name ORDER BY created, item_id ASC', 0, 1, array(':name' => $this->name))->fetchObject(); if ($item) { // Try to update the item. Only one thread can succeed in UPDATEing the // same row. We cannot rely on REQUEST_TIME because items might be // claimed by a single consumer which runs longer than 1 second. If we // continue to use REQUEST_TIME instead of the current time(), we steal // time from the lease, and will tend to reset items before the lease // should really expire. $update = $this->connection->update('queue')->fields(array('expire' => time() + $lease_time))->condition('item_id', $item->item_id)->condition('expire', 0); // If there are affected rows, this update succeeded. if ($update->execute()) { $item->data = unserialize($item->data); return $item; } } else { // No items currently available to claim. return FALSE; } } }
/** * Implements Drupal\Core\Config\StorageInterface::exists(). */ public function exists($name) { try { return (bool) $this->connection->queryRange('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :name', 0, 1, array(':collection' => $this->collection, ':name' => $name), $this->options)->fetchField(); } catch (\Exception $e) { // If we attempt a read without actually having the database or the table // available, just return FALSE so the caller can handle it. return FALSE; } }
/** * {@inheritdoc} */ public function read($sid) { $data = ''; if (!empty($sid)) { // Read the session data from the database. $query = $this->connection->queryRange('SELECT session FROM {sessions} WHERE sid = :sid', 0, 1, [':sid' => Crypt::hashBase64($sid)]); $data = (string) $query->fetchField(); } return $data; }
/** * {@inheritdoc} */ public function updateIndex(NodeInterface $node) { $nid = $node->id(); $count = $this->database->query("SELECT COUNT(cid) FROM {comment_field_data} c INNER JOIN {forum_index} i ON c.entity_id = i.nid WHERE c.entity_id = :nid AND c.field_name = 'comment_forum' AND c.entity_type = 'node' AND c.status = :status AND c.default_langcode = 1", array(':nid' => $nid, ':status' => CommentInterface::PUBLISHED))->fetchField(); if ($count > 0) { // Comments exist. $last_reply = $this->database->queryRange("SELECT cid, name, created, uid FROM {comment_field_data} WHERE entity_id = :nid AND field_name = 'comment_forum' AND entity_type = 'node' AND status = :status AND default_langcode = 1 ORDER BY cid DESC", 0, 1, array(':nid' => $nid, ':status' => CommentInterface::PUBLISHED))->fetchObject(); $this->database->update('forum_index')->fields(array('comment_count' => $count, 'last_comment_timestamp' => $last_reply->created))->condition('nid', $nid)->execute(); } else { // Comments do not exist. // @todo This should be actually filtering on the desired node language $this->database->update('forum_index')->fields(array('comment_count' => 0, 'last_comment_timestamp' => $node->getCreatedTime()))->condition('nid', $nid)->execute(); } }
/** * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { $exists = (bool) $this->connection->queryRange('SELECT 1 FROM {users_field_data} WHERE uid = :uid AND default_langcode = 1', 0, 1, array(':name' => $form_state->getValue('owner_uid')))->fetchField(); if (!$exists) { $form_state->setErrorByName('owner_uid', t('Enter a valid username.')); } }
/** * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { $exists = (bool) $this->connection->queryRange('SELECT 1 FROM {users} WHERE name = :name', 0, 1, array(':name' => $form_state['values']['owner_name']))->fetchField(); if (!$exists) { form_set_error('owner_name', $form_state, t('Enter a valid username.')); } }
/** * {@inheritdoc} */ public function languageAliasExists() { try { return (bool) $this->connection->queryRange('SELECT 1 FROM {url_alias} WHERE langcode <> :langcode', 0, 1, array(':langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED))->fetchField(); } catch (\Exception $e) { $this->catchException($e); return FALSE; } }
/** * {@inheritdoc} */ public function updateIndex() { // Interpret the cron limit setting as the maximum number of nodes to index // per cron run. $limit = (int) $this->searchSettings->get('index.cron_limit'); $result = $this->database->queryRange("SELECT n.nid, MAX(sd.reindex) FROM {node} n LEFT JOIN {search_dataset} sd ON sd.sid = n.nid AND sd.type = :type WHERE sd.sid IS NULL OR sd.reindex <> 0 GROUP BY n.nid ORDER BY MAX(sd.reindex) is null DESC, MAX(sd.reindex) ASC, n.nid ASC", 0, $limit, array(':type' => $this->getPluginId()), array('target' => 'replica')); $nids = $result->fetchCol(); if (!$nids) { return; } $node_storage = $this->entityManager->getStorage('node'); foreach ($node_storage->loadMultiple($nids) as $node) { $this->indexNode($node); } }
/** * {@inheritdoc} */ public function exists($alias, $source, $language = LanguageInterface::LANGCODE_NOT_SPECIFIED) { return (bool) $this->database->queryRange("SELECT pid FROM {url_alias} WHERE source <> :source AND alias = :alias AND langcode IN (:language, :language_none) ORDER BY langcode DESC, pid DESC", 0, 1, array(':source' => $source, ':alias' => $alias, ':language' => $language, ':language_none' => LanguageInterface::LANGCODE_NOT_SPECIFIED))->fetchField(); }