You are here

protected function DatabaseConnection_sqlsrv::expandArguments in Drupal driver for SQL Server and SQL Azure 7

This method is overriden to modify the way placeholder names are generated. This allows to have plain queries have a higher degree of repetitivity, allowing for a possible query manipulation cache. https://www.drupal.org/node/2146839

Overrides DatabaseConnection::expandArguments

1 call to DatabaseConnection_sqlsrv::expandArguments()
DatabaseConnection_sqlsrv::query in sqlsrv/database.inc
This method is overriden to manage the insecure (EMULATE_PREPARE) behaviour to prevent some compatibility issues with SQL Server.

File

sqlsrv/database.inc, line 314
Database interface code for Microsoft SQL Server.

Class

DatabaseConnection_sqlsrv

Code

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

  // If the placeholder value to insert is an array, assume that we need
  // to expand it out into a comma-delimited set of placeholders.
  foreach (array_filter($args, 'is_array') as $key => $data) {
    $new_keys = array();
    $pos = 0;
    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 . '_' . $pos] = $value;
      $pos++;
    }

    // Update the query with the new placeholders.
    // preg_replace 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;
}