function _drush_replace_query_placeholders

8.0.x dbtng.inc _drush_replace_query_placeholders($where, $args)
6.x dbtng.inc _drush_replace_query_placeholders($where, $args)
7.x dbtng.inc _drush_replace_query_placeholders($where, $args)
3.x drush.inc _drush_replace_query_placeholders($where, $args)
4.x drush.inc _drush_replace_query_placeholders($where, $args)
5.x dbtng.inc _drush_replace_query_placeholders($where, $args)
master dbtng.inc _drush_replace_query_placeholders($where, $args)

Replace named placeholders in a WHERE snippet.

Helper function to allow the usage of Drupal 7+ WHERE snippets with named placeholders in code for Drupal 6.

Parameters

$where: String with a WHERE snippet using named placeholders.

$args: Array of placeholder values.

Return value

String. $where filled with literals from $args.

Related topics

2 calls to _drush_replace_query_placeholders()
drush_db_delete in includes/dbtng.inc
A db_delete() that works for any version of Drupal.
drush_db_select in includes/dbtng.inc
A db_select() that works for any version of Drupal.

File

includes/dbtng.inc, line 26
Wrappers to abstract database operations from Drupal version.

Code

function _drush_replace_query_placeholders($where, $args) {
  foreach ($args as $key => $data) {
    if (is_array($data)) {
      $new_keys = array();
      // $data can't have keys that are a prefix of other keys to
      // prevent a corrupted result in the below calls to str_replace().
      // To avoid this we will use a zero padded indexed array of the values of $data.
      $pad_length = strlen((string) count(array_values($data)));
      foreach (array_values($data) as $i => $value) {
        if (!is_numeric($value)) {
          $value = "'" . $value . "'";
        }
        $new_keys[$key . '_' . str_pad($i, $pad_length, '0', STR_PAD_LEFT)] = $value;
      }
      $where = preg_replace('#' . $key . '\b#', implode(', ', array_keys($new_keys)), $where);
      unset($args[$key]);
      $args += $new_keys;
    }
    else if (!is_numeric($data)) {
      $args[$key] = "'" . $data . "'";
    }
  }

  foreach ($args as $key => $data) {
    $where = str_replace($key, $data, $where);
  }

  return $where;
}