You are here

class DatabaseStorage in Service Container 7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/Core/KeyValueStore/DatabaseStorage.php \Drupal\Core\KeyValueStore\DatabaseStorage

Defines a default key/value store implementation.

This is Drupal's default key/value store implementation. It uses the database to store key/value data.

Hierarchy

Expanded class hierarchy of DatabaseStorage

File

lib/Drupal/Core/KeyValueStore/DatabaseStorage.php, line 20
Contains \Drupal\Core\KeyValueStore\DatabaseStorage.

Namespace

Drupal\Core\KeyValueStore
View source
class DatabaseStorage extends StorageBase {

  /**
   * The serialization class to use.
   *
   * @var \Drupal\Component\Serialization\SerializationInterface
   */
  protected $serializer;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The name of the SQL table to use.
   *
   * @var string
   */
  protected $table;

  /**
   * Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().
   *
   * @param string $collection
   *   The name of the collection holding key and value pairs.
   * @param \Drupal\Component\Serialization\SerializationInterface $serializer
   *   The serialization class to use.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to use.
   * @param string $table
   *   The name of the SQL table to use, defaults to key_value.
   */
  public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value') {
    parent::__construct($collection);
    $this->serializer = $serializer;
    $this->connection = $connection;
    $this->table = $table;
  }

  /**
   * {@inheritdoc}
   */
  public function has($key) {
    return (bool) $this->connection
      ->query('SELECT 1 FROM {' . $this->connection
      ->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key', array(
      ':collection' => $this->collection,
      ':key' => $key,
    ))
      ->fetchField();
  }

  /**
   * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getMultiple().
   */
  public function getMultiple(array $keys) {
    $values = array();
    try {
      $result = $this->connection
        ->query('SELECT name, value FROM {' . $this->connection
        ->escapeTable($this->table) . '} WHERE name IN (:keys) AND collection = :collection', array(
        ':keys' => $keys,
        ':collection' => $this->collection,
      ))
        ->fetchAllAssoc('name');
      foreach ($keys as $key) {
        if (isset($result[$key])) {
          $values[$key] = $this->serializer
            ->decode($result[$key]->value);
        }
      }
    } catch (\Exception $e) {

      // @todo: Perhaps if the database is never going to be available,
      // key/value requests should return FALSE in order to allow exception
      // handling to occur but for now, keep it an array, always.
    }
    return $values;
  }

  /**
   * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getAll().
   */
  public function getAll() {
    $result = $this->connection
      ->query('SELECT name, value FROM {' . $this->connection
      ->escapeTable($this->table) . '} WHERE collection = :collection', array(
      ':collection' => $this->collection,
    ));
    $values = array();
    foreach ($result as $item) {
      if ($item) {
        $values[$item->name] = $this->serializer
          ->decode($item->value);
      }
    }
    return $values;
  }

  /**
   * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::set().
   */
  public function set($key, $value) {
    $this->connection
      ->merge($this->table)
      ->key(array(
      'name' => $key,
      'collection' => $this->collection,
    ))
      ->fields(array(
      'value' => $this->serializer
        ->encode($value),
    ))
      ->execute();
  }

  /**
   * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::setIfNotExists().
   */
  public function setIfNotExists($key, $value) {
    $result = $this->connection
      ->merge($this->table)
      ->insertFields(array(
      'collection' => $this->collection,
      'name' => $key,
      'value' => $this->serializer
        ->encode($value),
    ))
      ->condition('collection', $this->collection)
      ->condition('name', $key)
      ->execute();
    return $result == Merge::STATUS_INSERT;
  }

  /**
   * {@inheritdoc}
   */
  public function rename($key, $new_key) {
    $this->connection
      ->update($this->table)
      ->fields(array(
      'name' => $new_key,
    ))
      ->condition('collection', $this->collection)
      ->condition('name', $key)
      ->execute();
  }

  /**
   * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteMultiple().
   */
  public function deleteMultiple(array $keys) {

    // Delete in chunks when a large array is passed.
    while ($keys) {
      $this->connection
        ->delete($this->table)
        ->condition('name', array_splice($keys, 0, 1000), 'IN')
        ->condition('collection', $this->collection)
        ->execute();
    }
  }

  /**
   * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteAll().
   */
  public function deleteAll() {
    $this->connection
      ->delete($this->table)
      ->condition('collection', $this->collection)
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseStorage::$connection protected property The database connection.
DatabaseStorage::$serializer protected property The serialization class to use.
DatabaseStorage::$table protected property The name of the SQL table to use.
DatabaseStorage::deleteAll public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteAll(). Overrides KeyValueStoreInterface::deleteAll
DatabaseStorage::deleteMultiple public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteMultiple(). Overrides KeyValueStoreInterface::deleteMultiple 1
DatabaseStorage::getAll public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getAll(). Overrides KeyValueStoreInterface::getAll 1
DatabaseStorage::getMultiple public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getMultiple(). Overrides KeyValueStoreInterface::getMultiple 1
DatabaseStorage::has public function Returns whether a given key exists in the store. Overrides KeyValueStoreInterface::has 1
DatabaseStorage::rename public function Renames a key. Overrides KeyValueStoreInterface::rename
DatabaseStorage::set public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::set(). Overrides KeyValueStoreInterface::set
DatabaseStorage::setIfNotExists public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::setIfNotExists(). Overrides KeyValueStoreInterface::setIfNotExists
DatabaseStorage::__construct public function Overrides Drupal\Core\KeyValueStore\StorageBase::__construct(). Overrides StorageBase::__construct 1
StorageBase::$collection protected property The name of the collection holding key and value pairs.
StorageBase::delete public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::delete(). Overrides KeyValueStoreInterface::delete 1
StorageBase::get public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get(). Overrides KeyValueStoreInterface::get 1
StorageBase::getCollectionName public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getCollectionName(). Overrides KeyValueStoreInterface::getCollectionName
StorageBase::setMultiple public function Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::setMultiple(). Overrides KeyValueStoreInterface::setMultiple 1