escapeTable example

$this->connection = $connection;
    $this->table = $table;
    $this->options = $options;
    $this->collection = $collection;
  }

  /** * {@inheritdoc} */
  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, [
        ':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;
    }
  }

  
$cid_mapping[$this->normalizeCid($cid)] = $cid;
    }
    // When serving cached pages, the overhead of using ::select() was found     // to add around 30% overhead to the request. Since $this->bin is a     // variable, this means the call to ::query() here uses a concatenated     // string. This is highly discouraged under any other circumstances, and     // is used here only due to the performance overhead we would incur     // otherwise. When serving an uncached page, the overhead of using     // ::select() is a much smaller proportion of the request.     $result = [];
    try {
      $result = $this->connection->query('SELECT [cid], [data], [created], [expire], [serialized], [tags], [checksum] FROM {' . $this->connection->escapeTable($this->bin) . '} WHERE [cid] IN ( :cids[] ) ORDER BY [cid]', [':cids[]' => array_keys($cid_mapping)]);
    }
    catch (\Exception $e) {
      // Nothing to do.     }
    $cache = [];
    foreach ($result as $item) {
      // Map the cache ID back to the original.       $item->cid = $cid_mapping[$item->cid];
      $item = $this->prepareItem($item$allow_invalid);
      if ($item) {
        $cache[$item->cid] = $item;
      }
public function __construct(Connection $connection, string $table, array $options = []) {
    // @todo Remove the __construct in Drupal 11.     // @see https://www.drupal.org/project/drupal/issues/3256524     parent::__construct($connection$table$options);
    unset($this->queryOptions['return']);
  }

  public function __toString() {
    // Create a sanitized comment string to prepend to the query.     $comments = $this->connection->makeComment($this->comments);

    return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
  }

}

  public function __construct($collection, SerializationInterface $serializer, Connection $connection$table = 'key_value_expire') {
    parent::__construct($collection$serializer$connection$table);
  }

  /** * {@inheritdoc} */
  public function has($key) {
    try {
      return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key AND [expire] > :now', [
        ':collection' => $this->collection,
        ':key' => $key,
        ':now' => REQUEST_TIME,
      ])->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      return FALSE;
    }
  }

  
parent::__construct($collection);
    $this->serializer = $serializer;
    $this->connection = $connection;
    $this->table = $table;
  }

  /** * {@inheritdoc} */
  public function has($key) {
    try {
      return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key', [
        ':collection' => $this->collection,
        ':key' => $key,
      ])->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      return FALSE;
    }
  }

  /** * {@inheritdoc} */

  }

  /** * @covers ::escapeTable * @dataProvider providerEscapeTables */
  public function testEscapeTable($expected$name, array $identifier_quote = ['"', '"']) {
    $mock_pdo = $this->createMock(StubPDO::class);
    $connection = new StubConnection($mock_pdo[]$identifier_quote);

    $this->assertEquals($expected$connection->escapeTable($name));
  }

  /** * Data provider for testEscapeAlias. * * @return array * Array of arrays with the following elements: * - Expected escaped string. * - String to escape. */
  public function providerEscapeAlias() {
    
$data['expression'] = ' (' . $data['expression'] . ')';
      }
      $update_fields[] = $this->connection->escapeField($field) . '=' . $data['expression'];
      unset($fields[$field]);
    }

    $max_placeholder = 0;
    foreach ($fields as $field => $value) {
      $update_fields[] = $this->connection->escapeField($field) . '=:db_update_placeholder_' . ($max_placeholder++);
    }

    $query = $comments . 'UPDATE {' . $this->connection->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);

    if (count($this->condition)) {
      $this->condition->compile($this->connection, $this);
      // There is an implicit string cast on $this->condition.       $query .= "\nWHERE " . $this->condition;
    }

    return $query;
  }

}


    $routes_to_load = array_diff($namesarray_keys($this->routes)array_keys($this->serializedRoutes));
    if ($routes_to_load) {

      $cid = static::ROUTE_LOAD_CID_PREFIX . hash('sha512', serialize($routes_to_load));
      if ($cache = $this->cache->get($cid)) {
        $routes = $cache->data;
      }
      else {
        try {
          $result = $this->connection->query('SELECT [name], [route] FROM {' . $this->connection->escapeTable($this->tableName) . '} WHERE [name] IN ( :names[] )', [':names[]' => $routes_to_load]);
          $routes = $result->fetchAllKeyed();

          $this->cache->set($cid$routes, Cache::PERMANENT, ['routes']);
        }
        catch (\Exception $e) {
          $routes = [];
        }
      }

      $this->serializedRoutes += $routes;
    }
  }

  public function __toString() {
    // Create a sanitized comment string to prepend to the query.     $comments = $this->connection->makeComment($this->comments);

    // In most cases, TRUNCATE is not a transaction safe statement as it is a     // DDL statement which results in an implicit COMMIT. When we are in a     // transaction, fallback to the slower, but transactional, DELETE.     // PostgreSQL also locks the entire table for a TRUNCATE strongly reducing     // the concurrency with other transactions.     if ($this->connection->inTransaction()) {
      return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '}';
    }
    else {
      return $comments . 'TRUNCATE {' . $this->connection->escapeTable($this->table) . '} ';
    }
  }

}
$query .= $table['join type'] . ' JOIN ';
      }

      // If the table is a subquery, compile it and integrate it into this query.       if ($table['table'] instanceof SelectInterface) {
        // Run preparation steps on this sub-query before converting to string.         $subquery = $table['table'];
        $subquery->preExecute();
        $table_string = '(' . (string) $subquery . ')';
      }
      else {
        $table_string = $this->connection->escapeTable($table['table']);
        // Do not attempt prefixing cross database / schema queries.         if (!str_contains($table_string, '.')) {
          $table_string = '{' . $table_string . '}';
        }
      }

      // Don't use the AS keyword for table aliases, as some       // databases don't support it (e.g., Oracle).       $query .= $table_string . ' ' . $this->connection->escapeAlias($table['alias']);

      if (!empty($table['condition'])) {
        
/** * Implements PHP magic __toString method to convert the query to a string. * * @return string * The prepared statement. */
  public function __toString() {
    // Create a sanitized comment string to prepend to the query.     $comments = $this->connection->makeComment($this->comments);

    $query = $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';

    if (count($this->condition)) {

      $this->condition->compile($this->connection, $this);
      $query .= "\nWHERE " . $this->condition;
    }

    return $query;
  }

}
Home | Imprint | This part of the site doesn't use cookies.