ensureTableExists example

$this->connection->merge('cachetags')
          ->insertFields(['invalidations' => 1])
          ->expression('invalidations', '[invalidations] + 1')
          ->key('tag', $tag)
          ->execute();
      }
    }
    catch (\Exception $e) {
      // Create the cache table, which will be empty. This fixes cases during       // core install where cache tags are invalidated before the table is       // created.       if (!$this->ensureTableExists()) {
        throw $e;
      }
    }
  }

  /** * {@inheritdoc} */
  protected function getTagInvalidationCounts(array $tags) {
    try {
      return $this->connection->query('SELECT [tag], [invalidations] FROM {cachetags} WHERE [tag] IN ( :tags[] )', [':tags[]' => $tags])
        
    // stale data. The transaction makes it atomic to avoid unstable router     // states due to random failures.     try {
      $transaction = $this->connection->startTransaction();
      // We don't use truncate, because it is not guaranteed to be transaction       // safe.       try {
        $this->connection->delete($this->tableName)
          ->execute();
      }
      catch (\Exception $e) {
        if (!$this->ensureTableExists()) {
          throw $e;
        }
      }

      // Split the routes into chunks to avoid big INSERT queries.       $route_chunks = array_chunk($this->routes->all(), 50, TRUE);
      foreach ($route_chunks as $routes) {
        $insert = $this->connection->insert($this->tableName)->fields([
          'name',
          'fit',
          'path',
          


  /** * {@inheritdoc} */
  public function set($key$value) {
    try {
      $this->doSet($key$value);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.       if ($this->ensureTableExists()) {
        $this->doSet($key$value);
      }
      else {
        throw $e;
      }
    }
  }

  /** * Saves a value for a given key if it does not exist yet. * * This will be called by setIfNotExists() within a try block. * * @param string $key * The key of the data to store. * @param mixed $value * The data to store. * * @return bool * TRUE if the data was set, FALSE if it already existed. */
/** * {@inheritdoc} */
  public function createItem($data) {
    $try_again = FALSE;
    try {
      $id = $this->doCreateItem($data);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.       if (!$try_again = $this->ensureTableExists()) {
        // If the exception happened for other reason than the missing table,         // propagate the exception.         throw $e;
      }
    }
    // Now that the table has been created, try again if necessary.     if ($try_again) {
      $id = $this->doCreateItem($data);
    }
    return $id;
  }

  

  public function register($name$window = 3600, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->requestStack->getCurrentRequest()->getClientIp();
    }
    $try_again = FALSE;
    try {
      $this->doInsert($name$window$identifier);
    }
    catch (\Exception $e) {
      $try_again = $this->ensureTableExists();
      if (!$try_again) {
        throw $e;
      }
    }
    if ($try_again) {
      $this->doInsert($name$window$identifier);
    }
  }

  /** * Inserts an event into the flood table. * * @param string $name * The name of an event. * @param int $window * Number of seconds before this event expires. * @param string $identifier * Unique identifier of the current user. * * @see \Drupal\Core\Flood\DatabaseBackend::register */

  protected function safeExecuteSelect(SelectInterface $query) {
    try {
      return $query->execute();
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.       if ($this->ensureTableExists()) {
        return $query->execute();
      }
      // Some other failure that we can not recover from.       throw new PluginException($e->getMessage(), 0, $e);
    }
  }

  /** * {@inheritdoc} */
  public function save(array $link) {
    
/** * {@inheritdoc} */
  public function write($name, array $data) {
    $data = $this->encode($data);
    try {
      return $this->doWrite($name$data);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.       if ($this->ensureTableExists()) {
        return $this->doWrite($name$data);
      }
      // Some other failure that we can not recover from.       throw new StorageException($e->getMessage(), 0, $e);
    }
  }

  /** * Helper method so we can re-try a write. * * @param string $name * The config name. * @param string $data * The config data, already dumped to a string. * * @return bool */

  public function create(array $batch) {
    // Ensure that a session is started before using the CSRF token generator.     $this->session->start();
    $try_again = FALSE;
    try {
      // The batch table might not yet exist.       $this->doCreate($batch);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.       if (!$try_again = $this->ensureTableExists()) {
        // If the exception happened for other reason than the missing table,         // propagate the exception.         throw $e;
      }
    }
    // Now that the table has been created, try again if necessary.     if ($try_again) {
      $this->doCreate($batch);
    }
  }

  


  /** * {@inheritdoc} */
  public function setWithExpire($key$value$expire) {
    try {
      $this->doSetWithExpire($key$value$expire);
    }
    catch (\Exception $e) {
      // If there was an exception, then try to create the table.       if ($this->ensureTableExists()) {
        $this->doSetWithExpire($key$value$expire);
      }
      else {
        throw $e;
      }
    }
  }

  /** * Sets a value for a given key with a time to live if it does not yet exist. * * This will be called by setWithExpireIfNotExists() within a try block. * * @param string $key * The key of the data to store. * @param mixed $value * The data to store. * @param int $expire * The time to live for items, in seconds. * * @return bool * TRUE if the data was set, or FALSE if it already existed. */

        catch (IntegrityConstraintViolationException $e) {
          // Suppress the error. If this is our first pass through the loop,           // then $retry is FALSE. In this case, the insert failed because some           // other request acquired the lock but did not release it. We decide           // whether to retry by checking lockMayBeAvailable(). This will clear           // the offending row from the database table in case it has expired.           $retry = $retry ? FALSE : $this->lockMayBeAvailable($name);
        }
        catch (\Exception $e) {
          // Create the semaphore table if it does not exist and retry.           if ($this->ensureTableExists()) {
            // Retry only once.             $retry = !$retry;
          }
          else {
            throw $e;
          }
        }
        // We only retry in case the first attempt failed, but we then broke         // an expired lock.       } while ($retry);
    }
    
Home | Imprint | This part of the site doesn't use cookies.