expandArguments

  1. drupal
    1. drupal7
Versionen
drupal7 protected DatabaseConnection::expandArguments(&$query, &$args)

Expand out shorthand placeholders.

Drupal supports an alternate syntax for doing arrays of values. We therefore need to expand them out into a full, executable query string.

Übergabeparameter

$query The query string to modify.

$args The arguments for the query.

Rückgabewert

TRUE if the query was modified, FALSE otherwise.

Verwandte Themen

Code

includes/database/database.inc, line 623

<?php
protected function expandArguments(&$query, &$args) {
  $modified = FALSE;

  foreach ($args as $key => $data) {
    // If the placeholder value to insert is an array, assume that we need
    // to expand it out into a comma-delimited set of placeholders.
    if (is_array($data)) {
      $new_keys = array();
      foreach ($data as $i => $value) {
        // This assumes that there are no other placeholders that use the same
        // name.  For example, if the array placeholder is defined as :example
        // and there is already an :example_2 placeholder, this will generate
        // a duplicate key.  We do not account for that as the calling code
        // is already broken if that happens.
        $new_keys[$key . '_' . $i] = $value;
      }

      // Update the query with the new placeholders.
      // preg_replace is a little bit slower than str_replace, but it is
      // necessary to ensure the replacement does not affect placeholders
      // that start with the same exact text. For example, if the query
      // contains the placeholders :foo and :foobar, and :foo has an array
      // of values, using str_replace would affect both placeholders, but
      // using the following preg_replace would only affect :foo because it
      // is followed by a non-word character.
      $query = preg_replace('#' . $key . '\b#', implode(', ', array_keys($new_keys)), $query);

      // Update the args array with the new placeholders.
      unset($args[$key]);
      $args += $new_keys;

      $modified = TRUE;
    }
  }

  return $modified;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen