escapeLike example

/** * {@inheritdoc} */
  public function DgetUnion() {
    return $this->query->getUnion();
  }

  /** * {@inheritdoc} */
  public function escapeLike($string) {
    return $this->query->escapeLike($string);
  }

  /** * {@inheritdoc} */
  public function escapeField($string) {
    $this->query->escapeField($string);
    return $this;
  }

  /** * {@inheritdoc} */


  /** * Adds a where clause for the operation, 'equals'. */
  public function opEqual($field) {
    $this->query->addWhere($this->options['group']$field$this->value, $this->operator());
  }

  protected function opContains($field) {
    $operator = $this->getConditionOperator('LIKE');
    $this->query->addWhere($this->options['group']$field, '%' . $this->connection->escapeLike($this->value) . '%', $operator);
  }

  protected function opContainsWord($field) {
    $where = $this->operator == 'word' ? $this->query->getConnection()->condition('OR') : $this->query->getConnection()->condition('AND');

    // Don't filter on empty strings.     if (empty($this->value)) {
      return;
    }

    preg_match_all(static::WORDS_PATTERN, ' ' . $this->value, $matches, PREG_SET_ORDER);
    
public function opEqual($expression) {
    // By default, things like opEqual uses add_where, that doesn't support     // complex expressions, so override opEqual (and all operators below).     $placeholder = $this->placeholder();
    $operator = $this->getConditionOperator($this->operator());
    $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->value]);
  }

  protected function opContains($expression) {
    $placeholder = $this->placeholder();
    $operator = $this->getConditionOperator('LIKE');
    $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
  }

  /** * Filters by one or more words. * * By default opContainsWord uses add_where, that doesn't support complex * expressions. * * @param string $expression * The expression to add to the query. */
  
/** * {@inheritdoc} */
  public function DgetUnion() {
    return $this->union;
  }

  /** * {@inheritdoc} */
  public function escapeLike($string) {
    return $this->connection->escapeLike($string);
  }

  /** * {@inheritdoc} */
  public function escapeField($string) {
    return $this->connection->escapeField($string);
  }

  /** * {@inheritdoc} */
/** * Gets the breadcrumb cache entries. * * @return array * The breadcrumb cache entries. */
  protected function getBreadcrumbCacheEntries() {
    $database = \Drupal::database();
    $cache_entries = $database->select('cache_render')
      ->fields('cache_render')
      ->condition('cid', $database->escapeLike('entity_view:block:breadcrumb') . '%', 'LIKE')
      ->execute()
      ->fetchAllAssoc('cid');
    return $cache_entries;
  }

}


    // Ensure that the default operator is set to simplify the cases below.     if (empty($condition['operator'])) {
      $condition['operator'] = '=';
    }
    switch ($condition['operator']) {
      case '=':
        // If a field explicitly requests that queries should not be case         // sensitive, use the LIKE operator, otherwise keep =.         if ($case_sensitive === FALSE) {
          $condition['value'] = $sql_query->escapeLike($condition['value']);
          $condition['operator'] = 'LIKE';
        }
        break;

      case '<>':
        // If a field explicitly requests that queries should not be case         // sensitive, use the NOT LIKE operator, otherwise keep <>.         if ($case_sensitive === FALSE) {
          $condition['value'] = $sql_query->escapeLike($condition['value']);
          $condition['operator'] = 'NOT LIKE';
        }
        
    $default_options = [
      'match' => NULL,
      'match_operator' => 'CONTAINS',
      'limit' => 0,
      'ids' => NULL,
    ];
    $options += $default_options;

    // Restrict the autocomplete options based on what's been typed already.     if (isset($options['match'])) {
      $style_options = $this->getOption('style');
      $value = $this->connection->escapeLike($options['match']);
      if ($options['match_operator'] !== '=') {
        $value = $value . '%';
        if ($options['match_operator'] != 'STARTS_WITH') {
          $value = '%' . $value;
        }
      }

      // Multiple search fields are ORed together.       $conditions = $this->view->query->getConnection()->condition('OR');

      // Build the condition using the selected search fields.
// Process other options, string filter, query limit, etc.     if (!empty($options['filters'])) {
      if (count($options['filters']) > 1) {
        $filter = $this->connection->condition('OR');
        $query->condition($filter);
      }
      else {
        // If we have a single filter, just add it to the query.         $filter = $query;
      }
      foreach ($options['filters'] as $field => $string) {
        $filter->condition($this->dbFieldTable($field) . '.' . $field, '%' . $this->connection->escapeLike($string) . '%', 'LIKE');
      }
    }

    if (!empty($options['pager limit'])) {
      $query = $query->extend(PagerSelectExtender::class)->limit($options['pager limit']);
    }

    return $query;
  }

  /** * Creates a database record for a string object. * * @param \Drupal\locale\StringInterface $string * The string object. * * @return bool|int * If the operation failed, returns FALSE. * If it succeeded returns the last insert ID of the query, if one exists. * * @throws \Drupal\locale\StringStorageException * If the string is not suitable for this storage, an exception is thrown. */

  public function execute() {
    $results = [];
    if (!$this->isSearchExecutable()) {
      return $results;
    }

    // Process the keywords.     $keys = $this->keywords;
    // Escape for LIKE matching.     $keys = $this->database->escapeLike($keys);
    // Replace wildcards with MySQL/PostgreSQL wildcards.     $keys = preg_replace('!\*+!', '%', $keys);

    // Run the query to find matching users.     $query = $this->database
      ->select('users_field_data', 'users')
      ->extend(PagerSelectExtender::class);
    $query->fields('users', ['uid']);
    $query->condition('default_langcode', 1);
    if ($this->currentUser->hasPermission('administer users')) {
      // Administrators can also search in the otherwise private email field,
/** * {@inheritdoc} */
  public function preloadPathAlias($preloaded$langcode) {
    $select = $this->getBaseQuery()
      ->fields('base_table', ['path', 'alias']);

    if (!empty($preloaded)) {
      $conditions = $this->connection->condition('OR');
      foreach ($preloaded as $preloaded_item) {
        $conditions->condition('base_table.path', $this->connection->escapeLike($preloaded_item), 'LIKE');
      }
      $select->condition($conditions);
    }

    $this->addLanguageFallback($select$langcode);

    // We order by ID ASC so that fetchAllKeyed() returns the most recently     // created alias for each source. Subsequent queries using fetchField() must     // use ID DESC to have the same effect.     $select->orderBy('base_table.id', 'ASC');

    
->execute();

    // Match both "Ringo" and "Ring_".     $num_matches = $this->connection->select('test', 't')
      ->condition('name', 'Ring_', 'LIKE')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertSame('2', $num_matches, 'Found 2 records.');
    // Match only "Ring_" using a LIKE expression with no wildcards.     $num_matches = $this->connection->select('test', 't')
      ->condition('name', $this->connection->escapeLike('Ring_'), 'LIKE')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertSame('1', $num_matches, 'Found 1 record.');
  }

  /** * Tests a LIKE query containing a backslash. */
  public function testLikeBackslash() {
    $this->connection->insert('test')
      
Home | Imprint | This part of the site doesn't use cookies.